From nobody Sat Jul 25 23:42:05 2026 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (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 81F933921E7 for ; Sat, 11 Jul 2026 12:10:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783771819; cv=none; b=ORZwLT4rmWzq3EQbaMLYEaQ46QyZ9gM4yYQkpsAY6x12iX+ZZBV3QAHmHA+y1QUa8xgJICQscgaZK47z+va+x8SMw28jBvlGf2yPtKPBiVOM0ydmgHPly8I0G1zsO0f9nQhgw7nqYb7S3CgHi9zQlNrrRP3IKshAhJDNps54/kU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783771819; c=relaxed/simple; bh=S8b/4BsFSE8KlFURh/bHVzTH/0yIH3iw8lTmdGMTBMw=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=Dag3QGs71IetzNe1Ka0bNm0VZVNd2uIKEf+l2cbdDxuYS2gvKvPfCi8uG/crnyqe/Abb5TloGiejuvKd4Imrm4o/6Obea7JU/l2SX4N3/99wM39ZrX0etbOI3uqqCXTG5iuuOIImOcczGPe9ISnxJIKfLgJFCd/bhx0nOZvjXuw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=qENWZoxT; arc=none smtp.client-ip=209.85.214.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="qENWZoxT" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-2c6b67d5fa1so28742905ad.2 for ; Sat, 11 Jul 2026 05:10:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783771818; x=1784376618; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=rwcep/f1okQ2Nd2drpl9IskC13P+jKJfO97boATnYJE=; b=qENWZoxTladv0WZ0OjR6jzpKCKID5dHu7wwdWd91MXzFplAsDtkVCRQC0hhqhs4U/L 4K3TUNWr0F/tbSr749nVKhUQFc8O3IHL0Z1hPlzvumM9fXxUE+dubGkf2gMUZHd6PMvv DF8bm/mAwpK3+cdxLVRnhm+vltBnlSwjTWKdHEExJGoNu1iGpP9cGWB6/d4KUROOEX/D FInR+PK9RcEcpugGr9wynt8xt7dOH/1LSOw/08+NXz/UGkMMPZztNag9DqFhTCfKvPt+ BuVz4bqOxzl6Pqu9T9DV1NYpIr3y66BKvX4X9UJ58Vq1PNzz/FXrjnksjI6n6eH4kgcH FLrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783771818; x=1784376618; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=rwcep/f1okQ2Nd2drpl9IskC13P+jKJfO97boATnYJE=; b=AQKE0pNAeYyo1dBYJq1t+lnRT+LEow2OPDqBmGXuc2YRnm6Pf+YvKeqyWa4oemDgpu nDSA+BX16rqJzAwP9VEWA27rVsjJUJOKPL28QoD5lQh1rHJveeu8dmSxpLSswg+rBMml 919363WnhuT5o1EorKs0Vrfq5eIIzEuys177/Kmkq8uL8Hemb053LFgfv9rLXtelj9x0 9hv/BzFZZ3qB+CDkZEOwyG2nJ+UvGbW8VP9yLRyfbh2Toe5oxn6w2tFaSL6iQEFQjIga YtR7ObnjVTfxl31/GLPZIZWEoptnyvR1to9o1NYHqcc18MkzKB5lEkN8vQHAjomveHlI 2kAw== X-Forwarded-Encrypted: i=1; AHgh+RrNucWEGf5vBylCZoq6I/VtghJHkY1JFl/+3E/gU9tKnogOzu3MWiBEHJE5m63lTr/A0n+0Sfgz0kq0Scs=@vger.kernel.org X-Gm-Message-State: AOJu0YyhDMwGzNK5f3mVODf94F4SXh9wl037/VS3QPaJGCELfyJS1GBR vX0xzZmHTkip713okDTV5+BFC2+V+KpDEBG16F+crM5fRyJIY2yrs7wC X-Gm-Gg: AfdE7cmI54Ip1JamXc6YU2jsRw6U+/UKIct4GOMqozyQw4sW0WBHIVzWDU2X3qmIWdW BKab8TBO6ZZDBWSkGs5iBf25x++kQa8zc+jhgvr10VUZ7ENJiKGJvrsMYqcZ4cHfIiFkAZo9glY jXVGpUlnoFDkhtIWNB2LW+uXYJQotZzMe1IchEX5R3JVoa+RwK+5yadrNZydJbRajgSENFW9dui nzxw19ubVPHLU23vpVHZz1tqNCMQHJe7jpHcAaI7tHGaODsvfDM7XOSKEZJhoO3AFoElYHeJAcZ i7PR8u0jssvEBDpgh4WbmV18iCXEFy1KaVFlope7pEncwzS8I97UY/PewgQlJjuLTUKoDVtpogz KEIgfOLHg3T7jVIkSofWUaJ+82qPg3hrq91HL04wKq3Tn7znAerEmD4VXhfEZg5+7fsd8CwLa5r ggmpqajIEVzl/LD7M8ShqpuMVXd7M= X-Received: by 2002:a17:903:1b6b:b0:2ca:9d5a:8b6c with SMTP id d9443c01a7336-2ce9e79f42bmr29829555ad.5.1783771817755; Sat, 11 Jul 2026 05:10:17 -0700 (PDT) Received: from localhost.localdomain ([202.8.105.118]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2ccc9d3bc42sm72537085ad.63.2026.07.11.05.10.15 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sat, 11 Jul 2026 05:10:17 -0700 (PDT) From: Jemmy Wong To: Andrew Lunn , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] net/rps: consolidate RPS dispatch into helpers Date: Sat, 11 Jul 2026 20:10:08 +0800 Message-ID: <20260711121009.76842-1-jemmywong512@gmail.com> X-Mailer: git-send-email 2.54.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The RPS steering logic in netif_rx_internal(), netif_receive_skb_internal() and netif_receive_skb_list_internal() was open-coded three times, each with its own #ifdef CONFIG_RPS block and manual rcu_read_lock()/unlock() pairs. Factor it into two helpers, netif_rps() for the single-skb path and netif_rps_list() for the list path, sharing a common __netif_rps() inner helper that runs the get_rps_cpu()/enqueue_to_backlog() step under RCU. Both wrappers keep the early static_branch_unlikely(&rps_needed) check and only enter an RCU read-side critical section when RPS is actually enabled at runtime, so no extra rcu_read_lock()/unlock() is executed on the hot netif_rx_internal() path when RPS is compiled in but disabled. A new internal NET_RX_UNHANDLED sentinel lets a helper report "RPS did not take this skb" so the caller falls back to the local enqueue / __netif_receive_skb() path; it never escapes to callers. No functional change intended. Signed-off-by: Jemmy Wong --- Changes since v1 [1]: - Move rcu_read_lock()/unlock() inside the static_branch_unlikely(&rps_needed) check, so no RCU read-side section is entered when RPS is compiled in but disabled at runtime. Addresses Paolo's concern that netif_rx_internal() is performance critical and rcu_read_lock() is not a no-op. - Drop the guard(rcu) / scoped_guard(rcu) conversion; the RCU section is now scoped inside netif_rps() / netif_rps_list() themselves. - Split __netif_rps() out of netif_rps() so that netif_rps_list() can reuse the per-skb steering step without re-testing the static branch or dropping and reacquiring the RCU lock per skb. - netif_rps_list() now returns int; when RPS drains the entire list, netif_receive_skb_list_internal() skips the __netif_receive_skb_list() fall-through instead of walking an empty list. The result is decided directly from list_empty(&undo_list): drop the splice on the drained path and only splice back when there is something to fall through. Verified generated code with objdump on aarch64 with CONFIG_RPS=3Dy, CONFIG_XPS=3Dy, CONFIG_SMP=3Dy, CONFIG_PREEMPT=3Dy (so rcu_read_lock() expands to a real preempt_disable(), i.e. not a no-op -- the specific scenario Paolo asked about [2]): scripts/bloat-o-meter net/core/dev.o (before -> after): add/remove: 2/2 grow/shrink: 1/2 up/down: 720/-716 (4) Function old new delta __netif_rps - 660 +660 netif_receive_skb_list_internal 640 700 +60 netif_rx_internal 236 200 -36 netif_receive_skb 204 136 -68 get_rps_cpu 608 - -608 Total: Before=3D335841, After=3D335849, chg +0.00% netif_rx_internal fast path (!rps_needed, i.e. static branch not taken -- the path Paolo asked about [2]): - identical instruction sequence to base, no rcu_read_lock/unlock - stack frame 16 bytes smaller (0x50 -> 0x40) - static_branch NOP is preserved netif_receive_skb_internal: inlined in both builds; caller netif_receive_skb shrinks by 68 bytes. netif_receive_skb_list_internal: !rps_needed fast path unchanged; rps_needed=3Dtrue path grows +60 bytes for LIST_HEAD(undo_list) setup, the explicit list_empty(&undo_list)/list_splice_init() branch and its early NET_RX_SUCCESS return. [1] https://lore.kernel.org/all/20260702152830.39065-1-jemmywong512@gmail.c= om/ [2] https://lore.kernel.org/all/e53482b8-4e1d-4a04-929c-f5cb443ba59d@redhat= .com/ include/linux/netdevice.h | 5 +- net/core/dev.c | 118 +++++++++++++++++++++++--------------- 2 files changed, 74 insertions(+), 49 deletions(-) diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h index 9981d637f8b5..c265b78082e3 100644 --- a/include/linux/netdevice.h +++ b/include/linux/netdevice.h @@ -93,8 +93,9 @@ void netdev_set_default_ethtool_ops(struct net_device *de= v, void netdev_sw_irq_coalesce_default_on(struct net_device *dev); /* Backlog congestion levels */ -#define NET_RX_SUCCESS 0 /* keep 'em coming, baby */ -#define NET_RX_DROP 1 /* packet dropped */ +#define NET_RX_UNHANDLED -1 +#define NET_RX_SUCCESS 0 +#define NET_RX_DROP 1 #define MAX_NEST_DEV 8 diff --git a/net/core/dev.c b/net/core/dev.c index 4b3d5cfdf6e0..1a05ac8fdd50 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -5426,6 +5426,65 @@ static int enqueue_to_backlog(struct sk_buff *skb, i= nt cpu, return NET_RX_DROP; } +#ifdef CONFIG_RPS +static inline int __netif_rps(struct sk_buff *skb) +{ + int cpu; + int ret =3D NET_RX_UNHANDLED; + struct rps_dev_flow voidflow, *rflow =3D &voidflow; + + cpu =3D get_rps_cpu(skb->dev, skb, &rflow); + if (cpu >=3D 0) + ret =3D enqueue_to_backlog(skb, cpu, &rflow->last_qtail); + + return ret; +} +#endif + +static inline int netif_rps(struct sk_buff *skb) +{ + int ret =3D NET_RX_UNHANDLED; + +#ifdef CONFIG_RPS + if (!static_branch_unlikely(&rps_needed)) + return ret; + + rcu_read_lock(); + ret =3D __netif_rps(skb); + rcu_read_unlock(); +#endif + + return ret; +} + +static inline int netif_rps_list(struct list_head *head) +{ + int ret =3D NET_RX_UNHANDLED; + +#ifdef CONFIG_RPS + LIST_HEAD(undo_list); + struct sk_buff *skb, *next; + + if (!static_branch_unlikely(&rps_needed)) + return ret; + + rcu_read_lock(); + list_for_each_entry_safe(skb, next, head, list) { + skb_list_del_init(skb); + if (__netif_rps(skb) =3D=3D NET_RX_UNHANDLED) + list_add_tail(&skb->list, &undo_list); + } + rcu_read_unlock(); + + if (list_empty(&undo_list)) + ret =3D NET_RX_SUCCESS; + else + list_splice_init(&undo_list, head); +#endif + + return ret; +} + static struct netdev_rx_queue *netif_get_rxqueue(struct sk_buff *skb) { struct net_device *dev =3D skb->dev; @@ -5695,34 +5754,18 @@ EXPORT_SYMBOL_GPL(do_xdp_generic); static int netif_rx_internal(struct sk_buff *skb) { + unsigned int qtail; int ret; net_timestamp_check(READ_ONCE(net_hotdata.tstamp_prequeue), skb); trace_netif_rx(skb); -#ifdef CONFIG_RPS - if (static_branch_unlikely(&rps_needed)) { - struct rps_dev_flow voidflow, *rflow =3D &voidflow; - int cpu; - - rcu_read_lock(); - - cpu =3D get_rps_cpu(skb->dev, skb, &rflow); - if (cpu < 0) - cpu =3D smp_processor_id(); - - ret =3D enqueue_to_backlog(skb, cpu, &rflow->last_qtail); - - rcu_read_unlock(); - } else -#endif - { - unsigned int qtail; + ret =3D netif_rps(skb); + if (ret !=3D NET_RX_UNHANDLED) + return ret; - ret =3D enqueue_to_backlog(skb, smp_processor_id(), &qtail); - } - return ret; + return enqueue_to_backlog(skb, smp_processor_id(), &qtail); } /** @@ -6389,19 +6432,11 @@ static int netif_receive_skb_internal(struct sk_buf= f *skb) if (skb_defer_rx_timestamp(skb)) return NET_RX_SUCCESS; - rcu_read_lock(); -#ifdef CONFIG_RPS - if (static_branch_unlikely(&rps_needed)) { - struct rps_dev_flow voidflow, *rflow =3D &voidflow; - int cpu =3D get_rps_cpu(skb->dev, skb, &rflow); + ret =3D netif_rps(skb); + if (ret !=3D NET_RX_UNHANDLED) + return ret; - if (cpu >=3D 0) { - ret =3D enqueue_to_backlog(skb, cpu, &rflow->last_qtail); - rcu_read_unlock(); - return ret; - } - } -#endif + rcu_read_lock(); ret =3D __netif_receive_skb(skb); rcu_read_unlock(); return ret; @@ -6421,21 +6456,10 @@ void netif_receive_skb_list_internal(struct list_he= ad *head) } list_splice_init(&sublist, head); - rcu_read_lock(); -#ifdef CONFIG_RPS - if (static_branch_unlikely(&rps_needed)) { - list_for_each_entry_safe(skb, next, head, list) { - struct rps_dev_flow voidflow, *rflow =3D &voidflow; - int cpu =3D get_rps_cpu(skb->dev, skb, &rflow); + if (netif_rps_list(head) =3D=3D NET_RX_SUCCESS) + return; - if (cpu >=3D 0) { - /* Will be handled, remove from list */ - skb_list_del_init(skb); - enqueue_to_backlog(skb, cpu, &rflow->last_qtail); - } - } - } -#endif + rcu_read_lock(); __netif_receive_skb_list(head); rcu_read_unlock(); } -- 2.54.0 (Apple Git-157)