From patchwork Tue Nov 12 14:22:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frederic Weisbecker X-Patchwork-Id: 13872296 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 1738FD42B85 for ; Tue, 12 Nov 2024 14:23:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9CACB6B00E7; Tue, 12 Nov 2024 09:23:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9764F6B00F7; Tue, 12 Nov 2024 09:23:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 817396B00F8; Tue, 12 Nov 2024 09:23:33 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 5DB136B00E7 for ; Tue, 12 Nov 2024 09:23:33 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 13DF8160191 for ; Tue, 12 Nov 2024 14:23:33 +0000 (UTC) X-FDA: 82777660686.14.52794FE Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf04.hostedemail.com (Postfix) with ESMTP id 58B754001F for ; Tue, 12 Nov 2024 14:22:38 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=bX6I+zoH; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf04.hostedemail.com: domain of frederic@kernel.org designates 139.178.84.217 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=1731421235; 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=N6Kj0gZYTbZYKQG2dUeSbKMk8SIG5ub+2cY5qRXJj1I=; b=xSoB/ofNY7DoAvNj40+dcRj7uzsqVT5Yg6nlZtG9XwLTlMA4cvt+D8buyQtjtxKzLbiPK6 IE+5wAuNkvcU/bp7QW13lY/ef0DQINsqm/w8aEIlNT16ktccOMKWA/SEewazSp+FEFq5ZR brqgiqkefb5seJ3umIIwEBfGKikRsck= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=bX6I+zoH; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf04.hostedemail.com: domain of frederic@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=frederic@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731421235; a=rsa-sha256; cv=none; b=CdcBOtnG0fTYrxWhUiIrAqOy3+4iy73hgAKb4LXLz9u2ELHv0ClQPMpK6XRuj+QrsAS4zz YYUgjAxmQy6wScyPx/o9+XvK9XDlMo2Dga/0m1Abqu4EORGkXhlwzXIQXqV4b+Wd7oHvHK KHCukd3pw7/5/JFS29Mui4aZJ0w+PjY= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 210265C54E8; Tue, 12 Nov 2024 14:22:46 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 12BFCC4CECD; Tue, 12 Nov 2024 14:23:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731421410; bh=1Mtlm//q48SrsPqs3+PH5LEw1zSw/x3ZR0//41DWXT0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bX6I+zoHIDtpnApj8lTc4/kUq9/Avb5U6PIJXGr6R7ZH+w5tkNcKKlu21bICUDGN/ vayr3D5njBiqePvwXiwHyfN3kXc06v69j6prrZAqfNIOoSrm+C+z3Q4pL4M9q7C9uS wuZcO0UGrrDCK82FPd8TKMDwRf9wA/+UEHiQzK67WXEbvJsJe0NzgqK9g5y85trGjw 1q7fRsyVwt8VkR8OxvGOhleSa7Ia/nqon3h/KDe5lg2kCq7+DLI23O2etxWOwsasvC OoogZcMGZcCTjUSo3xgRwG4+kM9yXk8NPYiZZoznEPGulBno+uw9HmRgV70VCAv/Ql rD/v3aloGzdYQ== 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 13/21] kthread: Make sure kthread hasn't started while binding it Date: Tue, 12 Nov 2024 15:22:37 +0100 Message-ID: <20241112142248.20503-14-frederic@kernel.org> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20241112142248.20503-1-frederic@kernel.org> References: <20241112142248.20503-1-frederic@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 58B754001F X-Stat-Signature: 1ekmetpwy5w9mr5ogrgmi6jjdh6334pw X-Rspam-User: X-HE-Tag: 1731421358-625119 X-HE-Meta: U2FsdGVkX1+CxfQuljOjFzLEFDlZWQ4caZYmfo7aJEBcHXp2XVuv4JezvdhNdAFFGEmKk0GCYZOTASY3U9qNS0UhGfqGitAlUgLzjDYTkRB2FfdVudeNMT/d+/x/WwMvdxgupBFbdyyWHMW/BjXPT4UOwhmP/+EoC2+a8E0pX47Xxa/+1mTlUKHc4cNgiXfmzRUQwUfnPbuX622wrMrMx7af609wd1XgPG5HFazRdsku8NC45G6io7jK7E72p8xjr/1pzZkm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=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 9bb36897b6c6..b9bdb21a0101 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);