Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp5947685rwr; Mon, 24 Apr 2023 11:11:43 -0700 (PDT) X-Google-Smtp-Source: AKy350YXK5WnfPGh4IMOI16k3WBfV6XVtgAiUh8VdgZSvflkWPdwFc1N9iMZsXsjpC+WPRu/EoM/ X-Received: by 2002:a05:6a21:970c:b0:f3:b764:5de3 with SMTP id ub12-20020a056a21970c00b000f3b7645de3mr5864350pzb.48.1682359903121; Mon, 24 Apr 2023 11:11:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682359903; cv=none; d=google.com; s=arc-20160816; b=tvgB7F38uYkdR1cJu5PvWL82QNz+UqqGq5LPina/B8/Ia72UwGoXGPALG0jRd3/+y6 2l/igcjL/VTiGiVVqpxGKDZoWWcHyCq5lueOVykrCgkV62eUpkJMeknowiEStDdwkB8q b7XDG/zCAZg1MDUr1XAF9pkKNoaWnqwPjXyOE/gzFNLZYoDeqaHCmH1Iqyp3EKjaq+5d 49K3pZJKGOjgQdra5Q4gtg4sW8Z/IM/5GSDI/O1kD+fMQhAnyBlYYNVDpTQ9KTOL6sap n09+Q7PoL+LrkQB/BbzT+TF1pJZsj+o2Dr+1iK7X5ftBqyHEURb/cC7X2aTz6Wn3PdbD dPDg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=L5P4snIf93rapeABc13fU6yAaNb3CDkyp3GUI2CNFsE=; b=qIw+HjHXEJU9GmIdoDq8CjqNEtGTtbLqw9tgo+DS6yp1pA0wCgv8QewVyn4lokDEpn QBWnm3hvjUWbUBEb5FnMeYVauTMwULaNDrvn+xLZ9PhXhtdD8WVf7OchUZvroX36webq SeFu1prHe3Pa7YXSetT+ilAsbh1J7rWiq/WYSpZhMR4E2fF1InDIe5bGpDe+MvXA6kSs G6U2Q2AeTa87Qs2AdAqKLtBe3hJbv1A9Nv6cMMxDKOagiv90TuenNMPE+vNUVw5/+flC bqV04XdN2QjuQsI3IDbI4LKI6wU5EK7zaNYDpgah0umFGc+pHsBPPvYGkI9SFrmCzVPi Z2ug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=bz3XKFtg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u71-20020a62794a000000b0063601bd1cc9si11585925pfc.321.2023.04.24.11.11.31; Mon, 24 Apr 2023 11:11:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=bz3XKFtg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231694AbjDXSJX (ORCPT + 99 others); Mon, 24 Apr 2023 14:09:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48822 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229688AbjDXSJV (ORCPT ); Mon, 24 Apr 2023 14:09:21 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D93D86E91; Mon, 24 Apr 2023 11:09:20 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 73B2C622EC; Mon, 24 Apr 2023 18:09:20 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C8417C433D2; Mon, 24 Apr 2023 18:09:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1682359759; bh=DCPhmzjXeq8Lo44Zu22jyvJqFljz6eHBBKLb+VcR4h8=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To:From; b=bz3XKFtgJK/4SHg5scRNDEawqSH+rwy+/+N0pE2HH+hvD7acuajV2zGUCNYwwENxx bXHiZJV3KsaoFcF7pcjaqkzN9QBFB4vTIoIXDap3eAFrYavW1CinS77iPtR8Ga0knG gVfg4HyYq6rD0Bil+T2ZdEbOwbvlaEP4HY2O9APbrngyfZmK4Q+XGYkFMN/2DEAMnH 8kwmcyoXqA0Q+WB4rIwAOPCXI4WypsacITSB+qAmJFAa+KvV6cVuEAYfISvcuomA+W DJWNSwYu4Iwtkb66oO7/ZOw+ZhhwZGC7HNpuxoiSIPXiqQA71nsD0wp44s113eici6 8LkmgE8FzyFcw== Received: by paulmck-ThinkPad-P72.home (Postfix, from userid 1000) id 5C48315404E7; Mon, 24 Apr 2023 11:09:19 -0700 (PDT) Date: Mon, 24 Apr 2023 11:09:19 -0700 From: "Paul E. McKenney" To: Wander Lairson Costa Cc: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Will Deacon , Waiman Long , Boqun Feng , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , Valentin Schneider , "Eric W. Biederman" , Michael Ellerman , Andrew Morton , Oleg Nesterov , Kefeng Wang , "Liam R. Howlett" , Kees Cook , Christian Brauner , Andrei Vagin , Shakeel Butt , open list , "open list:PERFORMANCE EVENTS SUBSYSTEM" , Hu Chunyu , Thomas Gleixner Subject: Re: [PATCH v6 2/3] sched/task: Add the put_task_struct_atomic_safe() function Message-ID: <9ff7ea3a-b48b-4cff-851e-179713393531@paulmck-laptop> Reply-To: paulmck@kernel.org References: <20230414125532.14958-1-wander@redhat.com> <20230414125532.14958-3-wander@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230414125532.14958-3-wander@redhat.com> X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Apr 14, 2023 at 09:55:28AM -0300, Wander Lairson Costa wrote: > Due to the possibility of indirectly acquiring sleeping locks, it is > unsafe to call put_task_struct() in atomic contexts when the kernel is > compiled with PREEMPT_RT. > > To mitigate this issue, this commit introduces > put_task_struct_atomic_safe(), which schedules __put_task_struct() > through call_rcu() when PREEMPT_RT is enabled. While a workqueue would > be a more natural approach, we cannot allocate dynamic memory from > atomic context in PREEMPT_RT, making the code more complex. > > This implementation ensures safe execution in atomic contexts and > avoids any potential issues that may arise from using the non-atomic > version. > > Signed-off-by: Wander Lairson Costa > Reported-by: Hu Chunyu > Cc: Paul McKenney > Cc: Thomas Gleixner > --- > include/linux/sched/task.h | 31 +++++++++++++++++++++++++++++++ > kernel/fork.c | 8 ++++++++ > 2 files changed, 39 insertions(+) > > diff --git a/include/linux/sched/task.h b/include/linux/sched/task.h > index b597b97b1f8f..5c13b83d7008 100644 > --- a/include/linux/sched/task.h > +++ b/include/linux/sched/task.h > @@ -141,6 +141,37 @@ static inline void put_task_struct_many(struct task_struct *t, int nr) > > void put_task_struct_rcu_user(struct task_struct *task); > > +extern void __delayed_put_task_struct(struct rcu_head *rhp); > + > +static inline void put_task_struct_atomic_safe(struct task_struct *task) > +{ > + if (IS_ENABLED(CONFIG_PREEMPT_RT)) { > + /* > + * Decrement the refcount explicitly to avoid unnecessarily > + * calling call_rcu. > + */ > + if (refcount_dec_and_test(&task->usage)) > + /* > + * under PREEMPT_RT, we can't call put_task_struct > + * in atomic context because it will indirectly > + * acquire sleeping locks. > + * call_rcu() will schedule delayed_put_task_struct_rcu() > + * to be called in process context. > + * > + * __put_task_struct() is called called when > + * refcount_dec_and_test(&t->usage) succeeds. > + * > + * This means that it can't "conflict" with > + * put_task_struct_rcu_user() which abuses ->rcu the same > + * way; rcu_users has a reference so task->usage can't be > + * zero after rcu_users 1 -> 0 transition. > + */ > + call_rcu(&task->rcu, __delayed_put_task_struct); This will invoke __delayed_put_task_struct() with softirqs disabled. Or do softirq-disabled contexts count as non-atomic in PREEMPT_RT? Thanx, Paul > + } else { > + put_task_struct(task); > + } > +} > + > /* Free all architecture-specific resources held by a thread. */ > void release_thread(struct task_struct *dead_task); > > diff --git a/kernel/fork.c b/kernel/fork.c > index 0c92f224c68c..9884794fe4b8 100644 > --- a/kernel/fork.c > +++ b/kernel/fork.c > @@ -854,6 +854,14 @@ void __put_task_struct(struct task_struct *tsk) > } > EXPORT_SYMBOL_GPL(__put_task_struct); > > +void __delayed_put_task_struct(struct rcu_head *rhp) > +{ > + struct task_struct *task = container_of(rhp, struct task_struct, rcu); > + > + __put_task_struct(task); > +} > +EXPORT_SYMBOL_GPL(__delayed_put_task_struct); > + > void __init __weak arch_task_cache_init(void) { } > > /* > -- > 2.39.2 >