Message ID | Zd4DXTyCf17lcTfq@debian.debian |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-83528-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp2781972dyb; Tue, 27 Feb 2024 07:46:27 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUPezqLYJ2us73N04qyL0YxqjRZbP8mNXoMDsaNbehvLECgceEDiRXcHBTGZO6/NbIXiVpFpNuAWCihKpnQTTCj3aVF8w== X-Google-Smtp-Source: AGHT+IGNVsb0zOF/6a7zZf2fNlX+BZEfs/LcHCEMxYPBtPKuxBZNTjk045+ZJsIPKFcXfK8PKyzF X-Received: by 2002:a17:903:22c1:b0:1dc:8790:6824 with SMTP id y1-20020a17090322c100b001dc87906824mr11148955plg.15.1709048786851; Tue, 27 Feb 2024 07:46:26 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709048786; cv=pass; d=google.com; s=arc-20160816; b=LjJFG2Xh3dM13sifopvGRddkcdlh/QbYdGCLK/8BgGlo0kZyTyjeNSaHRQN0W4sJwT /2W6YQITCWgweHKcA4oT2VkUUlmDG1drmTIg/ENXYGEk4ryFyqhk8XHR/Rxa4VGRkniG OP8WLVhuJUfFtBOEOt4H+bl5gHNy/g0ZPLAd/cjzF0JJxnnuDl0R1dwA/K1PQZUmAy8w RRhgQjqayLc51//eX8W2vXhiIziU4yyRekLNOyzRh5JaoTLswX+DFDCMI+8zw5gifh5k O7XMxndQLcCl9aqEeKNBxkoBJf/3Zy5bUh4M4g+2AYA0zje2DSxaCB95qlEqFQ58nNCP pZ8Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-disposition:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:message-id:subject:cc:to:from:date :dkim-signature; bh=fvLPNpXdLGD+M5a3hZKAFIZfEPkaIJ/ji6J8mxzWC4I=; fh=LtSLhi3QgbRwK3rFOYnD5USf49XJ0IoP/hJsoADq88Y=; b=DyDjBmoflvK64c4H4qus3m5LHnGVOFRXrfB37Zp/Kgx0Y1qIomFGta9m8DXuimDx+7 ukZ1qupMkJgbO5TfTrPOOf3Vnt49BAgPpr+AiMdy6ewbL2tSaI1zPJvvACYOiRGouZsG Sg7j1AP4zDsC6NAsOplm2n94MA/FcjpL4Mk8woA4L5C2ERkjb39BvusPhIzhYil/0vNP +OODoGvpYVr+PbYIauttHGXOC3myFjQhUdzBsbxJ3nB6N6Uc78bndWv0UknrV8bmmGW2 P2fjL6o1ayxoYUfEl/gbYJol+BBbooP+jiK9ElVD0excvVZp3TR9hdklKgZGea3cbMet 2zXA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@cloudflare.com header.s=google09082023 header.b=QyaDeuTY; arc=pass (i=1 spf=pass spfdomain=cloudflare.com dkim=pass dkdomain=cloudflare.com dmarc=pass fromdomain=cloudflare.com); spf=pass (google.com: domain of linux-kernel+bounces-83528-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-83528-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=cloudflare.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id j12-20020a170903024c00b001dcafec2295si1582190plh.405.2024.02.27.07.46.26 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 07:46:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-83528-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@cloudflare.com header.s=google09082023 header.b=QyaDeuTY; arc=pass (i=1 spf=pass spfdomain=cloudflare.com dkim=pass dkdomain=cloudflare.com dmarc=pass fromdomain=cloudflare.com); spf=pass (google.com: domain of linux-kernel+bounces-83528-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-83528-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=cloudflare.com 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 382182878ED for <ouuuleilei@gmail.com>; Tue, 27 Feb 2024 15:45:35 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0DFEF1487FF; Tue, 27 Feb 2024 15:44:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=cloudflare.com header.i=@cloudflare.com header.b="QyaDeuTY" Received: from mail-yb1-f175.google.com (mail-yb1-f175.google.com [209.85.219.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C24741474A9 for <linux-kernel@vger.kernel.org>; Tue, 27 Feb 2024 15:44:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709048676; cv=none; b=pP5gz01JOznaG6u+GjrHr6eWT9haksXuTqYsBBXNZfpJj1LcJcHPIsv0EsnBp4aqqmK++j5THSt/1HWGKPvUbimQFoy170nI7QktH1lY+9Nh8ddE2gSH0wWGAVABIYmw+/Ac6hNZTT5mMJgaYnXK4NROnTK1dpsRKlQjlLwnNKY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709048676; c=relaxed/simple; bh=G9yIq1UUe7Jy5IKgqipKCB37DVtAPsay/xE5VGy7JGQ=; h=Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type: Content-Disposition; b=glvifuZlW1JkHW+hjLexMkjU4IHR5g69ZOtW4h4uNRx9dNI95zl8QCBTfwxP7wtUPb8hWSa0rm9LJ5nRVjknGeTWuHSFmh7HUHMmJvoKMtleu2ikc3adpvmxg7aQ7hEJIJH1RJ9AgKAGQ/Z/tL+cKeqVqn5xRcU6huLH48ofD1o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=cloudflare.com; spf=pass smtp.mailfrom=cloudflare.com; dkim=pass (2048-bit key) header.d=cloudflare.com header.i=@cloudflare.com header.b=QyaDeuTY; arc=none smtp.client-ip=209.85.219.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=cloudflare.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=cloudflare.com Received: by mail-yb1-f175.google.com with SMTP id 3f1490d57ef6-dc6d8bd612dso4444018276.1 for <linux-kernel@vger.kernel.org>; Tue, 27 Feb 2024 07:44:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google09082023; t=1709048673; x=1709653473; darn=vger.kernel.org; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=fvLPNpXdLGD+M5a3hZKAFIZfEPkaIJ/ji6J8mxzWC4I=; b=QyaDeuTYW/zCYpeqBDUNHYJTiVWsmtDI98B4keln5MbOuSLo5Xt76iWzrnHW7EWA+U vCYqfhdw+wEtEm3Zb3PCyonEXbfqiyArKvmsDdqWOEv+02S0ajmB2mswPZ4NtpJtn6MZ ip5s056V8p2hjJ+dgDP4hcWmZPTtR713NKa7lbD+NCW/nfHRjMotqtbpX3t31ghTl8Gw zs2Mmblbw4AY72XCTqV7Wk0VU2Nm3Th499miW1b0+vQsPj9lrgkyMQqh9iF3jwUW1q7g PkgCfHDvJ8hNq9V9ZpYVVycClX5Wt/3ec+a6+PbSF3szZugdRLSFxeT4+wwMa3hsoi+q HuYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709048673; x=1709653473; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=fvLPNpXdLGD+M5a3hZKAFIZfEPkaIJ/ji6J8mxzWC4I=; b=queceXfrDFS+EiE05AvZYUzvXi0EF4Fvu1H5cll4w9T8zTCJB8+RJZRzzVUixCNdag GF3RekHajoQP6Tp4pJ1FGMgTq41SiX0V8uQfgvcEQx8RVMIsNQNLyx0Cuy10m+VEAlNW Sa+GRerMDE+HZzZh2DuakAgn+mWowlq2yTcZk8URTVFwgHGVRAErP3NDqkc/F4As1pUj 52qct79phQW7yecjOlP6/opybi2T11InCY2KT6dhyuLKij2nT/imVFNqi9AhCL92LGng 6qxJoV2g6UPexMekV5HuRkvwTX8WET5IoFIKT+oYFP2ivzSSW9TK/qgE6U3E1vLeuw8e akzQ== X-Forwarded-Encrypted: i=1; AJvYcCVAoAwcXQ6M06zqr0+iOb+FM+HRWaAncM139R7MUvh6iBVuzHRIQ33giLnJUj5gIHzynDfaQT9a7ovDZ0HjnVc7QQdzodDUkIJZ2qcw X-Gm-Message-State: AOJu0YzyZg2SJTX+WW773Rxf0eD/HM5Qm51G9X7Mr8ghLrD8r9VePyK6 pYBwwGAMfj+CJPOPJ+2vRp3yk2DBH3FmhAAcMI3eDpTQu0Ai+CxZ7QTazuDF/nA= X-Received: by 2002:a25:13c4:0:b0:dcd:1854:9f43 with SMTP id 187-20020a2513c4000000b00dcd18549f43mr2079117ybt.3.1709048672424; Tue, 27 Feb 2024 07:44:32 -0800 (PST) Received: from debian.debian ([2a09:bac5:7a49:f91::18d:13]) by smtp.gmail.com with ESMTPSA id k25-20020ac86059000000b0042e8a53d216sm1796307qtm.86.2024.02.27.07.44.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 07:44:31 -0800 (PST) Date: Tue, 27 Feb 2024 07:44:29 -0800 From: Yan Zhai <yan@cloudflare.com> To: netdev@vger.kernel.org Cc: "David S. Miller" <davem@davemloft.net>, Eric Dumazet <edumazet@google.com>, Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>, Jiri Pirko <jiri@resnulli.us>, Simon Horman <horms@kernel.org>, Daniel Borkmann <daniel@iogearbox.net>, Lorenzo Bianconi <lorenzo@kernel.org>, Coco Li <lixiaoyan@google.com>, Wei Wang <weiwan@google.com>, Alexander Duyck <alexanderduyck@fb.com>, Hannes Frederic Sowa <hannes@stressinduktion.org>, linux-kernel@vger.kernel.org, rcu@vger.kernel.org, bpf@vger.kernel.org, kernel-team@cloudflare.com Subject: [PATCH] net: raise RCU qs after each threaded NAPI poll Message-ID: <Zd4DXTyCf17lcTfq@debian.debian> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792067540384979260 X-GMAIL-MSGID: 1792067540384979260 |
Series |
net: raise RCU qs after each threaded NAPI poll
|
|
Commit Message
Yan Zhai
Feb. 27, 2024, 3:44 p.m. UTC
We noticed task RCUs being blocked when threaded NAPIs are very busy in
production: detaching any BPF tracing programs, i.e. removing a ftrace
trampoline, will simply block for very long in rcu_tasks_wait_gp. This
ranges from hundreds of seconds to even an hour, severely harming any
observability tools that rely on BPF tracing programs. It can be
easily reproduced locally with following setup:
ip netns add test1
ip netns add test2
ip -n test1 link add veth1 type veth peer name veth2 netns test2
ip -n test1 link set veth1 up
ip -n test1 link set lo up
ip -n test2 link set veth2 up
ip -n test2 link set lo up
ip -n test1 addr add 192.168.1.2/31 dev veth1
ip -n test1 addr add 1.1.1.1/32 dev lo
ip -n test2 addr add 192.168.1.3/31 dev veth2
ip -n test2 addr add 2.2.2.2/31 dev lo
ip -n test1 route add default via 192.168.1.3
ip -n test2 route add default via 192.168.1.2
for i in `seq 10 210`; do
for j in `seq 10 210`; do
ip netns exec test2 iptables -I INPUT -s 3.3.$i.$j -p udp --dport 5201
done
done
ip netns exec test2 ethtool -K veth2 gro on
ip netns exec test2 bash -c 'echo 1 > /sys/class/net/veth2/threaded'
ip netns exec test1 ethtool -K veth1 tso off
Then run an iperf3 client/server and a bpftrace script can trigger it:
ip netns exec test2 iperf3 -s -B 2.2.2.2 >/dev/null&
ip netns exec test1 iperf3 -c 2.2.2.2 -B 1.1.1.1 -u -l 1500 -b 3g -t 100 >/dev/null&
bpftrace -e 'kfunc:__napi_poll{@=count();} interval:s:1{exit();}'
Above reproduce for net-next kernel with following RCU and preempt
configuraitons:
# RCU Subsystem
CONFIG_TREE_RCU=y
CONFIG_PREEMPT_RCU=y
# CONFIG_RCU_EXPERT is not set
CONFIG_SRCU=y
CONFIG_TREE_SRCU=y
CONFIG_TASKS_RCU_GENERIC=y
CONFIG_TASKS_RCU=y
CONFIG_TASKS_RUDE_RCU=y
CONFIG_TASKS_TRACE_RCU=y
CONFIG_RCU_STALL_COMMON=y
CONFIG_RCU_NEED_SEGCBLIST=y
# end of RCU Subsystem
# RCU Debugging
# CONFIG_RCU_SCALE_TEST is not set
# CONFIG_RCU_TORTURE_TEST is not set
# CONFIG_RCU_REF_SCALE_TEST is not set
CONFIG_RCU_CPU_STALL_TIMEOUT=21
CONFIG_RCU_EXP_CPU_STALL_TIMEOUT=0
# CONFIG_RCU_TRACE is not set
# CONFIG_RCU_EQS_DEBUG is not set
# end of RCU Debugging
CONFIG_PREEMPT_BUILD=y
# CONFIG_PREEMPT_NONE is not set
CONFIG_PREEMPT_VOLUNTARY=y
# CONFIG_PREEMPT is not set
CONFIG_PREEMPT_COUNT=y
CONFIG_PREEMPTION=y
CONFIG_PREEMPT_DYNAMIC=y
CONFIG_PREEMPT_RCU=y
CONFIG_HAVE_PREEMPT_DYNAMIC=y
CONFIG_HAVE_PREEMPT_DYNAMIC_CALL=y
CONFIG_PREEMPT_NOTIFIERS=y
# CONFIG_DEBUG_PREEMPT is not set
# CONFIG_PREEMPT_TRACER is not set
# CONFIG_PREEMPTIRQ_DELAY_TEST is not set
An interesting observation is that, while tasks RCUs are blocked,
related NAPI thread is still being scheduled (even across cores)
regularly. Looking at the gp conditions, I am inclining to cond_resched
after each __napi_poll being the problem: cond_resched enters the
scheduler with PREEMPT bit, which does not account as a gp for tasks
RCUs. Meanwhile, since the thread has been frequently resched, the
normal scheduling point (no PREEMPT bit, accounted as a task RCU gp)
seems to have very little chance to kick in. Given the nature of "busy
polling" program, such NAPI thread won't have task->nvcsw or task->on_rq
updated (other gp conditions), the result is that such NAPI thread is
put on RCU holdouts list for indefinitely long time.
This is simply fixed by mirroring the ksoftirqd behavior: after
NAPI/softirq work, raise a RCU QS to help expedite the RCU period. No
more blocking afterwards for the same setup.
Fixes: 29863d41bb6e ("net: implement threaded-able napi poll loop support")
Signed-off-by: Yan Zhai <yan@cloudflare.com>
---
net/core/dev.c | 4 ++++
1 file changed, 4 insertions(+)
Comments
On Tue, Feb 27, 2024 at 4:44 PM Yan Zhai <yan@cloudflare.com> wrote: > > We noticed task RCUs being blocked when threaded NAPIs are very busy in > production: detaching any BPF tracing programs, i.e. removing a ftrace > trampoline, will simply block for very long in rcu_tasks_wait_gp. This > ranges from hundreds of seconds to even an hour, severely harming any > observability tools that rely on BPF tracing programs. It can be > easily reproduced locally with following setup: > > ip netns add test1 > ip netns add test2 > > ip -n test1 link add veth1 type veth peer name veth2 netns test2 > > ip -n test1 link set veth1 up > ip -n test1 link set lo up > ip -n test2 link set veth2 up > ip -n test2 link set lo up > > ip -n test1 addr add 192.168.1.2/31 dev veth1 > ip -n test1 addr add 1.1.1.1/32 dev lo > ip -n test2 addr add 192.168.1.3/31 dev veth2 > ip -n test2 addr add 2.2.2.2/31 dev lo > > ip -n test1 route add default via 192.168.1.3 > ip -n test2 route add default via 192.168.1.2 > > for i in `seq 10 210`; do > for j in `seq 10 210`; do > ip netns exec test2 iptables -I INPUT -s 3.3.$i.$j -p udp --dport 5201 > done > done > > ip netns exec test2 ethtool -K veth2 gro on > ip netns exec test2 bash -c 'echo 1 > /sys/class/net/veth2/threaded' > ip netns exec test1 ethtool -K veth1 tso off > > Then run an iperf3 client/server and a bpftrace script can trigger it: > > ip netns exec test2 iperf3 -s -B 2.2.2.2 >/dev/null& > ip netns exec test1 iperf3 -c 2.2.2.2 -B 1.1.1.1 -u -l 1500 -b 3g -t 100 >/dev/null& > bpftrace -e 'kfunc:__napi_poll{@=count();} interval:s:1{exit();}' > > Above reproduce for net-next kernel with following RCU and preempt > configuraitons: > > # RCU Subsystem > CONFIG_TREE_RCU=y > CONFIG_PREEMPT_RCU=y > # CONFIG_RCU_EXPERT is not set > CONFIG_SRCU=y > CONFIG_TREE_SRCU=y > CONFIG_TASKS_RCU_GENERIC=y > CONFIG_TASKS_RCU=y > CONFIG_TASKS_RUDE_RCU=y > CONFIG_TASKS_TRACE_RCU=y > CONFIG_RCU_STALL_COMMON=y > CONFIG_RCU_NEED_SEGCBLIST=y > # end of RCU Subsystem > # RCU Debugging > # CONFIG_RCU_SCALE_TEST is not set > # CONFIG_RCU_TORTURE_TEST is not set > # CONFIG_RCU_REF_SCALE_TEST is not set > CONFIG_RCU_CPU_STALL_TIMEOUT=21 > CONFIG_RCU_EXP_CPU_STALL_TIMEOUT=0 > # CONFIG_RCU_TRACE is not set > # CONFIG_RCU_EQS_DEBUG is not set > # end of RCU Debugging > > CONFIG_PREEMPT_BUILD=y > # CONFIG_PREEMPT_NONE is not set > CONFIG_PREEMPT_VOLUNTARY=y > # CONFIG_PREEMPT is not set > CONFIG_PREEMPT_COUNT=y > CONFIG_PREEMPTION=y > CONFIG_PREEMPT_DYNAMIC=y > CONFIG_PREEMPT_RCU=y > CONFIG_HAVE_PREEMPT_DYNAMIC=y > CONFIG_HAVE_PREEMPT_DYNAMIC_CALL=y > CONFIG_PREEMPT_NOTIFIERS=y > # CONFIG_DEBUG_PREEMPT is not set > # CONFIG_PREEMPT_TRACER is not set > # CONFIG_PREEMPTIRQ_DELAY_TEST is not set > > An interesting observation is that, while tasks RCUs are blocked, > related NAPI thread is still being scheduled (even across cores) > regularly. Looking at the gp conditions, I am inclining to cond_resched > after each __napi_poll being the problem: cond_resched enters the > scheduler with PREEMPT bit, which does not account as a gp for tasks > RCUs. Meanwhile, since the thread has been frequently resched, the > normal scheduling point (no PREEMPT bit, accounted as a task RCU gp) > seems to have very little chance to kick in. Given the nature of "busy > polling" program, such NAPI thread won't have task->nvcsw or task->on_rq > updated (other gp conditions), the result is that such NAPI thread is > put on RCU holdouts list for indefinitely long time. > > This is simply fixed by mirroring the ksoftirqd behavior: after > NAPI/softirq work, raise a RCU QS to help expedite the RCU period. No > more blocking afterwards for the same setup. > > Fixes: 29863d41bb6e ("net: implement threaded-able napi poll loop support") > Signed-off-by: Yan Zhai <yan@cloudflare.com> > --- > net/core/dev.c | 4 ++++ > 1 file changed, 4 insertions(+) > > diff --git a/net/core/dev.c b/net/core/dev.c > index 275fd5259a4a..6e41263ff5d3 100644 > --- a/net/core/dev.c > +++ b/net/core/dev.c > @@ -6773,6 +6773,10 @@ static int napi_threaded_poll(void *data) > net_rps_action_and_irq_enable(sd); > } > skb_defer_free_flush(sd); > + > + if (!IS_ENABLED(CONFIG_PREEMPT_RT)) > + rcu_softirq_qs(); > + > local_bh_enable(); > > if (!repoll) > -- > 2.30.2 > Hmm.... Why napi_busy_loop() does not have a similar problem ? It is unclear why rcu_all_qs() in __cond_resched() is guarded by #ifndef CONFIG_PREEMPT_RCU rcu_all_qs(); #endif Thanks.
On Tue, Feb 27, 2024 at 05:44:17PM +0100, Eric Dumazet wrote: > On Tue, Feb 27, 2024 at 4:44 PM Yan Zhai <yan@cloudflare.com> wrote: > > > > We noticed task RCUs being blocked when threaded NAPIs are very busy in > > production: detaching any BPF tracing programs, i.e. removing a ftrace > > trampoline, will simply block for very long in rcu_tasks_wait_gp. This > > ranges from hundreds of seconds to even an hour, severely harming any > > observability tools that rely on BPF tracing programs. It can be > > easily reproduced locally with following setup: > > > > ip netns add test1 > > ip netns add test2 > > > > ip -n test1 link add veth1 type veth peer name veth2 netns test2 > > > > ip -n test1 link set veth1 up > > ip -n test1 link set lo up > > ip -n test2 link set veth2 up > > ip -n test2 link set lo up > > > > ip -n test1 addr add 192.168.1.2/31 dev veth1 > > ip -n test1 addr add 1.1.1.1/32 dev lo > > ip -n test2 addr add 192.168.1.3/31 dev veth2 > > ip -n test2 addr add 2.2.2.2/31 dev lo > > > > ip -n test1 route add default via 192.168.1.3 > > ip -n test2 route add default via 192.168.1.2 > > > > for i in `seq 10 210`; do > > for j in `seq 10 210`; do > > ip netns exec test2 iptables -I INPUT -s 3.3.$i.$j -p udp --dport 5201 > > done > > done > > > > ip netns exec test2 ethtool -K veth2 gro on > > ip netns exec test2 bash -c 'echo 1 > /sys/class/net/veth2/threaded' > > ip netns exec test1 ethtool -K veth1 tso off > > > > Then run an iperf3 client/server and a bpftrace script can trigger it: > > > > ip netns exec test2 iperf3 -s -B 2.2.2.2 >/dev/null& > > ip netns exec test1 iperf3 -c 2.2.2.2 -B 1.1.1.1 -u -l 1500 -b 3g -t 100 >/dev/null& > > bpftrace -e 'kfunc:__napi_poll{@=count();} interval:s:1{exit();}' > > > > Above reproduce for net-next kernel with following RCU and preempt > > configuraitons: > > > > # RCU Subsystem > > CONFIG_TREE_RCU=y > > CONFIG_PREEMPT_RCU=y > > # CONFIG_RCU_EXPERT is not set > > CONFIG_SRCU=y > > CONFIG_TREE_SRCU=y > > CONFIG_TASKS_RCU_GENERIC=y > > CONFIG_TASKS_RCU=y > > CONFIG_TASKS_RUDE_RCU=y > > CONFIG_TASKS_TRACE_RCU=y > > CONFIG_RCU_STALL_COMMON=y > > CONFIG_RCU_NEED_SEGCBLIST=y > > # end of RCU Subsystem > > # RCU Debugging > > # CONFIG_RCU_SCALE_TEST is not set > > # CONFIG_RCU_TORTURE_TEST is not set > > # CONFIG_RCU_REF_SCALE_TEST is not set > > CONFIG_RCU_CPU_STALL_TIMEOUT=21 > > CONFIG_RCU_EXP_CPU_STALL_TIMEOUT=0 > > # CONFIG_RCU_TRACE is not set > > # CONFIG_RCU_EQS_DEBUG is not set > > # end of RCU Debugging > > > > CONFIG_PREEMPT_BUILD=y > > # CONFIG_PREEMPT_NONE is not set > > CONFIG_PREEMPT_VOLUNTARY=y > > # CONFIG_PREEMPT is not set > > CONFIG_PREEMPT_COUNT=y > > CONFIG_PREEMPTION=y > > CONFIG_PREEMPT_DYNAMIC=y > > CONFIG_PREEMPT_RCU=y > > CONFIG_HAVE_PREEMPT_DYNAMIC=y > > CONFIG_HAVE_PREEMPT_DYNAMIC_CALL=y > > CONFIG_PREEMPT_NOTIFIERS=y > > # CONFIG_DEBUG_PREEMPT is not set > > # CONFIG_PREEMPT_TRACER is not set > > # CONFIG_PREEMPTIRQ_DELAY_TEST is not set > > > > An interesting observation is that, while tasks RCUs are blocked, > > related NAPI thread is still being scheduled (even across cores) > > regularly. Looking at the gp conditions, I am inclining to cond_resched > > after each __napi_poll being the problem: cond_resched enters the > > scheduler with PREEMPT bit, which does not account as a gp for tasks > > RCUs. Meanwhile, since the thread has been frequently resched, the > > normal scheduling point (no PREEMPT bit, accounted as a task RCU gp) > > seems to have very little chance to kick in. Given the nature of "busy > > polling" program, such NAPI thread won't have task->nvcsw or task->on_rq > > updated (other gp conditions), the result is that such NAPI thread is > > put on RCU holdouts list for indefinitely long time. > > > > This is simply fixed by mirroring the ksoftirqd behavior: after > > NAPI/softirq work, raise a RCU QS to help expedite the RCU period. No > > more blocking afterwards for the same setup. > > > > Fixes: 29863d41bb6e ("net: implement threaded-able napi poll loop support") > > Signed-off-by: Yan Zhai <yan@cloudflare.com> > > --- > > net/core/dev.c | 4 ++++ > > 1 file changed, 4 insertions(+) > > > > diff --git a/net/core/dev.c b/net/core/dev.c > > index 275fd5259a4a..6e41263ff5d3 100644 > > --- a/net/core/dev.c > > +++ b/net/core/dev.c > > @@ -6773,6 +6773,10 @@ static int napi_threaded_poll(void *data) > > net_rps_action_and_irq_enable(sd); > > } > > skb_defer_free_flush(sd); Please put a comment here stating that RCU readers cannot cross this point. I need to add lockdep to rcu_softirq_qs() to catch placing this in an RCU read-side critical section. And a header comment noting that from an RCU perspective, it acts as a momentary enabling of preemption. > > + if (!IS_ENABLED(CONFIG_PREEMPT_RT)) > > + rcu_softirq_qs(); > > + > > local_bh_enable(); > > > > if (!repoll) > > -- > > 2.30.2 > > > > Hmm.... > Why napi_busy_loop() does not have a similar problem ? > > It is unclear why rcu_all_qs() in __cond_resched() is guarded by > > #ifndef CONFIG_PREEMPT_RCU > rcu_all_qs(); > #endif The theory is that PREEMPT_RCU kernels have preemption, and get their quiescent states that way. The more recent practice involves things like PREEMPT_DYNAMIC and maybe soon PREEMPT_AUTO, which might require adjustments, so thank you for pointing this out! Back on the patch, my main other concern is that someone somewhere might be using something like synchronize_rcu() to wait for all in-progress softirq handlers to complete. But I don't know of such a thing, and if there is, there are workarounds, including synchronize_rcu_tasks(). So something to be aware of, not (as far as I know) something to block this commit. With the added comment: Acked-by: Paul E. McKenney <paulmck@kernel.org> Thanx, Paul
On Tue, Feb 27, 2024 at 10:44 AM Eric Dumazet <edumazet@google.com> wrote: > > Hmm.... > Why napi_busy_loop() does not have a similar problem ? > That's a good question. Let me try if I can repro this on a busy loop as well, since the structure seems very alike. > It is unclear why rcu_all_qs() in __cond_resched() is guarded by > > #ifndef CONFIG_PREEMPT_RCU > rcu_all_qs(); > #endif > > > Thanks.
On Tue, Feb 27, 2024 at 12:32 PM Paul E. McKenney <paulmck@kernel.org> wrote: > > On Tue, Feb 27, 2024 at 05:44:17PM +0100, Eric Dumazet wrote: > > On Tue, Feb 27, 2024 at 4:44 PM Yan Zhai <yan@cloudflare.com> wrote: > > > > > > We noticed task RCUs being blocked when threaded NAPIs are very busy in > > > production: detaching any BPF tracing programs, i.e. removing a ftrace > > > trampoline, will simply block for very long in rcu_tasks_wait_gp. This > > > ranges from hundreds of seconds to even an hour, severely harming any > > > observability tools that rely on BPF tracing programs. It can be > > > easily reproduced locally with following setup: > > > > > > ip netns add test1 > > > ip netns add test2 > > > > > > ip -n test1 link add veth1 type veth peer name veth2 netns test2 > > > > > > ip -n test1 link set veth1 up > > > ip -n test1 link set lo up > > > ip -n test2 link set veth2 up > > > ip -n test2 link set lo up > > > > > > ip -n test1 addr add 192.168.1.2/31 dev veth1 > > > ip -n test1 addr add 1.1.1.1/32 dev lo > > > ip -n test2 addr add 192.168.1.3/31 dev veth2 > > > ip -n test2 addr add 2.2.2.2/31 dev lo > > > > > > ip -n test1 route add default via 192.168.1.3 > > > ip -n test2 route add default via 192.168.1.2 > > > > > > for i in `seq 10 210`; do > > > for j in `seq 10 210`; do > > > ip netns exec test2 iptables -I INPUT -s 3.3.$i.$j -p udp --dport 5201 > > > done > > > done > > > > > > ip netns exec test2 ethtool -K veth2 gro on > > > ip netns exec test2 bash -c 'echo 1 > /sys/class/net/veth2/threaded' > > > ip netns exec test1 ethtool -K veth1 tso off > > > > > > Then run an iperf3 client/server and a bpftrace script can trigger it: > > > > > > ip netns exec test2 iperf3 -s -B 2.2.2.2 >/dev/null& > > > ip netns exec test1 iperf3 -c 2.2.2.2 -B 1.1.1.1 -u -l 1500 -b 3g -t 100 >/dev/null& > > > bpftrace -e 'kfunc:__napi_poll{@=count();} interval:s:1{exit();}' > > > > > > Above reproduce for net-next kernel with following RCU and preempt > > > configuraitons: > > > > > > # RCU Subsystem > > > CONFIG_TREE_RCU=y > > > CONFIG_PREEMPT_RCU=y > > > # CONFIG_RCU_EXPERT is not set > > > CONFIG_SRCU=y > > > CONFIG_TREE_SRCU=y > > > CONFIG_TASKS_RCU_GENERIC=y > > > CONFIG_TASKS_RCU=y > > > CONFIG_TASKS_RUDE_RCU=y > > > CONFIG_TASKS_TRACE_RCU=y > > > CONFIG_RCU_STALL_COMMON=y > > > CONFIG_RCU_NEED_SEGCBLIST=y > > > # end of RCU Subsystem > > > # RCU Debugging > > > # CONFIG_RCU_SCALE_TEST is not set > > > # CONFIG_RCU_TORTURE_TEST is not set > > > # CONFIG_RCU_REF_SCALE_TEST is not set > > > CONFIG_RCU_CPU_STALL_TIMEOUT=21 > > > CONFIG_RCU_EXP_CPU_STALL_TIMEOUT=0 > > > # CONFIG_RCU_TRACE is not set > > > # CONFIG_RCU_EQS_DEBUG is not set > > > # end of RCU Debugging > > > > > > CONFIG_PREEMPT_BUILD=y > > > # CONFIG_PREEMPT_NONE is not set > > > CONFIG_PREEMPT_VOLUNTARY=y > > > # CONFIG_PREEMPT is not set > > > CONFIG_PREEMPT_COUNT=y > > > CONFIG_PREEMPTION=y > > > CONFIG_PREEMPT_DYNAMIC=y > > > CONFIG_PREEMPT_RCU=y > > > CONFIG_HAVE_PREEMPT_DYNAMIC=y > > > CONFIG_HAVE_PREEMPT_DYNAMIC_CALL=y > > > CONFIG_PREEMPT_NOTIFIERS=y > > > # CONFIG_DEBUG_PREEMPT is not set > > > # CONFIG_PREEMPT_TRACER is not set > > > # CONFIG_PREEMPTIRQ_DELAY_TEST is not set > > > > > > An interesting observation is that, while tasks RCUs are blocked, > > > related NAPI thread is still being scheduled (even across cores) > > > regularly. Looking at the gp conditions, I am inclining to cond_resched > > > after each __napi_poll being the problem: cond_resched enters the > > > scheduler with PREEMPT bit, which does not account as a gp for tasks > > > RCUs. Meanwhile, since the thread has been frequently resched, the > > > normal scheduling point (no PREEMPT bit, accounted as a task RCU gp) > > > seems to have very little chance to kick in. Given the nature of "busy > > > polling" program, such NAPI thread won't have task->nvcsw or task->on_rq > > > updated (other gp conditions), the result is that such NAPI thread is > > > put on RCU holdouts list for indefinitely long time. > > > > > > This is simply fixed by mirroring the ksoftirqd behavior: after > > > NAPI/softirq work, raise a RCU QS to help expedite the RCU period. No > > > more blocking afterwards for the same setup. > > > > > > Fixes: 29863d41bb6e ("net: implement threaded-able napi poll loop support") > > > Signed-off-by: Yan Zhai <yan@cloudflare.com> > > > --- > > > net/core/dev.c | 4 ++++ > > > 1 file changed, 4 insertions(+) > > > > > > diff --git a/net/core/dev.c b/net/core/dev.c > > > index 275fd5259a4a..6e41263ff5d3 100644 > > > --- a/net/core/dev.c > > > +++ b/net/core/dev.c > > > @@ -6773,6 +6773,10 @@ static int napi_threaded_poll(void *data) > > > net_rps_action_and_irq_enable(sd); > > > } > > > skb_defer_free_flush(sd); > > Please put a comment here stating that RCU readers cannot cross > this point. > > I need to add lockdep to rcu_softirq_qs() to catch placing this in an > RCU read-side critical section. And a header comment noting that from > an RCU perspective, it acts as a momentary enabling of preemption. > Just to clarify, do you mean I should state that this polling function can not be called from within an RCU read critical section? Or do you mean any read critical sections need to end before raising this QS? Yan > > > + if (!IS_ENABLED(CONFIG_PREEMPT_RT)) > > > + rcu_softirq_qs(); > > > + > > > local_bh_enable(); > > > > > > if (!repoll) > > > -- > > > 2.30.2 > > > > > > > Hmm.... > > Why napi_busy_loop() does not have a similar problem ? > > > > It is unclear why rcu_all_qs() in __cond_resched() is guarded by > > > > #ifndef CONFIG_PREEMPT_RCU > > rcu_all_qs(); > > #endif > > The theory is that PREEMPT_RCU kernels have preemption, and get their > quiescent states that way. > > The more recent practice involves things like PREEMPT_DYNAMIC and maybe > soon PREEMPT_AUTO, which might require adjustments, so thank you for > pointing this out! > > Back on the patch, my main other concern is that someone somewhere might > be using something like synchronize_rcu() to wait for all in-progress > softirq handlers to complete. But I don't know of such a thing, and if > there is, there are workarounds, including synchronize_rcu_tasks(). > > So something to be aware of, not (as far as I know) something to block > this commit. > > With the added comment: > > Acked-by: Paul E. McKenney <paulmck@kernel.org> > > Thanx, Paul
On Tue, Feb 27, 2024 at 03:22:57PM -0600, Yan Zhai wrote: > On Tue, Feb 27, 2024 at 12:32 PM Paul E. McKenney <paulmck@kernel.org> wrote: > > > > On Tue, Feb 27, 2024 at 05:44:17PM +0100, Eric Dumazet wrote: > > > On Tue, Feb 27, 2024 at 4:44 PM Yan Zhai <yan@cloudflare.com> wrote: > > > > > > > > We noticed task RCUs being blocked when threaded NAPIs are very busy in > > > > production: detaching any BPF tracing programs, i.e. removing a ftrace > > > > trampoline, will simply block for very long in rcu_tasks_wait_gp. This > > > > ranges from hundreds of seconds to even an hour, severely harming any > > > > observability tools that rely on BPF tracing programs. It can be > > > > easily reproduced locally with following setup: > > > > > > > > ip netns add test1 > > > > ip netns add test2 > > > > > > > > ip -n test1 link add veth1 type veth peer name veth2 netns test2 > > > > > > > > ip -n test1 link set veth1 up > > > > ip -n test1 link set lo up > > > > ip -n test2 link set veth2 up > > > > ip -n test2 link set lo up > > > > > > > > ip -n test1 addr add 192.168.1.2/31 dev veth1 > > > > ip -n test1 addr add 1.1.1.1/32 dev lo > > > > ip -n test2 addr add 192.168.1.3/31 dev veth2 > > > > ip -n test2 addr add 2.2.2.2/31 dev lo > > > > > > > > ip -n test1 route add default via 192.168.1.3 > > > > ip -n test2 route add default via 192.168.1.2 > > > > > > > > for i in `seq 10 210`; do > > > > for j in `seq 10 210`; do > > > > ip netns exec test2 iptables -I INPUT -s 3.3.$i.$j -p udp --dport 5201 > > > > done > > > > done > > > > > > > > ip netns exec test2 ethtool -K veth2 gro on > > > > ip netns exec test2 bash -c 'echo 1 > /sys/class/net/veth2/threaded' > > > > ip netns exec test1 ethtool -K veth1 tso off > > > > > > > > Then run an iperf3 client/server and a bpftrace script can trigger it: > > > > > > > > ip netns exec test2 iperf3 -s -B 2.2.2.2 >/dev/null& > > > > ip netns exec test1 iperf3 -c 2.2.2.2 -B 1.1.1.1 -u -l 1500 -b 3g -t 100 >/dev/null& > > > > bpftrace -e 'kfunc:__napi_poll{@=count();} interval:s:1{exit();}' > > > > > > > > Above reproduce for net-next kernel with following RCU and preempt > > > > configuraitons: > > > > > > > > # RCU Subsystem > > > > CONFIG_TREE_RCU=y > > > > CONFIG_PREEMPT_RCU=y > > > > # CONFIG_RCU_EXPERT is not set > > > > CONFIG_SRCU=y > > > > CONFIG_TREE_SRCU=y > > > > CONFIG_TASKS_RCU_GENERIC=y > > > > CONFIG_TASKS_RCU=y > > > > CONFIG_TASKS_RUDE_RCU=y > > > > CONFIG_TASKS_TRACE_RCU=y > > > > CONFIG_RCU_STALL_COMMON=y > > > > CONFIG_RCU_NEED_SEGCBLIST=y > > > > # end of RCU Subsystem > > > > # RCU Debugging > > > > # CONFIG_RCU_SCALE_TEST is not set > > > > # CONFIG_RCU_TORTURE_TEST is not set > > > > # CONFIG_RCU_REF_SCALE_TEST is not set > > > > CONFIG_RCU_CPU_STALL_TIMEOUT=21 > > > > CONFIG_RCU_EXP_CPU_STALL_TIMEOUT=0 > > > > # CONFIG_RCU_TRACE is not set > > > > # CONFIG_RCU_EQS_DEBUG is not set > > > > # end of RCU Debugging > > > > > > > > CONFIG_PREEMPT_BUILD=y > > > > # CONFIG_PREEMPT_NONE is not set > > > > CONFIG_PREEMPT_VOLUNTARY=y > > > > # CONFIG_PREEMPT is not set > > > > CONFIG_PREEMPT_COUNT=y > > > > CONFIG_PREEMPTION=y > > > > CONFIG_PREEMPT_DYNAMIC=y > > > > CONFIG_PREEMPT_RCU=y > > > > CONFIG_HAVE_PREEMPT_DYNAMIC=y > > > > CONFIG_HAVE_PREEMPT_DYNAMIC_CALL=y > > > > CONFIG_PREEMPT_NOTIFIERS=y > > > > # CONFIG_DEBUG_PREEMPT is not set > > > > # CONFIG_PREEMPT_TRACER is not set > > > > # CONFIG_PREEMPTIRQ_DELAY_TEST is not set > > > > > > > > An interesting observation is that, while tasks RCUs are blocked, > > > > related NAPI thread is still being scheduled (even across cores) > > > > regularly. Looking at the gp conditions, I am inclining to cond_resched > > > > after each __napi_poll being the problem: cond_resched enters the > > > > scheduler with PREEMPT bit, which does not account as a gp for tasks > > > > RCUs. Meanwhile, since the thread has been frequently resched, the > > > > normal scheduling point (no PREEMPT bit, accounted as a task RCU gp) > > > > seems to have very little chance to kick in. Given the nature of "busy > > > > polling" program, such NAPI thread won't have task->nvcsw or task->on_rq > > > > updated (other gp conditions), the result is that such NAPI thread is > > > > put on RCU holdouts list for indefinitely long time. > > > > > > > > This is simply fixed by mirroring the ksoftirqd behavior: after > > > > NAPI/softirq work, raise a RCU QS to help expedite the RCU period. No > > > > more blocking afterwards for the same setup. > > > > > > > > Fixes: 29863d41bb6e ("net: implement threaded-able napi poll loop support") > > > > Signed-off-by: Yan Zhai <yan@cloudflare.com> > > > > --- > > > > net/core/dev.c | 4 ++++ > > > > 1 file changed, 4 insertions(+) > > > > > > > > diff --git a/net/core/dev.c b/net/core/dev.c > > > > index 275fd5259a4a..6e41263ff5d3 100644 > > > > --- a/net/core/dev.c > > > > +++ b/net/core/dev.c > > > > @@ -6773,6 +6773,10 @@ static int napi_threaded_poll(void *data) > > > > net_rps_action_and_irq_enable(sd); > > > > } > > > > skb_defer_free_flush(sd); > > > > Please put a comment here stating that RCU readers cannot cross > > this point. > > > > I need to add lockdep to rcu_softirq_qs() to catch placing this in an > > RCU read-side critical section. And a header comment noting that from > > an RCU perspective, it acts as a momentary enabling of preemption. > > > Just to clarify, do you mean I should state that this polling function > can not be called from within an RCU read critical section? Or do you > mean any read critical sections need to end before raising this QS? Yes to both. I am preparing a patch to make lockdep complain if you do something like this: rcu_read_lock(); do_something(); rcu_softirq_qs(); do_something_else(); rcu_read_unlock(); However, it will still be perfectly legal to do something like this: local_bh_disable(); do_something(); rcu_softirq_qs(); // A do_something_else(); local_bh_enable(); // B Which might surprise someone expection a synchronize_rcu() to wait for execution to reach B. Because of that rcu_softirq_qs(), that synchronize_rcu() could instead return as soon as execution reached A. So I am adding this example to a new kernel-doc header for rcu_softirq_qs(). Thanx, Paul > Yan > > > > > + if (!IS_ENABLED(CONFIG_PREEMPT_RT)) > > > > + rcu_softirq_qs(); > > > > + > > > > local_bh_enable(); > > > > > > > > if (!repoll) > > > > -- > > > > 2.30.2 > > > > > > > > > > Hmm.... > > > Why napi_busy_loop() does not have a similar problem ? > > > > > > It is unclear why rcu_all_qs() in __cond_resched() is guarded by > > > > > > #ifndef CONFIG_PREEMPT_RCU > > > rcu_all_qs(); > > > #endif > > > > The theory is that PREEMPT_RCU kernels have preemption, and get their > > quiescent states that way. > > > > The more recent practice involves things like PREEMPT_DYNAMIC and maybe > > soon PREEMPT_AUTO, which might require adjustments, so thank you for > > pointing this out! > > > > Back on the patch, my main other concern is that someone somewhere might > > be using something like synchronize_rcu() to wait for all in-progress > > softirq handlers to complete. But I don't know of such a thing, and if > > there is, there are workarounds, including synchronize_rcu_tasks(). > > > > So something to be aware of, not (as far as I know) something to block > > this commit. > > > > With the added comment: > > > > Acked-by: Paul E. McKenney <paulmck@kernel.org> > > > > Thanx, Paul
On Tue, 27 Feb 2024 10:32:22 -0800 Paul E. McKenney wrote: > > > + if (!IS_ENABLED(CONFIG_PREEMPT_RT)) > > > + rcu_softirq_qs(); > > > + > > > local_bh_enable(); > > > > > > if (!repoll) > > > > Hmm.... > > Why napi_busy_loop() does not have a similar problem ? > > > > It is unclear why rcu_all_qs() in __cond_resched() is guarded by > > > > #ifndef CONFIG_PREEMPT_RCU > > rcu_all_qs(); > > #endif > > The theory is that PREEMPT_RCU kernels have preemption, and get their > quiescent states that way. But that doesn't work well enough? Assuming that's the case why don't we add it with the inverse ifdef condition next to the cond_resched() which follows a few lines down? skb_defer_free_flush(sd); + + if (!IS_ENABLED(CONFIG_PREEMPT_RT)) + rcu_softirq_qs(); + local_bh_enable(); if (!repoll) break; cond_resched(); } We won't repoll majority of the time.
On Tue, Feb 27, 2024 at 07:10:01PM -0800, Jakub Kicinski wrote: > On Tue, 27 Feb 2024 10:32:22 -0800 Paul E. McKenney wrote: > > > > + if (!IS_ENABLED(CONFIG_PREEMPT_RT)) > > > > + rcu_softirq_qs(); > > > > + > > > > local_bh_enable(); > > > > > > > > if (!repoll) > > > > > > Hmm.... > > > Why napi_busy_loop() does not have a similar problem ? > > > > > > It is unclear why rcu_all_qs() in __cond_resched() is guarded by > > > > > > #ifndef CONFIG_PREEMPT_RCU > > > rcu_all_qs(); > > > #endif > > > > The theory is that PREEMPT_RCU kernels have preemption, and get their > > quiescent states that way. > > But that doesn't work well enough? > > Assuming that's the case why don't we add it with the inverse ifdef > condition next to the cond_resched() which follows a few lines down? > > skb_defer_free_flush(sd); > + > + if (!IS_ENABLED(CONFIG_PREEMPT_RT)) > + rcu_softirq_qs(); > + > local_bh_enable(); > > if (!repoll) > break; > > cond_resched(); > } > > We won't repoll majority of the time. I am not completely clear on what you are proposing, but one complication is that We need preemption disabled across calls to rcu_softirq_qs() and we cannot have preemption disabled across calls to cond_resched(). Another complication is that although CONFIG_PREEMPT_RT kernels are built with CONFIG_PREEMPT_RCU, the reverse is not always the case. And if we are not repolling, don't we have a high probability of doing a voluntary context when we reach napi_thread_wait() at the beginning of that loop? All in all, I suspect that I am missing your point. Thanx, Paul
"Paul E. McKenney" <paulmck@kernel.org> writes: > On Tue, Feb 27, 2024 at 05:44:17PM +0100, Eric Dumazet wrote: >> On Tue, Feb 27, 2024 at 4:44 PM Yan Zhai <yan@cloudflare.com> wrote: >> > >> > We noticed task RCUs being blocked when threaded NAPIs are very busy in >> > production: detaching any BPF tracing programs, i.e. removing a ftrace >> > trampoline, will simply block for very long in rcu_tasks_wait_gp. This >> > ranges from hundreds of seconds to even an hour, severely harming any >> > observability tools that rely on BPF tracing programs. It can be >> > easily reproduced locally with following setup: >> > >> > ip netns add test1 >> > ip netns add test2 >> > >> > ip -n test1 link add veth1 type veth peer name veth2 netns test2 >> > >> > ip -n test1 link set veth1 up >> > ip -n test1 link set lo up >> > ip -n test2 link set veth2 up >> > ip -n test2 link set lo up >> > >> > ip -n test1 addr add 192.168.1.2/31 dev veth1 >> > ip -n test1 addr add 1.1.1.1/32 dev lo >> > ip -n test2 addr add 192.168.1.3/31 dev veth2 >> > ip -n test2 addr add 2.2.2.2/31 dev lo >> > >> > ip -n test1 route add default via 192.168.1.3 >> > ip -n test2 route add default via 192.168.1.2 >> > >> > for i in `seq 10 210`; do >> > for j in `seq 10 210`; do >> > ip netns exec test2 iptables -I INPUT -s 3.3.$i.$j -p udp --dport 5201 >> > done >> > done >> > >> > ip netns exec test2 ethtool -K veth2 gro on >> > ip netns exec test2 bash -c 'echo 1 > /sys/class/net/veth2/threaded' >> > ip netns exec test1 ethtool -K veth1 tso off >> > >> > Then run an iperf3 client/server and a bpftrace script can trigger it: >> > >> > ip netns exec test2 iperf3 -s -B 2.2.2.2 >/dev/null& >> > ip netns exec test1 iperf3 -c 2.2.2.2 -B 1.1.1.1 -u -l 1500 -b 3g -t 100 >/dev/null& >> > bpftrace -e 'kfunc:__napi_poll{@=count();} interval:s:1{exit();}' >> > >> > Above reproduce for net-next kernel with following RCU and preempt >> > configuraitons: >> > >> > # RCU Subsystem >> > CONFIG_TREE_RCU=y >> > CONFIG_PREEMPT_RCU=y >> > # CONFIG_RCU_EXPERT is not set >> > CONFIG_SRCU=y >> > CONFIG_TREE_SRCU=y >> > CONFIG_TASKS_RCU_GENERIC=y >> > CONFIG_TASKS_RCU=y >> > CONFIG_TASKS_RUDE_RCU=y >> > CONFIG_TASKS_TRACE_RCU=y >> > CONFIG_RCU_STALL_COMMON=y >> > CONFIG_RCU_NEED_SEGCBLIST=y >> > # end of RCU Subsystem >> > # RCU Debugging >> > # CONFIG_RCU_SCALE_TEST is not set >> > # CONFIG_RCU_TORTURE_TEST is not set >> > # CONFIG_RCU_REF_SCALE_TEST is not set >> > CONFIG_RCU_CPU_STALL_TIMEOUT=21 >> > CONFIG_RCU_EXP_CPU_STALL_TIMEOUT=0 >> > # CONFIG_RCU_TRACE is not set >> > # CONFIG_RCU_EQS_DEBUG is not set >> > # end of RCU Debugging >> > >> > CONFIG_PREEMPT_BUILD=y >> > # CONFIG_PREEMPT_NONE is not set >> > CONFIG_PREEMPT_VOLUNTARY=y >> > # CONFIG_PREEMPT is not set >> > CONFIG_PREEMPT_COUNT=y >> > CONFIG_PREEMPTION=y >> > CONFIG_PREEMPT_DYNAMIC=y >> > CONFIG_PREEMPT_RCU=y >> > CONFIG_HAVE_PREEMPT_DYNAMIC=y >> > CONFIG_HAVE_PREEMPT_DYNAMIC_CALL=y >> > CONFIG_PREEMPT_NOTIFIERS=y >> > # CONFIG_DEBUG_PREEMPT is not set >> > # CONFIG_PREEMPT_TRACER is not set >> > # CONFIG_PREEMPTIRQ_DELAY_TEST is not set >> > >> > An interesting observation is that, while tasks RCUs are blocked, >> > related NAPI thread is still being scheduled (even across cores) >> > regularly. Looking at the gp conditions, I am inclining to cond_resched >> > after each __napi_poll being the problem: cond_resched enters the >> > scheduler with PREEMPT bit, which does not account as a gp for tasks >> > RCUs. Meanwhile, since the thread has been frequently resched, the >> > normal scheduling point (no PREEMPT bit, accounted as a task RCU gp) >> > seems to have very little chance to kick in. Given the nature of "busy >> > polling" program, such NAPI thread won't have task->nvcsw or task->on_rq >> > updated (other gp conditions), the result is that such NAPI thread is >> > put on RCU holdouts list for indefinitely long time. >> > >> > This is simply fixed by mirroring the ksoftirqd behavior: after >> > NAPI/softirq work, raise a RCU QS to help expedite the RCU period. No >> > more blocking afterwards for the same setup. >> > >> > Fixes: 29863d41bb6e ("net: implement threaded-able napi poll loop support") >> > Signed-off-by: Yan Zhai <yan@cloudflare.com> >> > --- >> > net/core/dev.c | 4 ++++ >> > 1 file changed, 4 insertions(+) >> > >> > diff --git a/net/core/dev.c b/net/core/dev.c >> > index 275fd5259a4a..6e41263ff5d3 100644 >> > --- a/net/core/dev.c >> > +++ b/net/core/dev.c >> > @@ -6773,6 +6773,10 @@ static int napi_threaded_poll(void *data) >> > net_rps_action_and_irq_enable(sd); >> > } >> > skb_defer_free_flush(sd); > > Please put a comment here stating that RCU readers cannot cross > this point. > > I need to add lockdep to rcu_softirq_qs() to catch placing this in an > RCU read-side critical section. And a header comment noting that from > an RCU perspective, it acts as a momentary enabling of preemption. OK, so one question here: for XDP, we're basically treating local_bh_disable/enable() as the RCU critical section, cf the discussion we had a few years ago that led to this being documented[0]. So why is it OK to have the rcu_softirq_qs() inside the bh disable/enable pair, but not inside an rcu_read_lock() section? Also, looking at the patch in question: >> > + if (!IS_ENABLED(CONFIG_PREEMPT_RT)) >> > + rcu_softirq_qs(); >> > + >> > local_bh_enable(); Why does that local_bh_enable() not accomplish the same thing as the qs? -Toke [0] https://lore.kernel.org/bpf/20210624160609.292325-6-toke@redhat.com/
On Tue, 27 Feb 2024 20:42:24 -0800 Paul E. McKenney wrote: > On Tue, Feb 27, 2024 at 07:10:01PM -0800, Jakub Kicinski wrote: > > On Tue, 27 Feb 2024 10:32:22 -0800 Paul E. McKenney wrote: > > > The theory is that PREEMPT_RCU kernels have preemption, and get their > > > quiescent states that way. > > > > But that doesn't work well enough? > > > > Assuming that's the case why don't we add it with the inverse ifdef > > condition next to the cond_resched() which follows a few lines down? > > > > skb_defer_free_flush(sd); > > + > > + if (!IS_ENABLED(CONFIG_PREEMPT_RT)) > > + rcu_softirq_qs(); > > + > > local_bh_enable(); > > > > if (!repoll) > > break; > > > > cond_resched(); > > } > > > > We won't repoll majority of the time. > > I am not completely clear on what you are proposing, but one complication > is that We need preemption disabled across calls to rcu_softirq_qs() > and we cannot have preemption disabled across calls to cond_resched(). I was thinking of using rcu_all_qs(), like cond_resched() does. Not sure how it compares in terms of functionality and cost. > Another complication is that although CONFIG_PREEMPT_RT kernels are > built with CONFIG_PREEMPT_RCU, the reverse is not always the case. > And if we are not repolling, don't we have a high probability of doing > a voluntary context when we reach napi_thread_wait() at the beginning > of that loop? Very much so, which is why adding the cost of rcu_softirq_qs() for every NAPI run feels like an overkill.
On Wed, Feb 28, 2024 at 12:50:53PM +0100, Toke Høiland-Jørgensen wrote: > "Paul E. McKenney" <paulmck@kernel.org> writes: > > > On Tue, Feb 27, 2024 at 05:44:17PM +0100, Eric Dumazet wrote: > >> On Tue, Feb 27, 2024 at 4:44 PM Yan Zhai <yan@cloudflare.com> wrote: > >> > > >> > We noticed task RCUs being blocked when threaded NAPIs are very busy in > >> > production: detaching any BPF tracing programs, i.e. removing a ftrace > >> > trampoline, will simply block for very long in rcu_tasks_wait_gp. This > >> > ranges from hundreds of seconds to even an hour, severely harming any > >> > observability tools that rely on BPF tracing programs. It can be > >> > easily reproduced locally with following setup: > >> > > >> > ip netns add test1 > >> > ip netns add test2 > >> > > >> > ip -n test1 link add veth1 type veth peer name veth2 netns test2 > >> > > >> > ip -n test1 link set veth1 up > >> > ip -n test1 link set lo up > >> > ip -n test2 link set veth2 up > >> > ip -n test2 link set lo up > >> > > >> > ip -n test1 addr add 192.168.1.2/31 dev veth1 > >> > ip -n test1 addr add 1.1.1.1/32 dev lo > >> > ip -n test2 addr add 192.168.1.3/31 dev veth2 > >> > ip -n test2 addr add 2.2.2.2/31 dev lo > >> > > >> > ip -n test1 route add default via 192.168.1.3 > >> > ip -n test2 route add default via 192.168.1.2 > >> > > >> > for i in `seq 10 210`; do > >> > for j in `seq 10 210`; do > >> > ip netns exec test2 iptables -I INPUT -s 3.3.$i.$j -p udp --dport 5201 > >> > done > >> > done > >> > > >> > ip netns exec test2 ethtool -K veth2 gro on > >> > ip netns exec test2 bash -c 'echo 1 > /sys/class/net/veth2/threaded' > >> > ip netns exec test1 ethtool -K veth1 tso off > >> > > >> > Then run an iperf3 client/server and a bpftrace script can trigger it: > >> > > >> > ip netns exec test2 iperf3 -s -B 2.2.2.2 >/dev/null& > >> > ip netns exec test1 iperf3 -c 2.2.2.2 -B 1.1.1.1 -u -l 1500 -b 3g -t 100 >/dev/null& > >> > bpftrace -e 'kfunc:__napi_poll{@=count();} interval:s:1{exit();}' > >> > > >> > Above reproduce for net-next kernel with following RCU and preempt > >> > configuraitons: > >> > > >> > # RCU Subsystem > >> > CONFIG_TREE_RCU=y > >> > CONFIG_PREEMPT_RCU=y > >> > # CONFIG_RCU_EXPERT is not set > >> > CONFIG_SRCU=y > >> > CONFIG_TREE_SRCU=y > >> > CONFIG_TASKS_RCU_GENERIC=y > >> > CONFIG_TASKS_RCU=y > >> > CONFIG_TASKS_RUDE_RCU=y > >> > CONFIG_TASKS_TRACE_RCU=y > >> > CONFIG_RCU_STALL_COMMON=y > >> > CONFIG_RCU_NEED_SEGCBLIST=y > >> > # end of RCU Subsystem > >> > # RCU Debugging > >> > # CONFIG_RCU_SCALE_TEST is not set > >> > # CONFIG_RCU_TORTURE_TEST is not set > >> > # CONFIG_RCU_REF_SCALE_TEST is not set > >> > CONFIG_RCU_CPU_STALL_TIMEOUT=21 > >> > CONFIG_RCU_EXP_CPU_STALL_TIMEOUT=0 > >> > # CONFIG_RCU_TRACE is not set > >> > # CONFIG_RCU_EQS_DEBUG is not set > >> > # end of RCU Debugging > >> > > >> > CONFIG_PREEMPT_BUILD=y > >> > # CONFIG_PREEMPT_NONE is not set > >> > CONFIG_PREEMPT_VOLUNTARY=y > >> > # CONFIG_PREEMPT is not set > >> > CONFIG_PREEMPT_COUNT=y > >> > CONFIG_PREEMPTION=y > >> > CONFIG_PREEMPT_DYNAMIC=y > >> > CONFIG_PREEMPT_RCU=y > >> > CONFIG_HAVE_PREEMPT_DYNAMIC=y > >> > CONFIG_HAVE_PREEMPT_DYNAMIC_CALL=y > >> > CONFIG_PREEMPT_NOTIFIERS=y > >> > # CONFIG_DEBUG_PREEMPT is not set > >> > # CONFIG_PREEMPT_TRACER is not set > >> > # CONFIG_PREEMPTIRQ_DELAY_TEST is not set > >> > > >> > An interesting observation is that, while tasks RCUs are blocked, > >> > related NAPI thread is still being scheduled (even across cores) > >> > regularly. Looking at the gp conditions, I am inclining to cond_resched > >> > after each __napi_poll being the problem: cond_resched enters the > >> > scheduler with PREEMPT bit, which does not account as a gp for tasks > >> > RCUs. Meanwhile, since the thread has been frequently resched, the > >> > normal scheduling point (no PREEMPT bit, accounted as a task RCU gp) > >> > seems to have very little chance to kick in. Given the nature of "busy > >> > polling" program, such NAPI thread won't have task->nvcsw or task->on_rq > >> > updated (other gp conditions), the result is that such NAPI thread is > >> > put on RCU holdouts list for indefinitely long time. > >> > > >> > This is simply fixed by mirroring the ksoftirqd behavior: after > >> > NAPI/softirq work, raise a RCU QS to help expedite the RCU period. No > >> > more blocking afterwards for the same setup. > >> > > >> > Fixes: 29863d41bb6e ("net: implement threaded-able napi poll loop support") > >> > Signed-off-by: Yan Zhai <yan@cloudflare.com> > >> > --- > >> > net/core/dev.c | 4 ++++ > >> > 1 file changed, 4 insertions(+) > >> > > >> > diff --git a/net/core/dev.c b/net/core/dev.c > >> > index 275fd5259a4a..6e41263ff5d3 100644 > >> > --- a/net/core/dev.c > >> > +++ b/net/core/dev.c > >> > @@ -6773,6 +6773,10 @@ static int napi_threaded_poll(void *data) > >> > net_rps_action_and_irq_enable(sd); > >> > } > >> > skb_defer_free_flush(sd); > > > > Please put a comment here stating that RCU readers cannot cross > > this point. > > > > I need to add lockdep to rcu_softirq_qs() to catch placing this in an > > RCU read-side critical section. And a header comment noting that from > > an RCU perspective, it acts as a momentary enabling of preemption. > > OK, so one question here: for XDP, we're basically treating > local_bh_disable/enable() as the RCU critical section, cf the discussion > we had a few years ago that led to this being documented[0]. So why is > it OK to have the rcu_softirq_qs() inside the bh disable/enable pair, > but not inside an rcu_read_lock() section? In general, it is not OK. And it is not OK in this case if this happens to be one of the local_bh_disable() regions that XDP is waiting on. Except that that region ends right after the rcu_softirq_qs(), so that should not be a problem. But you are quite right, that is an accident waiting to happen, so it would be better if the patch did something like this: local_bh_enable(); if (!IS_ENABLED(CONFIG_PREEMPT_RT)) { preempt_disable(); rcu_softirq_qs(); preempt_enable(); } Though maybe something like this would be better: local_bh_enable(); if (!IS_ENABLED(CONFIG_PREEMPT_RT)) rcu_softirq_qs_enable(local_bh_enable()); else local_bh_enable(); A bit ugly, but it does allow exact checking of the rules and also avoids extra overhead. I could imagine pulling the CONFIG_PREEMPT_RT check into the body of rcu_softirq_qs_enable(). But is there a better way? > Also, looking at the patch in question: > > >> > + if (!IS_ENABLED(CONFIG_PREEMPT_RT)) > >> > + rcu_softirq_qs(); > >> > + > >> > local_bh_enable(); > > Why does that local_bh_enable() not accomplish the same thing as the qs? In this case, because it does not create the appearance of a voluntary context switch needed by RCU Tasks. So the wait for trampoline evacuation could still take a very long time. Thanx, Paul > -Toke > > [0] https://lore.kernel.org/bpf/20210624160609.292325-6-toke@redhat.com/ >
On Wed, Feb 28, 2024 at 06:43:43AM -0800, Jakub Kicinski wrote: > On Tue, 27 Feb 2024 20:42:24 -0800 Paul E. McKenney wrote: > > On Tue, Feb 27, 2024 at 07:10:01PM -0800, Jakub Kicinski wrote: > > > On Tue, 27 Feb 2024 10:32:22 -0800 Paul E. McKenney wrote: > > > > The theory is that PREEMPT_RCU kernels have preemption, and get their > > > > quiescent states that way. > > > > > > But that doesn't work well enough? > > > > > > Assuming that's the case why don't we add it with the inverse ifdef > > > condition next to the cond_resched() which follows a few lines down? > > > > > > skb_defer_free_flush(sd); > > > + > > > + if (!IS_ENABLED(CONFIG_PREEMPT_RT)) > > > + rcu_softirq_qs(); > > > + > > > local_bh_enable(); > > > > > > if (!repoll) > > > break; > > > > > > cond_resched(); > > > } > > > > > > We won't repoll majority of the time. > > > > I am not completely clear on what you are proposing, but one complication > > is that We need preemption disabled across calls to rcu_softirq_qs() > > and we cannot have preemption disabled across calls to cond_resched(). > > I was thinking of using rcu_all_qs(), like cond_resched() does. > Not sure how it compares in terms of functionality and cost. It is probably a bit cheaper, but it does nothing for Tasks RCU. And that "_all" in the name is a holdover from when there were separate mechanisms for bh, sched, and preempt, so maybe we should change that name. > > Another complication is that although CONFIG_PREEMPT_RT kernels are > > built with CONFIG_PREEMPT_RCU, the reverse is not always the case. > > And if we are not repolling, don't we have a high probability of doing > > a voluntary context when we reach napi_thread_wait() at the beginning > > of that loop? > > Very much so, which is why adding the cost of rcu_softirq_qs() > for every NAPI run feels like an overkill. Would it be better to do the rcu_softirq_qs() only once every 1000 times or some such? Or once every HZ jiffies? Or is there a better way? Thanx, Paul
On Wed, 28 Feb 2024 07:15:42 -0800 Paul E. McKenney wrote: > > > Another complication is that although CONFIG_PREEMPT_RT kernels are > > > built with CONFIG_PREEMPT_RCU, the reverse is not always the case. > > > And if we are not repolling, don't we have a high probability of doing > > > a voluntary context when we reach napi_thread_wait() at the beginning > > > of that loop? > > > > Very much so, which is why adding the cost of rcu_softirq_qs() > > for every NAPI run feels like an overkill. > > Would it be better to do the rcu_softirq_qs() only once every 1000 times > or some such? Or once every HZ jiffies? > > Or is there a better way? Right, we can do that. Yan Zhai, have you measured the performance impact / time spent in the call?
On 2/27/2024 1:32 PM, Paul E. McKenney wrote: > On Tue, Feb 27, 2024 at 05:44:17PM +0100, Eric Dumazet wrote: >> On Tue, Feb 27, 2024 at 4:44 PM Yan Zhai <yan@cloudflare.com> wrote: >>> We noticed task RCUs being blocked when threaded NAPIs are very busy in >>> production: detaching any BPF tracing programs, i.e. removing a ftrace >>> trampoline, will simply block for very long in rcu_tasks_wait_gp. This >>> ranges from hundreds of seconds to even an hour, severely harming any >>> observability tools that rely on BPF tracing programs. It can be >>> easily reproduced locally with following setup: >>> >>> ip netns add test1 >>> ip netns add test2 >>> >>> ip -n test1 link add veth1 type veth peer name veth2 netns test2 >>> >>> ip -n test1 link set veth1 up >>> ip -n test1 link set lo up >>> ip -n test2 link set veth2 up >>> ip -n test2 link set lo up >>> >>> ip -n test1 addr add 192.168.1.2/31 dev veth1 >>> ip -n test1 addr add 1.1.1.1/32 dev lo >>> ip -n test2 addr add 192.168.1.3/31 dev veth2 >>> ip -n test2 addr add 2.2.2.2/31 dev lo >>> >>> ip -n test1 route add default via 192.168.1.3 >>> ip -n test2 route add default via 192.168.1.2 >>> >>> for i in `seq 10 210`; do >>> for j in `seq 10 210`; do >>> ip netns exec test2 iptables -I INPUT -s 3.3.$i.$j -p udp --dport 5201 >>> done >>> done >>> >>> ip netns exec test2 ethtool -K veth2 gro on >>> ip netns exec test2 bash -c 'echo 1 > /sys/class/net/veth2/threaded' >>> ip netns exec test1 ethtool -K veth1 tso off >>> >>> Then run an iperf3 client/server and a bpftrace script can trigger it: >>> >>> ip netns exec test2 iperf3 -s -B 2.2.2.2 >/dev/null& >>> ip netns exec test1 iperf3 -c 2.2.2.2 -B 1.1.1.1 -u -l 1500 -b 3g -t 100 >/dev/null& >>> bpftrace -e 'kfunc:__napi_poll{@=count();} interval:s:1{exit();}' >>> >>> Above reproduce for net-next kernel with following RCU and preempt >>> configuraitons: >>> >>> # RCU Subsystem >>> CONFIG_TREE_RCU=y >>> CONFIG_PREEMPT_RCU=y >>> # CONFIG_RCU_EXPERT is not set >>> CONFIG_SRCU=y >>> CONFIG_TREE_SRCU=y >>> CONFIG_TASKS_RCU_GENERIC=y >>> CONFIG_TASKS_RCU=y >>> CONFIG_TASKS_RUDE_RCU=y >>> CONFIG_TASKS_TRACE_RCU=y >>> CONFIG_RCU_STALL_COMMON=y >>> CONFIG_RCU_NEED_SEGCBLIST=y >>> # end of RCU Subsystem >>> # RCU Debugging >>> # CONFIG_RCU_SCALE_TEST is not set >>> # CONFIG_RCU_TORTURE_TEST is not set >>> # CONFIG_RCU_REF_SCALE_TEST is not set >>> CONFIG_RCU_CPU_STALL_TIMEOUT=21 >>> CONFIG_RCU_EXP_CPU_STALL_TIMEOUT=0 >>> # CONFIG_RCU_TRACE is not set >>> # CONFIG_RCU_EQS_DEBUG is not set >>> # end of RCU Debugging >>> >>> CONFIG_PREEMPT_BUILD=y >>> # CONFIG_PREEMPT_NONE is not set >>> CONFIG_PREEMPT_VOLUNTARY=y >>> # CONFIG_PREEMPT is not set >>> CONFIG_PREEMPT_COUNT=y >>> CONFIG_PREEMPTION=y >>> CONFIG_PREEMPT_DYNAMIC=y >>> CONFIG_PREEMPT_RCU=y >>> CONFIG_HAVE_PREEMPT_DYNAMIC=y >>> CONFIG_HAVE_PREEMPT_DYNAMIC_CALL=y >>> CONFIG_PREEMPT_NOTIFIERS=y >>> # CONFIG_DEBUG_PREEMPT is not set >>> # CONFIG_PREEMPT_TRACER is not set >>> # CONFIG_PREEMPTIRQ_DELAY_TEST is not set >>> >>> An interesting observation is that, while tasks RCUs are blocked, >>> related NAPI thread is still being scheduled (even across cores) >>> regularly. Looking at the gp conditions, I am inclining to cond_resched >>> after each __napi_poll being the problem: cond_resched enters the >>> scheduler with PREEMPT bit, which does not account as a gp for tasks >>> RCUs. Meanwhile, since the thread has been frequently resched, the >>> normal scheduling point (no PREEMPT bit, accounted as a task RCU gp) >>> seems to have very little chance to kick in. Given the nature of "busy >>> polling" program, such NAPI thread won't have task->nvcsw or task->on_rq >>> updated (other gp conditions), the result is that such NAPI thread is >>> put on RCU holdouts list for indefinitely long time. >>> >>> This is simply fixed by mirroring the ksoftirqd behavior: after >>> NAPI/softirq work, raise a RCU QS to help expedite the RCU period. No >>> more blocking afterwards for the same setup. >>> >>> Fixes: 29863d41bb6e ("net: implement threaded-able napi poll loop support") >>> Signed-off-by: Yan Zhai <yan@cloudflare.com> >>> --- >>> net/core/dev.c | 4 ++++ >>> 1 file changed, 4 insertions(+) >>> >>> diff --git a/net/core/dev.c b/net/core/dev.c >>> index 275fd5259a4a..6e41263ff5d3 100644 >>> --- a/net/core/dev.c >>> +++ b/net/core/dev.c >>> @@ -6773,6 +6773,10 @@ static int napi_threaded_poll(void *data) >>> net_rps_action_and_irq_enable(sd); >>> } >>> skb_defer_free_flush(sd); > Please put a comment here stating that RCU readers cannot cross > this point. > > I need to add lockdep to rcu_softirq_qs() to catch placing this in an > RCU read-side critical section. And a header comment noting that from > an RCU perspective, it acts as a momentary enabling of preemption. Agreed, also does PREEMPT_RT not have similar issue? I noticed Thomas had added this to softirq.c [1] but the changelog did not have details. Also optionally, I wonder if calling rcu_tasks_qs() directly is better (for documentation if anything) since the issue is Tasks RCU specific. Also code comment above the rcu_softirq_qs() call about cond_resched() not taking care of Tasks RCU would be great! Reviewed-by: Joel Fernandes (Google) <joel@joelfernandes.org> thanks, - Joel [1] @@ -381,8 +553,10 @@ asmlinkage __visible void __softirq_entry __do_softirq(void) pending >>= softirq_bit; } - if (__this_cpu_read(ksoftirqd) == current) + if (!IS_ENABLED(CONFIG_PREEMPT_RT) && + __this_cpu_read(ksoftirqd) == current) rcu_softirq_qs(); + local_irq_disable();
On Wed, Feb 28, 2024 at 9:10 AM Paul E. McKenney <paulmck@kernel.org> wrote: > > On Wed, Feb 28, 2024 at 12:50:53PM +0100, Toke Høiland-Jørgensen wrote: > > "Paul E. McKenney" <paulmck@kernel.org> writes: > > > > > On Tue, Feb 27, 2024 at 05:44:17PM +0100, Eric Dumazet wrote: > > >> On Tue, Feb 27, 2024 at 4:44 PM Yan Zhai <yan@cloudflare.com> wrote: > > >> > > > >> > We noticed task RCUs being blocked when threaded NAPIs are very busy in > > >> > production: detaching any BPF tracing programs, i.e. removing a ftrace > > >> > trampoline, will simply block for very long in rcu_tasks_wait_gp. This > > >> > ranges from hundreds of seconds to even an hour, severely harming any > > >> > observability tools that rely on BPF tracing programs. It can be > > >> > easily reproduced locally with following setup: > > >> > > > >> > ip netns add test1 > > >> > ip netns add test2 > > >> > > > >> > ip -n test1 link add veth1 type veth peer name veth2 netns test2 > > >> > > > >> > ip -n test1 link set veth1 up > > >> > ip -n test1 link set lo up > > >> > ip -n test2 link set veth2 up > > >> > ip -n test2 link set lo up > > >> > > > >> > ip -n test1 addr add 192.168.1.2/31 dev veth1 > > >> > ip -n test1 addr add 1.1.1.1/32 dev lo > > >> > ip -n test2 addr add 192.168.1.3/31 dev veth2 > > >> > ip -n test2 addr add 2.2.2.2/31 dev lo > > >> > > > >> > ip -n test1 route add default via 192.168.1.3 > > >> > ip -n test2 route add default via 192.168.1.2 > > >> > > > >> > for i in `seq 10 210`; do > > >> > for j in `seq 10 210`; do > > >> > ip netns exec test2 iptables -I INPUT -s 3.3.$i.$j -p udp --dport 5201 > > >> > done > > >> > done > > >> > > > >> > ip netns exec test2 ethtool -K veth2 gro on > > >> > ip netns exec test2 bash -c 'echo 1 > /sys/class/net/veth2/threaded' > > >> > ip netns exec test1 ethtool -K veth1 tso off > > >> > > > >> > Then run an iperf3 client/server and a bpftrace script can trigger it: > > >> > > > >> > ip netns exec test2 iperf3 -s -B 2.2.2.2 >/dev/null& > > >> > ip netns exec test1 iperf3 -c 2.2.2.2 -B 1.1.1.1 -u -l 1500 -b 3g -t 100 >/dev/null& > > >> > bpftrace -e 'kfunc:__napi_poll{@=count();} interval:s:1{exit();}' > > >> > > > >> > Above reproduce for net-next kernel with following RCU and preempt > > >> > configuraitons: > > >> > > > >> > # RCU Subsystem > > >> > CONFIG_TREE_RCU=y > > >> > CONFIG_PREEMPT_RCU=y > > >> > # CONFIG_RCU_EXPERT is not set > > >> > CONFIG_SRCU=y > > >> > CONFIG_TREE_SRCU=y > > >> > CONFIG_TASKS_RCU_GENERIC=y > > >> > CONFIG_TASKS_RCU=y > > >> > CONFIG_TASKS_RUDE_RCU=y > > >> > CONFIG_TASKS_TRACE_RCU=y > > >> > CONFIG_RCU_STALL_COMMON=y > > >> > CONFIG_RCU_NEED_SEGCBLIST=y > > >> > # end of RCU Subsystem > > >> > # RCU Debugging > > >> > # CONFIG_RCU_SCALE_TEST is not set > > >> > # CONFIG_RCU_TORTURE_TEST is not set > > >> > # CONFIG_RCU_REF_SCALE_TEST is not set > > >> > CONFIG_RCU_CPU_STALL_TIMEOUT=21 > > >> > CONFIG_RCU_EXP_CPU_STALL_TIMEOUT=0 > > >> > # CONFIG_RCU_TRACE is not set > > >> > # CONFIG_RCU_EQS_DEBUG is not set > > >> > # end of RCU Debugging > > >> > > > >> > CONFIG_PREEMPT_BUILD=y > > >> > # CONFIG_PREEMPT_NONE is not set > > >> > CONFIG_PREEMPT_VOLUNTARY=y > > >> > # CONFIG_PREEMPT is not set > > >> > CONFIG_PREEMPT_COUNT=y > > >> > CONFIG_PREEMPTION=y > > >> > CONFIG_PREEMPT_DYNAMIC=y > > >> > CONFIG_PREEMPT_RCU=y > > >> > CONFIG_HAVE_PREEMPT_DYNAMIC=y > > >> > CONFIG_HAVE_PREEMPT_DYNAMIC_CALL=y > > >> > CONFIG_PREEMPT_NOTIFIERS=y > > >> > # CONFIG_DEBUG_PREEMPT is not set > > >> > # CONFIG_PREEMPT_TRACER is not set > > >> > # CONFIG_PREEMPTIRQ_DELAY_TEST is not set > > >> > > > >> > An interesting observation is that, while tasks RCUs are blocked, > > >> > related NAPI thread is still being scheduled (even across cores) > > >> > regularly. Looking at the gp conditions, I am inclining to cond_resched > > >> > after each __napi_poll being the problem: cond_resched enters the > > >> > scheduler with PREEMPT bit, which does not account as a gp for tasks > > >> > RCUs. Meanwhile, since the thread has been frequently resched, the > > >> > normal scheduling point (no PREEMPT bit, accounted as a task RCU gp) > > >> > seems to have very little chance to kick in. Given the nature of "busy > > >> > polling" program, such NAPI thread won't have task->nvcsw or task->on_rq > > >> > updated (other gp conditions), the result is that such NAPI thread is > > >> > put on RCU holdouts list for indefinitely long time. > > >> > > > >> > This is simply fixed by mirroring the ksoftirqd behavior: after > > >> > NAPI/softirq work, raise a RCU QS to help expedite the RCU period. No > > >> > more blocking afterwards for the same setup. > > >> > > > >> > Fixes: 29863d41bb6e ("net: implement threaded-able napi poll loop support") > > >> > Signed-off-by: Yan Zhai <yan@cloudflare.com> > > >> > --- > > >> > net/core/dev.c | 4 ++++ > > >> > 1 file changed, 4 insertions(+) > > >> > > > >> > diff --git a/net/core/dev.c b/net/core/dev.c > > >> > index 275fd5259a4a..6e41263ff5d3 100644 > > >> > --- a/net/core/dev.c > > >> > +++ b/net/core/dev.c > > >> > @@ -6773,6 +6773,10 @@ static int napi_threaded_poll(void *data) > > >> > net_rps_action_and_irq_enable(sd); > > >> > } > > >> > skb_defer_free_flush(sd); > > > > > > Please put a comment here stating that RCU readers cannot cross > > > this point. > > > > > > I need to add lockdep to rcu_softirq_qs() to catch placing this in an > > > RCU read-side critical section. And a header comment noting that from > > > an RCU perspective, it acts as a momentary enabling of preemption. > > > > OK, so one question here: for XDP, we're basically treating > > local_bh_disable/enable() as the RCU critical section, cf the discussion > > we had a few years ago that led to this being documented[0]. So why is > > it OK to have the rcu_softirq_qs() inside the bh disable/enable pair, > > but not inside an rcu_read_lock() section? > > In general, it is not OK. And it is not OK in this case if this happens > to be one of the local_bh_disable() regions that XDP is waiting on. > Except that that region ends right after the rcu_softirq_qs(), so that > should not be a problem. > > But you are quite right, that is an accident waiting to happen, so it > would be better if the patch did something like this: > > local_bh_enable(); > if (!IS_ENABLED(CONFIG_PREEMPT_RT)) { > preempt_disable(); > rcu_softirq_qs(); > preempt_enable(); > } > Yeah we need preempt for this call. When I first attempt it after local_bh_enable, I got the bug call: [ 1166.384279] BUG: using __this_cpu_read() in preemptible [00000000] code: napi/veth2-66/8439 [ 1166.385337] caller is rcu_softirq_qs+0x16/0x130 [ 1166.385900] CPU: 3 PID: 8439 Comm: napi/veth2-66 Not tainted 6.7.0-rc8-g3fbf61207c66-dirty #75 [ 1166.386950] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.16.2-debian-1.16.2-1 04/01/2014 [ 1166.388110] Call Trace: [ 1166.388417] <TASK> [ 1166.388684] dump_stack_lvl+0x36/0x50 [ 1166.389147] check_preemption_disabled+0xd1/0xe0 [ 1166.389725] rcu_softirq_qs+0x16/0x130 [ 1166.390190] napi_threaded_poll+0x21e/0x260 [ 1166.390702] ? __pfx_napi_threaded_poll+0x10/0x10 [ 1166.391277] kthread+0xf7/0x130 [ 1166.391643] ? __pfx_kthread+0x10/0x10 [ 1166.392130] ret_from_fork+0x34/0x50 [ 1166.392574] ? __pfx_kthread+0x10/0x10 [ 1166.393048] ret_from_fork_asm+0x1b/0x30 [ 1166.393530] </TASK> Since this patch is trying to mirror what __do_softirq has, should the similar notes/changes apply to that side as well? > Though maybe something like this would be better: > > local_bh_enable(); > if (!IS_ENABLED(CONFIG_PREEMPT_RT)) > rcu_softirq_qs_enable(local_bh_enable()); > else > local_bh_enable(); > > A bit ugly, but it does allow exact checking of the rules and also > avoids extra overhead. > > I could imagine pulling the CONFIG_PREEMPT_RT check into the body of > rcu_softirq_qs_enable(). > > But is there a better way? > > > Also, looking at the patch in question: > > > > >> > + if (!IS_ENABLED(CONFIG_PREEMPT_RT)) > > >> > + rcu_softirq_qs(); > > >> > + > > >> > local_bh_enable(); > > > > Why does that local_bh_enable() not accomplish the same thing as the qs? > > In this case, because it does not create the appearance of a voluntary > context switch needed by RCU Tasks. So the wait for trampoline evacuation > could still take a very long time. > > Thanx, Paul > > > -Toke > > > > [0] https://lore.kernel.org/bpf/20210624160609.292325-6-toke@redhat.com/ > >
On Wed, Feb 28, 2024 at 9:35 AM Jakub Kicinski <kuba@kernel.org> wrote: > > On Wed, 28 Feb 2024 07:15:42 -0800 Paul E. McKenney wrote: > > > > Another complication is that although CONFIG_PREEMPT_RT kernels are > > > > built with CONFIG_PREEMPT_RCU, the reverse is not always the case. > > > > And if we are not repolling, don't we have a high probability of doing > > > > a voluntary context when we reach napi_thread_wait() at the beginning > > > > of that loop? > > > > > > Very much so, which is why adding the cost of rcu_softirq_qs() > > > for every NAPI run feels like an overkill. > > > > Would it be better to do the rcu_softirq_qs() only once every 1000 times > > or some such? Or once every HZ jiffies? > > > > Or is there a better way? > > Right, we can do that. Yan Zhai, have you measured the performance > impact / time spent in the call? For the case it hits the problem, the __napi_poll itself is usually consuming much of the cycles, so I didn't notice any difference in terms of tput. And it is in fact repolling all the time as the customer traffic might not implement proper backoff. So using a loop counter or jiffies to cap the number of invocations sounds like a decent improvement. Let me briefly check the overhead in the normal case, too Yan
On Wed, Feb 28, 2024 at 9:37 AM Joel Fernandes <joel@joelfernandes.org> wrote: > Also optionally, I wonder if calling rcu_tasks_qs() directly is better > (for documentation if anything) since the issue is Tasks RCU specific. Also > code comment above the rcu_softirq_qs() call about cond_resched() not taking > care of Tasks RCU would be great! > Yes it's quite surprising to me that cond_resched does not help here, I will include that comment. Raising just the task RCU QS seems sufficient to the problem we encountered. But according to commit d28139c4e967 ("rcu: Apply RCU-bh QSes to RCU-sched and RCU-preempt when safe"), there might be additional threat factor in __do_softirq that also applies to threaded poll. Yan > Reviewed-by: Joel Fernandes (Google) <joel@joelfernandes.org> > > thanks, > > - Joel > [1] > @@ -381,8 +553,10 @@ asmlinkage __visible void __softirq_entry __do_softirq(void) > pending >>= softirq_bit; > } > > - if (__this_cpu_read(ksoftirqd) == current) > + if (!IS_ENABLED(CONFIG_PREEMPT_RT) && > + __this_cpu_read(ksoftirqd) == current) > rcu_softirq_qs(); > + > local_irq_disable();
On Wed, Feb 28, 2024 at 10:37:51AM -0600, Yan Zhai wrote: > On Wed, Feb 28, 2024 at 9:37 AM Joel Fernandes <joel@joelfernandes.org> wrote: > > Also optionally, I wonder if calling rcu_tasks_qs() directly is better > > (for documentation if anything) since the issue is Tasks RCU specific. Also > > code comment above the rcu_softirq_qs() call about cond_resched() not taking > > care of Tasks RCU would be great! > > > Yes it's quite surprising to me that cond_resched does not help here, In theory, it would be possible to make cond_resched() take care of Tasks RCU. In practice, the lazy-preemption work is looking to get rid of cond_resched(). But if for some reason cond_resched() needs to stay around, doing that work might make sense. > I will include that comment. Raising just the task RCU QS seems > sufficient to the problem we encountered. But according to commit > d28139c4e967 ("rcu: Apply RCU-bh QSes to RCU-sched and RCU-preempt > when safe"), there might be additional threat factor in __do_softirq > that also applies to threaded poll. We did look into more focused alternatives for Tasks RCU a few days ago, but they all had problems, for example, requiring that it be possible to get exact information on the instruction pointers for interrupts on any given CPU's stack. The key point of Tasks RCU is to work out when an old tracing trampoline may safely be freed, so a better way of doing that would remove the need for this sort of addition to NAPI polling. (Adding Steve and Mark for their thoughts.) Thanx, Paul > Yan > > > > Reviewed-by: Joel Fernandes (Google) <joel@joelfernandes.org> > > > > thanks, > > > > - Joel > > [1] > > @@ -381,8 +553,10 @@ asmlinkage __visible void __softirq_entry __do_softirq(void) > > pending >>= softirq_bit; > > } > > > > - if (__this_cpu_read(ksoftirqd) == current) > > + if (!IS_ENABLED(CONFIG_PREEMPT_RT) && > > + __this_cpu_read(ksoftirqd) == current) > > rcu_softirq_qs(); > > + > > local_irq_disable();
On Wed, Feb 28, 2024 at 09:48:42AM -0600, Yan Zhai wrote: > On Wed, Feb 28, 2024 at 9:10 AM Paul E. McKenney <paulmck@kernel.org> wrote: > > > > On Wed, Feb 28, 2024 at 12:50:53PM +0100, Toke Høiland-Jørgensen wrote: > > > "Paul E. McKenney" <paulmck@kernel.org> writes: > > > > > > > On Tue, Feb 27, 2024 at 05:44:17PM +0100, Eric Dumazet wrote: > > > >> On Tue, Feb 27, 2024 at 4:44 PM Yan Zhai <yan@cloudflare.com> wrote: > > > >> > > > > >> > We noticed task RCUs being blocked when threaded NAPIs are very busy in > > > >> > production: detaching any BPF tracing programs, i.e. removing a ftrace > > > >> > trampoline, will simply block for very long in rcu_tasks_wait_gp. This > > > >> > ranges from hundreds of seconds to even an hour, severely harming any > > > >> > observability tools that rely on BPF tracing programs. It can be > > > >> > easily reproduced locally with following setup: > > > >> > > > > >> > ip netns add test1 > > > >> > ip netns add test2 > > > >> > > > > >> > ip -n test1 link add veth1 type veth peer name veth2 netns test2 > > > >> > > > > >> > ip -n test1 link set veth1 up > > > >> > ip -n test1 link set lo up > > > >> > ip -n test2 link set veth2 up > > > >> > ip -n test2 link set lo up > > > >> > > > > >> > ip -n test1 addr add 192.168.1.2/31 dev veth1 > > > >> > ip -n test1 addr add 1.1.1.1/32 dev lo > > > >> > ip -n test2 addr add 192.168.1.3/31 dev veth2 > > > >> > ip -n test2 addr add 2.2.2.2/31 dev lo > > > >> > > > > >> > ip -n test1 route add default via 192.168.1.3 > > > >> > ip -n test2 route add default via 192.168.1.2 > > > >> > > > > >> > for i in `seq 10 210`; do > > > >> > for j in `seq 10 210`; do > > > >> > ip netns exec test2 iptables -I INPUT -s 3.3.$i.$j -p udp --dport 5201 > > > >> > done > > > >> > done > > > >> > > > > >> > ip netns exec test2 ethtool -K veth2 gro on > > > >> > ip netns exec test2 bash -c 'echo 1 > /sys/class/net/veth2/threaded' > > > >> > ip netns exec test1 ethtool -K veth1 tso off > > > >> > > > > >> > Then run an iperf3 client/server and a bpftrace script can trigger it: > > > >> > > > > >> > ip netns exec test2 iperf3 -s -B 2.2.2.2 >/dev/null& > > > >> > ip netns exec test1 iperf3 -c 2.2.2.2 -B 1.1.1.1 -u -l 1500 -b 3g -t 100 >/dev/null& > > > >> > bpftrace -e 'kfunc:__napi_poll{@=count();} interval:s:1{exit();}' > > > >> > > > > >> > Above reproduce for net-next kernel with following RCU and preempt > > > >> > configuraitons: > > > >> > > > > >> > # RCU Subsystem > > > >> > CONFIG_TREE_RCU=y > > > >> > CONFIG_PREEMPT_RCU=y > > > >> > # CONFIG_RCU_EXPERT is not set > > > >> > CONFIG_SRCU=y > > > >> > CONFIG_TREE_SRCU=y > > > >> > CONFIG_TASKS_RCU_GENERIC=y > > > >> > CONFIG_TASKS_RCU=y > > > >> > CONFIG_TASKS_RUDE_RCU=y > > > >> > CONFIG_TASKS_TRACE_RCU=y > > > >> > CONFIG_RCU_STALL_COMMON=y > > > >> > CONFIG_RCU_NEED_SEGCBLIST=y > > > >> > # end of RCU Subsystem > > > >> > # RCU Debugging > > > >> > # CONFIG_RCU_SCALE_TEST is not set > > > >> > # CONFIG_RCU_TORTURE_TEST is not set > > > >> > # CONFIG_RCU_REF_SCALE_TEST is not set > > > >> > CONFIG_RCU_CPU_STALL_TIMEOUT=21 > > > >> > CONFIG_RCU_EXP_CPU_STALL_TIMEOUT=0 > > > >> > # CONFIG_RCU_TRACE is not set > > > >> > # CONFIG_RCU_EQS_DEBUG is not set > > > >> > # end of RCU Debugging > > > >> > > > > >> > CONFIG_PREEMPT_BUILD=y > > > >> > # CONFIG_PREEMPT_NONE is not set > > > >> > CONFIG_PREEMPT_VOLUNTARY=y > > > >> > # CONFIG_PREEMPT is not set > > > >> > CONFIG_PREEMPT_COUNT=y > > > >> > CONFIG_PREEMPTION=y > > > >> > CONFIG_PREEMPT_DYNAMIC=y > > > >> > CONFIG_PREEMPT_RCU=y > > > >> > CONFIG_HAVE_PREEMPT_DYNAMIC=y > > > >> > CONFIG_HAVE_PREEMPT_DYNAMIC_CALL=y > > > >> > CONFIG_PREEMPT_NOTIFIERS=y > > > >> > # CONFIG_DEBUG_PREEMPT is not set > > > >> > # CONFIG_PREEMPT_TRACER is not set > > > >> > # CONFIG_PREEMPTIRQ_DELAY_TEST is not set > > > >> > > > > >> > An interesting observation is that, while tasks RCUs are blocked, > > > >> > related NAPI thread is still being scheduled (even across cores) > > > >> > regularly. Looking at the gp conditions, I am inclining to cond_resched > > > >> > after each __napi_poll being the problem: cond_resched enters the > > > >> > scheduler with PREEMPT bit, which does not account as a gp for tasks > > > >> > RCUs. Meanwhile, since the thread has been frequently resched, the > > > >> > normal scheduling point (no PREEMPT bit, accounted as a task RCU gp) > > > >> > seems to have very little chance to kick in. Given the nature of "busy > > > >> > polling" program, such NAPI thread won't have task->nvcsw or task->on_rq > > > >> > updated (other gp conditions), the result is that such NAPI thread is > > > >> > put on RCU holdouts list for indefinitely long time. > > > >> > > > > >> > This is simply fixed by mirroring the ksoftirqd behavior: after > > > >> > NAPI/softirq work, raise a RCU QS to help expedite the RCU period. No > > > >> > more blocking afterwards for the same setup. > > > >> > > > > >> > Fixes: 29863d41bb6e ("net: implement threaded-able napi poll loop support") > > > >> > Signed-off-by: Yan Zhai <yan@cloudflare.com> > > > >> > --- > > > >> > net/core/dev.c | 4 ++++ > > > >> > 1 file changed, 4 insertions(+) > > > >> > > > > >> > diff --git a/net/core/dev.c b/net/core/dev.c > > > >> > index 275fd5259a4a..6e41263ff5d3 100644 > > > >> > --- a/net/core/dev.c > > > >> > +++ b/net/core/dev.c > > > >> > @@ -6773,6 +6773,10 @@ static int napi_threaded_poll(void *data) > > > >> > net_rps_action_and_irq_enable(sd); > > > >> > } > > > >> > skb_defer_free_flush(sd); > > > > > > > > Please put a comment here stating that RCU readers cannot cross > > > > this point. > > > > > > > > I need to add lockdep to rcu_softirq_qs() to catch placing this in an > > > > RCU read-side critical section. And a header comment noting that from > > > > an RCU perspective, it acts as a momentary enabling of preemption. > > > > > > OK, so one question here: for XDP, we're basically treating > > > local_bh_disable/enable() as the RCU critical section, cf the discussion > > > we had a few years ago that led to this being documented[0]. So why is > > > it OK to have the rcu_softirq_qs() inside the bh disable/enable pair, > > > but not inside an rcu_read_lock() section? > > > > In general, it is not OK. And it is not OK in this case if this happens > > to be one of the local_bh_disable() regions that XDP is waiting on. > > Except that that region ends right after the rcu_softirq_qs(), so that > > should not be a problem. > > > > But you are quite right, that is an accident waiting to happen, so it > > would be better if the patch did something like this: > > > > local_bh_enable(); > > if (!IS_ENABLED(CONFIG_PREEMPT_RT)) { > > preempt_disable(); > > rcu_softirq_qs(); > > preempt_enable(); > > } > > > Yeah we need preempt for this call. When I first attempt it after > local_bh_enable, I got the bug call: > [ 1166.384279] BUG: using __this_cpu_read() in preemptible [00000000] > code: napi/veth2-66/8439 > [ 1166.385337] caller is rcu_softirq_qs+0x16/0x130 > [ 1166.385900] CPU: 3 PID: 8439 Comm: napi/veth2-66 Not tainted > 6.7.0-rc8-g3fbf61207c66-dirty #75 > [ 1166.386950] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), > BIOS 1.16.2-debian-1.16.2-1 04/01/2014 > [ 1166.388110] Call Trace: > [ 1166.388417] <TASK> > [ 1166.388684] dump_stack_lvl+0x36/0x50 > [ 1166.389147] check_preemption_disabled+0xd1/0xe0 > [ 1166.389725] rcu_softirq_qs+0x16/0x130 > [ 1166.390190] napi_threaded_poll+0x21e/0x260 > [ 1166.390702] ? __pfx_napi_threaded_poll+0x10/0x10 > [ 1166.391277] kthread+0xf7/0x130 > [ 1166.391643] ? __pfx_kthread+0x10/0x10 > [ 1166.392130] ret_from_fork+0x34/0x50 > [ 1166.392574] ? __pfx_kthread+0x10/0x10 > [ 1166.393048] ret_from_fork_asm+0x1b/0x30 > [ 1166.393530] </TASK> > > Since this patch is trying to mirror what __do_softirq has, should the > similar notes/changes apply to that side as well? Up to now, the rcu_softirq_qs() was a special function strictly for use by __do_softirq(), hence the lack of documentation. I will let the __do_softirq() maintainers decide what they would like to do there, if anything. Thanx, Paul > > Though maybe something like this would be better: > > > > local_bh_enable(); > > if (!IS_ENABLED(CONFIG_PREEMPT_RT)) > > rcu_softirq_qs_enable(local_bh_enable()); > > else > > local_bh_enable(); > > > > A bit ugly, but it does allow exact checking of the rules and also > > avoids extra overhead. > > > > I could imagine pulling the CONFIG_PREEMPT_RT check into the body of > > rcu_softirq_qs_enable(). > > > > But is there a better way? > > > > > Also, looking at the patch in question: > > > > > > >> > + if (!IS_ENABLED(CONFIG_PREEMPT_RT)) > > > >> > + rcu_softirq_qs(); > > > >> > + > > > >> > local_bh_enable(); > > > > > > Why does that local_bh_enable() not accomplish the same thing as the qs? > > > > In this case, because it does not create the appearance of a voluntary > > context switch needed by RCU Tasks. So the wait for trampoline evacuation > > could still take a very long time. > > > > Thanx, Paul > > > > > -Toke > > > > > > [0] https://lore.kernel.org/bpf/20210624160609.292325-6-toke@redhat.com/ > > >
On Wed, Feb 28, 2024 at 12:18 PM Paul E. McKenney <paulmck@kernel.org> wrote: > > On Wed, Feb 28, 2024 at 10:37:51AM -0600, Yan Zhai wrote: > > On Wed, Feb 28, 2024 at 9:37 AM Joel Fernandes <joel@joelfernandes.org> wrote: > > > Also optionally, I wonder if calling rcu_tasks_qs() directly is better > > > (for documentation if anything) since the issue is Tasks RCU specific Also > > > code comment above the rcu_softirq_qs() call about cond_resched() not taking > > > care of Tasks RCU would be great! > > > > > Yes it's quite surprising to me that cond_resched does not help here, > > In theory, it would be possible to make cond_resched() take care of > Tasks RCU. In practice, the lazy-preemption work is looking to get rid > of cond_resched(). But if for some reason cond_resched() needs to stay > around, doing that work might make sense. In my opinion, cond_resched() doing Tasks-RCU QS does not make sense (to me), because cond_resched() is to inform the scheduler to run something else possibly of higher priority while the current task is still runnable. On the other hand, what's not permitted in a Tasks RCU reader is a voluntary sleep. So IMO even though cond_resched() is a voluntary call, it is still not a sleep but rather a preemption point. So a Tasks RCU reader should perfectly be able to be scheduled out in the middle of a read-side critical section (in current code) by calling cond_resched(). It is just like involuntary preemption in the middle of a RCU reader, in disguise, Right? thanks, - Joel
On Wed, Feb 28, 2024 at 03:14:34PM -0500, Joel Fernandes wrote: > On Wed, Feb 28, 2024 at 12:18 PM Paul E. McKenney <paulmck@kernel.org> wrote: > > > > On Wed, Feb 28, 2024 at 10:37:51AM -0600, Yan Zhai wrote: > > > On Wed, Feb 28, 2024 at 9:37 AM Joel Fernandes <joel@joelfernandes.org> wrote: > > > > Also optionally, I wonder if calling rcu_tasks_qs() directly is better > > > > (for documentation if anything) since the issue is Tasks RCU specific. Also > > > > code comment above the rcu_softirq_qs() call about cond_resched() not taking > > > > care of Tasks RCU would be great! > > > > > > > Yes it's quite surprising to me that cond_resched does not help here, > > > > In theory, it would be possible to make cond_resched() take care of > > Tasks RCU. In practice, the lazy-preemption work is looking to get rid > > of cond_resched(). But if for some reason cond_resched() needs to stay > > around, doing that work might make sense. > > In my opinion, cond_resched() doing Tasks-RCU QS does not make sense > (to me), because cond_resched() is to inform the scheduler to run > something else possibly of higher priority while the current task is > still runnable. On the other hand, what's not permitted in a Tasks RCU > reader is a voluntary sleep. So IMO even though cond_resched() is a > voluntary call, it is still not a sleep but rather a preemption point. From the viewpoint of Task RCU's users, the point is to figure out when it is OK to free an already-removed tracing trampoline. The current Task RCU implementation relies on the fact that tracing trampolines do not do voluntary context switches. > So a Tasks RCU reader should perfectly be able to be scheduled out in > the middle of a read-side critical section (in current code) by > calling cond_resched(). It is just like involuntary preemption in the > middle of a RCU reader, in disguise, Right? You lost me on this one. This for example is not permitted: rcu_read_lock(); cond_resched(); rcu_read_unlock(); But in a CONFIG_PREEMPT=y kernel, that RCU reader could be preempted. So cond_resched() looks like a voluntary context switch to me. Recall that vanilla non-preemptible RCU will treat them as quiescent states if the grace period extends long enough. What am I missing here? Thanx, Paul
> On Feb 28, 2024, at 4:13 PM, Paul E. McKenney <paulmck@kernel.org> wrote: > > On Wed, Feb 28, 2024 at 03:14:34PM -0500, Joel Fernandes wrote: >>> On Wed, Feb 28, 2024 at 12:18 PM Paul E. McKenney <paulmck@kernel.org> wrote: >>> >>> On Wed, Feb 28, 2024 at 10:37:51AM -0600, Yan Zhai wrote: >>>> On Wed, Feb 28, 2024 at 9:37 AM Joel Fernandes <joel@joelfernandes.org> wrote: >>>>> Also optionally, I wonder if calling rcu_tasks_qs() directly is better >>>>> (for documentation if anything) since the issue is Tasks RCU specific. Also >>>>> code comment above the rcu_softirq_qs() call about cond_resched() not taking >>>>> care of Tasks RCU would be great! >>>>> >>>> Yes it's quite surprising to me that cond_resched does not help here, >>> >>> In theory, it would be possible to make cond_resched() take care of >>> Tasks RCU. In practice, the lazy-preemption work is looking to get rid >>> of cond_resched(). But if for some reason cond_resched() needs to stay >>> around, doing that work might make sense. >> >> In my opinion, cond_resched() doing Tasks-RCU QS does not make sense >> (to me), because cond_resched() is to inform the scheduler to run >> something else possibly of higher priority while the current task is >> still runnable. On the other hand, what's not permitted in a Tasks RCU >> reader is a voluntary sleep. So IMO even though cond_resched() is a >> voluntary call, it is still not a sleep but rather a preemption point. > > From the viewpoint of Task RCU's users, the point is to figure out > when it is OK to free an already-removed tracing trampoline. The > current Task RCU implementation relies on the fact that tracing > trampolines do not do voluntary context switches. Yes. > >> So a Tasks RCU reader should perfectly be able to be scheduled out in >> the middle of a read-side critical section (in current code) by >> calling cond_resched(). It is just like involuntary preemption in the >> middle of a RCU reader, in disguise, Right? > > You lost me on this one. This for example is not permitted: > > rcu_read_lock(); > cond_resched(); > rcu_read_unlock(); > > But in a CONFIG_PREEMPT=y kernel, that RCU reader could be preempted. > > So cond_resched() looks like a voluntary context switch to me. Recall > that vanilla non-preemptible RCU will treat them as quiescent states if > the grace period extends long enough. > > What am I missing here? That we are discussing Tasks-RCU read side section? Sorry I should have been more clear. I thought sleeping was not permitted in Tasks RCU reader, but non-sleep context switches (example involuntarily getting preempted were). - Joel > > Thanx, Paul
On Wed, Feb 28, 2024 at 04:27:47PM -0500, Joel Fernandes wrote: > > > > On Feb 28, 2024, at 4:13 PM, Paul E. McKenney <paulmck@kernel.org> wrote: > > > > On Wed, Feb 28, 2024 at 03:14:34PM -0500, Joel Fernandes wrote: > >>> On Wed, Feb 28, 2024 at 12:18 PM Paul E. McKenney <paulmck@kernel.org> wrote: > >>> > >>> On Wed, Feb 28, 2024 at 10:37:51AM -0600, Yan Zhai wrote: > >>>> On Wed, Feb 28, 2024 at 9:37 AM Joel Fernandes <joel@joelfernandes.org> wrote: > >>>>> Also optionally, I wonder if calling rcu_tasks_qs() directly is better > >>>>> (for documentation if anything) since the issue is Tasks RCU specific. Also > >>>>> code comment above the rcu_softirq_qs() call about cond_resched() not taking > >>>>> care of Tasks RCU would be great! > >>>>> > >>>> Yes it's quite surprising to me that cond_resched does not help here, > >>> > >>> In theory, it would be possible to make cond_resched() take care of > >>> Tasks RCU. In practice, the lazy-preemption work is looking to get rid > >>> of cond_resched(). But if for some reason cond_resched() needs to stay > >>> around, doing that work might make sense. > >> > >> In my opinion, cond_resched() doing Tasks-RCU QS does not make sense > >> (to me), because cond_resched() is to inform the scheduler to run > >> something else possibly of higher priority while the current task is > >> still runnable. On the other hand, what's not permitted in a Tasks RCU > >> reader is a voluntary sleep. So IMO even though cond_resched() is a > >> voluntary call, it is still not a sleep but rather a preemption point. > > > > From the viewpoint of Task RCU's users, the point is to figure out > > when it is OK to free an already-removed tracing trampoline. The > > current Task RCU implementation relies on the fact that tracing > > trampolines do not do voluntary context switches. > > Yes. > > > > >> So a Tasks RCU reader should perfectly be able to be scheduled out in > >> the middle of a read-side critical section (in current code) by > >> calling cond_resched(). It is just like involuntary preemption in the > >> middle of a RCU reader, in disguise, Right? > > > > You lost me on this one. This for example is not permitted: > > > > rcu_read_lock(); > > cond_resched(); > > rcu_read_unlock(); > > > > But in a CONFIG_PREEMPT=y kernel, that RCU reader could be preempted. > > > > So cond_resched() looks like a voluntary context switch to me. Recall > > that vanilla non-preemptible RCU will treat them as quiescent states if > > the grace period extends long enough. > > > > What am I missing here? > > That we are discussing Tasks-RCU read side section? Sorry I should have been more clear. I thought sleeping was not permitted in Tasks RCU reader, but non-sleep context switches (example involuntarily getting preempted were). Well, to your initial point, cond_resched() does eventually invoke preempt_schedule_common(), so you are quite correct that as far as Tasks RCU is concerned, cond_resched() is not a quiescent state. Thanx, Paul
> On Feb 28, 2024, at 4:52 PM, Paul E. McKenney <paulmck@kernel.org> wrote: > > On Wed, Feb 28, 2024 at 04:27:47PM -0500, Joel Fernandes wrote: >> >> >>>> On Feb 28, 2024, at 4:13 PM, Paul E. McKenney <paulmck@kernel.org> wrote: >>> >>> On Wed, Feb 28, 2024 at 03:14:34PM -0500, Joel Fernandes wrote: >>>>> On Wed, Feb 28, 2024 at 12:18 PM Paul E. McKenney <paulmck@kernel.org> wrote: >>>>> >>>>> On Wed, Feb 28, 2024 at 10:37:51AM -0600, Yan Zhai wrote: >>>>>> On Wed, Feb 28, 2024 at 9:37 AM Joel Fernandes <joel@joelfernandes.org> wrote: >>>>>>> Also optionally, I wonder if calling rcu_tasks_qs() directly is better >>>>>>> (for documentation if anything) since the issue is Tasks RCU specific. Also >>>>>>> code comment above the rcu_softirq_qs() call about cond_resched() not taking >>>>>>> care of Tasks RCU would be great! >>>>>>> >>>>>> Yes it's quite surprising to me that cond_resched does not help here, >>>>> >>>>> In theory, it would be possible to make cond_resched() take care of >>>>> Tasks RCU. In practice, the lazy-preemption work is looking to get rid >>>>> of cond_resched(). But if for some reason cond_resched() needs to stay >>>>> around, doing that work might make sense. >>>> >>>> In my opinion, cond_resched() doing Tasks-RCU QS does not make sense >>>> (to me), because cond_resched() is to inform the scheduler to run >>>> something else possibly of higher priority while the current task is >>>> still runnable. On the other hand, what's not permitted in a Tasks RCU >>>> reader is a voluntary sleep. So IMO even though cond_resched() is a >>>> voluntary call, it is still not a sleep but rather a preemption point. >>> >>> From the viewpoint of Task RCU's users, the point is to figure out >>> when it is OK to free an already-removed tracing trampoline. The >>> current Task RCU implementation relies on the fact that tracing >>> trampolines do not do voluntary context switches. >> >> Yes. >> >>> >>>> So a Tasks RCU reader should perfectly be able to be scheduled out in >>>> the middle of a read-side critical section (in current code) by >>>> calling cond_resched(). It is just like involuntary preemption in the >>>> middle of a RCU reader, in disguise, Right? >>> >>> You lost me on this one. This for example is not permitted: >>> >>> rcu_read_lock(); >>> cond_resched(); >>> rcu_read_unlock(); >>> >>> But in a CONFIG_PREEMPT=y kernel, that RCU reader could be preempted. >>> >>> So cond_resched() looks like a voluntary context switch to me. Recall >>> that vanilla non-preemptible RCU will treat them as quiescent states if >>> the grace period extends long enough. >>> >>> What am I missing here? >> >> That we are discussing Tasks-RCU read side section? Sorry I should have been more clear. I thought sleeping was not permitted in Tasks RCU reader, but non-sleep context switches (example involuntarily getting preempted were). > > Well, to your initial point, cond_resched() does eventually invoke > preempt_schedule_common(), so you are quite correct that as far as > Tasks RCU is concerned, cond_resched() is not a quiescent state. Thanks for confirming. :-) - Joel > > Thanx, Paul
On Wed, Feb 28, 2024 at 05:10:43PM -0500, Joel Fernandes wrote: > > > > On Feb 28, 2024, at 4:52 PM, Paul E. McKenney <paulmck@kernel.org> wrote: > > > > On Wed, Feb 28, 2024 at 04:27:47PM -0500, Joel Fernandes wrote: > >> > >> > >>>> On Feb 28, 2024, at 4:13 PM, Paul E. McKenney <paulmck@kernel.org> wrote: > >>> > >>> On Wed, Feb 28, 2024 at 03:14:34PM -0500, Joel Fernandes wrote: > >>>>> On Wed, Feb 28, 2024 at 12:18 PM Paul E. McKenney <paulmck@kernel.org> wrote: > >>>>> > >>>>> On Wed, Feb 28, 2024 at 10:37:51AM -0600, Yan Zhai wrote: > >>>>>> On Wed, Feb 28, 2024 at 9:37 AM Joel Fernandes <joel@joelfernandes.org> wrote: > >>>>>>> Also optionally, I wonder if calling rcu_tasks_qs() directly is better > >>>>>>> (for documentation if anything) since the issue is Tasks RCU specific. Also > >>>>>>> code comment above the rcu_softirq_qs() call about cond_resched() not taking > >>>>>>> care of Tasks RCU would be great! > >>>>>>> > >>>>>> Yes it's quite surprising to me that cond_resched does not help here, > >>>>> > >>>>> In theory, it would be possible to make cond_resched() take care of > >>>>> Tasks RCU. In practice, the lazy-preemption work is looking to get rid > >>>>> of cond_resched(). But if for some reason cond_resched() needs to stay > >>>>> around, doing that work might make sense. > >>>> > >>>> In my opinion, cond_resched() doing Tasks-RCU QS does not make sense > >>>> (to me), because cond_resched() is to inform the scheduler to run > >>>> something else possibly of higher priority while the current task is > >>>> still runnable. On the other hand, what's not permitted in a Tasks RCU > >>>> reader is a voluntary sleep. So IMO even though cond_resched() is a > >>>> voluntary call, it is still not a sleep but rather a preemption point. > >>> > >>> From the viewpoint of Task RCU's users, the point is to figure out > >>> when it is OK to free an already-removed tracing trampoline. The > >>> current Task RCU implementation relies on the fact that tracing > >>> trampolines do not do voluntary context switches. > >> > >> Yes. > >> > >>> > >>>> So a Tasks RCU reader should perfectly be able to be scheduled out in > >>>> the middle of a read-side critical section (in current code) by > >>>> calling cond_resched(). It is just like involuntary preemption in the > >>>> middle of a RCU reader, in disguise, Right? > >>> > >>> You lost me on this one. This for example is not permitted: > >>> > >>> rcu_read_lock(); > >>> cond_resched(); > >>> rcu_read_unlock(); > >>> > >>> But in a CONFIG_PREEMPT=y kernel, that RCU reader could be preempted. > >>> > >>> So cond_resched() looks like a voluntary context switch to me. Recall > >>> that vanilla non-preemptible RCU will treat them as quiescent states if > >>> the grace period extends long enough. > >>> > >>> What am I missing here? > >> > >> That we are discussing Tasks-RCU read side section? Sorry I should have been more clear. I thought sleeping was not permitted in Tasks RCU reader, but non-sleep context switches (example involuntarily getting preempted were). > > > > Well, to your initial point, cond_resched() does eventually invoke > > preempt_schedule_common(), so you are quite correct that as far as > > Tasks RCU is concerned, cond_resched() is not a quiescent state. > > Thanks for confirming. :-) However, given that the current Tasks RCU use cases wait for trampolines to be evacuated, Tasks RCU could make the choice that cond_resched() be a quiescent state, for example, by adjusting rcu_all_qs() and rcu_urgent_qs accordingly. But this seems less pressing given the chance that cond_resched() might go away in favor of lazy preemption. Thanx, Paul
On Wed, 28 Feb 2024 14:19:11 -0800 "Paul E. McKenney" <paulmck@kernel.org> wrote: > > > > > > Well, to your initial point, cond_resched() does eventually invoke > > > preempt_schedule_common(), so you are quite correct that as far as > > > Tasks RCU is concerned, cond_resched() is not a quiescent state. > > > > Thanks for confirming. :-) > > However, given that the current Tasks RCU use cases wait for trampolines > to be evacuated, Tasks RCU could make the choice that cond_resched() > be a quiescent state, for example, by adjusting rcu_all_qs() and > .rcu_urgent_qs accordingly. > > But this seems less pressing given the chance that cond_resched() might > go away in favor of lazy preemption. Although cond_resched() is technically a "preemption point" and not truly a voluntary schedule, I would be happy to state that it's not allowed to be called from trampolines, or their callbacks. Now the question is, does BPF programs ever call cond_resched()? I don't think they do. [ Added Alexei ] -- Steve
On Wed, Feb 28, 2024 at 2:31 PM Steven Rostedt <rostedt@goodmis.org> wrote: > > On Wed, 28 Feb 2024 14:19:11 -0800 > "Paul E. McKenney" <paulmck@kernel.org> wrote: > > > > > > > > > Well, to your initial point, cond_resched() does eventually invoke > > > > preempt_schedule_common(), so you are quite correct that as far as > > > > Tasks RCU is concerned, cond_resched() is not a quiescent state. > > > > > > Thanks for confirming. :-) > > > > However, given that the current Tasks RCU use cases wait for trampolines > > to be evacuated, Tasks RCU could make the choice that cond_resched() > > be a quiescent state, for example, by adjusting rcu_all_qs() and > > .rcu_urgent_qs accordingly. > > > > But this seems less pressing given the chance that cond_resched() might > > go away in favor of lazy preemption. > > Although cond_resched() is technically a "preemption point" and not truly a > voluntary schedule, I would be happy to state that it's not allowed to be > called from trampolines, or their callbacks. Now the question is, does BPF > programs ever call cond_resched()? I don't think they do. > > [ Added Alexei ] I'm a bit lost in this thread :) Just answering the above question. bpf progs never call cond_resched() directly. But there are sleepable (aka faultable) bpf progs that can call some helper or kfunc that may call cond_resched() in some path. sleepable bpf progs are protected by rcu_tasks_trace. That's a very different one vs rcu_tasks.
On Wed, Feb 28, 2024 at 05:33:07PM -0500, Steven Rostedt wrote: > On Wed, 28 Feb 2024 14:19:11 -0800 > "Paul E. McKenney" <paulmck@kernel.org> wrote: > > > > > > > > > Well, to your initial point, cond_resched() does eventually invoke > > > > preempt_schedule_common(), so you are quite correct that as far as > > > > Tasks RCU is concerned, cond_resched() is not a quiescent state. > > > > > > Thanks for confirming. :-) > > > > However, given that the current Tasks RCU use cases wait for trampolines > > to be evacuated, Tasks RCU could make the choice that cond_resched() > > be a quiescent state, for example, by adjusting rcu_all_qs() and > > .rcu_urgent_qs accordingly. > > > > But this seems less pressing given the chance that cond_resched() might > > go away in favor of lazy preemption. > > Although cond_resched() is technically a "preemption point" and not truly a > voluntary schedule, I would be happy to state that it's not allowed to be > called from trampolines, or their callbacks. Now the question is, does BPF > programs ever call cond_resched()? I don't think they do. Nor do I, but I too must defer to Alexei. ;-) > [ Added Alexei ] The other issue with making cond_resched() be a Tasks RCU quiescent state is that the CONFIG_PREEMPTION=y version of cond_resched() would need to stop being a complete no-op. Which actually might be OK. Thanx, Paul
On Wed, Feb 28, 2024 at 02:48:44PM -0800, Alexei Starovoitov wrote: > On Wed, Feb 28, 2024 at 2:31 PM Steven Rostedt <rostedt@goodmis.org> wrote: > > > > On Wed, 28 Feb 2024 14:19:11 -0800 > > "Paul E. McKenney" <paulmck@kernel.org> wrote: > > > > > > > > > > > > Well, to your initial point, cond_resched() does eventually invoke > > > > > preempt_schedule_common(), so you are quite correct that as far as > > > > > Tasks RCU is concerned, cond_resched() is not a quiescent state. > > > > > > > > Thanks for confirming. :-) > > > > > > However, given that the current Tasks RCU use cases wait for trampolines > > > to be evacuated, Tasks RCU could make the choice that cond_resched() > > > be a quiescent state, for example, by adjusting rcu_all_qs() and > > > .rcu_urgent_qs accordingly. > > > > > > But this seems less pressing given the chance that cond_resched() might > > > go away in favor of lazy preemption. > > > > Although cond_resched() is technically a "preemption point" and not truly a > > voluntary schedule, I would be happy to state that it's not allowed to be > > called from trampolines, or their callbacks. Now the question is, does BPF > > programs ever call cond_resched()? I don't think they do. > > > > [ Added Alexei ] > > I'm a bit lost in this thread :) > Just answering the above question. > bpf progs never call cond_resched() directly. > But there are sleepable (aka faultable) bpf progs that > can call some helper or kfunc that may call cond_resched() > in some path. > sleepable bpf progs are protected by rcu_tasks_trace. > That's a very different one vs rcu_tasks. Suppose that the various cond_resched() invocations scattered throughout the kernel acted as RCU Tasks quiescent states, so that as soon as a given task executed a cond_resched(), synchronize_rcu_tasks() might return or call_rcu_tasks() might invoke its callback. Would that cause BPF any trouble? My guess is "no", because it looks like BPF is using RCU Tasks (as you say, as opposed to RCU Tasks Trace) only to wait for execution to leave a trampoline. But I trust you much more than I trust myself on this topic! Thanx, Paul
Hi Eric, On Tue, Feb 27, 2024 at 10:44 AM Eric Dumazet <edumazet@google.com> wrote: > > Hmm.... > Why napi_busy_loop() does not have a similar problem ? > I just tried and can reproduce similar behavior on sk busy poll. However, the interesting thing is, this can happen if I set a super high polling interval but just send rare packets. In my case I had a 5 sec polling interval (unlikely to be realistic in prod but just for demonstration), then used nc to send a few packets. Here is what bpftrace react: Normal: time sudo bpftrace -e 'kfunc:napi_busy_loop{@=count();} interval:s:1{exit();} kfunc:udp_recvmsg {printf("%ld\n", args->sk->sk_ll_usec);}' Attaching 3 probes... @: 0 real 0m1.527s user 0m0.073s sys 0m0.128s Extra wait when polling: time sudo bpftrace -e 'kfunc:napi_busy_loop{@=count();} interval:s:1{exit();} kfunc:udp_recvmsg {printf("%ld\n", args->sk->sk_ll_usec);}' Attaching 3 probes... 5000000 @: 16 real 0m11.167s user 0m0.070s sys 0m0.120s So the symptoms are the same, bpftrace cannot exit despite having an 1sec timeout. But the execution pattern for these two are probably different: NAPI threads would keep polling by itself, whereas sk poll program might only poll when there is no immediate data. When there are packets, it switches to process packets instead of polling any more. Yan
On 2/28/2024 5:58 PM, Paul E. McKenney wrote: > On Wed, Feb 28, 2024 at 02:48:44PM -0800, Alexei Starovoitov wrote: >> On Wed, Feb 28, 2024 at 2:31 PM Steven Rostedt <rostedt@goodmis.org> wrote: >>> >>> On Wed, 28 Feb 2024 14:19:11 -0800 >>> "Paul E. McKenney" <paulmck@kernel.org> wrote: >>> >>>>>> >>>>>> Well, to your initial point, cond_resched() does eventually invoke >>>>>> preempt_schedule_common(), so you are quite correct that as far as >>>>>> Tasks RCU is concerned, cond_resched() is not a quiescent state. >>>>> >>>>> Thanks for confirming. :-) >>>> >>>> However, given that the current Tasks RCU use cases wait for trampolines >>>> to be evacuated, Tasks RCU could make the choice that cond_resched() >>>> be a quiescent state, for example, by adjusting rcu_all_qs() and >>>> .rcu_urgent_qs accordingly. >>>> >>>> But this seems less pressing given the chance that cond_resched() might >>>> go away in favor of lazy preemption. >>> >>> Although cond_resched() is technically a "preemption point" and not truly a >>> voluntary schedule, I would be happy to state that it's not allowed to be >>> called from trampolines, or their callbacks. Now the question is, does BPF >>> programs ever call cond_resched()? I don't think they do. >>> >>> [ Added Alexei ] >> >> I'm a bit lost in this thread :) >> Just answering the above question. >> bpf progs never call cond_resched() directly. >> But there are sleepable (aka faultable) bpf progs that >> can call some helper or kfunc that may call cond_resched() >> in some path. >> sleepable bpf progs are protected by rcu_tasks_trace. >> That's a very different one vs rcu_tasks. > > Suppose that the various cond_resched() invocations scattered throughout > the kernel acted as RCU Tasks quiescent states, so that as soon as a > given task executed a cond_resched(), synchronize_rcu_tasks() might > return or call_rcu_tasks() might invoke its callback. > > Would that cause BPF any trouble? > > My guess is "no", because it looks like BPF is using RCU Tasks (as you > say, as opposed to RCU Tasks Trace) only to wait for execution to leave a > trampoline. But I trust you much more than I trust myself on this topic! But it uses RCU Tasks Trace as well (for sleepable bpf programs), not just Tasks? Looks like that's what Alexei said above as well, and I confirmed it in bpf/trampoline.c /* The trampoline without fexit and fmod_ret progs doesn't call original * function and doesn't use percpu_ref. * Use call_rcu_tasks_trace() to wait for sleepable progs to finish. * Then use call_rcu_tasks() to wait for the rest of trampoline asm * and normal progs. */ call_rcu_tasks_trace(&im->rcu, __bpf_tramp_image_put_rcu_tasks); The code comment says it uses both. Thanks, - Joel
On Thu, Feb 29, 2024 at 09:21:48AM -0500, Joel Fernandes wrote: > > > On 2/28/2024 5:58 PM, Paul E. McKenney wrote: > > On Wed, Feb 28, 2024 at 02:48:44PM -0800, Alexei Starovoitov wrote: > >> On Wed, Feb 28, 2024 at 2:31 PM Steven Rostedt <rostedt@goodmis.org> wrote: > >>> > >>> On Wed, 28 Feb 2024 14:19:11 -0800 > >>> "Paul E. McKenney" <paulmck@kernel.org> wrote: > >>> > >>>>>> > >>>>>> Well, to your initial point, cond_resched() does eventually invoke > >>>>>> preempt_schedule_common(), so you are quite correct that as far as > >>>>>> Tasks RCU is concerned, cond_resched() is not a quiescent state. > >>>>> > >>>>> Thanks for confirming. :-) > >>>> > >>>> However, given that the current Tasks RCU use cases wait for trampolines > >>>> to be evacuated, Tasks RCU could make the choice that cond_resched() > >>>> be a quiescent state, for example, by adjusting rcu_all_qs() and > >>>> .rcu_urgent_qs accordingly. > >>>> > >>>> But this seems less pressing given the chance that cond_resched() might > >>>> go away in favor of lazy preemption. > >>> > >>> Although cond_resched() is technically a "preemption point" and not truly a > >>> voluntary schedule, I would be happy to state that it's not allowed to be > >>> called from trampolines, or their callbacks. Now the question is, does BPF > >>> programs ever call cond_resched()? I don't think they do. > >>> > >>> [ Added Alexei ] > >> > >> I'm a bit lost in this thread :) > >> Just answering the above question. > >> bpf progs never call cond_resched() directly. > >> But there are sleepable (aka faultable) bpf progs that > >> can call some helper or kfunc that may call cond_resched() > >> in some path. > >> sleepable bpf progs are protected by rcu_tasks_trace. > >> That's a very different one vs rcu_tasks. > > > > Suppose that the various cond_resched() invocations scattered throughout > > the kernel acted as RCU Tasks quiescent states, so that as soon as a > > given task executed a cond_resched(), synchronize_rcu_tasks() might > > return or call_rcu_tasks() might invoke its callback. > > > > Would that cause BPF any trouble? > > > > My guess is "no", because it looks like BPF is using RCU Tasks (as you > > say, as opposed to RCU Tasks Trace) only to wait for execution to leave a > > trampoline. But I trust you much more than I trust myself on this topic! > > But it uses RCU Tasks Trace as well (for sleepable bpf programs), not just > Tasks? Looks like that's what Alexei said above as well, and I confirmed it in > bpf/trampoline.c > > /* The trampoline without fexit and fmod_ret progs doesn't call original > * function and doesn't use percpu_ref. > * Use call_rcu_tasks_trace() to wait for sleepable progs to finish. > * Then use call_rcu_tasks() to wait for the rest of trampoline asm > * and normal progs. > */ > call_rcu_tasks_trace(&im->rcu, __bpf_tramp_image_put_rcu_tasks); > > The code comment says it uses both. BPF does quite a few interesting things with these. But would you like to look at the update-side uses of RCU Tasks Rude to see if lazy preemption affects them? I don't believe that there are any problems here, but we do need to check. Thanx, Paul
> On Feb 29, 2024, at 11:57 AM, Paul E. McKenney <paulmck@kernel.org> wrote: > > On Thu, Feb 29, 2024 at 09:21:48AM -0500, Joel Fernandes wrote: >> >> >>> On 2/28/2024 5:58 PM, Paul E. McKenney wrote: >>> On Wed, Feb 28, 2024 at 02:48:44PM -0800, Alexei Starovoitov wrote: >>>> On Wed, Feb 28, 2024 at 2:31 PM Steven Rostedt <rostedt@goodmisorg> wrote: >>>>> >>>>> On Wed, 28 Feb 2024 14:19:11 -0800 >>>>> "Paul E. McKenney" <paulmck@kernel.org> wrote: >>>>> >>>>>>>> >>>>>>>> Well, to your initial point, cond_resched() does eventually invoke >>>>>>>> preempt_schedule_common(), so you are quite correct that as far as >>>>>>>> Tasks RCU is concerned, cond_resched() is not a quiescent state. >>>>>>> >>>>>>> Thanks for confirming. :-) >>>>>> >>>>>> However, given that the current Tasks RCU use cases wait for trampolines >>>>>> to be evacuated, Tasks RCU could make the choice that cond_resched() >>>>>> be a quiescent state, for example, by adjusting rcu_all_qs() and >>>>>> .rcu_urgent_qs accordingly. >>>>>> >>>>>> But this seems less pressing given the chance that cond_resched() might >>>>>> go away in favor of lazy preemption. >>>>> >>>>> Although cond_resched() is technically a "preemption point" and not truly a >>>>> voluntary schedule, I would be happy to state that it's not allowed to be >>>>> called from trampolines, or their callbacks. Now the question is, does BPF >>>>> programs ever call cond_resched()? I don't think they do. >>>>> >>>>> [ Added Alexei ] >>>> >>>> I'm a bit lost in this thread :) >>>> Just answering the above question. >>>> bpf progs never call cond_resched() directly. >>>> But there are sleepable (aka faultable) bpf progs that >>>> can call some helper or kfunc that may call cond_resched() >>>> in some path. >>>> sleepable bpf progs are protected by rcu_tasks_trace. >>>> That's a very different one vs rcu_tasks. >>> >>> Suppose that the various cond_resched() invocations scattered throughout >>> the kernel acted as RCU Tasks quiescent states, so that as soon as a >>> given task executed a cond_resched(), synchronize_rcu_tasks() might >>> return or call_rcu_tasks() might invoke its callback. >>> >>> Would that cause BPF any trouble? >>> >>> My guess is "no", because it looks like BPF is using RCU Tasks (as you >>> say, as opposed to RCU Tasks Trace) only to wait for execution to leave a >>> trampoline. But I trust you much more than I trust myself on this topic! >> >> But it uses RCU Tasks Trace as well (for sleepable bpf programs), not just >> Tasks? Looks like that's what Alexei said above as well, and I confirmed it in >> bpf/trampoline.c >> >> /* The trampoline without fexit and fmod_ret progs doesn't call original >> * function and doesn't use percpu_ref. >> * Use call_rcu_tasks_trace() to wait for sleepable progs to finish. >> * Then use call_rcu_tasks() to wait for the rest of trampoline asm >> * and normal progs. >> */ >> call_rcu_tasks_trace(&im->rcu, __bpf_tramp_image_put_rcu_tasks); >> >> The code comment says it uses both. > > BPF does quite a few interesting things with these. > > But would you like to look at the update-side uses of RCU Tasks Rude > to see if lazy preemption affects them? I don't believe that there > are any problems here, but we do need to check. Sure I will be happy to. I am planning look at it in detail over the 3 day weekend. Too much fun! ;-) thanks, - Joel > > Thanx, Paul
On Thu, Feb 29, 2024 at 12:41:55PM -0500, Joel Fernandes wrote: > > On Feb 29, 2024, at 11:57 AM, Paul E. McKenney <paulmck@kernel.org> wrote: > > On Thu, Feb 29, 2024 at 09:21:48AM -0500, Joel Fernandes wrote: > >>> On 2/28/2024 5:58 PM, Paul E. McKenney wrote: > >>> On Wed, Feb 28, 2024 at 02:48:44PM -0800, Alexei Starovoitov wrote: > >>>> On Wed, Feb 28, 2024 at 2:31 PM Steven Rostedt <rostedt@goodmis.org> wrote: > >>>>> > >>>>> On Wed, 28 Feb 2024 14:19:11 -0800 > >>>>> "Paul E. McKenney" <paulmck@kernel.org> wrote: > >>>>> > >>>>>>>> > >>>>>>>> Well, to your initial point, cond_resched() does eventually invoke > >>>>>>>> preempt_schedule_common(), so you are quite correct that as far as > >>>>>>>> Tasks RCU is concerned, cond_resched() is not a quiescent state. > >>>>>>> > >>>>>>> Thanks for confirming. :-) > >>>>>> > >>>>>> However, given that the current Tasks RCU use cases wait for trampolines > >>>>>> to be evacuated, Tasks RCU could make the choice that cond_resched() > >>>>>> be a quiescent state, for example, by adjusting rcu_all_qs() and > >>>>>> .rcu_urgent_qs accordingly. > >>>>>> > >>>>>> But this seems less pressing given the chance that cond_resched() might > >>>>>> go away in favor of lazy preemption. > >>>>> > >>>>> Although cond_resched() is technically a "preemption point" and not truly a > >>>>> voluntary schedule, I would be happy to state that it's not allowed to be > >>>>> called from trampolines, or their callbacks. Now the question is, does BPF > >>>>> programs ever call cond_resched()? I don't think they do. > >>>>> > >>>>> [ Added Alexei ] > >>>> > >>>> I'm a bit lost in this thread :) > >>>> Just answering the above question. > >>>> bpf progs never call cond_resched() directly. > >>>> But there are sleepable (aka faultable) bpf progs that > >>>> can call some helper or kfunc that may call cond_resched() > >>>> in some path. > >>>> sleepable bpf progs are protected by rcu_tasks_trace. > >>>> That's a very different one vs rcu_tasks. > >>> > >>> Suppose that the various cond_resched() invocations scattered throughout > >>> the kernel acted as RCU Tasks quiescent states, so that as soon as a > >>> given task executed a cond_resched(), synchronize_rcu_tasks() might > >>> return or call_rcu_tasks() might invoke its callback. > >>> > >>> Would that cause BPF any trouble? > >>> > >>> My guess is "no", because it looks like BPF is using RCU Tasks (as you > >>> say, as opposed to RCU Tasks Trace) only to wait for execution to leave a > >>> trampoline. But I trust you much more than I trust myself on this topic! > >> > >> But it uses RCU Tasks Trace as well (for sleepable bpf programs), not just > >> Tasks? Looks like that's what Alexei said above as well, and I confirmed it in > >> bpf/trampoline.c > >> > >> /* The trampoline without fexit and fmod_ret progs doesn't call original > >> * function and doesn't use percpu_ref. > >> * Use call_rcu_tasks_trace() to wait for sleepable progs to finish. > >> * Then use call_rcu_tasks() to wait for the rest of trampoline asm > >> * and normal progs. > >> */ > >> call_rcu_tasks_trace(&im->rcu, __bpf_tramp_image_put_rcu_tasks); > >> > >> The code comment says it uses both. > > > > BPF does quite a few interesting things with these. > > > > But would you like to look at the update-side uses of RCU Tasks Rude > > to see if lazy preemption affects them? I don't believe that there > > are any problems here, but we do need to check. > > Sure I will be happy to. I am planning look at it in detail over the 3 day weekend. Too much fun! ;-) Thank you, and looking forward to seeing what you come up with! The canonical concern would be that someone somewhere is using either call_rcu_tasks_rude() or synchronize_rcu_tasks_rude() to wait for non-preemptible regions of code that does not account for the possibility of preemption in CONFIG_PREEMPT_NONE or PREEMPT_PREEMPT_VOLUNTARY kernels. I *think* that these are used only to handle the possibility of tracepoints on functions on the entry/exit path and on the RCU-not-watching portions of the idle loop. If so, then there is no difference in behavior for lazy preemption. But who knows? Thanx, Paul
(Shrinking CC a bit) On Thu, Feb 29, 2024 at 1:29 PM Paul E. McKenney <paulmck@kernel.org> wrote: > > On Thu, Feb 29, 2024 at 12:41:55PM -0500, Joel Fernandes wrote: > > > On Feb 29, 2024, at 11:57 AM, Paul E. McKenney <paulmck@kernel.org> wrote: > > > On Thu, Feb 29, 2024 at 09:21:48AM -0500, Joel Fernandes wrote: > > >>> On 2/28/2024 5:58 PM, Paul E. McKenney wrote: > > >>> On Wed, Feb 28, 2024 at 02:48:44PM -0800, Alexei Starovoitov wrote: > > >>>> On Wed, Feb 28, 2024 at 2:31 PM Steven Rostedt <rostedt@goodmis.org> wrote: > > >>>>> > > >>>>> On Wed, 28 Feb 2024 14:19:11 -0800 > > >>>>> "Paul E. McKenney" <paulmck@kernel.org> wrote: > > >>>>> > > >>>>>>>> > > >>>>>>>> Well, to your initial point, cond_resched() does eventually invoke > > >>>>>>>> preempt_schedule_common(), so you are quite correct that as far as > > >>>>>>>> Tasks RCU is concerned, cond_resched() is not a quiescent state. > > >>>>>>> > > >>>>>>> Thanks for confirming. :-) > > >>>>>> > > >>>>>> However, given that the current Tasks RCU use cases wait for trampolines > > >>>>>> to be evacuated, Tasks RCU could make the choice that cond_resched() > > >>>>>> be a quiescent state, for example, by adjusting rcu_all_qs() and > > >>>>>> .rcu_urgent_qs accordingly. > > >>>>>> > > >>>>>> But this seems less pressing given the chance that cond_resched() might > > >>>>>> go away in favor of lazy preemption. > > >>>>> > > >>>>> Although cond_resched() is technically a "preemption point" and not truly a > > >>>>> voluntary schedule, I would be happy to state that it's not allowed to be > > >>>>> called from trampolines, or their callbacks. Now the question is, does BPF > > >>>>> programs ever call cond_resched()? I don't think they do. > > >>>>> > > >>>>> [ Added Alexei ] > > >>>> > > >>>> I'm a bit lost in this thread :) > > >>>> Just answering the above question. > > >>>> bpf progs never call cond_resched() directly. > > >>>> But there are sleepable (aka faultable) bpf progs that > > >>>> can call some helper or kfunc that may call cond_resched() > > >>>> in some path. > > >>>> sleepable bpf progs are protected by rcu_tasks_trace. > > >>>> That's a very different one vs rcu_tasks. > > >>> > > >>> Suppose that the various cond_resched() invocations scattered throughout > > >>> the kernel acted as RCU Tasks quiescent states, so that as soon as a > > >>> given task executed a cond_resched(), synchronize_rcu_tasks() might > > >>> return or call_rcu_tasks() might invoke its callback. > > >>> > > >>> Would that cause BPF any trouble? > > >>> > > >>> My guess is "no", because it looks like BPF is using RCU Tasks (as you > > >>> say, as opposed to RCU Tasks Trace) only to wait for execution to leave a > > >>> trampoline. But I trust you much more than I trust myself on this topic! > > >> > > >> But it uses RCU Tasks Trace as well (for sleepable bpf programs), not just > > >> Tasks? Looks like that's what Alexei said above as well, and I confirmed it in > > >> bpf/trampoline.c > > >> > > >> /* The trampoline without fexit and fmod_ret progs doesn't call original > > >> * function and doesn't use percpu_ref. > > >> * Use call_rcu_tasks_trace() to wait for sleepable progs to finish. > > >> * Then use call_rcu_tasks() to wait for the rest of trampoline asm > > >> * and normal progs. > > >> */ > > >> call_rcu_tasks_trace(&im->rcu, __bpf_tramp_image_put_rcu_tasks); > > >> > > >> The code comment says it uses both. > > > > > > BPF does quite a few interesting things with these. > > > > > > But would you like to look at the update-side uses of RCU Tasks Rude > > > to see if lazy preemption affects them? I don't believe that there > > > are any problems here, but we do need to check. > > > > Sure I will be happy to. I am planning look at it in detail over the 3 day weekend. Too much fun! ;-) > > Thank you, and looking forward to seeing what you come up with! > > The canonical concern would be that someone somewhere is using either > call_rcu_tasks_rude() or synchronize_rcu_tasks_rude() to wait for > non-preemptible regions of code that does not account for the possibility > of preemption in CONFIG_PREEMPT_NONE or PREEMPT_PREEMPT_VOLUNTARY kernels. > > I *think* that these are used only to handle the possibility > of tracepoints on functions on the entry/exit path and on the > RCU-not-watching portions of the idle loop. If so, then there is no > difference in behavior for lazy preemption. But who knows? Hi Paul, regarding CONFIG_PREEMPT_AUTO, for Tasks RCU rude, I think the following patch will address your concern about quiescent states on CPUs spinning away in kernel mode: "sched/fair: handle tick expiry under lazy preemption" Link: https://lore.kernel.org/all/20240213055554.1802415-24-ankur.a.arora@oracle.com/ In this patch Ankur makes sure that the scheduling-clock interrupt will reschedule the CPU after a tick and not let queued tasks starve due to lazy re-scheduling. So my impression is the "schedule_on_each_cpu()" should schedule a worker thread in time to apply the implied Tasks RCU quiescent state even if the rescheduling was a LAZY-reschedule. Also, not sure if the "voluntary mode" of CONFIG_PREEMPT_AUTO behaves differently. My feeling is regardless of preemption mode, CONFIG_PREEMPT_AUTO should always preempt after a tick if something else needs to run. It just will not preempt immediately like before (although CFS did already have some wakeup preemption logic to slow it down a bit). I am reviewing Ankur's patches more to confirm that and also reviewing his patches more to see how it could affect. thanks, - Joel
On Fri, Mar 01, 2024 at 09:24:15PM -0500, Joel Fernandes wrote: > (Shrinking CC a bit) > > On Thu, Feb 29, 2024 at 1:29 PM Paul E. McKenney <paulmck@kernel.org> wrote: > > > > On Thu, Feb 29, 2024 at 12:41:55PM -0500, Joel Fernandes wrote: > > > > On Feb 29, 2024, at 11:57 AM, Paul E. McKenney <paulmck@kernel.org> wrote: > > > > On Thu, Feb 29, 2024 at 09:21:48AM -0500, Joel Fernandes wrote: > > > >>> On 2/28/2024 5:58 PM, Paul E. McKenney wrote: > > > >>> On Wed, Feb 28, 2024 at 02:48:44PM -0800, Alexei Starovoitov wrote: > > > >>>> On Wed, Feb 28, 2024 at 2:31 PM Steven Rostedt <rostedt@goodmis.org> wrote: > > > >>>>> > > > >>>>> On Wed, 28 Feb 2024 14:19:11 -0800 > > > >>>>> "Paul E. McKenney" <paulmck@kernel.org> wrote: > > > >>>>> > > > >>>>>>>> > > > >>>>>>>> Well, to your initial point, cond_resched() does eventually invoke > > > >>>>>>>> preempt_schedule_common(), so you are quite correct that as far as > > > >>>>>>>> Tasks RCU is concerned, cond_resched() is not a quiescent state. > > > >>>>>>> > > > >>>>>>> Thanks for confirming. :-) > > > >>>>>> > > > >>>>>> However, given that the current Tasks RCU use cases wait for trampolines > > > >>>>>> to be evacuated, Tasks RCU could make the choice that cond_resched() > > > >>>>>> be a quiescent state, for example, by adjusting rcu_all_qs() and > > > >>>>>> .rcu_urgent_qs accordingly. > > > >>>>>> > > > >>>>>> But this seems less pressing given the chance that cond_resched() might > > > >>>>>> go away in favor of lazy preemption. > > > >>>>> > > > >>>>> Although cond_resched() is technically a "preemption point" and not truly a > > > >>>>> voluntary schedule, I would be happy to state that it's not allowed to be > > > >>>>> called from trampolines, or their callbacks. Now the question is, does BPF > > > >>>>> programs ever call cond_resched()? I don't think they do. > > > >>>>> > > > >>>>> [ Added Alexei ] > > > >>>> > > > >>>> I'm a bit lost in this thread :) > > > >>>> Just answering the above question. > > > >>>> bpf progs never call cond_resched() directly. > > > >>>> But there are sleepable (aka faultable) bpf progs that > > > >>>> can call some helper or kfunc that may call cond_resched() > > > >>>> in some path. > > > >>>> sleepable bpf progs are protected by rcu_tasks_trace. > > > >>>> That's a very different one vs rcu_tasks. > > > >>> > > > >>> Suppose that the various cond_resched() invocations scattered throughout > > > >>> the kernel acted as RCU Tasks quiescent states, so that as soon as a > > > >>> given task executed a cond_resched(), synchronize_rcu_tasks() might > > > >>> return or call_rcu_tasks() might invoke its callback. > > > >>> > > > >>> Would that cause BPF any trouble? > > > >>> > > > >>> My guess is "no", because it looks like BPF is using RCU Tasks (as you > > > >>> say, as opposed to RCU Tasks Trace) only to wait for execution to leave a > > > >>> trampoline. But I trust you much more than I trust myself on this topic! > > > >> > > > >> But it uses RCU Tasks Trace as well (for sleepable bpf programs), not just > > > >> Tasks? Looks like that's what Alexei said above as well, and I confirmed it in > > > >> bpf/trampoline.c > > > >> > > > >> /* The trampoline without fexit and fmod_ret progs doesn't call original > > > >> * function and doesn't use percpu_ref. > > > >> * Use call_rcu_tasks_trace() to wait for sleepable progs to finish. > > > >> * Then use call_rcu_tasks() to wait for the rest of trampoline asm > > > >> * and normal progs. > > > >> */ > > > >> call_rcu_tasks_trace(&im->rcu, __bpf_tramp_image_put_rcu_tasks); > > > >> > > > >> The code comment says it uses both. > > > > > > > > BPF does quite a few interesting things with these. > > > > > > > > But would you like to look at the update-side uses of RCU Tasks Rude > > > > to see if lazy preemption affects them? I don't believe that there > > > > are any problems here, but we do need to check. > > > > > > Sure I will be happy to. I am planning look at it in detail over the 3 day weekend. Too much fun! ;-) > > > > Thank you, and looking forward to seeing what you come up with! > > > > The canonical concern would be that someone somewhere is using either > > call_rcu_tasks_rude() or synchronize_rcu_tasks_rude() to wait for > > non-preemptible regions of code that does not account for the possibility > > of preemption in CONFIG_PREEMPT_NONE or PREEMPT_PREEMPT_VOLUNTARY kernels. > > > > I *think* that these are used only to handle the possibility > > of tracepoints on functions on the entry/exit path and on the > > RCU-not-watching portions of the idle loop. If so, then there is no > > difference in behavior for lazy preemption. But who knows? > > Hi Paul, regarding CONFIG_PREEMPT_AUTO, for Tasks RCU rude, I think > the following patch will address your concern about quiescent states > on CPUs spinning away in kernel mode: > > "sched/fair: handle tick expiry under lazy preemption" > Link: https://lore.kernel.org/all/20240213055554.1802415-24-ankur.a.arora@oracle.com/ > > In this patch Ankur makes sure that the scheduling-clock interrupt > will reschedule the CPU after a tick and not let queued tasks starve > due to lazy re-scheduling. So my impression is the > "schedule_on_each_cpu()" should schedule a worker thread in time to > apply the implied Tasks RCU quiescent state even if the rescheduling > was a LAZY-reschedule. > > Also, not sure if the "voluntary mode" of CONFIG_PREEMPT_AUTO behaves > differently. My feeling is regardless of preemption mode, > CONFIG_PREEMPT_AUTO should always preempt after a tick if something > else needs to run. It just will not preempt immediately like before > (although CFS did already have some wakeup preemption logic to slow it > down a bit). I am reviewing Ankur's patches more to confirm that and > also reviewing his patches more to see how it could affect. Thank you for the info! As you noted, one thing that Ankur's series changes is that preemption can occur anywhere that it is not specifically disabled in kernels built with CONFIG_PREEMPT_NONE=y or CONFIG_PREEMPT_VOLUNTARY=y. This in turn changes Tasks Rude RCU's definition of a quiescent state for these kernels, adding all code regions where preemption is not specifically disabled to the list of such quiescent states. Although from what I know, this is OK, it would be good to check the calls to call_rcu_tasks_rude() or synchronize_rcu_tasks_rude() are set up so as to expect these new quiescent states. One example where it would definitely be OK is if there was a call to synchronize_rcu_tasks() right before or after that call to synchronize_rcu_tasks_rude(). Would you be willing to check the call sites to verify that they are OK with this change in semantics? Thanx, Paul
On Sat, Mar 2, 2024 at 7:25 PM Paul E. McKenney <paulmck@kernel.org> wrote: > > On Fri, Mar 01, 2024 at 09:24:15PM -0500, Joel Fernandes wrote: > > (Shrinking CC a bit) > > > > On Thu, Feb 29, 2024 at 1:29 PM Paul E. McKenney <paulmck@kernel.org> wrote: > > > > > > On Thu, Feb 29, 2024 at 12:41:55PM -0500, Joel Fernandes wrote: > > > > > On Feb 29, 2024, at 11:57 AM, Paul E. McKenney <paulmck@kernel.org> wrote: > > > > > On Thu, Feb 29, 2024 at 09:21:48AM -0500, Joel Fernandes wrote: > > > > >>> On 2/28/2024 5:58 PM, Paul E. McKenney wrote: > > > > >>> On Wed, Feb 28, 2024 at 02:48:44PM -0800, Alexei Starovoitov wrote: > > > > >>>> On Wed, Feb 28, 2024 at 2:31 PM Steven Rostedt <rostedt@goodmis.org> wrote: > > > > >>>>> > > > > >>>>> On Wed, 28 Feb 2024 14:19:11 -0800 > > > > >>>>> "Paul E. McKenney" <paulmck@kernel.org> wrote: > > > > >>>>> > > > > >>>>>>>> > > > > >>>>>>>> Well, to your initial point, cond_resched() does eventually invoke > > > > >>>>>>>> preempt_schedule_common(), so you are quite correct that as far as > > > > >>>>>>>> Tasks RCU is concerned, cond_resched() is not a quiescent state. > > > > >>>>>>> > > > > >>>>>>> Thanks for confirming. :-) > > > > >>>>>> > > > > >>>>>> However, given that the current Tasks RCU use cases wait for trampolines > > > > >>>>>> to be evacuated, Tasks RCU could make the choice that cond_resched() > > > > >>>>>> be a quiescent state, for example, by adjusting rcu_all_qs() and > > > > >>>>>> .rcu_urgent_qs accordingly. > > > > >>>>>> > > > > >>>>>> But this seems less pressing given the chance that cond_resched() might > > > > >>>>>> go away in favor of lazy preemption. > > > > >>>>> > > > > >>>>> Although cond_resched() is technically a "preemption point" and not truly a > > > > >>>>> voluntary schedule, I would be happy to state that it's not allowed to be > > > > >>>>> called from trampolines, or their callbacks. Now the question is, does BPF > > > > >>>>> programs ever call cond_resched()? I don't think they do. > > > > >>>>> > > > > >>>>> [ Added Alexei ] > > > > >>>> > > > > >>>> I'm a bit lost in this thread :) > > > > >>>> Just answering the above question. > > > > >>>> bpf progs never call cond_resched() directly. > > > > >>>> But there are sleepable (aka faultable) bpf progs that > > > > >>>> can call some helper or kfunc that may call cond_resched() > > > > >>>> in some path. > > > > >>>> sleepable bpf progs are protected by rcu_tasks_trace. > > > > >>>> That's a very different one vs rcu_tasks. > > > > >>> > > > > >>> Suppose that the various cond_resched() invocations scattered throughout > > > > >>> the kernel acted as RCU Tasks quiescent states, so that as soon as a > > > > >>> given task executed a cond_resched(), synchronize_rcu_tasks() might > > > > >>> return or call_rcu_tasks() might invoke its callback. > > > > >>> > > > > >>> Would that cause BPF any trouble? > > > > >>> > > > > >>> My guess is "no", because it looks like BPF is using RCU Tasks (as you > > > > >>> say, as opposed to RCU Tasks Trace) only to wait for execution to leave a > > > > >>> trampoline. But I trust you much more than I trust myself on this topic! > > > > >> > > > > >> But it uses RCU Tasks Trace as well (for sleepable bpf programs), not just > > > > >> Tasks? Looks like that's what Alexei said above as well, and I confirmed it in > > > > >> bpf/trampoline.c > > > > >> > > > > >> /* The trampoline without fexit and fmod_ret progs doesn't call original > > > > >> * function and doesn't use percpu_ref. > > > > >> * Use call_rcu_tasks_trace() to wait for sleepable progs to finish. > > > > >> * Then use call_rcu_tasks() to wait for the rest of trampoline asm > > > > >> * and normal progs. > > > > >> */ > > > > >> call_rcu_tasks_trace(&im->rcu, __bpf_tramp_image_put_rcu_tasks); > > > > >> > > > > >> The code comment says it uses both. > > > > > > > > > > BPF does quite a few interesting things with these. > > > > > > > > > > But would you like to look at the update-side uses of RCU Tasks Rude > > > > > to see if lazy preemption affects them? I don't believe that there > > > > > are any problems here, but we do need to check. > > > > > > > > Sure I will be happy to. I am planning look at it in detail over the 3 day weekend. Too much fun! ;-) > > > > > > Thank you, and looking forward to seeing what you come up with! > > > > > > The canonical concern would be that someone somewhere is using either > > > call_rcu_tasks_rude() or synchronize_rcu_tasks_rude() to wait for > > > non-preemptible regions of code that does not account for the possibility > > > of preemption in CONFIG_PREEMPT_NONE or PREEMPT_PREEMPT_VOLUNTARY kernels. > > > > > > I *think* that these are used only to handle the possibility > > > of tracepoints on functions on the entry/exit path and on the > > > RCU-not-watching portions of the idle loop. If so, then there is no > > > difference in behavior for lazy preemption. But who knows? > > > > Hi Paul, regarding CONFIG_PREEMPT_AUTO, for Tasks RCU rude, I think > > the following patch will address your concern about quiescent states > > on CPUs spinning away in kernel mode: > > > > "sched/fair: handle tick expiry under lazy preemption" > > Link: https://lore.kernel.org/all/20240213055554.1802415-24-ankur.a.arora@oracle.com/ > > > > In this patch Ankur makes sure that the scheduling-clock interrupt > > will reschedule the CPU after a tick and not let queued tasks starve > > due to lazy re-scheduling. So my impression is the > > "schedule_on_each_cpu()" should schedule a worker thread in time to > > apply the implied Tasks RCU quiescent state even if the rescheduling > > was a LAZY-reschedule. > > > > Also, not sure if the "voluntary mode" of CONFIG_PREEMPT_AUTO behaves > > differently. My feeling is regardless of preemption mode, > > CONFIG_PREEMPT_AUTO should always preempt after a tick if something > > else needs to run. It just will not preempt immediately like before > > (although CFS did already have some wakeup preemption logic to slow it > > down a bit). I am reviewing Ankur's patches more to confirm that and > > also reviewing his patches more to see how it could affect. > > Thank you for the info! > > As you noted, one thing that Ankur's series changes is that preemption > can occur anywhere that it is not specifically disabled in kernels > built with CONFIG_PREEMPT_NONE=y or CONFIG_PREEMPT_VOLUNTARY=y. This in > turn changes Tasks Rude RCU's definition of a quiescent state for these > kernels, adding all code regions where preemption is not specifically > disabled to the list of such quiescent states. > > Although from what I know, this is OK, it would be good to check the > calls to call_rcu_tasks_rude() or synchronize_rcu_tasks_rude() are set > up so as to expect these new quiescent states. One example where it > would definitely be OK is if there was a call to synchronize_rcu_tasks() > right before or after that call to synchronize_rcu_tasks_rude(). > > Would you be willing to check the call sites to verify that they > are OK with this change in semantics? Yes, I will analyze and make sure those users did not unexpectedly assume something about AUTO (i.e. preempt enabled sections using readers). Btw, as I think you mentioned, with Ankur's patch even with CONFIG_PREEMPT_NONE=y, a preemption on the tick boundary can occur (in preempt=none mode)! Btw, For RUDE - If we wish to preempt sooner on "preempt=voluntary" of future CONFIG_PREEMPT_AUTO=y kernels, then we can potentially replace the schedule_on_each_cpu() with a higher priority (higher class) per-CPU threads like RT. Then wake them all up and waiting till the next tick is not needed for a CPU to be marked quiescent. Would something like that be of interest? Thanks.
Hi Paul, On 3/2/2024 8:01 PM, Joel Fernandes wrote: >> As you noted, one thing that Ankur's series changes is that preemption >> can occur anywhere that it is not specifically disabled in kernels >> built with CONFIG_PREEMPT_NONE=y or CONFIG_PREEMPT_VOLUNTARY=y. This in >> turn changes Tasks Rude RCU's definition of a quiescent state for these >> kernels, adding all code regions where preemption is not specifically >> disabled to the list of such quiescent states. >> >> Although from what I know, this is OK, it would be good to check the >> calls to call_rcu_tasks_rude() or synchronize_rcu_tasks_rude() are set >> up so as to expect these new quiescent states. One example where it >> would definitely be OK is if there was a call to synchronize_rcu_tasks() >> right before or after that call to synchronize_rcu_tasks_rude(). >> >> Would you be willing to check the call sites to verify that they >> are OK with this change in > Yes, I will analyze and make sure those users did not unexpectedly > assume something about AUTO (i.e. preempt enabled sections using > readers). Other than RCU test code, there are just 3 call sites for RUDE right now, all in ftrace.c. (Long story short, PREEMPT_AUTO should not cause wreckage in TASKS_RCU_RUDE other than any preexisting wreckage that !PREEMPT_AUTO already had. Steve is on CC as well to CMIIW). Case 1: For !CONFIG_DYNAMIC_FTRACE update of ftrace_trace_function This config is itself expected to be slow. However seeing what it does, it is trying to make sure the global function pointer "ftrace_trace_function" is updated and any readers of that pointers would have finished reading it. I don't personally think preemption has to be disabled across the entirety of the section that calls into this function. So sensitivity to preempt disabling should not be relevant for this case IMO, but lets see if ftrace folks disagree (on CC). It has more to do with, any callers of this function pointer are no longer calling into the old function. Case 2: Trampoline structures accessing For this there is a code comment that says preemption will disabled so it should not be dependent on any of the preemptiblity modes, because preempt_disable() should disable preempt with PREEMPT_AUTO. /* * We need to do a hard force of sched synchronization. * This is because we use preempt_disable() to do RCU, but * the function tracers can be called where RCU is not watching * (like before user_exit()). We can not rely on the RCU * infrastructure to do the synchronization, thus we must do it * ourselves. */ synchronize_rcu_tasks_rude(); [...] ftrace_trampoline_free(ops); Code comment probably needs update because it says 'can not rely on RCU..' ;-) My *guess* is the preempt_disable() mentioned in this case is ftrace_ops_trampoline() where trampoline-related datas tructures are accessed for stack unwinding purposes. This is a data structure protection thing AFAICS and nothing to do with "trampoline execution" itself which needs "Tasks RCU" to allow for preemption in trampolines. Case 3: This has to do with update of function graph tracing and there is the same comment as case 2, where preempt will be disabled in readers, so it should be safe for PREEMPT_AUTO (famous last words). Though I am not yet able to locate that preempt_disable() which is not an PREEMPT_AUTO-related issue anyway. Maybe its buried in function graph tracing logic somewhere? Finally, my thought also was, if any of these thread usages/cases of Tasks RCU RUDE assume working only on a CONFIG_PREEMPT_NONE=y or CONFIG_PREEMPT_VOLUNTARY=y kernel, that could be worrying but AFAICS, they don't assume anything related to that. thanks, - Joel
Hi Paul, On 3/2/2024 8:01 PM, Joel Fernandes wrote: >> As you noted, one thing that Ankur's series changes is that preemption >> can occur anywhere that it is not specifically disabled in kernels >> built with CONFIG_PREEMPT_NONE=y or CONFIG_PREEMPT_VOLUNTARY=y. This in >> turn changes Tasks Rude RCU's definition of a quiescent state for these >> kernels, adding all code regions where preemption is not specifically >> disabled to the list of such quiescent states. >> >> Although from what I know, this is OK, it would be good to check the >> calls to call_rcu_tasks_rude() or synchronize_rcu_tasks_rude() are set >> up so as to expect these new quiescent states. One example where it >> would definitely be OK is if there was a call to synchronize_rcu_tasks() >> right before or after that call to synchronize_rcu_tasks_rude(). >> >> Would you be willing to check the call sites to verify that they >> are OK with this change in > Yes, I will analyze and make sure those users did not unexpectedly > assume something about AUTO (i.e. preempt enabled sections using > readers). Other than RCU test code, there are just 3 call sites for RUDE right now, all in ftrace.c. (Long story short, PREEMPT_AUTO should not cause wreckage in TASKS_RCU_RUDE other than any preexisting wreckage that !PREEMPT_AUTO already had. Steve is on CC as well to CMIIW). Case 1: For !CONFIG_DYNAMIC_FTRACE update of ftrace_trace_function This config is itself expected to be slow. However seeing what it does, it is trying to make sure the global function pointer "ftrace_trace_function" is updated and any readers of that pointers would have finished reading it. I don't personally think preemption has to be disabled across the entirety of the section that calls into this function. So sensitivity to preempt disabling should not be relevant for this case IMO, but lets see if ftrace folks disagree (on CC). It has more to do with, any callers of this function pointer are no longer calling into the old function. Case 2: Trampoline structures accessing For this there is a code comment that says preemption will disabled so it should not be dependent on any of the preemptiblity modes, because preempt_disable() should disable preempt with PREEMPT_AUTO. /* * We need to do a hard force of sched synchronization. * This is because we use preempt_disable() to do RCU, but * the function tracers can be called where RCU is not watching * (like before user_exit()). We can not rely on the RCU * infrastructure to do the synchronization, thus we must do it * ourselves. */ synchronize_rcu_tasks_rude(); [...] ftrace_trampoline_free(ops); Code comment probably needs update because it says 'can not rely on RCU..' ;-) My *guess* is the preempt_disable() mentioned in this case is ftrace_ops_trampoline() where trampoline-related datas tructures are accessed for stack unwinding purposes. This is a data structure protection thing AFAICS and nothing to do with "trampoline execution" itself which needs "Tasks RCU" to allow for preemption in trampolines. Case 3: This has to do with update of function graph tracing and there is the same comment as case 2, where preempt will be disabled in readers, so it should be safe for PREEMPT_AUTO (famous last words). Though I am not yet able to locate that preempt_disable() which is not an PREEMPT_AUTO-related issue anyway. Maybe its buried in function graph tracing logic somewhere? Finally, my thought also was, if any of these thread usages/cases of Tasks RCU RUDE assume working only on a CONFIG_PREEMPT_NONE=y or CONFIG_PREEMPT_VOLUNTARY=y kernel, that could be worrying but AFAICS, they don't assume anything related to that. thanks, - Joel
diff --git a/net/core/dev.c b/net/core/dev.c index 275fd5259a4a..6e41263ff5d3 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -6773,6 +6773,10 @@ static int napi_threaded_poll(void *data) net_rps_action_and_irq_enable(sd); } skb_defer_free_flush(sd); + + if (!IS_ENABLED(CONFIG_PREEMPT_RT)) + rcu_softirq_qs(); + local_bh_enable(); if (!repoll)