From patchwork Fri Apr 14 12:55:27 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: 83395 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp358719vqo; Fri, 14 Apr 2023 05:58:41 -0700 (PDT) X-Google-Smtp-Source: AKy350ag31kuGv2h8gUj7UkSB2YTRDztwO1xohTLQMI3DR5fXISG49PM2rI6hLAXfOIZ9Y/JrsuT X-Received: by 2002:a17:90b:10a:b0:246:fad6:6932 with SMTP id p10-20020a17090b010a00b00246fad66932mr5564730pjz.8.1681477121674; Fri, 14 Apr 2023 05:58:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681477121; cv=none; d=google.com; s=arc-20160816; b=hl2X9tBqGlIVOhEUZJxwepAFAGmlDSM1z4LjHOSUUuO4VQT5TDpr4wBEq0fRrj72lA 3sKj7cy7HqLvT2arCxDkohipWfnR+X3/8aWL68b6U+ej2qGqQS6c9CMYWA1YwlLmMCNz WO5po2/EknnP9ouVN+TW57rtEnJZQld1tR2i6B52ZI45e/ChIKI2i9QHE44OBC6dmxak /WIDnE2IgQDWB5JENsE8Fj/zNls4x6wmO5PSEZ6Dq53XTQ5W/pdqr6cwCehm/gUnou/e jBOR7e/0Oh+8AxO5K8gY5XWoVS0GQHWcx+VCDhJyM96Ww50TD4jIwINI58dW5Cz3eSlW ijUQ== 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=YrYqlnliydYiAyvEAuZGBKT9tvoR1n5A0ejNaepolyI=; b=lxFuiFPe8fHjsOaEHbVAevku5q2JgxwrEsW1hus3c1xm7U1ZL7pjhf/exX6MNHFiL4 dfbvmljShWZLkdGsvrAVj6RjDlS7reOZLft2LUN8xIafPb4rZ+byagAVzp5P1/rYE1XD WywyIVTG/uoLb3ix/OoMOkeSunMZSK9GOtc3ZRLfR9UxjthLrGqAnIG5HKXm8lH2U4n3 3b/5B6Mf3sId7EtMdI0dIPi27tq28Rz6QvBilHxUu1snNqhDXTLN0CxTvOTqB5rzcfRU ENhzTpCMBLpbtvJ79OstMXXYgEW1FwRZ2ElaZMSJcWs3XhDe7ShQbkJ4BNQHDzKC9L8p rwtg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=NfwmMvkQ; 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 kk2-20020a17090b4a0200b0023d2b3ac7a2si3950074pjb.60.2023.04.14.05.58.28; Fri, 14 Apr 2023 05:58:41 -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=NfwmMvkQ; 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 S230088AbjDNM5E (ORCPT + 99 others); Fri, 14 Apr 2023 08:57:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40110 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229766AbjDNM5C (ORCPT ); Fri, 14 Apr 2023 08:57:02 -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 809E5977F for ; Fri, 14 Apr 2023 05:56:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681476973; 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=YrYqlnliydYiAyvEAuZGBKT9tvoR1n5A0ejNaepolyI=; b=NfwmMvkQC8XHFUeU+H2lE2/LA95/m6GeIba51GdCAEjtUoXcqdEDuL2o4vyI7LArpeL0M/ RpEQmMPixi91XRJRRvm1t5u0IlmecEcG8OnvU9TjNVOtcE9H+NtH6P55mn0xxZql8kOT2d T1jcOAxeGo7p67+NHZaPRmCyF9GcIAE= 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-286-6Oqx-tBnN12S4UgBWipkuQ-1; Fri, 14 Apr 2023 08:55:57 -0400 X-MC-Unique: 6Oqx-tBnN12S4UgBWipkuQ-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C39D4185A791; Fri, 14 Apr 2023 12:55:55 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.22.9.8]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4FE9E406AA66; Fri, 14 Apr 2023 12:55:49 +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 , "Russell King (Oracle)" , Oleg Nesterov , Kefeng Wang , 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) Cc: Sebastian Andrzej Siewior Subject: [PATCH v6 1/3] sched/core: warn on call put_task_struct in invalid context Date: Fri, 14 Apr 2023 09:55:27 -0300 Message-Id: <20230414125532.14958-2-wander@redhat.com> In-Reply-To: <20230414125532.14958-1-wander@redhat.com> References: <20230414125532.14958-1-wander@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 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,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?1763156554238739912?= X-GMAIL-MSGID: =?utf-8?q?1763156554238739912?= 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 Fri Apr 14 12:55:28 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: 83401 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp374580vqo; Fri, 14 Apr 2023 06:16:46 -0700 (PDT) X-Google-Smtp-Source: AKy350ay7bZpFofYjR5thSQhs7e7URdyLkNFAv3vywqE0ESovPJ7oRrgtRxjWQE2mp2/L/UioGS8 X-Received: by 2002:a17:902:e810:b0:1a6:4a64:4d27 with SMTP id u16-20020a170902e81000b001a64a644d27mr3286120plg.40.1681478206399; Fri, 14 Apr 2023 06:16:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681478206; cv=none; d=google.com; s=arc-20160816; b=U66oaG9I+F9+9moJYv7Hgbm+07u/tnPmmwJss5bFqjFdasyh3VDFIr1jrJ47Pggjyc 6q3lT+0P9wnt7MV+0RcjhSxRhbX6FAntlkxOu7vhUG96etXLkllyu6Noz6gcA+qXzJek pZherznWiXwAZKErMcwsL88xdnzWC8K3uLltytqecU8iJf2u4HL5PkFnS8D5EjbjT8+k 8nGmzK/mjIAGQ+iSKee1HQSX463Qes3MFm+4kvAkCckCajpcGfydql7aqcPMhVa0oDVx AoKUXdx3S9dldkUY4ROiSRVbuVxzaTLa2TKLaZE5af1abNoBc72tTHuf8t7CY6Fs0GXr JUTQ== 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=vWFEUBEXcCftzEc0hD+kF0Un2IVbNKstSMRg6ONrT+s=; b=X3c1ukUMgR3sOeJEwu1MFAP+q2DDkvsZeX7mUYypzXHTafyoxkDHF/fYzadfotd7vb i3IDqkzSH6l4mpVgwE0Qs+M5tsXitfoervY1VQ4vQnxT++koxqrdw93ZxC6O5M8UUaOW R733qgvudVwqUN7zJo+fZxM2SBp1rEC4Np6pSa6zWfhXqj+nfEIjOUQjaEyTzLdSmIVB GUzfZsCbnW8ok3IM7rJop5tbPd3Oy4q/PGWGL17XibM1rpzSVxgXU9R5Axz9toSSKTqB ZCm+vxuK7uoYf8Wrw12g+EFaogrnbsYEOo1jMdLZ9YE84ViYATERKUx2m6Lk6Gp++/6h K0Jw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=IYIVdq5e; 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 l19-20020a170902d35300b001a1a0db7f61si4464423plk.336.2023.04.14.06.16.32; Fri, 14 Apr 2023 06:16:46 -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=IYIVdq5e; 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 S230167AbjDNM5M (ORCPT + 99 others); Fri, 14 Apr 2023 08:57:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40200 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230081AbjDNM5G (ORCPT ); Fri, 14 Apr 2023 08:57:06 -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 7589D6584 for ; Fri, 14 Apr 2023 05:56:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681476977; 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=vWFEUBEXcCftzEc0hD+kF0Un2IVbNKstSMRg6ONrT+s=; b=IYIVdq5e6oJZhrn+9439dc8BsHt+FMpGmDeKGC0+sCLyS+LoG/uFgzKVrRhGtbQvKx/ST+ FDKquLJfb5OK47hzRCRf17RGpkOpv2P9MX39xl6B6wWJFilHZ2ETL9Em8dJmA8vhPYNhfo y0n9djHomqmVJUdl2fXTwgDNYxaawII= 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-265-7T0V0wkQOSK6KOzPfQ9_AQ-1; Fri, 14 Apr 2023 08:56:07 -0400 X-MC-Unique: 7T0V0wkQOSK6KOzPfQ9_AQ-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3B6FA185A790; Fri, 14 Apr 2023 12:56:06 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.22.9.8]) by smtp.corp.redhat.com (Postfix) with ESMTP id 28918406AA66; Fri, 14 Apr 2023 12:55:59 +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 , Michael Ellerman , Andrew Morton , Oleg Nesterov , Kefeng Wang , "Liam R. Howlett" , Kees Cook , 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 v6 2/3] sched/task: Add the put_task_struct_atomic_safe() function Date: Fri, 14 Apr 2023 09:55:28 -0300 Message-Id: <20230414125532.14958-3-wander@redhat.com> In-Reply-To: <20230414125532.14958-1-wander@redhat.com> References: <20230414125532.14958-1-wander@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 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,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?1763157691287104603?= X-GMAIL-MSGID: =?utf-8?q?1763157691287104603?= 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 Reviewed-by: Paul E. McKenney --- 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() + * 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); + } 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 = 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 Fri Apr 14 12:55:29 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: 83396 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp358722vqo; Fri, 14 Apr 2023 05:58:42 -0700 (PDT) X-Google-Smtp-Source: AKy350YhKVl5qwTKhiw8WnZ9u8z6Fd2EY27cpKoAPcDlKG008v2G8PBDSnNe6FqibWlgH8NaAL1W X-Received: by 2002:a05:6a00:2da6:b0:632:d636:62cd with SMTP id fb38-20020a056a002da600b00632d63662cdmr9953171pfb.4.1681477122452; Fri, 14 Apr 2023 05:58:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681477122; cv=none; d=google.com; s=arc-20160816; b=gciDAQQ0sKfoFhVuNin67KjMd0W3f8Wat6AhCWycpLk7M5wutj5LT21K/a+m7GxcAp RGk43kzK8SKDnz3zNAJCP5lT6ovX5m6ViSNmpICcv9Kygt7MKvMdl7wEVu9MRto58gbb x/Z//EZRGS1P3AtV38Bvhr8uhV7c5QDaSGWK9fKkBOaF+ohVVjCeQJMjWvZEnpZTEK6T C5ErWj+pvHmBWZu30dWGLU2uHeygTgHnNn732Xv3R/T8nW8FAGQ/rLLPRT5FGP2pInmM ySjV8th4SmYfsa1hFJi8Dd6CMYQD/rXvOFfWWoWDVhF03A/JzLKP2WkEkyBM58RewWj9 XmUQ== 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=8MIchSc/IRBHqG8cQoqV4sLghuhrbfRz6nKRE2TruRs=; b=XKSlSV6+UxMPSjcX+P2cd5771vuAnMq6fqi5ypBxh2r0jJjrGLWzc6ifItgNs1/buh qIzoVE3sYmmbUdpnNvM5UA79MBg+f3X4cNmr1TsN38Oc29E/9Vnb942kt83WOLj8liHh o95kQ+e3BRQUVbAuzBEHy8oHWkj+GwvIbM27DDjKS4spTr6vY0MByVWwYkU/I36nZSKt p24i02MfuJveHHHOo8RVmpaVV118qZJMO67snqitJNlpL4Epe93DGv80QZmux6k2m07O pL4EkfF8x7lcA+987KCwhBK0zbWaopaurtCKStbw5gDB34LKvph2iehk4yXmlsTdMfIF oyNQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=iyoBXxA7; 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 t75-20020a63784e000000b0051b7357bb0csi1225591pgc.32.2023.04.14.05.58.28; Fri, 14 Apr 2023 05:58:42 -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=iyoBXxA7; 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 S229907AbjDNM5J (ORCPT + 99 others); Fri, 14 Apr 2023 08:57:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40272 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230098AbjDNM5G (ORCPT ); Fri, 14 Apr 2023 08:57:06 -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 9FC9A5FDE for ; Fri, 14 Apr 2023 05:56:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681476980; 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=8MIchSc/IRBHqG8cQoqV4sLghuhrbfRz6nKRE2TruRs=; b=iyoBXxA7PZa3KHRyuYXJJGY5wpig8ZB1maTdfTUnZUwrXDibH9m2p5NFDBv//RUu3Cn8JE nUCXXdyw4urjCG+r31U5BO6tbcRDFOz+X5BZE3ua2sZGFP0u7ey7XuTetEBZnwI7I12itg tmfGqw/oCEikm+1iXrZ5NKDxygJBuQ0= 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-108-HizxkZDTNICfXSF6gf899w-1; Fri, 14 Apr 2023 08:56:17 -0400 X-MC-Unique: HizxkZDTNICfXSF6gf899w-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 81C1485530D; Fri, 14 Apr 2023 12:56:16 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.22.9.8]) by smtp.corp.redhat.com (Postfix) with ESMTP id ADB80406AA66; Fri, 14 Apr 2023 12:56:09 +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 , Michael Ellerman , Brian Cain , Kefeng Wang , Oleg Nesterov , Andrew Morton , "Liam R. Howlett" , Christian Brauner , 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 v6 3/3] treewide: replace put_task_struct() witht the atomic safe version Date: Fri, 14 Apr 2023 09:55:29 -0300 Message-Id: <20230414125532.14958-4-wander@redhat.com> In-Reply-To: <20230414125532.14958-1-wander@redhat.com> References: <20230414125532.14958-1-wander@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 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,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?1763156554928955710?= X-GMAIL-MSGID: =?utf-8?q?1763156554928955710?= 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; }