Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp2784054rwb; Fri, 20 Jan 2023 07:19:42 -0800 (PST) X-Google-Smtp-Source: AMrXdXsYcrLmQ1yMxyphr01lpWPWWJ4BkFau1x5v535fk0XyaJ4brZazNI+cgNSh8veBucLUmrXq X-Received: by 2002:a17:907:cbc5:b0:7ad:9455:d57d with SMTP id vk5-20020a170907cbc500b007ad9455d57dmr16797841ejc.74.1674227982660; Fri, 20 Jan 2023 07:19:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674227982; cv=none; d=google.com; s=arc-20160816; b=ov+iYJdrGQr/bvXV4g4uRn1faCgC2KbmxnQ6jSVEmKkAerYShQu+zbpVKwDTpCytbt Hv8c+sCcQm7dufyo98RYUGFpT+CnlB8Cxgu8AJwSiVrr/vzLu2Gm6P/mK0D8ZlzRIUbV qjiKWbzU/KdTITDQdHLnrXDvjVq0zLNFOTaSMOtnTdY1vFnPSSwVF/IQAloI2ungvI6b skPL5r2AGZmshwrIDdfNMz8if7cDDs2zslTraG7/qZy9aE4p9RyAZN3hyh5OnZIo9r3h 68mX4R/ahrWhGGRX8jEGt2x/nfVPIzRxXO+Zb1USMFN8CZabjEA4PJ0b/ZFiJQVoIjI/ ClJw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=JV/ESkSwVKzhkko42aCx2p6hA+p3ybUeT3T+J1aTWFk=; b=BDZ4ewbhmkb723h84q2/x6lFvTiNg4zHbkelvJ7GRV7ssvFx7rQMyKHRDxsfGwcxAr KD57L/9kg6xC5qL/sZtyiSO2+R6zciYjTUxhmSA6YMO2FfvRh/LoYV/JXI99Qszy9cTZ tIPRDXY7T3h9EGFu/hX1RnmUUrfO15NxSfB7QH3ABWqjGmFkFZ84tx5j6/MZvnnkPe6Q RABEkxiAjBwBc4Hnb19papqinpIpyF457U9htpx1LtGoZSDS2TJEx9RZpXPrawJF7T/h oeGTa+tWN7jseaoIdUQ+VmPvYoKdwLVZcndGv17NjoloHKUGvPRDExu9ZCkJdSJoKhwf KgZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=J5ALeAYw; 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 ho38-20020a1709070ea600b0084ce55f4b7csi51715073ejc.470.2023.01.20.07.19.30; Fri, 20 Jan 2023 07:19:42 -0800 (PST) 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=J5ALeAYw; 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 S231239AbjATPEI (ORCPT + 49 others); Fri, 20 Jan 2023 10:04:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38458 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230391AbjATPEH (ORCPT ); Fri, 20 Jan 2023 10:04:07 -0500 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 38D647DF96 for ; Fri, 20 Jan 2023 07:03:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1674226999; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JV/ESkSwVKzhkko42aCx2p6hA+p3ybUeT3T+J1aTWFk=; b=J5ALeAYwCS0mgLtuxWdUdQcOzRVo1I/kkXGEZ2RbAKNo4WnFSaaRKCos22MUAjDwN3mnjQ Uclpe2zc4/YbozVzhMgM8/xco8Uw2oNJmoK0ZAazuMs/uwweHAz3ziT7C1o3U3/DE3z0aD za7AGfru0Jz8v/iSLYCdpkUYEpmvF9I= 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-490-hTSGCuu2PG2YUUno0xEQkw-1; Fri, 20 Jan 2023 10:03:18 -0500 X-MC-Unique: hTSGCuu2PG2YUUno0xEQkw-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 164F8181E3F2; Fri, 20 Jan 2023 15:03:17 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.22.32.187]) by smtp.corp.redhat.com (Postfix) with ESMTP id 69408492B02; Fri, 20 Jan 2023 15:03:01 +0000 (UTC) From: Wander Lairson Costa To: Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , Valentin Schneider , "Eric W. Biederman" , Wander Lairson Costa , Stafford Horne , Kefeng Wang , Oleg Nesterov , Andrew Morton , Thomas Gleixner , Sebastian Andrzej Siewior , Andy Lutomirski , "Liam R. Howlett" , Fenghua Yu , Andrei Vagin , linux-kernel@vger.kernel.org (open list) Cc: Paul McKenney Subject: [PATCH v2 1/4] sched/task: Add the put_task_struct_atomic_safe function Date: Fri, 20 Jan 2023 12:02:39 -0300 Message-Id: <20230120150246.20797-2-wander@redhat.com> In-Reply-To: <20230120150246.20797-1-wander@redhat.com> References: <20230120150246.20797-1-wander@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Spam-Status: No, score=-2.1 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 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 With PREEMPT_RT, it is unsafe to call put_task_struct() in atomic contexts because it indirectly acquires sleeping locks. Introduce put_task_struct_atomic_safe(), which schedules __put_task_struct() through call_rcu() when the kernel is compiled with PREEMPT_RT. A more natural approach would use a workqueue, but since in PREEMPT_RT we can't allocate dynamic memory from atomic context, the code would become more complex because we would need to put the work_struct instance in the task_struct and initialize it when we allocate a new task_struct. Signed-off-by: Wander Lairson Costa Cc: Paul McKenney Cc: Thomas Gleixner --- include/linux/sched/task.h | 21 +++++++++++++++++++++ kernel/fork.c | 8 ++++++++ 2 files changed, 29 insertions(+) diff --git a/include/linux/sched/task.h b/include/linux/sched/task.h index 357e0068497c..80b4c5812563 100644 --- a/include/linux/sched/task.h +++ b/include/linux/sched/task.h @@ -127,6 +127,27 @@ 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(&task->rcu, __delayed_put_task_struct); + } 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 9f7fe3541897..3d7a4e9311b3 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -859,6 +859,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.0