From patchwork Mon Sep 16 22:49:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frederic Weisbecker X-Patchwork-Id: 13805806 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 4D0A9C3ABCB for ; Mon, 16 Sep 2024 22:50:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6370E6B0089; Mon, 16 Sep 2024 18:50:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5E6646B008A; Mon, 16 Sep 2024 18:50:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 486D96B008C; Mon, 16 Sep 2024 18:50:06 -0400 (EDT) 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 2A7EF6B0089 for ; Mon, 16 Sep 2024 18:50:06 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id C9F85120176 for ; Mon, 16 Sep 2024 22:50:05 +0000 (UTC) X-FDA: 82572095970.22.868B1A3 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf06.hostedemail.com (Postfix) with ESMTP id 1A065180009 for ; Mon, 16 Sep 2024 22:50:03 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="nRLAXb/Q"; spf=pass (imf06.hostedemail.com: domain of frederic@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=frederic@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726526857; 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=cjaBm1fH+rcbzdDAj2QxrY+3nx5buM39J8og4oWKIUQ=; b=680po28xzk7js3k1ZcPzH4xL6d1JdnIqhqWYw5HDVX2wRpiLHmFpxvQDahIl+MN9aHM9Qb CsD9GZmyTQ9t9A33RXWlb4uk2Pnd5tfkfK4jyMhIJwY/PCz05AxpIjoRrrJu3zV5hT9GI3 M2gnjo7ejJDDYrD4sGwW/rE979xWrUQ= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="nRLAXb/Q"; spf=pass (imf06.hostedemail.com: domain of frederic@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=frederic@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726526857; a=rsa-sha256; cv=none; b=rYaJC+bvOrLZV+LlPsQNVInEn2iX1UWnb2HDhfttXU94KVWQJJVD4rujCIOpr5/MO0jhjL QLAxyN1BzMUaRzZ0+dtGTwgCoBhDyLPfOe3MovZqIX6rkzyq6J2daS6O2dWclN9WsRvBEv qBnjLTTT1ZtYcN7qfTEK6KkGhf3Yyl4= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 4FDAF5C216B; Mon, 16 Sep 2024 22:49:59 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AAB21C4CECC; Mon, 16 Sep 2024 22:49:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1726527002; bh=sbJMoxIqu4qr1nD/+Ksno6FjhK0vmuAI9ZNJjfPGBuI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nRLAXb/Qy6nANXMBkUtDMV6bPI7XGa+ARiNyBck4/MFprVyaKt2CssHmWQE0gtmxt qlPRfGIFG2mKyThl6aB/HQrTcLkSeZg9zwzTm+v+Mu6O8hWpCV7eE4E8xZynKD/kfP bb+OHznYw6umYjFoFjztRzkc89/FWva73yWbEBLrVbnzQFJTKH/0MREoqSs6mRgu5l kifMAWClluK9fMjTGYXhWwaqoLZMZIFN/6dVGbsemJacgbqtar1+tnVjPGavfWjBbQ NmJ+9iSLA6lO2ZCl7l8A3RBTZ9KnPoBWRM5U32Ye3nQSFrmE3pcpLnAeX+OOKQhYQk gWH/sCQ25r2Jg== 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 Subject: [PATCH 11/19] kthread: Make sure kthread hasn't started while binding it Date: Tue, 17 Sep 2024 00:49:15 +0200 Message-ID: <20240916224925.20540-12-frederic@kernel.org> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240916224925.20540-1-frederic@kernel.org> References: <20240916224925.20540-1-frederic@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 1A065180009 X-Stat-Signature: sjhpmstkssbpf154pui63963dh3p63pa X-Rspam-User: X-HE-Tag: 1726527003-709625 X-HE-Meta: U2FsdGVkX19S0sUbsRg05YrbtYw51cTcgG6ZrCx/jNK1s9odUKAglqB3z1V6JzvaAdJv0WEY5jE3TuULVvnbaTFrfui3WvS+1SgUmQV7EU0W5wbhuAW21CvW6bGFiBWYBEUdV2z45I3BKWPHv4N19cP1iraP1bD1U4sjDVu5QV99QLjxAxxCuqp5O+RzsYcqzOpwzdBb6WZw9saLxECXEqavZ5+PFSCSDyuzCLQ6DXH13npG5FSXfsj8HQttnJNtiD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amicity=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 f7be976ff88a..ecb719f54f7a 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);