From patchwork Fri Feb 16 18:04:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tejun Heo X-Patchwork-Id: 202295 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:c619:b0:108:e6aa:91d0 with SMTP id hn25csp691744dyb; Fri, 16 Feb 2024 10:09:59 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWjnGXzapF4i9K6QuMckwjwGnCQj0aF58L8zqkF0uGsvf8djofkO0bfm3LZZI9NlJO/Fg54UGuYV0ItF8KjmHRuLLw8CA== X-Google-Smtp-Source: AGHT+IHA6syisaw4OXMiCVQ+WNnXZBR5xbcLaqi2IirEZ2f83tFiqrNDcXf8RD/FQU7v/BeXhnwp X-Received: by 2002:a05:6102:2a70:b0:46e:c5c5:6e00 with SMTP id hp16-20020a0561022a7000b0046ec5c56e00mr5083556vsb.33.1708106999251; Fri, 16 Feb 2024 10:09:59 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708106999; cv=pass; d=google.com; s=arc-20160816; b=QvLqG3b8XvkQV7Y0DEhCOi8Z6FCwQw5ZrjvjzX8ngFcRQL92l1S6THMb9YCyIkONXD 8NXOY7yOEbeVQOQ8ShbG+qTYyD2sUMVVx6LUZZYQCKkTk3SK1fvbgbLqAAQiOHqf9GJK tPNM8q10k12WytRoH+R7L102e2RTSIiKP2kvJwJjCqTDSqlaNSm06Ryz8Zgx8Xo7TvE5 7qIMEkhgd/a8IwfXzsXW9mqXj7+qgUtSBXdLBDSR7N/EYeP85h9NPloZ+sMfi6dK8qER u5nzQXlBnKVYzLc2AcMA1/Gyum3LRaDluVnlRaNZ+9cuAIl0NsBt37n7sESThlsk1Nr7 NQ+A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:sender:dkim-signature; bh=N9bk7H/R6dZtCT0NyxymPB9Me8HfbuqTGpGRRxjeyPY=; fh=hPPepoEKa3aQxzY69A4XiFblvkPIJsKxMx/UnNtDBzs=; b=F04KEq7eAuuu8ie34ndtQrBWS3q4QbGyCSHTN//tQTcLN2Lnri+9bEvw0iJgZVQhkn eL/1JByrzsTxcDObnAPZnAu5qD+BWyLLjGkvqHB8gdEnvWSKwxg8o0Zo7hNfS18Jzol5 AbQpqZAAnukv0e5YAHuiFo7KU84ZVF43buVVh8ehWNIMbXdEMvlhQfabvYndaa4sE8cA 1acBXB0BgOktezfa5WrrmtVe1qM4MLAQn2DzTH6xgyVVmd/EDw+GSWTEo4mwfgkFih/s mURqFepg36Ri0jD0qCXsfssjSGFa4+7ABY12qsmbUpScqQ0LTQA01cFbC47qREqVl0PX 60jA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Q5FE57mY; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-69134-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-69134-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id o42-20020a0561023faa00b0046d13cef75fsi117843vsv.293.2024.02.16.10.09.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Feb 2024 10:09:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-69134-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Q5FE57mY; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-69134-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-69134-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 67B411C21F82 for ; Fri, 16 Feb 2024 18:09:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7E720146915; Fri, 16 Feb 2024 18:06:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Q5FE57mY" Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 45848146008 for ; Fri, 16 Feb 2024 18:06:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708106804; cv=none; b=SDKxA8kHjtGtIaGZUuFMmgSKorozY3mB290kcT7m+CwArKFHGjyxggAizn52hcFHwt4QZ8avkw3Wv+jcYkQu2zfojhkrun+Fvo43VdoU3gQoqNehtS5mzuviqOII/R95YGpU1oAY0XOdr2PSlaOOf+vhyVGY9WSMqhhYqgoPm8I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708106804; c=relaxed/simple; bh=AGf1jpF8ueWGlL1VPqRKidiHEBIaTbTQYlw7Y3Hn+5I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZcxjgudJPpzeyzfxzjXuVK/8Hb6BbJ7NyEpeXFiIznB4J5/5i7KbUv0szhsvHMhjUpAfEP0kM58n0oSxBlf1GXNdAN9cLUunqST93ca4nfATIT/puhyUTkX3ZXQ8p25j+Av3xopvVvmot7cZ0bKirSap5vjhudB9a/1Kzeh6q+4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Q5FE57mY; arc=none smtp.client-ip=209.85.214.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-1d93edfa76dso8835175ad.1 for ; Fri, 16 Feb 2024 10:06:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708106803; x=1708711603; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=N9bk7H/R6dZtCT0NyxymPB9Me8HfbuqTGpGRRxjeyPY=; b=Q5FE57mYI4SwF31ybzAyFyLBzpjr+kn5mAiyU5FySGAqaBTDoJZuxjDb+wrb+dvPVb id2ZJksRE1FDB4roeR8mkB7KGmRdAz0zhnDSrM9rd2BWphAoZaS/Z3XYc1uKbFEJ5CyR JyPn8iQft2Ihwu5KkICUPo2M52u3zfijOslGq87rvrBIMV3ETGKX64Fwc6fh7FfuTAu4 3f3BC7u4du8VD+ivqE3ubSp/NH24lTNXz4oE49NWOhsGPkSiCNiPIIlRnEHbOpVbxEps yB3Ofb0PU3YAHKZjGHr6PmD7v5k3HO02hmh3upFat1oR3l8nbqMjyJoNlG6J67QjqC7U FzUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708106803; x=1708711603; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=N9bk7H/R6dZtCT0NyxymPB9Me8HfbuqTGpGRRxjeyPY=; b=i22hNDd10vr5uhrgAg/UHcBImto7hsv4+NUWZRwssErcnf6XHHZMf535Xgyp4Ujx5r 4ysxoRxptWvJ1pVRQFWzNTLHP8f/06bHibVdxHQZdi/uZKojk6tuRSZwwk92aYrx8Or3 jyaGuNDUwM1BTTGeIf9vpY9Rkj0ZQpjQa29fAqe0Q4YcW/jfyA3iojAKt+zduOR9mpzC 6qrLQ8Np2/I8R11DTE6lOc8v+JoeRpdrXE0nLctQ01FnvgRv44pTYbHQkex5XS7bKku+ PAfKPd9LRO/UVYlmFup+ILaZXT/+Jb4WnUHSpZ/a3FkcP+XZ0fVejve8pPIi3poilCzW HOQA== X-Forwarded-Encrypted: i=1; AJvYcCU1dE+XI/oNaZ/KX+EgjMmBvJERpB6HOulqaVCESVQ2iKjm07he7meviDIA3iy99RJhGAoD5FXRo+FkK04+Pp+RK+HIOPRKLPyq+6yP X-Gm-Message-State: AOJu0Yy2A9ZasqgwUt7Qy69VUYjqGb3EnTE94f+9PQcY8ZI4NHcQh6sz CK54cGVBlDeLVMIBpzFbJqGZw1F1MM/p00vTg+855V+mjia57aXCcDD9MJm/UP4= X-Received: by 2002:a17:902:dac1:b0:1d9:3101:c46d with SMTP id q1-20020a170902dac100b001d93101c46dmr7033665plx.55.1708106802661; Fri, 16 Feb 2024 10:06:42 -0800 (PST) Received: from localhost (dhcp-141-239-158-86.hawaiiantel.net. [141.239.158.86]) by smtp.gmail.com with ESMTPSA id u12-20020a170903308c00b001da11733477sm160054plc.75.2024.02.16.10.06.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Feb 2024 10:06:42 -0800 (PST) Sender: Tejun Heo From: Tejun Heo To: jiangshanlai@gmail.com Cc: torvalds@linux-foundation.org, linux-kernel@vger.kernel.org, allen.lkml@gmail.com, kernel-team@meta.com, Tejun Heo Subject: [PATCH 16/17] workqueue: Allow cancel_work_sync() and disable_work() from atomic contexts on BH work items Date: Fri, 16 Feb 2024 08:04:57 -1000 Message-ID: <20240216180559.208276-17-tj@kernel.org> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240216180559.208276-1-tj@kernel.org> References: <20240216180559.208276-1-tj@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791080004759250528 X-GMAIL-MSGID: 1791080004759250528 Now that work_grab_pending() can always grab the PENDING bit without sleeping, the only thing that prevents allowing cancel_work_sync() of a BH work item from an atomic context is the flushing of the in-flight instance. When we're flushing a BH work item for cancel_work_sync(), we know that the work item is not queued and must be executing in a BH context, which means that it's safe to busy-wait for its completion from a non-hardirq atomic context. This patch updates __flush_work() so that it busy-waits when flushing a BH work item for cancel_work_sync(). might_sleep() is pushed from start_flush_work() to its callers - when operating on a BH work item, __cancel_work_sync() now enforces !in_hardirq() instead of might_sleep(). This allows cancel_work_sync() and disable_work() to be called from non-hardirq atomic contexts on BH work items. Signed-off-by: Tejun Heo --- kernel/workqueue.c | 62 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 15 deletions(-) diff --git a/kernel/workqueue.c b/kernel/workqueue.c index f6ea25628701..00eac314e62a 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c @@ -4004,8 +4004,6 @@ static struct worker_pool *start_flush_work(struct work_struct *work, struct pool_workqueue *pwq; struct workqueue_struct *wq; - might_sleep(); - pool = get_work_pool(work); if (!pool) return NULL; @@ -4072,7 +4070,32 @@ static bool __flush_work(struct work_struct *work, bool from_cancel) if (!pool) return false; - wait_for_completion(&barr.done); + if ((pool->flags & POOL_BH) && from_cancel) { + /* + * We're flushing a BH work item which is being canceled. It + * must have been executing during start_flush_work() and can't + * currently be queued. If @work is still executing, we know it + * is running in the BH context and thus can be busy-waited. + * + * On RT, prevent a live lock when current preempted soft + * interrupt processing or prevents ksoftirqd from running by + * keeping flipping BH. If the tasklet runs on a different CPU + * then this has no effect other than doing the BH + * disable/enable dance for nothing. This is copied from + * kernel/softirq.c::tasklet_unlock_spin_wait(). + */ + while (!try_wait_for_completion(&barr.done)) { + if (IS_ENABLED(CONFIG_PREEMPT_RT)) { + local_bh_disable(); + local_bh_enable(); + } else { + cpu_relax(); + } + } + } else { + wait_for_completion(&barr.done); + } + destroy_work_on_stack(&barr.work); return true; } @@ -4090,6 +4113,7 @@ static bool __flush_work(struct work_struct *work, bool from_cancel) */ bool flush_work(struct work_struct *work) { + might_sleep(); return __flush_work(work, false); } EXPORT_SYMBOL_GPL(flush_work); @@ -4179,6 +4203,11 @@ static bool __cancel_work_sync(struct work_struct *work, u32 cflags) ret = __cancel_work(work, cflags | WORK_CANCEL_DISABLE); + if (*work_data_bits(work) & WORK_OFFQ_BH) + WARN_ON_ONCE(in_hardirq()); + else + might_sleep(); + /* * Skip __flush_work() during early boot when we know that @work isn't * executing. This allows canceling during early boot. @@ -4205,19 +4234,19 @@ EXPORT_SYMBOL(cancel_work); * cancel_work_sync - cancel a work and wait for it to finish * @work: the work to cancel * - * Cancel @work and wait for its execution to finish. This function - * can be used even if the work re-queues itself or migrates to - * another workqueue. On return from this function, @work is - * guaranteed to be not pending or executing on any CPU. + * Cancel @work and wait for its execution to finish. This function can be used + * even if the work re-queues itself or migrates to another workqueue. On return + * from this function, @work is guaranteed to be not pending or executing on any + * CPU as long as there aren't racing enqueues. * - * cancel_work_sync(&delayed_work->work) must not be used for - * delayed_work's. Use cancel_delayed_work_sync() instead. + * cancel_work_sync(&delayed_work->work) must not be used for delayed_work's. + * Use cancel_delayed_work_sync() instead. * - * The caller must ensure that the workqueue on which @work was last - * queued can't be destroyed before this function returns. + * Must be called from a sleepable context if @work was last queued on a non-BH + * workqueue. Can also be called from non-hardirq atomic contexts including BH + * if @work was last queued on a BH workqueue. * - * Return: - * %true if @work was pending, %false otherwise. + * Returns %true if @work was pending, %false otherwise. */ bool cancel_work_sync(struct work_struct *work) { @@ -4287,8 +4316,11 @@ EXPORT_SYMBOL_GPL(disable_work); * Similar to disable_work() but also wait for @work to finish if currently * executing. * - * Must be called from a sleepable context. Returns %true if @work was pending, - * %false otherwise. + * Must be called from a sleepable context if @work was last queued on a non-BH + * workqueue. Can also be called from non-hardirq atomic contexts including BH + * if @work was last queued on a BH workqueue. + * + * Returns %true if @work was pending, %false otherwise. */ bool disable_work_sync(struct work_struct *work) {