Received: by 2002:a05:6358:53a8:b0:117:f937:c515 with SMTP id z40csp4392561rwe; Mon, 17 Apr 2023 11:55:28 -0700 (PDT) X-Google-Smtp-Source: AKy350ay32j9mzQ8/fcEjHHR4gsVw0/pFG4ZEVBP3LE8MZ1HZh/V2amZkpHXdbzODkZVAaAZDkSu X-Received: by 2002:a17:90a:f190:b0:246:b2de:f13f with SMTP id bv16-20020a17090af19000b00246b2def13fmr16604757pjb.24.1681757728262; Mon, 17 Apr 2023 11:55:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681757728; cv=none; d=google.com; s=arc-20160816; b=rR8/KqmNyE/n8Okm3+/KzOkkRpAxZp9gUe1/9IRdcxPKI+e52IeKjMZCns84xg2d+G bEVlNEdavFPj+pfeTZf65/0EoaZKMX4lFhlbbMUST8iCSR1oKSvdE34bk/TkjiLkko7o TqYlHbiUlayuRxA7Z8r/iNjMmXfUM4iB+jJ5SsbJ+JAMoHte72RnpZe+TrgAEeX4n9pj pYRyFNu6mhLP9N2xmqOJaLprsmAm/PXbgCRl7dba/hUPtD8kaksWuAbZPZwfd1ux7WZs q9ZdUPTCCxCMVzQHi476kiPG2di0R4JXNKtRxmU0vuE5VajKYOrCfyZHtFavWlP3etky 4W8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=L+Zz2A4a6oXCi75vniy0YiXcn3MLhYcxpdDk4A6UviE=; b=mXFSE280/8cadsGGfzRb9y/wnMu3zDFXvGZz+bBsxIpVYQV+xQd9nD5/4U4xvyhe5h RxhTPK5FgGyK7UVcqkw9X/l460tiPl724y7s192WNFQuDiT8/9vgZycEys7EkF+bzrBh GrZaUHn7Fr8p1p8b5sw2tiS4VbwxTYoMjKL4aNSCQxdHhegmDHiZNEV7CqA5ZmSV4Wct hkdfWrP3g8Nl+7sAnM8+nFvymfotWf2CY+MOzaFbSXpkimLc/qKPuLlOp5qbOkWMETkq f0d7oqcpXu/ChtXIFd/ZihsXXWIECwVecl5a4JttagbwTosLvqAqZ+xglu8aC6A+6LlL CkPg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="IIL/TP9p"; 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 lw1-20020a17090b180100b002476364b8bcsi6631752pjb.117.2023.04.17.11.55.14; Mon, 17 Apr 2023 11:55:28 -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="IIL/TP9p"; 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 S230104AbjDQSws (ORCPT + 99 others); Mon, 17 Apr 2023 14:52:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51992 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229966AbjDQSwq (ORCPT ); Mon, 17 Apr 2023 14:52:46 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6EEF08F for ; Mon, 17 Apr 2023 11:51:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681757513; 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=L+Zz2A4a6oXCi75vniy0YiXcn3MLhYcxpdDk4A6UviE=; b=IIL/TP9pQ70KymLCorifRPaERuMmTULSv1QfGhYWH7bcB6NDHPUinr922L5nvDYBTez/2z V/gjCpf/7zbBABe750H7XcJu0l25wBgjR3Z2EjwPO8sDZIBLeAUOJAnJ/prdcUgqiH0LCN 6usV+jmfMXqPZmWEbtQZCl+NMaeq9R0= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-664-YWgog8K5O76cVU0Sa94r2w-1; Mon, 17 Apr 2023 14:51:50 -0400 X-MC-Unique: YWgog8K5O76cVU0Sa94r2w-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5E9BF101A531; Mon, 17 Apr 2023 18:51:48 +0000 (UTC) Received: from [10.18.17.153] (dhcp-17-153.bos.redhat.com [10.18.17.153]) by smtp.corp.redhat.com (Postfix) with ESMTP id 848D9492B03; Mon, 17 Apr 2023 18:51:45 +0000 (UTC) Message-ID: Date: Mon, 17 Apr 2023 14:51:45 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.7.1 Subject: Re: [PATCH v6 2/3] sched/task: Add the put_task_struct_atomic_safe() function Content-Language: en-US To: Wander Lairson Costa , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Will Deacon , 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" Cc: Hu Chunyu , Paul McKenney , Thomas Gleixner References: <20230414125532.14958-1-wander@redhat.com> <20230414125532.14958-3-wander@redhat.com> From: Waiman Long In-Reply-To: <20230414125532.14958-3-wander@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE, 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 4/14/23 08:55, 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() delayed_put_task_struct_rcu()? > + * to be called in process context. > + * > + * __put_task_struct() is called called when "called called"? > + * 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. Note that put_task_struct_rcu_user() isn't the only user of task->rcu. delayed_free_task() in kernel/fork.c also uses it, though it is only called in the error case. Still you may need to take a look to make sure that there is no conflict. Cheers, Longman