Message ID | 20230523031933.13421-1-qiang.zhang1211@gmail.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1866473vqo; Mon, 22 May 2023 20:26:54 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7pWoDvEvzncwMeCJ/KhRdk4X4BSVL+4OP3egnNGWgG5MWa9DnF2HcggccpVC4SUcgb4uHp X-Received: by 2002:a17:903:2793:b0:1ae:62a6:fe61 with SMTP id jw19-20020a170903279300b001ae62a6fe61mr12828970plb.54.1684812413953; Mon, 22 May 2023 20:26:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684812413; cv=none; d=google.com; s=arc-20160816; b=0d3WiFgl3h7wmq+OgG8VXAxhnQy2zazQu1eT6SUrxM0ZeIB6euwk03anP0zcNtOnV0 W/dgSiDPUQ4Gdp3dA1f1UdXtvl1RKrr818sB47JJVl893aj4ZLgfwydzUjzDDcNDC72n sikRS6xutdKs8mvoop/VMc9yOeItmsemfGLAxLGQhxwD67QSn9i0FbmqUMP2rYa7YazP ELAX7J0al3LuOhYVEQpUsszEu5on7+KEE7fTRxx82Zz0aJh5i3R4cKNLoeFJqjZ8s+v3 eSxKQaFYaR0Wu6P7L/b45CB8Cf49us1ZkGoVGDyT3xxusqxeonrqGI16n8+ymOQk7aA/ SP4w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from :dkim-signature; bh=XyH0W6DJZjpRDDDSjv6qa9noG3PE0QyQNSULfSparfs=; b=Nyrz9MRQTWP9XXJX4OND/KtHT8HyJJHP5hDuRVJnX6slAae7ylGoBxrkp4W0YKdSIg cecqJ4BmF3NJekenj6S+S8hhQv/mhPpuyMu0df/ZWVxIeGOwi2UDAi9MVCjydMGg498x ZJh8w/htuz4yzRzUIW1jeQe+S3QWRUkqOW28ddz3jED0NlwYiPrXAKHqgpndJJe3ylIs pYv01yuhxrGfVd09fAsEdWzcQFOiriy4j6/FJ5OxNJMJjlzO+nmKkrFKgV57D7XpmqbO qJdvJPylGJtP4LEZTI0fEul4sJRnyPpU/4jTZc0EmvVRwl1OqVh9zTYtBsPkIL3BuS9o n01A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=HW2B75lL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w10-20020a170902e88a00b001a9930c626dsi6154181plg.273.2023.05.22.20.26.39; Mon, 22 May 2023 20:26:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=HW2B75lL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232298AbjEWDTt (ORCPT <rfc822;ahmedalshaiji.dev@gmail.com> + 99 others); Mon, 22 May 2023 23:19:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39234 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230034AbjEWDTo (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 22 May 2023 23:19:44 -0400 Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9D8E490 for <linux-kernel@vger.kernel.org>; Mon, 22 May 2023 20:19:43 -0700 (PDT) Received: by mail-pj1-x1030.google.com with SMTP id 98e67ed59e1d1-25343f0c693so4678734a91.3 for <linux-kernel@vger.kernel.org>; Mon, 22 May 2023 20:19:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684811983; x=1687403983; h=message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=XyH0W6DJZjpRDDDSjv6qa9noG3PE0QyQNSULfSparfs=; b=HW2B75lLxyMGCv4mkCrH4Sqyc5BxN34tMEWRTl84JtRFdMGKjvV2Xep5NzgHvgB8qO ndOMEC6a/RtDk/6lEGzYyImVU8Jmi3yuDIuFhGfZcKgJsGMUMsgXuv0xBzfU1XYsDt90 VX6YEcTXvniIFijqpbWXPmTfS7VbmSIPh+satyy2kwJir2B963L0z8jsTZGuAEvCOINC lOgwiArNaUsbXtswMxrBPkdapegcPkUpl4CkIbIn+8UTiqcDFnUL46NlIAPrwyYPz6SF P5q7CUr7qmXGyTav3EBYqKwJKwRHNH0vsctFSuhopP01hzl6AH+sHOBtcnsWsAiNYEMB 55jg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684811983; x=1687403983; h=message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XyH0W6DJZjpRDDDSjv6qa9noG3PE0QyQNSULfSparfs=; b=fefMJZtIs1y/RxRuIOADzaV1fEqYsobNR4BYynhHm3S6LCnswud+Jne+ScIV31HEGb CkPhPW61F6tfSrPrl3RCfOCznDuWYLOiLC9PIvnmQYi2JyEsiDIvYU3rDXYLYuDln3zb v6Y4tMHExngZKHc0cQ2w8E/H2EdkyS5ZUQOjDIBWor/DRhYIEi6TTtdhisd3U5ubYLgV 7wyaoMpYKuxJg2DQeOhOUEiHvtgS6i9ILNt51f+fSFA7Wfg9eq2QZq4ZtrbkYCsftOvl ssgwzs133ARG77J+shb5pvD7rX8/fzlDf1h89LCcPpSJFyZdkAIFm26iqFd2d7u7HpwC x3Zg== X-Gm-Message-State: AC+VfDyhcVtGM7874ADhhowZBHIt70bmJrLIas+j8ZSfg5ZV19RdqsbF kk6UYqQn/wCcY/IvJbO4Lao= X-Received: by 2002:a17:90b:170e:b0:23f:83de:7e4a with SMTP id ko14-20020a17090b170e00b0023f83de7e4amr12066082pjb.7.1684811982957; Mon, 22 May 2023 20:19:42 -0700 (PDT) Received: from MSCND1355B05.fareast.nevint.com ([183.242.39.186]) by smtp.gmail.com with ESMTPSA id o10-20020a17090aac0a00b002467717fa60sm4802675pjq.16.2023.05.22.20.19.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 May 2023 20:19:42 -0700 (PDT) From: Zqiang <qiang.zhang1211@gmail.com> To: tj@kernel.org, jiangshanlai@gmail.com, naresh.kamboju@linaro.org, qiang.zhang1211@gmail.com Cc: linux-kernel@vger.kernel.org Subject: [PATCH] workqueue: Fix warning triggered when nr_running is checked in worker_enter_idle() Date: Tue, 23 May 2023 11:19:33 +0800 Message-Id: <20230523031933.13421-1-qiang.zhang1211@gmail.com> X-Mailer: git-send-email 2.17.1 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766653861491782587?= X-GMAIL-MSGID: =?utf-8?q?1766653861491782587?= |
Series |
workqueue: Fix warning triggered when nr_running is checked in worker_enter_idle()
|
|
Commit Message
Z qiang
May 23, 2023, 3:19 a.m. UTC
Currently, the nr_running can be modified from timer tick, that means
the timer tick can run in not-irq-protected critical section to modify
nr_runnig, consider the following scenario:
CPU0
kworker/0:2 (events)
worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND);
->pool->nr_running++; (1)
process_one_work()
->worker->current_func(work);
->schedule()
->wq_worker_sleeping()
->pool->nr_running--; (0)
....
->wq_worker_running()
....
CPU0 by interrupt:
wq_worker_tick()
->worker_set_flags(worker, WORKER_CPU_INTENSIVE);
->pool->nr_running--; (-1)
->worker->flags |= WORKER_CPU_INTENSIVE;
....
->if (!(worker->flags & WORKER_NOT_RUNNING))
->pool->nr_running++; (will not execute)
....
->worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
->pool->nr_running++; (0)
....
worker_set_flags(worker, WORKER_PREP);
->pool->nr_running--; (-1)
....
worker_enter_idle()
->WARN_ON_ONCE(pool->nr_workers == pool->nr_idle && pool->nr_running);
if the nr_workers is equal to nr_idle, due to the nr_running is not zero,
will trigger WARN_ON_ONCE().
[ 2.460602] WARNING: CPU: 0 PID: 63 at kernel/workqueue.c:1999 worker_enter_idle+0xb2/0xc0
[ 2.462163] Modules linked in:
[ 2.463401] CPU: 0 PID: 63 Comm: kworker/0:2 Not tainted 6.4.0-rc2-next-20230519 #1
[ 2.463771] Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.14.0-2 04/01/2014
[ 2.465127] Workqueue: 0x0 (events)
[ 2.465678] RIP: 0010:worker_enter_idle+0xb2/0xc0
...
[ 2.472614] Call Trace:
[ 2.473152] <TASK>
[ 2.474182] worker_thread+0x71/0x430
[ 2.474992] ? _raw_spin_unlock_irqrestore+0x28/0x50
[ 2.475263] kthread+0x103/0x120
[ 2.475493] ? __pfx_worker_thread+0x10/0x10
[ 2.476355] ? __pfx_kthread+0x10/0x10
[ 2.476635] ret_from_fork+0x2c/0x50
[ 2.477051] </TASK>
This commit therefore add irq protection in wq_worker_running() to
block timer tick modify nr_running.
Reported-by: Naresh Kamboju <naresh.kamboju@linaro.org>
Closes: https://qa-reports.linaro.org/lkft/linux-next-master/build/next-20230519/testrun/17078554/suite/boot/test/clang-nightly-lkftconfig/log
Signed-off-by: Zqiang <qiang.zhang1211@gmail.com>
---
kernel/workqueue.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
Comments
Hi Zqiang, On Tue, 23 May 2023 at 08:49, Zqiang <qiang.zhang1211@gmail.com> wrote: > > Currently, the nr_running can be modified from timer tick, that means > the timer tick can run in not-irq-protected critical section to modify > nr_runnig, consider the following scenario: > > CPU0 > kworker/0:2 (events) > worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); > ->pool->nr_running++; (1) > > process_one_work() > ->worker->current_func(work); > ->schedule() > ->wq_worker_sleeping() > ->pool->nr_running--; (0) > .... > ->wq_worker_running() > .... > CPU0 by interrupt: > wq_worker_tick() > ->worker_set_flags(worker, WORKER_CPU_INTENSIVE); > ->pool->nr_running--; (-1) > ->worker->flags |= WORKER_CPU_INTENSIVE; > .... > ->if (!(worker->flags & WORKER_NOT_RUNNING)) > ->pool->nr_running++; (will not execute) > .... > ->worker_clr_flags(worker, WORKER_CPU_INTENSIVE); > ->pool->nr_running++; (0) > .... > worker_set_flags(worker, WORKER_PREP); > ->pool->nr_running--; (-1) > .... > worker_enter_idle() > ->WARN_ON_ONCE(pool->nr_workers == pool->nr_idle && pool->nr_running); > > if the nr_workers is equal to nr_idle, due to the nr_running is not zero, > will trigger WARN_ON_ONCE(). > > [ 2.460602] WARNING: CPU: 0 PID: 63 at kernel/workqueue.c:1999 worker_enter_idle+0xb2/0xc0 > [ 2.462163] Modules linked in: > [ 2.463401] CPU: 0 PID: 63 Comm: kworker/0:2 Not tainted 6.4.0-rc2-next-20230519 #1 > [ 2.463771] Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.14.0-2 04/01/2014 > [ 2.465127] Workqueue: 0x0 (events) > [ 2.465678] RIP: 0010:worker_enter_idle+0xb2/0xc0 > ... > [ 2.472614] Call Trace: > [ 2.473152] <TASK> > [ 2.474182] worker_thread+0x71/0x430 > [ 2.474992] ? _raw_spin_unlock_irqrestore+0x28/0x50 > [ 2.475263] kthread+0x103/0x120 > [ 2.475493] ? __pfx_worker_thread+0x10/0x10 > [ 2.476355] ? __pfx_kthread+0x10/0x10 > [ 2.476635] ret_from_fork+0x2c/0x50 > [ 2.477051] </TASK> > > This commit therefore add irq protection in wq_worker_running() to > block timer tick modify nr_running. > > Reported-by: Naresh Kamboju <naresh.kamboju@linaro.org> > Closes: https://qa-reports.linaro.org/lkft/linux-next-master/build/next-20230519/testrun/17078554/suite/boot/test/clang-nightly-lkftconfig/log > Signed-off-by: Zqiang <qiang.zhang1211@gmail.com> Reported-by: Linux Kernel Functional Testing <lkft@linaro.org> Tested-by: Anders Roxell <anders.roxell@linaro.org> This proposed fix patch applied on top of Linux next and tested for more than 100 times and reported issues has been fixed. Thank you. - Naresh > --- > kernel/workqueue.c | 4 ++-- > 1 file changed, 2 insertions(+), 2 deletions(-) > > diff --git a/kernel/workqueue.c b/kernel/workqueue.c > index 9c5c1cfa478f..f8d739fef311 100644 > --- a/kernel/workqueue.c > +++ b/kernel/workqueue.c > @@ -1060,10 +1060,9 @@ void wq_worker_running(struct task_struct *task) > * and leave with an unexpected pool->nr_running == 1 on the newly unbound > * pool. Protect against such race. > */ > - preempt_disable(); > + local_irq_disable(); > if (!(worker->flags & WORKER_NOT_RUNNING)) > worker->pool->nr_running++; > - preempt_enable(); > > /* > * CPU intensive auto-detection cares about how long a work item hogged > @@ -1072,6 +1071,7 @@ void wq_worker_running(struct task_struct *task) > worker->current_at = worker->task->se.sum_exec_runtime; > > worker->sleeping = 0; > + local_irq_enable(); > } > > /** > -- > 2.17.1 >
> > Currently, the nr_running can be modified from timer tick, that means > the timer tick can run in not-irq-protected critical section to modify > nr_runnig, consider the following scenario: > > CPU0 > kworker/0:2 (events) > worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); > ->pool->nr_running++; (1) > > process_one_work() > ->worker->current_func(work); > ->schedule() > ->wq_worker_sleeping() > ->pool->nr_running--; (0) > .... > ->wq_worker_running() > .... > CPU0 by interrupt: > wq_worker_tick() > ->worker_set_flags(worker, WORKER_CPU_INTENSIVE); > ->pool->nr_running--; (-1) > ->worker->flags |= WORKER_CPU_INTENSIVE; > .... > ->if (!(worker->flags & WORKER_NOT_RUNNING)) > ->pool->nr_running++; (will not execute) > .... > ->worker_clr_flags(worker, WORKER_CPU_INTENSIVE); > ->pool->nr_running++; (0) > .... > worker_set_flags(worker, WORKER_PREP); > ->pool->nr_running--; (-1) > .... > worker_enter_idle() > ->WARN_ON_ONCE(pool->nr_workers == pool->nr_idle && pool->nr_running); > > if the nr_workers is equal to nr_idle, due to the nr_running is not zero, > will trigger WARN_ON_ONCE(). > > [ 2.460602] WARNING: CPU: 0 PID: 63 at kernel/workqueue.c:1999 worker_enter_idle+0xb2/0xc0 > [ 2.462163] Modules linked in: > [ 2.463401] CPU: 0 PID: 63 Comm: kworker/0:2 Not tainted 6.4.0-rc2-next-20230519 #1 > [ 2.463771] Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.14.0-2 04/01/2014 > [ 2.465127] Workqueue: 0x0 (events) > [ 2.465678] RIP: 0010:worker_enter_idle+0xb2/0xc0 > ... > [ 2.472614] Call Trace: > [ 2.473152] <TASK> > [ 2.474182] worker_thread+0x71/0x430 > [ 2.474992] ? _raw_spin_unlock_irqrestore+0x28/0x50 > [ 2.475263] kthread+0x103/0x120 > [ 2.475493] ? __pfx_worker_thread+0x10/0x10 > [ 2.476355] ? __pfx_kthread+0x10/0x10 > [ 2.476635] ret_from_fork+0x2c/0x50 > [ 2.477051] </TASK> > > This commit therefore add irq protection in wq_worker_running() to > block timer tick modify nr_running. > > Reported-by: Naresh Kamboju <naresh.kamboju@linaro.org> > Closes: https://qa-reports.linaro.org/lkft/linux-next-master/build/next-20230519/testrun/17078554/suite/boot/test/clang-nightly-lkftconfig/log > Signed-off-by: Zqiang <qiang.zhang1211@gmail.com> > --- > kernel/workqueue.c | 4 ++-- > 1 file changed, 2 insertions(+), 2 deletions(-) > > diff --git a/kernel/workqueue.c b/kernel/workqueue.c > index 9c5c1cfa478f..f8d739fef311 100644 > --- a/kernel/workqueue.c > +++ b/kernel/workqueue.c > @@ -1060,10 +1060,9 @@ void wq_worker_running(struct task_struct *task) > * and leave with an unexpected pool->nr_running == 1 on the newly unbound > * pool. Protect against such race. > */ > - preempt_disable(); Sorry, Here can still be interrupted by wq_worker_tick() before invoke local_irq_disbale(). will resend v2. > + local_irq_disable(); > if (!(worker->flags & WORKER_NOT_RUNNING)) > worker->pool->nr_running++; > - preempt_enable(); > > /* > * CPU intensive auto-detection cares about how long a work item hogged > @@ -1072,6 +1071,7 @@ void wq_worker_running(struct task_struct *task) > worker->current_at = worker->task->se.sum_exec_runtime; > > worker->sleeping = 0; > + local_irq_enable(); > } > > /** > -- > 2.17.1 >
diff --git a/kernel/workqueue.c b/kernel/workqueue.c index 9c5c1cfa478f..f8d739fef311 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c @@ -1060,10 +1060,9 @@ void wq_worker_running(struct task_struct *task) * and leave with an unexpected pool->nr_running == 1 on the newly unbound * pool. Protect against such race. */ - preempt_disable(); + local_irq_disable(); if (!(worker->flags & WORKER_NOT_RUNNING)) worker->pool->nr_running++; - preempt_enable(); /* * CPU intensive auto-detection cares about how long a work item hogged @@ -1072,6 +1071,7 @@ void wq_worker_running(struct task_struct *task) worker->current_at = worker->task->se.sum_exec_runtime; worker->sleeping = 0; + local_irq_enable(); } /**