Received: by 2002:a05:7208:3188:b0:7e:5202:c8b4 with SMTP id r8csp803448rbd; Fri, 23 Feb 2024 04:17:32 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCW87yMY85IZ+23qMFPi/kyhbF4yhrIBnLurVZjkCaLJNoEltyiOMtzK8mnpq+lzGA/hYVx1f1dUCXQcjNSTJsieHePF/D74+j0ZfEhTXw== X-Google-Smtp-Source: AGHT+IEcuIIauTHOPPEfDhX72oLl6wzOkZo69RbvCFaNQyLFe/E6OSVNwfFsg568pLwj2mmbmME8 X-Received: by 2002:a05:6870:34c:b0:21e:5873:2c47 with SMTP id n12-20020a056870034c00b0021e58732c47mr1757056oaf.43.1708690652228; Fri, 23 Feb 2024 04:17:32 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708690652; cv=pass; d=google.com; s=arc-20160816; b=b2sr7GZnfywtr2Ty/KrhbIfNahmdHRb1b85OnLJjnsSZEbJRzdlLRiL1nx+ozwCkIb vP3LAtT5WrMdCDbboU0e9nDozHL5RWn2/n6W8dcacmIDSkbUw94he9/LsaLDR9mKxKrc kYDXvNmynD/H/r+KmaHxGur87risr73Lq5V1bGcSu4c85Q7FNSZ69lhlOvVZsVwauvGV G1T7HMoRXsP9A6MMMXU/V0BlaEXeLAVzYvgD3/mThfDxCvZ1Ab9VgqvPggIxevjisbTh 3Gp3AKlbb1n4x3swABy171QkFrG+cLdOkBk+xPXHY8fdchXqZ3ddDfjhnjpcVPg5T0IQ Mgcw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=ehiVugJc5LFoBXcwhN3y3S/C1TyP/0lpLlGXcxrkB20=; fh=d5zl+3Scu35r5wqTzcBelsZoJk0KfxZ0EMdfAfEzmy4=; b=tmwKkLypuwAwf+ugyqkfbUi+Og6DsQEEXJtQ3Sk+AkD9540remuym+lL+wPgsOf6Im 7ruQcvD60KN6p8s2P4CDa0Fpl/gl4Ndr8ZuRCo+IUP44aQCGW6cx0IliI6/v/eEQOjJj 76mHgvOCkATYIGLQa2LQ+1N4gUOoqwu0YX2qEf4YzlN2QPalBUV98+BkwAhe4aeuEuZ2 Pm/MpArMhXPZMAgu2n06CGad6mMa6LsgGUIdSOjJOPREMh/EAv4avpwX0YolrHztsjFX Z3TaGtpUtlxNKmweF1fRIfnpMbsttnvGZa1HNuPUFJ/hk+5s+bRYiMUw1h4bp5rg3MyQ vyLA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=pxxS1kyc; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-78284-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-78284-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id e22-20020a656796000000b005d55aec96b2si11915842pgr.41.2024.02.23.04.17.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Feb 2024 04:17:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-78284-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=pxxS1kyc; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-78284-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-78284-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id DCFB3286BED for ; Fri, 23 Feb 2024 12:17:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4F0397BAE6; Fri, 23 Feb 2024 12:17:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="pxxS1kyc" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6B46A2E417; Fri, 23 Feb 2024 12:17:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708690637; cv=none; b=W1d8LSTJVyNc1uV1cN23nzG5nppNYDHrHuml3S6EWiJsMYKFt2GdzYfE5EjRCujJPW5Fhs1OT+uzYGYvxAm4gXFBN8U7UVgK+GjmQM649QBOiCfLdjiQszIhFBMilMZ0WFcrCmXRzMpUkSZy+OZi/A5qCwbjkkQLEYrhr9xU8jU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708690637; c=relaxed/simple; bh=dD+Amox90za99RFzb8DfVKU4tgSGxx3W9Z7qT51AAcI=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=N0Dolhad35Y147fDfApe2SqJ7V9iAEZ5GTne0urUPgIISJgUeVaNMIenp3u9kA+FbJ2tcs9GFwy54DmNVOAVZOTXF9EIEfSKOIqEKVPOLJqASV8mQGEMh2/RTwOyMmlWLkDDG79wrrph1wK5GEK4zsfaKkcNBQTX7EnGkXesUfg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=pxxS1kyc; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id B05A0C433C7; Fri, 23 Feb 2024 12:17:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708690637; bh=dD+Amox90za99RFzb8DfVKU4tgSGxx3W9Z7qT51AAcI=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=pxxS1kycaZswlj15eFxU59COD51PSZN2AXkjIUp8pTrNIf+p05WWq4T3No8ZuytQE S4rQrVDbJwPYHOde4GIgWD763DBD9v/zJZVH1Zo36B6Hrzkosc4Fyywo/kW1731sWb YV42CAn+KSrrxLaXVRHSR6gHm7/9Cpu8sZGMmoi0X0qWg+fXpJ0EdV6dbervfMTd1O nzCF9n7oaYWOiq5tnFQUM4+SpguW9DgKj7GX9Xo0Hn+9pG8ytEY5QcAwxBLqPpAQcF 6wYBwzGJ2Yggj/Z67/xWqa/YaoXRa2lTS8Yn9pzMCDZcuoaft5cVMyfSKvaFQvs65N GYiWHUQbxU8mQ== Date: Fri, 23 Feb 2024 13:17:14 +0100 From: Frederic Weisbecker To: "Paul E. McKenney" Cc: Boqun Feng , linux-kernel@vger.kernel.org, rcu@vger.kernel.org, Neeraj Upadhyay , Thomas Gleixner , Sebastian Siewior , Anna-Maria Behnsen , Steven Rostedt , Neeraj Upadhyay , Joel Fernandes , Josh Triplett , Mathieu Desnoyers , Lai Jiangshan , Zqiang Subject: Re: [PATCH v2 6/6] rcu-tasks: Maintain real-time response in rcu_tasks_postscan() Message-ID: References: <20240217012745.3446231-1-boqun.feng@gmail.com> <20240217012745.3446231-7-boqun.feng@gmail.com> <9a27f638-4eb0-42b3-b8a3-201d6ed9406b@paulmck-laptop> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <9a27f638-4eb0-42b3-b8a3-201d6ed9406b@paulmck-laptop> On Thu, Feb 22, 2024 at 02:56:46PM -0800, Paul E. McKenney wrote: > On Thu, Feb 22, 2024 at 12:52:24PM -0800, Paul E. McKenney wrote: > > > > diff --git a/kernel/rcu/tasks.h b/kernel/rcu/tasks.h > > > > index 4dc355b2ac22..866743e0796f 100644 > > > > --- a/kernel/rcu/tasks.h > > > > +++ b/kernel/rcu/tasks.h > > > > @@ -971,13 +971,32 @@ static void rcu_tasks_postscan(struct list_head *hop) > > > > */ > > > > > > > > for_each_possible_cpu(cpu) { > > > > + unsigned long j = jiffies + 1; > > > > struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rcu_tasks.rtpcpu, cpu); > > > > struct task_struct *t; > > > > + struct task_struct *t1; > > > > + struct list_head tmp; > > > > > > > > raw_spin_lock_irq_rcu_node(rtpcp); > > > > - list_for_each_entry(t, &rtpcp->rtp_exit_list, rcu_tasks_exit_list) > > > > + list_for_each_entry_safe(t, t1, &rtpcp->rtp_exit_list, rcu_tasks_exit_list) { > > > > if (list_empty(&t->rcu_tasks_holdout_list)) > > > > rcu_tasks_pertask(t, hop); > > > > + > > > > + // RT kernels need frequent pauses, otherwise > > > > + // pause at least once per pair of jiffies. > > > > + if (!IS_ENABLED(CONFIG_PREEMPT_RT) && time_before(jiffies, j)) > > > > + continue; > > > > + > > > > + // Keep our place in the list while pausing. > > > > + // Nothing else traverses this list, so adding a > > > > + // bare list_head is OK. > > > > + list_add(&tmp, &t->rcu_tasks_exit_list); > > > > > > I'm a bit confused about what this does... Oh, ok now I see what you're doing! My fear was that t goes off but doesn't remove itself and then the list_del() crashes. But no actually tmp places itself after t and then if t exits, it removes itself before tmp and that's fine. > > > > > > > + raw_spin_unlock_irq_rcu_node(rtpcp); > > > > + cond_resched(); // For CONFIG_PREEMPT=n kernels > > > > + raw_spin_lock_irq_rcu_node(rtpcp); > > > > + list_del(&tmp); > > > > > > Isn't there a risk that t is reaped by then? If it was not observed on_rq > > > while calling rcu_tasks_pertask() then there is no get_task_struct. > > > > That is OK, courtesy of the _safe in list_for_each_entry_safe(). > > > > > And what about t1? Can't it be reaped as well? > > > > It can, and that is a problem, good catch! > > > > My current thought is to add this before the list_del(), which is > > admittedly a bit crude: > > > > t1 = tmp.next; > > OK, OK... ;-) > > t1 = list_entry(tmp.next, struct task_struct, rcu_tasks_exit_list); > > Is there still a better way? That should work. An (untested) alternative that fiddles a bit less with list internals could look like this: diff --git a/kernel/rcu/tasks.h b/kernel/rcu/tasks.h index 866743e0796f..0ff2b554f5b5 100644 --- a/kernel/rcu/tasks.h +++ b/kernel/rcu/tasks.h @@ -973,12 +973,13 @@ static void rcu_tasks_postscan(struct list_head *hop) for_each_possible_cpu(cpu) { unsigned long j = jiffies + 1; struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rcu_tasks.rtpcpu, cpu); - struct task_struct *t; - struct task_struct *t1; - struct list_head tmp; raw_spin_lock_irq_rcu_node(rtpcp); - list_for_each_entry_safe(t, t1, &rtpcp->rtp_exit_list, rcu_tasks_exit_list) { + while (!list_empty(&rtpcp->rtp_exit_list)) { + struct task_struct *t; + t = list_first_entry(&rtpcp->rtp_exit_list, typeof(*t), rcu_tasks_exit_list); + list_del_init(&t->rcu_tasks_exit_list); + if (list_empty(&t->rcu_tasks_holdout_list)) rcu_tasks_pertask(t, hop); @@ -987,14 +988,9 @@ static void rcu_tasks_postscan(struct list_head *hop) if (!IS_ENABLED(CONFIG_PREEMPT_RT) && time_before(jiffies, j)) continue; - // Keep our place in the list while pausing. - // Nothing else traverses this list, so adding a - // bare list_head is OK. - list_add(&tmp, &t->rcu_tasks_exit_list); raw_spin_unlock_irq_rcu_node(rtpcp); cond_resched(); // For CONFIG_PREEMPT=n kernels raw_spin_lock_irq_rcu_node(rtpcp); - list_del(&tmp); j = jiffies + 1; } raw_spin_unlock_irq_rcu_node(rtpcp); @@ -1219,7 +1215,6 @@ void exit_tasks_rcu_stop(void) struct rcu_tasks_percpu *rtpcp; struct task_struct *t = current; - WARN_ON_ONCE(list_empty(&t->rcu_tasks_exit_list)); rtpcp = per_cpu_ptr(rcu_tasks.rtpcpu, t->rcu_tasks_exit_cpu); raw_spin_lock_irqsave_rcu_node(rtpcp, flags); list_del_init(&t->rcu_tasks_exit_list);