From patchwork Tue Apr 25 11:43:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wander Lairson Costa X-Patchwork-Id: 87372 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp3345234vqo; Tue, 25 Apr 2023 04:51:58 -0700 (PDT) X-Google-Smtp-Source: AKy350Yf2C9eVbMa1v+XDsjXUjP8YvNNCQ82dendnlvpRd5A2VuqOm46hLtyOtRgXkBmSf+c4qb8 X-Received: by 2002:a17:902:e848:b0:1a1:ab92:5c88 with SMTP id t8-20020a170902e84800b001a1ab925c88mr21186079plg.13.1682423518482; Tue, 25 Apr 2023 04:51:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682423518; cv=none; d=google.com; s=arc-20160816; b=zMAXqpIN1e3GX+eGXYOSdWZjsvQbR4loA1lSFjtsrTJp6XlGsGSt2zdKFMsZR1UbDB av+Sojf1/bPoSMwcVXwNjq6u8I7ML1T6g4tchKCI0G3UtICsTdLhqjGdnA6fT4MOjVk3 BH2LhJCwlN8dtKW+lmg9MMU47ipsA2PHWq0D502C15m57anl6QVgNgKijCDfklBzfwyb 4P675PeUtes11Q3A91bqloFwB3VLtYyDJtfgGe6ysPL6/nuA7bUETNKN4ZV7kMkwvaTM KGNnZmVEscpXYLkRAFvmr4I8KGNCk/oKCbhPxre1ClKL4lqJtvLRMI4XHqu2WgwOm7W5 XFAw== 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=vFQziVpAX6XaFGXqO40sBdWG90AbxvK36TAoQxxNkWo=; b=Gu3A9nqjEV/vpzM39ldnY0aiKeyAbE9X6SN00U+C6PXJuBe+M0dOhEt2/VNTTjQDrG S8IZh+tnrKNL5rEXV+LfSwok2EbABqMuAUAvq7InH95QEtqDPnQQSQ+0imB+naUxrEEy 8iMvErdip70Lduff/hJOmLFdv3C0Q7/Fvzw0XuiT1wLD8LgVlN2q1ozXlvXx4Krhs/LX dYusy8n1vyA4pQvcgammF8Dvsgs1WZ+SHBh93XoN8FkGcEN7iKDxp+U1VznZAXJN3yFl B3IadCcXGDx/JkYIED8zdQ8teH8rP3l6YACbmdZL8Y+Il9kQW0LKK2lzmYlJcg63mJ+O DXPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=GgepjmAX; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m6-20020a170902db0600b001a637201992si14244861plx.488.2023.04.25.04.51.44; Tue, 25 Apr 2023 04:51:58 -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=GgepjmAX; 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 S233898AbjDYLoh (ORCPT + 99 others); Tue, 25 Apr 2023 07:44:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45202 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233922AbjDYLof (ORCPT ); Tue, 25 Apr 2023 07:44:35 -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 B1EF3468A for ; Tue, 25 Apr 2023 04:43:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1682423027; 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=vFQziVpAX6XaFGXqO40sBdWG90AbxvK36TAoQxxNkWo=; b=GgepjmAX5XZDbs2iWgz7WO0AkEgp8pYpWEMR9O3VbW1AkBDyFdMZgLMXklCB2lXg6P3adW BoKM+yBR7X8jQR3blI0qJyOgCc/+x0A05Q4xxUoFykYISnuk795v+n0G/HbwqPUD75GpMW 9x88WOx7UYQ119qagw5NU1xvrKE+0SQ= 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-131-UQkMa4ztO0O8A3npY_Q1aA-1; Tue, 25 Apr 2023 07:43:43 -0400 X-MC-Unique: UQkMa4ztO0O8A3npY_Q1aA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5506F1C08786; Tue, 25 Apr 2023 11:43:42 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.22.32.181]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5A307C15BA0; Tue, 25 Apr 2023 11:43:35 +0000 (UTC) From: Wander Lairson Costa To: 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" , Wander Lairson Costa , Kefeng Wang , Michael Ellerman , Oleg Nesterov , Andrew Morton , "Liam R. Howlett" , Christian Brauner , Kees Cook , Andrei Vagin , Shakeel Butt , linux-kernel@vger.kernel.org (open list), linux-perf-users@vger.kernel.org (open list:PERFORMANCE EVENTS SUBSYSTEM) Cc: Sebastian Andrzej Siewior Subject: [PATCH v7 1/3] sched/core: warn on call put_task_struct in invalid context Date: Tue, 25 Apr 2023 08:43:01 -0300 Message-Id: <20230425114307.36889-2-wander@redhat.com> In-Reply-To: <20230425114307.36889-1-wander@redhat.com> References: <20230425114307.36889-1-wander@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 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=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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1764148923357888686?= X-GMAIL-MSGID: =?utf-8?q?1764148923357888686?= Under PREEMPT_RT, spinlocks become sleepable locks. put_task_struct() indirectly acquires a spinlock. Therefore, it can't be called in atomic/interrupt context in RT kernels. To prevent such conditions, add a check for atomic/interrupt context before calling put_task_struct(). Signed-off-by: Wander Lairson Costa Suggested-by: Sebastian Andrzej Siewior --- include/linux/sched/task.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/include/linux/sched/task.h b/include/linux/sched/task.h index 357e0068497c..b597b97b1f8f 100644 --- a/include/linux/sched/task.h +++ b/include/linux/sched/task.h @@ -113,14 +113,28 @@ static inline struct task_struct *get_task_struct(struct task_struct *t) extern void __put_task_struct(struct task_struct *t); +#define PUT_TASK_RESCHED_OFFSETS \ + (rcu_preempt_depth() << MIGHT_RESCHED_RCU_SHIFT) + +#define __put_task_might_resched() \ + __might_resched(__FILE__, __LINE__, PUT_TASK_RESCHED_OFFSETS) + +#define put_task_might_resched() \ + do { \ + if (IS_ENABLED(CONFIG_PREEMPT_RT)) \ + __put_task_might_resched(); \ + } while (0) + static inline void put_task_struct(struct task_struct *t) { + put_task_might_resched(); if (refcount_dec_and_test(&t->usage)) __put_task_struct(t); } static inline void put_task_struct_many(struct task_struct *t, int nr) { + put_task_might_resched(); if (refcount_sub_and_test(nr, &t->usage)) __put_task_struct(t); } From patchwork Tue Apr 25 11:43:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wander Lairson Costa X-Patchwork-Id: 87369 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp3342903vqo; Tue, 25 Apr 2023 04:46:57 -0700 (PDT) X-Google-Smtp-Source: AKy350b6CfVH/EIA2jxwCOiT4t9wTWjG7vajUYSuzTCXfeAN4tadlvN9mL7q1m8eQR4kbTBqXvRv X-Received: by 2002:a17:90a:e509:b0:247:1e1e:57c0 with SMTP id t9-20020a17090ae50900b002471e1e57c0mr16746683pjy.14.1682423216940; Tue, 25 Apr 2023 04:46:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682423216; cv=none; d=google.com; s=arc-20160816; b=zw2P2V4zDS0Ow+j0XiYSiJq2wR2umsaasGvp9J9UEJt0NU6TnpQ6cOFUG0nPIoKaem 8GuxzCa7fMgmcuLX9W/Yo0xQesFLU96Q7J3Cs7U1+EpUUcj6VrTvhwZLMf6qK3FmJ4Xo T/cYmCyDVKeWCU2e0WXmnFP7+cPE6vuFihxIppwg/mPlISZJZVeX2jWBembHbT1nUhs+ wkGZFbe+mCnA91YUKl+icwE/9tZuw2R5qIMNFJaIGqEc/AfGw188fWS7yk5m3hB7VzO6 +PLYVWExos3YEAcu4P91RdftnRPc86XiSzffiDFOOZxB2436V6chLjs07uOXu9REaASF ddpQ== 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=U265zX8tq5WfRp+xioeBIGxCCS4jZo0GQceEaEesDaM=; b=eK1rOOeZNCIZar/QDgD/NxCgB9CfCznJsWBXD8Atyx1PqHVgim6g82iZUfH3uCEuEz EhwPPZuk6X+ZpsrVOYRCaO1Vl1gxFP8cVRR8Mh3hB7A0N8SQ+ccZHReWLtq8ZQfAMAxv Wx9i+WC41lhKWfhsm4h25Z3p+moJoDVzILE/MDYDXAq+DTZow4tGXZEaNDXz+qpvK0HT 3gAetE2VUv7MGVXQRgNdDPzigH1veqK6USW70gdJ+orNT093NNZDQoLViPQ/4ioSWzNS q+jemlKd0OMbr3PPK0ZunWHYp1s2vsk/d3e1MqfqHPAVua+5im7OM8HI9+AY7mjTJsPl Ga4Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=i9ApcuX9; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t6-20020a654b86000000b00514314780easi13884031pgq.34.2023.04.25.04.46.41; Tue, 25 Apr 2023 04:46: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=i9ApcuX9; 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 S233932AbjDYLpB (ORCPT + 99 others); Tue, 25 Apr 2023 07:45:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45254 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233774AbjDYLo7 (ORCPT ); Tue, 25 Apr 2023 07:44:59 -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 6E97B49E2 for ; Tue, 25 Apr 2023 04:44:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1682423039; 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=U265zX8tq5WfRp+xioeBIGxCCS4jZo0GQceEaEesDaM=; b=i9ApcuX9AGzIohD4pz/ye8b+e3GNVllF/7HCx1kL8mTywDFJEE5ASclP6yb+AzEBebZ9DJ ZoovtnYnNJ2Lq76Eik8VAkhDj6c1XdLGvzeYWTxNZyJjWu6iyQHaItMIt8P0khMpFtHiKn s6h0uoirtZWFHLqrs6id/odVkx8o8AE= 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-536-Wt_spITUMDODh06iTe24Og-1; Tue, 25 Apr 2023 07:43:54 -0400 X-MC-Unique: Wt_spITUMDODh06iTe24Og-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4A6DE3C11783; Tue, 25 Apr 2023 11:43:53 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.22.32.181]) by smtp.corp.redhat.com (Postfix) with ESMTP id C629DC15BA0; Tue, 25 Apr 2023 11:43:45 +0000 (UTC) From: Wander Lairson Costa To: 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" , Wander Lairson Costa , Oleg Nesterov , Brian Cain , Kefeng Wang , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Christian Brauner , Andrei Vagin , Shakeel Butt , linux-kernel@vger.kernel.org (open list), linux-perf-users@vger.kernel.org (open list:PERFORMANCE EVENTS SUBSYSTEM) Cc: Hu Chunyu , Paul McKenney , Thomas Gleixner Subject: [PATCH v7 2/3] sched/task: Add the put_task_struct_atomic_safe() function Date: Tue, 25 Apr 2023 08:43:02 -0300 Message-Id: <20230425114307.36889-3-wander@redhat.com> In-Reply-To: <20230425114307.36889-1-wander@redhat.com> References: <20230425114307.36889-1-wander@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1764148607038797255?= X-GMAIL-MSGID: =?utf-8?q?1764148607038797255?= 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 Reviewed-by: Paul McKenney Cc: Thomas Gleixner --- include/linux/sched/task.h | 35 +++++++++++++++++++++++++++++++++++ kernel/fork.c | 8 ++++++++ 2 files changed, 43 insertions(+) diff --git a/include/linux/sched/task.h b/include/linux/sched/task.h index b597b97b1f8f..cf774b83b2ec 100644 --- a/include/linux/sched/task.h +++ b/include/linux/sched/task.h @@ -141,6 +141,41 @@ 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() + * to be called in process context. + * + * __put_task_struct() is 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. + * + * delayed_free_task() also uses ->rcu, but it is only called + * when it fails to fork a process. Therefore, there is no + * way it can conflict with put_task_struct(). + */ + 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 ea332319dffe..7f016b691b1d 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) { } /* From patchwork Tue Apr 25 11:43:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wander Lairson Costa X-Patchwork-Id: 87371 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp3344396vqo; Tue, 25 Apr 2023 04:50:12 -0700 (PDT) X-Google-Smtp-Source: AKy350Ya0YwPFmhVyy1iPaP+LUcSqoqcPJiQploOPoM5Z0nrFxi8yExbNgbzHDmMWXtIE82RTSU8 X-Received: by 2002:a05:6a21:788e:b0:e7:4f8a:d04d with SMTP id bf14-20020a056a21788e00b000e74f8ad04dmr22558953pzc.57.1682423412556; Tue, 25 Apr 2023 04:50:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682423412; cv=none; d=google.com; s=arc-20160816; b=P5AlTq9Ogde0WHSgqhntALJFrLq1A4EcE+mVvjVcwRBqoq/s7QiTn1PQUKODg51kwC s4SaA97HM8c7PE6fUyUvfYvtzTyY0Dk9QX8jif/cRhb8JQtD2m7kfLiEUU37JOticFtM QEZMpyJGfDvTG43A0/WgnyOoqa4K9xiG6f6H8+Yv/FnmFHpgDlEe/hu+MXlVXbcnUsf5 1/LOTRyf+H+O/h+rfhKCnAf9wyJtjasB6eUty28SJVHxe6SlbZra9AVkuU8Ah6miC3r8 aeNGICwa92pa+VAufOhI1UvpzNiHpmImFevVnPwtYZtyOBqJKmRWdCwWpN93mSeqHdf9 y5pw== 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:to:from :dkim-signature; bh=EIn6jOD/NeIKj2oP7nKtWE4+cRY7zlmSfjRegYEWY78=; b=VD7ZNqmHvAm8v30Hj6nHxL+2nDeMjnJjncrdfcEvl3INlQgoxaWml6C3e4MpNUM6yA Oz0GdvgtDYw8funET3JvTvuXeSFRCDOq+9y3z1C2ahHfVQRuVuT35NR2UpNrqEgY6DRW xYPl22s6rCSPEsp01SpnWasQ0CikVw/w/sZADa0l8Ov/X5QYrsDyt8Z/XGxN3ruc/LcJ MlQyZglmxkAMP+ZMtrwcxF8hYJOVzh3zl0SaAu2DO5msBqmBmGn1O68XHyOlO+JRPp2X NP4GU6Kip2iBJBNBszeYCwRy71yJ/F/SAA8KVn9AUJ/s2A1YgaF63bPoosZL7MjiCKi7 88Og== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=O5fSBrrj; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v202-20020a6361d3000000b004fba312c234si13177310pgb.401.2023.04.25.04.49.57; Tue, 25 Apr 2023 04:50: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=O5fSBrrj; 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 S233943AbjDYLpM (ORCPT + 99 others); Tue, 25 Apr 2023 07:45:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45252 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233972AbjDYLpH (ORCPT ); Tue, 25 Apr 2023 07:45:07 -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 A42166A45 for ; Tue, 25 Apr 2023 04:44:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1682423049; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EIn6jOD/NeIKj2oP7nKtWE4+cRY7zlmSfjRegYEWY78=; b=O5fSBrrjWFAFn4BhQ+kU29pmHIAZa8A5HHq21XBHpAVfxj0HnBObkB9r8va9SdsgQGQ9F5 pyqQ9BAyNlNaPygMl7487x4Fc80sLVV0sav3fu1R/z7w8GIAD+rQHliKzF0P/KIi4eo4Q0 N/CYP3PyofaZhFjGtbbFYY0Dj5rN9S0= 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-620-k5AQPd14OvSQiUNg_thmAA-1; Tue, 25 Apr 2023 07:44:04 -0400 X-MC-Unique: k5AQPd14OvSQiUNg_thmAA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 85060884EC0; Tue, 25 Apr 2023 11:44:03 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.22.32.181]) by smtp.corp.redhat.com (Postfix) with ESMTP id A3A30C15BA0; Tue, 25 Apr 2023 11:43:56 +0000 (UTC) From: Wander Lairson Costa To: 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" , Wander Lairson Costa , Huacai Chen , Kefeng Wang , Oleg Nesterov , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Andrei Vagin , Shakeel Butt , linux-kernel@vger.kernel.org (open list), linux-perf-users@vger.kernel.org (open list:PERFORMANCE EVENTS SUBSYSTEM) Subject: [PATCH v7 3/3] treewide: replace put_task_struct() with the atomic safe version Date: Tue, 25 Apr 2023 08:43:03 -0300 Message-Id: <20230425114307.36889-4-wander@redhat.com> In-Reply-To: <20230425114307.36889-1-wander@redhat.com> References: <20230425114307.36889-1-wander@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1764148812038328908?= X-GMAIL-MSGID: =?utf-8?q?1764148812038328908?= In places where put_task_struct() is called in a non-sleepable context, we replace those calls by put_task_struct_atomic_safe(). These call sites were found by running internal regression tests and looking for warnings generated by put_task_might_resched(). Signed-off-by: Wander Lairson Costa Cc: Valentin Schneider --- kernel/events/core.c | 6 +++--- kernel/locking/rtmutex.c | 10 +++++----- kernel/sched/core.c | 6 +++--- kernel/sched/deadline.c | 16 ++++++++-------- kernel/sched/rt.c | 4 ++-- 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index 435815d3be3f..8f823da02324 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -1181,7 +1181,7 @@ static void put_ctx(struct perf_event_context *ctx) if (ctx->parent_ctx) put_ctx(ctx->parent_ctx); if (ctx->task && ctx->task != TASK_TOMBSTONE) - put_task_struct(ctx->task); + put_task_struct_atomic_safe(ctx->task); call_rcu(&ctx->rcu_head, free_ctx); } } @@ -13019,7 +13019,7 @@ static void perf_event_exit_task_context(struct task_struct *child) RCU_INIT_POINTER(child->perf_event_ctxp, NULL); put_ctx(child_ctx); /* cannot be last */ WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE); - put_task_struct(current); /* cannot be last */ + put_task_struct_atomic_safe(current); /* cannot be last */ clone_ctx = unclone_ctx(child_ctx); raw_spin_unlock_irq(&child_ctx->lock); @@ -13124,7 +13124,7 @@ void perf_event_free_task(struct task_struct *task) */ RCU_INIT_POINTER(task->perf_event_ctxp, NULL); WRITE_ONCE(ctx->task, TASK_TOMBSTONE); - put_task_struct(task); /* cannot be last */ + put_task_struct_atomic_safe(task); /* cannot be last */ raw_spin_unlock_irq(&ctx->lock); diff --git a/kernel/locking/rtmutex.c b/kernel/locking/rtmutex.c index 728f434de2bb..3ecb8d6ae039 100644 --- a/kernel/locking/rtmutex.c +++ b/kernel/locking/rtmutex.c @@ -509,7 +509,7 @@ static __always_inline void rt_mutex_wake_up_q(struct rt_wake_q_head *wqh) { if (IS_ENABLED(CONFIG_PREEMPT_RT) && wqh->rtlock_task) { wake_up_state(wqh->rtlock_task, TASK_RTLOCK_WAIT); - put_task_struct(wqh->rtlock_task); + put_task_struct_atomic_safe(wqh->rtlock_task); wqh->rtlock_task = NULL; } @@ -649,7 +649,7 @@ static int __sched rt_mutex_adjust_prio_chain(struct task_struct *task, "task: %s (%d)\n", max_lock_depth, top_task->comm, task_pid_nr(top_task)); } - put_task_struct(task); + put_task_struct_atomic_safe(task); return -EDEADLK; } @@ -817,7 +817,7 @@ static int __sched rt_mutex_adjust_prio_chain(struct task_struct *task, * No requeue[7] here. Just release @task [8] */ raw_spin_unlock(&task->pi_lock); - put_task_struct(task); + put_task_struct_atomic_safe(task); /* * [9] check_exit_conditions_3 protected by lock->wait_lock. @@ -886,7 +886,7 @@ static int __sched rt_mutex_adjust_prio_chain(struct task_struct *task, /* [8] Release the task */ raw_spin_unlock(&task->pi_lock); - put_task_struct(task); + put_task_struct_atomic_safe(task); /* * [9] check_exit_conditions_3 protected by lock->wait_lock. @@ -990,7 +990,7 @@ static int __sched rt_mutex_adjust_prio_chain(struct task_struct *task, out_unlock_pi: raw_spin_unlock_irq(&task->pi_lock); out_put_task: - put_task_struct(task); + put_task_struct_atomic_safe(task); return ret; } diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 0d18c3969f90..a4783f0c9f01 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -1007,7 +1007,7 @@ void wake_up_q(struct wake_q_head *head) * the queueing in wake_q_add() so as not to miss wakeups. */ wake_up_process(task); - put_task_struct(task); + put_task_struct_atomic_safe(task); } } @@ -2528,7 +2528,7 @@ int push_cpu_stop(void *arg) raw_spin_rq_unlock(rq); raw_spin_unlock_irq(&p->pi_lock); - put_task_struct(p); + put_task_struct_atomic_safe(p); return 0; } @@ -9316,7 +9316,7 @@ static int __balance_push_cpu_stop(void *arg) rq_unlock(rq, &rf); raw_spin_unlock_irq(&p->pi_lock); - put_task_struct(p); + put_task_struct_atomic_safe(p); return 0; } diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c index 71b24371a6f7..0f8b8a490dc0 100644 --- a/kernel/sched/deadline.c +++ b/kernel/sched/deadline.c @@ -327,7 +327,7 @@ static void dl_change_utilization(struct task_struct *p, u64 new_bw) * so we are still safe. */ if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1) - put_task_struct(p); + put_task_struct_atomic_safe(p); } __sub_rq_bw(p->dl.dl_bw, &rq->dl); __add_rq_bw(new_bw, &rq->dl); @@ -467,7 +467,7 @@ static void task_contending(struct sched_dl_entity *dl_se, int flags) * so we are still safe. */ if (hrtimer_try_to_cancel(&dl_se->inactive_timer) == 1) - put_task_struct(dl_task_of(dl_se)); + put_task_struct_atomic_safe(dl_task_of(dl_se)); } else { /* * Since "dl_non_contending" is not set, the @@ -1207,7 +1207,7 @@ static enum hrtimer_restart dl_task_timer(struct hrtimer *timer) * This can free the task_struct, including this hrtimer, do not touch * anything related to that after this. */ - put_task_struct(p); + put_task_struct_atomic_safe(p); return HRTIMER_NORESTART; } @@ -1442,7 +1442,7 @@ static enum hrtimer_restart inactive_task_timer(struct hrtimer *timer) dl_se->dl_non_contending = 0; unlock: task_rq_unlock(rq, p, &rf); - put_task_struct(p); + put_task_struct_atomic_safe(p); return HRTIMER_NORESTART; } @@ -1899,7 +1899,7 @@ static void migrate_task_rq_dl(struct task_struct *p, int new_cpu __maybe_unused * so we are still safe. */ if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1) - put_task_struct(p); + put_task_struct_atomic_safe(p); } sub_rq_bw(&p->dl, &rq->dl); rq_unlock(rq, &rf); @@ -2351,7 +2351,7 @@ static int push_dl_task(struct rq *rq) /* No more tasks */ goto out; - put_task_struct(next_task); + put_task_struct_atomic_safe(next_task); next_task = task; goto retry; } @@ -2366,7 +2366,7 @@ static int push_dl_task(struct rq *rq) double_unlock_balance(rq, later_rq); out: - put_task_struct(next_task); + put_task_struct_atomic_safe(next_task); return ret; } @@ -2633,7 +2633,7 @@ static void switched_from_dl(struct rq *rq, struct task_struct *p) static void switched_to_dl(struct rq *rq, struct task_struct *p) { if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1) - put_task_struct(p); + put_task_struct_atomic_safe(p); /* If p is not queued we will update its parameters at next wakeup. */ if (!task_on_rq_queued(p)) { diff --git a/kernel/sched/rt.c b/kernel/sched/rt.c index 0a11f44adee5..e58a84535f61 100644 --- a/kernel/sched/rt.c +++ b/kernel/sched/rt.c @@ -2150,7 +2150,7 @@ static int push_rt_task(struct rq *rq, bool pull) /* * Something has shifted, try again. */ - put_task_struct(next_task); + put_task_struct_atomic_safe(next_task); next_task = task; goto retry; } @@ -2163,7 +2163,7 @@ static int push_rt_task(struct rq *rq, bool pull) double_unlock_balance(rq, lowest_rq); out: - put_task_struct(next_task); + put_task_struct_atomic_safe(next_task); return ret; }