Received: by 2002:a05:6358:53a8:b0:117:f937:c515 with SMTP id z40csp5497966rwe; Tue, 18 Apr 2023 07:39:12 -0700 (PDT) X-Google-Smtp-Source: AKy350Y0/lWuEATZ4hq5BlS9zWSUmKmCqbMUEgsGCg/Nxuvas46Ih2Gu8pdjBZlJm5B4Dflbv/Rq X-Received: by 2002:a17:90b:215:b0:247:35f8:81d2 with SMTP id fy21-20020a17090b021500b0024735f881d2mr2247569pjb.29.1681828752257; Tue, 18 Apr 2023 07:39:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681828752; cv=none; d=google.com; s=arc-20160816; b=Qex1px6kifwk8s5a5SgQ4qU9F4pldL6PiuULxvKczZ3HKblrULhIEJt0ST7gj/CWXm BIAl39x/6KVGIrg+cgUyn2rPOq6LFUbuPy0fQAyBKPtCLYCllHhgVq41sNvZKB1NFBsN OyQdfigNIgcbzbBlcagA5XW0eZXDuTtYeN3x0GeqcnTViLdplt/KRvse7Uwso5PnfPCl Vtx7IGm5FJzOTeUYQeA3xKArTolzi+FSo61citJAOf4M/E3ngWoIgo2cva8dlngyAeWq sKmnSOy+UP0KBdz4DTxnR6IfsRMoAfezltIPR2VFVT3B2Ntx6DzWMUo4/5wmQ9OSe77E itAg== 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=COGo9cj6qGV2etafdzVW8HU3mddm/mK/P3hZ5LiglSA=; b=k7ErU46hAorDP+UlKQuHzU7ALVrhiWUp03wMxkU/wBW5lM1EEvRDvJ6R6knLnfXd4p WK9XK52kDHHVsZ8fJhQmORt69S0Nhrth5hCuXCT5IbWOooy9gCFsrgcBiBcQ6j6tR7QC faPAKZ3oTkEVc7DZ2FtWYCjMpFdFSatw28oGlainlmCOnKSOzSZaxXes/Df9xLyJTQPl Hq4RuKJuR1RIiTBOp5jC9hwB7M/a1sx3EwSBLQSgbsNuXpzXUhtuyRY1obnSlEzDAui5 4JQuvPms1zUATHwU3zQ0epR0VkrsHOxvZ9luBVY/z3McyquSt3sMXAxZLek/LZDQ7sQH xTJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ijirBzCW; 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 r126-20020a632b84000000b0051f110fb97esi2326565pgr.82.2023.04.18.07.39.00; Tue, 18 Apr 2023 07:39:12 -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=ijirBzCW; 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 S231367AbjDRO1w (ORCPT + 99 others); Tue, 18 Apr 2023 10:27:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35660 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230429AbjDRO1v (ORCPT ); Tue, 18 Apr 2023 10:27:51 -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 775EF13FBF for ; Tue, 18 Apr 2023 07:26:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681828014; 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=COGo9cj6qGV2etafdzVW8HU3mddm/mK/P3hZ5LiglSA=; b=ijirBzCW4O9u885T5Qq5j82ob+pOlR8888kEsWMDzi9XnxxRrgTAVzmKpOsJYAwz7yQhJq ej92/W/HSLWKv9bM/ZN5cewClnkuqsN1JbHw2bv1hsXhI+pWc50oqsb5YqKvouLJH364wQ kIQb2VSO/+NvkBLl9wkFHIOeeBmXA50= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-6-B7N9H7S7PfOaWZD-mhYluQ-1; Tue, 18 Apr 2023 10:26:52 -0400 X-MC-Unique: B7N9H7S7PfOaWZD-mhYluQ-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 61FE3280BF65; Tue, 18 Apr 2023 14:26:50 +0000 (UTC) Received: from [10.22.34.98] (unknown [10.22.34.98]) by smtp.corp.redhat.com (Postfix) with ESMTP id E34B3483EC4; Tue, 18 Apr 2023 14:26:48 +0000 (UTC) Message-ID: <3f54d775-56ef-4e2b-769c-309cb5d8207c@redhat.com> Date: Tue, 18 Apr 2023 10:26:48 -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 Cc: 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" , 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: 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.7 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/18/23 10:18, Wander Lairson Costa wrote: > On Mon, Apr 17, 2023 at 02:51:45PM -0400, Waiman Long wrote: >> 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. >> > delayed_free_task() is called when a process fails to start. Therefore, AFAICT, > there is no way it can conflict with put_task_struct(). I think so too, but for completeness, you should document somewhere that it is a possible conflicting user. Cheers, Longman