From patchwork Thu Sep 26 22:49:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frederic Weisbecker X-Patchwork-Id: 13813707 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id ECAE9CCFA07 for ; Thu, 26 Sep 2024 22:49:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D7E396B0096; Thu, 26 Sep 2024 18:49:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D2EC96B009B; Thu, 26 Sep 2024 18:49:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BF5AC6B009C; Thu, 26 Sep 2024 18:49:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 9F9416B0096 for ; Thu, 26 Sep 2024 18:49:48 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 0DEF91C553D for ; Thu, 26 Sep 2024 22:49:48 +0000 (UTC) X-FDA: 82608383256.07.8FB7CC6 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf07.hostedemail.com (Postfix) with ESMTP id 6240140010 for ; Thu, 26 Sep 2024 22:49:46 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=TZybb+rL; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf07.hostedemail.com: domain of frederic@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=frederic@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727390969; a=rsa-sha256; cv=none; b=Fvp+baRdbGXlJGUqDfGKe4mGAtk+Ojq+FbCIAOsAtnIII0taBqyKX7BKad7iZEAJ43GMxC +q3Oei+cVCuNYPzwOG14f25AEN1zgegR9K1MaW82wIds/10OWKAneZD6/N1/1bDnFF1A9s /ijiysG02dcf8ouVXFerxJZiSBXlx0c= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=TZybb+rL; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf07.hostedemail.com: domain of frederic@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=frederic@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1727390969; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=fAsnL8h86z9fMuyJ4gi6Hc0Ny9v4bVl8bJ3ZfSfvpPs=; b=pn1zKv5u3o0v8zIWZtzNpyqOCJ5VG3YBCxZ4s/9HyxGRyUSq1mraV+r5HwgOh3p2w4oehE OrbW/ea1B4Ey9cOwSTi9KnKN70sA9KwwV17EMX7/fHdW6ZSF/JHbbGMFta2OFZ3wfQq2bR ecowf6mnjqhcrhwSh3jtbAMtLxvn69s= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 7F833A44219; Thu, 26 Sep 2024 22:49:37 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 14BFEC4CECE; Thu, 26 Sep 2024 22:49:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727390985; bh=OmLFF6xgwbA0hM9UsQOF4k2zgh/bcASGVcZgCAmI/wE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TZybb+rLGew6y4q8nSczjj8DHRzJC7OPnSggLAYdW0ADMoiewKcozZLiLAW5sPq4b z/w7b2NScoCYFn5Y/Vu1k0LnkOv267r9y6sguOW4Nv2aO4I7KXAEGOqyeRdLQChUMM 4cTaoClZ1K67txUjJeVPMY4Il+eDer2maAH5KqQoz0Z9Yw9rGHdsmUmNnXCXQE0T10 MCioZOjqH18cVl7ms8sVxuKS31EpXnLgg1Dlb0+4pwj9q3OZxhffnGHGs/ou8q3fwC YN471CMyZJ3Ua5hJe27BPWBvBRj3fkTtvgx7DdcqZcLHY6H98mFfFSQVAxEosOWDlo CNXTOM4e0Ln8Q== From: Frederic Weisbecker To: LKML Cc: Frederic Weisbecker , Andrew Morton , Kees Cook , Peter Zijlstra , Thomas Gleixner , Michal Hocko , Vlastimil Babka , linux-mm@kvack.org, "Paul E. McKenney" , Neeraj Upadhyay , Joel Fernandes , Boqun Feng , Zqiang , rcu@vger.kernel.org, Uladzislau Rezki Subject: [PATCH 12/20] kthread: Make sure kthread hasn't started while binding it Date: Fri, 27 Sep 2024 00:49:00 +0200 Message-ID: <20240926224910.11106-13-frederic@kernel.org> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240926224910.11106-1-frederic@kernel.org> References: <20240926224910.11106-1-frederic@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: mz1uoda4sstmbssufbf3rayrr5kqy13h X-Rspamd-Queue-Id: 6240140010 X-Rspamd-Server: rspam02 X-HE-Tag: 1727390986-158171 X-HE-Meta: U2FsdGVkX1+2//wQUiGl1tmWet+jFdXEJuvsCzBrsnZG10EB/wVsHKpbHhK2i7IHzlwRe0aO/OREAyEVFuDM3EfkzN8s4pHooSn2y+S0D2kOsHWaWJuIf4Gf4DQCVQsoBM3nmM/vgP2nnYD4XtzIhR6m/NhS2mR5sw/coWSCCHyE8f8t3PF+M9ZIM04mlmDcyC+UcrgLAsSCPSFZfdwx4WZXkm4tFJIjYYrLkEF7grjlhrSw/5oWn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=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Make sure the kthread is sleeping in the schedule_preempt_disabled() call before calling its handler when kthread_bind[_mask]() is called on it. This provides a sanity check verifying that the task is not randomly blocked later at some point within its function handler, in which case it could be just concurrently awaken, leaving the call to do_set_cpus_allowed() without any effect until the next voluntary sleep. Rely on the wake-up ordering to ensure that the newly introduced "started" field returns the expected value: TASK A TASK B ------ ------ READ kthread->started wake_up_process(B) rq_lock() ... rq_unlock() // RELEASE schedule() rq_lock() // ACQUIRE // schedule task B rq_unlock() WRITE kthread->started Similarly, writing kthread->started before subsequent voluntary sleeps will be visible after calling wait_task_inactive() in __kthread_bind_mask(), reporting potential misuse of the API. Upcoming patches will make further use of this facility. Acked-by: Vlastimil Babka Signed-off-by: Frederic Weisbecker --- kernel/kthread.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/kernel/kthread.c b/kernel/kthread.c index db4ceb0f503c..1527a522cdd3 100644 --- a/kernel/kthread.c +++ b/kernel/kthread.c @@ -53,6 +53,7 @@ struct kthread_create_info struct kthread { unsigned long flags; unsigned int cpu; + int started; int result; int (*threadfn)(void *); void *data; @@ -382,6 +383,8 @@ static int kthread(void *_create) schedule_preempt_disabled(); preempt_enable(); + self->started = 1; + ret = -EINTR; if (!test_bit(KTHREAD_SHOULD_STOP, &self->flags)) { cgroup_kthread_ready(); @@ -540,7 +543,9 @@ static void __kthread_bind(struct task_struct *p, unsigned int cpu, unsigned int void kthread_bind_mask(struct task_struct *p, const struct cpumask *mask) { + struct kthread *kthread = to_kthread(p); __kthread_bind_mask(p, mask, TASK_UNINTERRUPTIBLE); + WARN_ON_ONCE(kthread->started); } /** @@ -554,7 +559,9 @@ void kthread_bind_mask(struct task_struct *p, const struct cpumask *mask) */ void kthread_bind(struct task_struct *p, unsigned int cpu) { + struct kthread *kthread = to_kthread(p); __kthread_bind(p, cpu, TASK_UNINTERRUPTIBLE); + WARN_ON_ONCE(kthread->started); } EXPORT_SYMBOL(kthread_bind);