Received: by 2002:a05:7412:40d:b0:e2:908c:2ebd with SMTP id 13csp179594rdf; Mon, 20 Nov 2023 22:14:14 -0800 (PST) X-Google-Smtp-Source: AGHT+IF3QWGb5TdwWc5r9sKWm+wzidbMpRWJzLoY9/RPYV5CXoIvPgIaHFA1bJ2II7XrckLBJDOm X-Received: by 2002:a05:6358:7e87:b0:16b:fdb2:76fa with SMTP id o7-20020a0563587e8700b0016bfdb276famr13748876rwn.23.1700547254393; Mon, 20 Nov 2023 22:14:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700547254; cv=none; d=google.com; s=arc-20160816; b=qmboJr6RrfBPqFG9TrdrF2cbVsmwMbdMYHOzGYKaIRJ7tPV+rQP3L/TK7sqOYDrZyC fAtbYDFUCOmza6tBAWJm/XS273yCc87vdfOXEnAN+lJfasPhNl7QCIDeVzqFRRlXJq/T 2zH/6OpYxXcGbO1fcgvXPzUGBcUhqen2LkL/zk0rh4A0otejMr9YhSzeOTELFr+2cxq1 Cly8415sxESYPygxlcQR0Sr70ma0TwRiJHsx1m84TXECFY0TWmpgtKrv1UcpA9nV71DA qAV7zHR9hjz9U3WuwftwCqyoWBs4f+y6gh13r4byq8LTvxtfSqdKIxYW5WKDlX6yr/fT F02g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=GOIjomM++7B1dIn3OS6W7wRGVNhGN6yLF0JTgK8dl3k=; fh=6WBftKCVRssa+5YDf2zErFLrzmNH7zSPnLeFRHwIV30=; b=WPY7Q0I1yjhfeuEbvuzmC95iHOrbPd0iyJ1etN/qRfV02+E3cT7SRtqzmacWQFhcV+ fC3hWxqLPnDx7UwV42k6HVB/4CIe9jWWUUixa80QPwADlXS3vWzYlVXX1ppUpIjdWfif U+OU2zekdMMxyRDiarx7P2us6QoNkXASEbxQciB0Q5MS84dKjfx+e8VSgzcJNW7Mn1TO Bsj4mPutfFpHv4oc2oXkqpz+pjHyroC79fKOyl/fvA3V2Nug8ODweWZUypKjESWPe/Bk ljWm+URzGkmUF6ZgPV0B7g/bvtcC2Bb44C9LOuAjYlMOgVh56ZM0ZqOyY5gGfuKn3pWx 4O3Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=AYW66h7U; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id g124-20020a636b82000000b005ac8d44bad5si9363998pgc.7.2023.11.20.22.14.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Nov 2023 22:14:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=AYW66h7U; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id B5C9B801C0A1; Mon, 20 Nov 2023 22:14:11 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230243AbjKUGOM (ORCPT + 99 others); Tue, 21 Nov 2023 01:14:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39420 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229512AbjKUGOL (ORCPT ); Tue, 21 Nov 2023 01:14:11 -0500 Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4A547CA for ; Mon, 20 Nov 2023 22:14:07 -0800 (PST) Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-6cb74a527ceso1524640b3a.2 for ; Mon, 20 Nov 2023 22:14:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1700547245; x=1701152045; darn=vger.kernel.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=GOIjomM++7B1dIn3OS6W7wRGVNhGN6yLF0JTgK8dl3k=; b=AYW66h7UUNtlp6fRYmIWp8YqodscOmBC+d/T08UUMP69WZPi/eAzo5s+ZsAVHG4/wD HS3m4h0owXrp0QE2Mz30px07n32QqdY+qTyV9GSqAhSYYAgGGYsLcqXjIimo0m5P+Wpi StLfeXofKppZCJ6HA+68pNjT8jx2X5M9IDx3h5tVFbjawk8cd5tKoq9ajX4l3yjyLURN rziL61t4XbrglXCNxhNLKQsS0Z73dyth3rO/ihGvIST2tU3N0ldlVzD6xlwpvZuPKneM xkQc5z87kb9rmMnRMgnO4qKkI/zkdtOu/02KoC6ASJ0zVh/q9clIiUn+oBNYpDaPI7qs lvcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700547245; x=1701152045; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=GOIjomM++7B1dIn3OS6W7wRGVNhGN6yLF0JTgK8dl3k=; b=qx7Aqklm6Q71p7GNw1fqLAhBHQS9p3Pnr8WldwgyFZHVZZS/Y5ZmvFstAFvvmZ8Bzc sIePaCe58vMsAHm/XwiXQwAzOFneOy4KNGeDZDe0dnmzzviGe4HcUt32sPpq5C5TaIeV MLjtLxqEKomTIIK1imFXkWWkZTgirxTXsKSoQwLYdaKZkvlOFj6s8CHpuCe/S6S2H9A1 qcEhu0bqIB90W3v/7epgUy2w1AJBUlKARRmCb8PpCmSrzFhS4YS/iwMEZRnZApZkJ6Bd pACZmncgmkn8sKWH6043a/V9gzkgSZYeryx05lGGl6/f0nKe+hro/dh9+9RkuO3C/unj 3yxg== X-Gm-Message-State: AOJu0Yx5pQnkRCLJmjGAmW9u2w5UcO6rFd1sfOEpc3cmk9juUSYZ3pIv QhTzc1qXtJYRnhlS7zKe3U3dUVoqH9qO9Aw14vQ= X-Received: by 2002:a05:6a20:e609:b0:186:5265:ed1c with SMTP id my9-20020a056a20e60900b001865265ed1cmr8147665pzb.6.1700547245543; Mon, 20 Nov 2023 22:14:05 -0800 (PST) MIME-Version: 1.0 References: <20231107215742.363031-1-ankur.a.arora@oracle.com> <20231107215742.363031-49-ankur.a.arora@oracle.com> <2027da00-273d-41cf-b9e7-460776181083@paulmck-laptop> <87lear4wj6.fsf@oracle.com> <46a4c47a-ba1c-4776-a6f8-6c2146cbdd0d@paulmck-laptop> <31d50051-e42c-4ef2-a1ac-e45370c3752e@paulmck-laptop> In-Reply-To: <31d50051-e42c-4ef2-a1ac-e45370c3752e@paulmck-laptop> From: Z qiang Date: Tue, 21 Nov 2023 14:13:53 +0800 Message-ID: Subject: Re: [RFC PATCH 48/86] rcu: handle quiescent states for PREEMPT_RCU=n To: paulmck@kernel.org Cc: Ankur Arora , linux-kernel@vger.kernel.org, tglx@linutronix.de, peterz@infradead.org, torvalds@linux-foundation.org, linux-mm@kvack.org, x86@kernel.org, akpm@linux-foundation.org, luto@kernel.org, bp@alien8.de, dave.hansen@linux.intel.com, hpa@zytor.com, mingo@redhat.com, juri.lelli@redhat.com, vincent.guittot@linaro.org, willy@infradead.org, mgorman@suse.de, jon.grimm@amd.com, bharata@amd.com, raghavendra.kt@amd.com, boris.ostrovsky@oracle.com, konrad.wilk@oracle.com, jgross@suse.com, andrew.cooper3@citrix.com, mingo@kernel.org, bristot@kernel.org, mathieu.desnoyers@efficios.com, geert@linux-m68k.org, glaubitz@physik.fu-berlin.de, anton.ivanov@cambridgegreys.com, mattst88@gmail.com, krypton@ulrich-teichert.org, rostedt@goodmis.org, David.Laight@aculab.com, richard@nod.at, mjguzik@gmail.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS, 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 20 Nov 2023 22:14:11 -0800 (PST) > > On Mon, Nov 20, 2023 at 09:17:57PM -0800, Paul E. McKenney wrote: > > On Mon, Nov 20, 2023 at 07:26:05PM -0800, Ankur Arora wrote: > > > > > > Paul E. McKenney writes: > > > > On Tue, Nov 07, 2023 at 01:57:34PM -0800, Ankur Arora wrote: > > > >> cond_resched() is used to provide urgent quiescent states for > > > >> read-side critical sections on PREEMPT_RCU=n configurations. > > > >> This was necessary because lacking preempt_count, there was no > > > >> way for the tick handler to know if we were executing in RCU > > > >> read-side critical section or not. > > > >> > > > >> An always-on CONFIG_PREEMPT_COUNT, however, allows the tick to > > > >> reliably report quiescent states. > > > >> > > > >> Accordingly, evaluate preempt_count() based quiescence in > > > >> rcu_flavor_sched_clock_irq(). > > > >> > > > >> Suggested-by: Paul E. McKenney > > > >> Signed-off-by: Ankur Arora > > > >> --- > > > >> kernel/rcu/tree_plugin.h | 3 ++- > > > >> kernel/sched/core.c | 15 +-------------- > > > >> 2 files changed, 3 insertions(+), 15 deletions(-) > > > >> > > > >> diff --git a/kernel/rcu/tree_plugin.h b/kernel/rcu/tree_plugin.h > > > >> index f87191e008ff..618f055f8028 100644 > > > >> --- a/kernel/rcu/tree_plugin.h > > > >> +++ b/kernel/rcu/tree_plugin.h > > > >> @@ -963,7 +963,8 @@ static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp) > > > >> */ > > > >> static void rcu_flavor_sched_clock_irq(int user) > > > >> { > > > >> - if (user || rcu_is_cpu_rrupt_from_idle()) { > > > >> + if (user || rcu_is_cpu_rrupt_from_idle() || > > > >> + !(preempt_count() & (PREEMPT_MASK | SOFTIRQ_MASK))) { > > > > > > > > This looks good. > > > > > > > >> /* > > > >> * Get here if this CPU took its interrupt from user > > > >> diff --git a/kernel/sched/core.c b/kernel/sched/core.c > > > >> index bf5df2b866df..15db5fb7acc7 100644 > > > >> --- a/kernel/sched/core.c > > > >> +++ b/kernel/sched/core.c > > > >> @@ -8588,20 +8588,7 @@ int __sched _cond_resched(void) > > > >> preempt_schedule_common(); > > > >> return 1; > > > >> } > > > >> - /* > > > >> - * In preemptible kernels, ->rcu_read_lock_nesting tells the tick > > > >> - * whether the current CPU is in an RCU read-side critical section, > > > >> - * so the tick can report quiescent states even for CPUs looping > > > >> - * in kernel context. In contrast, in non-preemptible kernels, > > > >> - * RCU readers leave no in-memory hints, which means that CPU-bound > > > >> - * processes executing in kernel context might never report an > > > >> - * RCU quiescent state. Therefore, the following code causes > > > >> - * cond_resched() to report a quiescent state, but only when RCU > > > >> - * is in urgent need of one. > > > >> - * / > > > >> -#ifndef CONFIG_PREEMPT_RCU > > > >> - rcu_all_qs(); > > > >> -#endif > > > > > > > > But... > > > > > > > > Suppose we have a long-running loop in the kernel that regularly > > > > enables preemption, but only momentarily. Then the added > > > > rcu_flavor_sched_clock_irq() check would almost always fail, making > > > > for extremely long grace periods. > > > > > > So, my thinking was that if RCU wants to end a grace period, it would > > > force a context switch by setting TIF_NEED_RESCHED (and as patch 38 mentions > > > RCU always uses the the eager version) causing __schedule() to call > > > rcu_note_context_switch(). > > > That's similar to the preempt_schedule_common() case in the > > > _cond_resched() above. > > > > But that requires IPIing that CPU, correct? > > > > > But if I see your point, RCU might just want to register a quiescent > > > state and for this long-running loop rcu_flavor_sched_clock_irq() does > > > seem to fall down. > > > > > > > Or did I miss a change that causes preempt_enable() to help RCU out? > > > > > > Something like this? > > > > > > diff --git a/include/linux/preempt.h b/include/linux/preempt.h > > > index dc5125b9c36b..e50f358f1548 100644 > > > --- a/include/linux/preempt.h > > > +++ b/include/linux/preempt.h > > > @@ -222,6 +222,8 @@ do { \ > > > barrier(); \ > > > if (unlikely(preempt_count_dec_and_test())) \ > > > __preempt_schedule(); \ > > > + if (!(preempt_count() & (PREEMPT_MASK | SOFTIRQ_MASK))) \ > > > + rcu_all_qs(); \ > > > } while (0) > > > > Or maybe something like this to lighten the load a bit: > > > > #define preempt_enable() \ > > do { \ > > barrier(); \ > > if (unlikely(preempt_count_dec_and_test())) { \ > > __preempt_schedule(); \ > > if (raw_cpu_read(rcu_data.rcu_urgent_qs) && \ > > !(preempt_count() & (PREEMPT_MASK | SOFTIRQ_MASK))) \ > > rcu_all_qs(); \ > > } \ > > } while (0) > > > > And at that point, we should be able to drop the PREEMPT_MASK, not > > that it makes any difference that I am aware of: > > > > #define preempt_enable() \ > > do { \ > > barrier(); \ > > if (unlikely(preempt_count_dec_and_test())) { \ > > __preempt_schedule(); \ > > if (raw_cpu_read(rcu_data.rcu_urgent_qs) && \ > > !(preempt_count() & SOFTIRQ_MASK)) \ > > rcu_all_qs(); \ > > } \ > > } while (0) > > > > Except that we can migrate as soon as that preempt_count_dec_and_test() > > returns. And that rcu_all_qs() disables and re-enables preemption, > > which will result in undesired recursion. Sigh. > > > > So maybe something like this: > > > > #define preempt_enable() \ > > do { \ > > if (raw_cpu_read(rcu_data.rcu_urgent_qs) && \ > > !(preempt_count() & SOFTIRQ_MASK)) \ > > Sigh. This needs to include (PREEMPT_MASK | SOFTIRQ_MASK), > but check for equality to something like (1UL << PREEMPT_SHIFT). > For PREEMPT_RCU=n and CONFIG_PREEMPT_COUNT=y kernels for report QS in preempt_enable(), we can refer to this: void rcu_read_unlock_strict(void) { struct rcu_data *rdp; if (irqs_disabled() || preempt_count() || !rcu_state.gp_kthread) return; rdp = this_cpu_ptr(&rcu_data); rdp->cpu_no_qs.b.norm = false; rcu_report_qs_rdp(rdp); udelay(rcu_unlock_delay); } The rcu critical section may be in the NMI handler needs to be considered. Thanks Zqiang > > Clearly time to sleep. :-/ > > Thanx, Paul > > > rcu_all_qs(); \ > > barrier(); \ > > if (unlikely(preempt_count_dec_and_test())) { \ > > __preempt_schedule(); \ > > } \ > > } while (0) > > > > Then rcu_all_qs() becomes something like this: > > > > void rcu_all_qs(void) > > { > > unsigned long flags; > > > > /* Load rcu_urgent_qs before other flags. */ > > if (!smp_load_acquire(this_cpu_ptr(&rcu_data.rcu_urgent_qs))) > > return; > > this_cpu_write(rcu_data.rcu_urgent_qs, false); > > if (unlikely(raw_cpu_read(rcu_data.rcu_need_heavy_qs))) { > > local_irq_save(flags); > > rcu_momentary_dyntick_idle(); > > local_irq_restore(flags); > > } > > rcu_qs(); > > } > > EXPORT_SYMBOL_GPL(rcu_all_qs); > > > > > Though I do wonder about the likelihood of hitting the case you describe > > > and maybe instead of adding the check on every preempt_enable() > > > it might be better to instead force a context switch in the > > > rcu_flavor_sched_clock_irq() (as we do in the PREEMPT_RCU=y case.) > > > > Maybe. But rcu_all_qs() is way lighter weight than a context switch. > > > > Thanx, Paul