Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp5985009rwr; Mon, 24 Apr 2023 11:48:57 -0700 (PDT) X-Google-Smtp-Source: AKy350YKZqfUzfo4UrlSdtQAHH3rVBq6NFNPwObXVMe7lmDPZNM0HOUGXpvED/UWqt1QVlj9TTbV X-Received: by 2002:a05:6a21:1195:b0:f0:5f9a:f5d5 with SMTP id oj21-20020a056a21119500b000f05f9af5d5mr15939413pzb.41.1682362136789; Mon, 24 Apr 2023 11:48:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682362136; cv=none; d=google.com; s=arc-20160816; b=xla83TsjxlwVZzoAxRiv3muG8zZB6X3TusmHZhnxONeNO34/vApnv1nBwExjrQVcW0 S/YQyXT8v43NSnphNRoKEPVcm/+WeYlMgNd9L0tczXi4S6kOcLLiDOM3TEEcBDR90VxB PeMSyRtS3/rJQfy68CgjGVHTmC207CEkuihbYCeewy1Oj9O3KcGSgOCIlCkkwdpV6WUE Zf5huGcOwdo3zp7uyJZZpoKeaqwRXd8rbVg6kNoprFeMhvzAksDliN+eotM9TIayWn0P kpq0zCh35VYMnVIzyzw5+BK4tPXALoB2GmtkP9EQbr5Fg6xXeOx9+PsB1pzfHW6reth3 QTqA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=jzfkRQJBM4MmbV2JBtc9KiZMKD+rx5eFNORnbfXZFvs=; b=FTVgpSHwFHOkHr+nU6tkTZVXsW91ZtaaZEjQHso6ZkFnj/tFR8VxUkxc+7zCBRkeXU 1dk0Uv4o7C+dLKbwYmSIXQJCox088R9tx6w2tLzQOnOGemEKE0ABUm+v72TZPQCG33+F +JVxSjN4NaIeu4oC3HMpR0huUOAjtl+IbwMLlKW8T4I1x5ZVHJ9VuiBMIGk6vgzAYaH+ Hjv4fc/vO8OtY4wuBm7d8G4rpIyYpVkKlrzkWYBeYnVEHMh8eOYVf3ULOcRaC1+QJg6a KjEZHMoJfRdTN3AFxGG8b0d76B/65bz1mb51E/CCKtZ8TKv9EVoUupqJJBI3hTCA+/+4 6pIQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Kt5wGxDR; 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=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s6-20020a656446000000b00517aa9db92dsi11055291pgv.396.2023.04.24.11.48.45; Mon, 24 Apr 2023 11:48:56 -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=@redhat.com header.s=mimecast20190719 header.b=Kt5wGxDR; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232334AbjDXSoN (ORCPT + 99 others); Mon, 24 Apr 2023 14:44:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42476 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232389AbjDXSoM (ORCPT ); Mon, 24 Apr 2023 14:44:12 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E5F9F6183 for ; Mon, 24 Apr 2023 11:43:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1682361804; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jzfkRQJBM4MmbV2JBtc9KiZMKD+rx5eFNORnbfXZFvs=; b=Kt5wGxDR7DPAqBvRdyJDynDlIpWlB9aEMVkUCTqOOdboYOeNpUqHmqxOAO2bnNmNqUNbK9 eGGrk17AUOmB6JX4LZEZXwC4D3EnKwsDVljvLQpLc+cgsJlluJGSTuUd2plbfecfnkm2AP E0hcs8ORDvOxLg/4YFG1f/ciYYrIMoM= Received: from mail-pj1-f69.google.com (mail-pj1-f69.google.com [209.85.216.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-417-wJ7iHbDkNIOb4aYWSfTeRA-1; Mon, 24 Apr 2023 14:43:22 -0400 X-MC-Unique: wJ7iHbDkNIOb4aYWSfTeRA-1 Received: by mail-pj1-f69.google.com with SMTP id 98e67ed59e1d1-2473661358eso4774478a91.1 for ; Mon, 24 Apr 2023 11:43:22 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682361801; x=1684953801; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jzfkRQJBM4MmbV2JBtc9KiZMKD+rx5eFNORnbfXZFvs=; b=jVFdYc+inIfvYFIgjU0bIA2LEmL0gJbJcmnr+QO6HNdymZifaSd35EV3XtAbpfCbFD DR5Y9dR88wutFLhRFkQ6ri9IvUpJ0vSsRO6G+Ae/FxymacTk+rVvhkfDBAuzWWR6oSuM Qzi6AjklZMg1EWqej4te/CYDfbpkIr9zhdrRUqyKKbrXhu2UIxyvLBHBsOb2tgzE2pyw OspiIsScQ3/qx73uMTtsJhk+fWUDwK091GTgcd6tL41a/ARhoevQyCCdxAvbNiyOxlMv IiGf1gKRUpD1UIz/LysITVTbAXuVXQdsyPRPQxTE4ZD7I9VbJPiGWAyzDsu+6uIlJx08 eeoQ== X-Gm-Message-State: AAQBX9fObcMTkuOcITZDJdw0P4YLcqFF6y46DCtVTOxTqdhJpY4SuPX0 KmDhKjmFPg9NgNoGOlXDv19a9zxN6uTXaHMF612UesqPCHkEAHwihXimdlvRvzjo/ZYpE3IhJN/ LAfp1cBrLMN1tNYkTUySVaq4295o206B0xO0tKKWH X-Received: by 2002:a17:90a:688e:b0:22b:b832:d32 with SMTP id a14-20020a17090a688e00b0022bb8320d32mr13936267pjd.9.1682361801464; Mon, 24 Apr 2023 11:43:21 -0700 (PDT) X-Received: by 2002:a17:90a:688e:b0:22b:b832:d32 with SMTP id a14-20020a17090a688e00b0022bb8320d32mr13936242pjd.9.1682361801207; Mon, 24 Apr 2023 11:43:21 -0700 (PDT) MIME-Version: 1.0 References: <20230414125532.14958-1-wander@redhat.com> <20230414125532.14958-3-wander@redhat.com> <9ff7ea3a-b48b-4cff-851e-179713393531@paulmck-laptop> In-Reply-To: <9ff7ea3a-b48b-4cff-851e-179713393531@paulmck-laptop> From: Wander Lairson Costa Date: Mon, 24 Apr 2023 15:43:09 -0300 Message-ID: Subject: Re: [PATCH v6 2/3] sched/task: Add the put_task_struct_atomic_safe() function To: paulmck@kernel.org 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 Mon, Apr 24, 2023 at 3:09=E2=80=AFPM Paul E. McKenney wrote: > > 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 tas= k_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 *tas= k) > > +{ > > + if (IS_ENABLED(CONFIG_PREEMPT_RT)) { > > + /* > > + * Decrement the refcount explicitly to avoid unnecessari= ly > > + * calling call_rcu. > > + */ > > + if (refcount_dec_and_test(&task->usage)) > > + /* > > + * under PREEMPT_RT, we can't call put_task_struc= t > > + * in atomic context because it will indirectly > > + * acquire sleeping locks. > > + * call_rcu() will schedule delayed_put_task_stru= ct_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? > softirqs execute in thread context in PREEMPT_RT. We are good here. > 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 =3D 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 > > >