From patchwork Fri Jul 26 21:56:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frederic Weisbecker X-Patchwork-Id: 13743186 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 67632C3DA4A for ; Fri, 26 Jul 2024 21:57:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F25856B0088; Fri, 26 Jul 2024 17:57:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ED6026B0089; Fri, 26 Jul 2024 17:57:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D9E196B008A; Fri, 26 Jul 2024 17:57:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id BC3CB6B0088 for ; Fri, 26 Jul 2024 17:57:37 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id EE5EE1C057D for ; Fri, 26 Jul 2024 21:57:36 +0000 (UTC) X-FDA: 82383266112.17.EB10236 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf17.hostedemail.com (Postfix) with ESMTP id 4CE634000A for ; Fri, 26 Jul 2024 21:57:35 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=dWmrsooC; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf17.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=1722031018; a=rsa-sha256; cv=none; b=Dojhv2GVl57noXn6NChd3vuo2YJx8PZ5psxszrGgN85kGRQr5p16YemrorOqzXsHT1LJmL VfUYcnOBy5ZMNGWRaoyctbzRIMIVm7/Aatwa6Pkr85g+QTBqeWtmjzaedboDrC+yq30NZJ KGksvv6iBFQ/EMa+FA3hMjA4Gw6d7WI= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=dWmrsooC; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf17.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=1722031018; 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=G+lSjvJQqRDoZKM4s7Tcf6sdPWaFThzlyfeuxJg7u60=; b=KxGbrEz69b3H5QxDdtlk/GiF5cbO91pQ1ab6RpGLe4nJYQErDKkhG+b+PNKxbTpjEqYSdi KAVbMnQjRGCahru8d9mM7/K/43oF66onEVnkvR7nDEAY2HALLkVj/ZNdzk7mM+EUmELMSP 4jn1sJ8LtkraSHPVjN+qcL+2iHbhea4= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 2EDF86199B; Fri, 26 Jul 2024 21:57:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A1FDDC4AF09; Fri, 26 Jul 2024 21:57:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722031053; bh=MUWUbgWSkKV4GLdom7VkftM4X5LXDstggWbHIa2p29w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dWmrsooCdYxUdk/aCUzDpll4gueCMQsfkinYrKzAWc1v/GAaIsHjE3rm8GWfqufpy gKRyDSNOjiXVu5BGliAyGQJhFUrk1RQuDB0kNKtBzDasPdPZvrIqiCgBy2WKWw0dXy zwLgigU+SitEoGE7TrTGk6i+9RF0xdScyWgbLzJH5VQihFensb8LOZ/Astq125xDGo bUggEcJQVzgvcNRlDFR4LEap94c9Tg6FMyRYZqG70PLQLhhXB7cvyZa2XUx1ECWnPF KMrNN2lDQKT3EJi/PW5+2xQjAbSKxLd7az60jjUo0uD2e8O5hDCpQPdjuMG827Vb1O aQ0djG1ROPjcg== 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: [RFC PATCH 11/20] kthread: Make sure kthread hasn't started while binding it Date: Fri, 26 Jul 2024 23:56:47 +0200 Message-ID: <20240726215701.19459-12-frederic@kernel.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240726215701.19459-1-frederic@kernel.org> References: <20240726215701.19459-1-frederic@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 4CE634000A X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: huen16wthfyxsd96zs49tjbq5respc9i X-HE-Tag: 1722031055-517014 X-HE-Meta: U2FsdGVkX18uiazs19SM3/bsF3hgIElG7Z8m94oXSbizDvu/o5Wz2IRJrn5Pm2ifioXqiUg+JPgU7/n+xg+k6YNW+DDq5gdUGpuo/en1DttZKsqAk9UQUmjrESRPt4bh3Nawt2Tq+3NmEHHm4VmSXju+LBUL+riYs9G+uwV5R9C/CoXcdY9uR0q/WhYUrJ+Po0wU2iaGkMWAR7EFlGdM8Z9NnjUCG9/EPadVHepkVdi/r40v9fuOE4VmXq8DHG5eaiIMVw5NKDH7YxKGxYnMi8iLD6LA7PjCy/eEQORzYugaVrKcD1fPQekhE+kVjvyqYn1aCFzQ6XExn7TIEraXY4/exDQuhyYbDTlPHnCwPozpbKQXSsh6oNuVClYMcFTbBXrcDR0JqgKF+aATJpf+hNNl7TiPltHJGtM6pO3eE555/rRBXU1m2JjeAVu8MD++C8USzQCC2Y6iIrr4/Lzp5YJl/nrNZtFJH0CQitiTJ6DVXl0X7M2CASrw+WUMF7W1COjwg1p8kT6LCsqDqRyKs7dHuGbJn0QI9ia2QSI1lDb+3Aiy+mrAqhKrkRN63cu4qQVh6YipWUHq83uIjEXI6XiLIrgkZRtEwNzosFf9OsbTFc4QwhkWlM3m2LL6ISrrFMYca9cOEoXQEV5NMTum1LolhUSuVs7gnCjDBQrW7iESyLUZ5FIp/xzxyjvTV0cKsdD1Kg6j7DY+1RbIZ7hU80mlpdmFVUSgurYnY1Z8LpLjdzZX1gRdu276nNdJylKezpmgYNyNTvRYosYPZIWUyjeiibFd34qlFeen7o0rwzZUwxUg0U2545ffpoB4dXvQdKvo+fP5XrDTs/47p74ULk9hOlnk26499dqdvzuorVTgcsI+g3WMJwgmLhAUeKY7vWbE6x4kw5D0JSAQessWhxy4yqJjOgsmu4p2XgKosV4A1GSnpKhDQMlUZdTEEbyzfk5TDmAie+R/M56kPyj 5zK/adP4 +zX8pKKmqR3h5LVZhoSJ11tRocUCa3ULuvJ+8UDFuagSUKVS9afD2GJhCYV86Gn37YMVWcb5sCq1bKx3xefk+j+EQ7JjwKsLc5H9b/9txVAryAiwkkoC2aJjE+KrRmX4qtJCj2mUA+HfObwOfwNMZiu/GXaicufG1xHPZlheya4KXQnyX/mI3ggLtTcEnbXkvVZJQ3sDgpqylUbDujlr3s+mk6HxFgfqMPPkFLzhZKqLwT04ujLkN+s0cOe4Hr8Y7FDzpfnnsJqLwPjGT2KJTJafQEr2iP1xKa2dt/EwrC9o56e5luoHzwgaUPg== X-Bogosity: Ham, tests=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. Signed-off-by: Frederic Weisbecker Acked-by: Vlastimil Babka --- 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);