🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #23570

[PATCH net-next v3 1/3] tun: don't disable BHs for the XDP program check of every packet

#23570
From: Antonio Quartulli
Date: Fri, 27 Nov 2026 14:14
64 lines
11280 bytes
For every packet, tun_get_user() disables BHs and enters an RCU section
only to find out whether tun's XDP program is attached, which only TAP
devices can have. tun_build_skb() takes the RCU read lock for the same
NULL test.

Test the pointer with rcu_access_pointer() instead, and enter the
BH-disabled RCU section in tun_get_user() only with a program attached.
A program attached concurrently could already miss the packets in
flight.

1.2% fewer instructions per 1400-byte UDP packet written and dropped at
the raw table, 0.7% fewer when forwarded (qemu -icount, x86_64).

Signed-off-by: Antonio Quartulli <antonio@mandelbit.com>
---
 drivers/net/tun.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index 7894dbdf0369..e2fe7174e438 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -1721,14 +1721,13 @@ static struct sk_buff *tun_build_skb(struct tun_struct *tun,
 	size_t copied;
 	int pad = TUN_RX_PAD;
 	int metasize = 0;
+	bool has_xdp;
 	int err = 0;
 
-	rcu_read_lock();
-	xdp_prog = rcu_dereference(tun->xdp_prog);
-	if (xdp_prog)
+	has_xdp = rcu_access_pointer(tun->xdp_prog);
+	if (has_xdp)
 		pad += XDP_PACKET_HEADROOM;
 	buflen += SKB_DATA_ALIGN(len + pad);
-	rcu_read_unlock();
 
 	alloc_frag->offset = ALIGN((u64)alloc_frag->offset, SMP_CACHE_BYTES);
 	if (unlikely(!skb_page_frag_refill(buflen, alloc_frag, GFP_KERNEL)))
@@ -1745,7 +1744,7 @@ static struct sk_buff *tun_build_skb(struct tun_struct *tun,
 	 * of xdp_prog above, this should be rare and for simplicity
 	 * we do XDP on skb in case the headroom is not enough.
 	 */
-	if (hdr->gso_type || !xdp_prog) {
+	if (hdr->gso_type || !has_xdp) {
 		*skb_xdp = 1;
 		return __tun_build_skb(tfile, alloc_frag, buf, buflen, len,
 				       pad, metasize);
@@ -1988,7 +1987,10 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
 	skb_probe_transport_header(skb);
 	skb_record_rx_queue(skb, tfile->queue_index);
 
-	if (skb_xdp) {
+	/* Don't disable BHs and enter an RCU section for every packet only
+	 * to find that no program is attached, the common case.
+	 */
+	if (skb_xdp && rcu_access_pointer(tun->xdp_prog)) {
 		struct bpf_prog *xdp_prog;
 		int ret;
 
-- 
2.56.1
Technical Details
Message-ID: <20261127141406.651406-2-antonio@mandelbit.com>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-372763-linux-kernel-netdev=dewy.ru
References: <20261127141406.651406-1-antonio@mandelbit.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-372763-linux-kernel-netdev=dewy.ru
From: antonio@mandelbit.com (Antonio Quartulli)
Newsgroups: linux.kernel.netdev
Subject: [PATCH net-next v3 1/3] tun: don't disable BHs for the XDP program check of every packet
Date: Fri, 27 Nov 2026 15:14:03 +0100
Organization: Usenet Network
Message-ID: <20261127141406.651406-2-antonio@mandelbit.com>
References: <20261127141406.651406-1-antonio@mandelbit.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="747153"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:MioGUYzCHdmphfWCQXdlgNXs33s= sha256:dWDsFIjaQ80r9ZJrQDOQMMsJ/ZfQQcO2LB+GUiEWG90=
	sha1:MMwDAGNG6+9s8K/+FWTwOXYPDEU= sha256:Rd6rBXl2G3aXgndt+blTUTwTxPth7fxmAw7VvOpNz8w=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id 11A9D125F5F; Fri, 27 Nov 2026 17:14:41 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.051
X-Spam-Status: No, score=-3.051 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
	DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=mandelbit.com
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id dLXSjsr2ZkSA;
	Fri, 27 Nov 2026 17:14:37 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-372763-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=none (p=none dis=none) header.from=mandelbit.com
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
	by mail.dewy.ru (Postfix) with ESMTPS id 87091125F60; Fri, 27 Nov 2026 17:14:27 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sea.lore.kernel.org (Postfix) with ESMTP id D21092F90F; Fri, 27 Nov 2026 14:14:21 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1795788857;
	b=aO6nVNhUbxhtSEFfcqrFXkb6Jc5JjILyRaUkaiTtXqVstSax5pBKfnffuAWOgSBCJZ/K
	 tr1SSHndChqAR78Nq2q751EoLRyAmOq8gfpsCtO9u1AnFdn0zspL/ljEZn/XXrwG+nlRb
	 +b7PXA+TQfNOH8UAdLGJ7I5gT9Jz2ckUBjD1hjL9Z+CMnM9XWjV3Vjvl+hTtuFGQ8CGiI
	 qnlE8Hu48gQymBqz9c8m2owW/Kiq4O0BzLyVUudCeY5KXeBYhiTkT/+CB542nxmd3qmaB
	 56qDjr9EaApSw0lMoW5yCx52QCv5DCOE0T5KrV3UqKSxySmkM5KgVprZEj4UpYN37hw==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795788857;
	h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:From:To:
	 Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References:
	 X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe:
	 List-Unsubscribe:List-Owner:Precedence:MIME-Version:
	 Content-Transfer-Encoding:X-Rspamd-Queue-Id;
	bh=k2fo81eFplJpgtv+AO8ZwoWQN0C7pqxKh2F1qv8H3hQ=;
	b=AAILWug3BzLe80maf98cGX6ruF1PReGBBp3jd9mm65v7Zhu1jycTOZkAMmmqNfFJm0IW
	 MfkvSEweLpPrt0jD954JvTcanlCTLYg47SEHU0O32Gy9dOui6/lNuvKGXkh0vZn23NOq1
	 +K6WfIYgZ4HZGgcS8OeoLIAYGTEe8OGBLNLx9Y+xvVjiSkMGyN1m4DBEMw91PsTqTTSBs
	 UcqIWZFLvlFYYDa+kfNJp/OzoQKvoIJE6mqgDBiNPTN+PQFZwX3xCsWFbT9vmleRPPIZv
	 DXAikwUbtT1KUnLA3oVjyv1bN24/iFBDmCliIX30YI87LTLjxQ/viNTbNqKB59/8O3Q==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=mandelbit.com header.i=@mandelbit.com header.a=rsa-sha256 header.s=MBO0001 header.b=plZVvE60;
	dmarc=none header.from=mandelbit.com;
	spf=pass smtp.mailfrom=mandelbit.com;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 09CA9606A682;
	Fri, 27 Nov 2026 14:14:17 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key, unprotected) header.d=mandelbit.com header.i=@mandelbit.com header.a=rsa-sha256 header.s=MBO0001 header.b=plZVvE60
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=195.10.208.50
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1795788856;
	b=sNPaVn/kt6sDwV7Byp5BUtWsGJdwCjEZZy+/ldfAcc8mfd3sWHJ2f+Ai3csQldpwAdv0
	 rHh2++GmZDvOBeyOi4YgtgzYR2MrppbjUkjd1eLje7j+TQjJFR6DQa3xGKLbix3PgvuGq
	 VLw2sj4JnRrx5O4ovwJXtscN+vCmaLUXvUcQ7jWPs24PG6Z0jWJzTzXedOBUcdKOJaa6z
	 qXYOold0E+QzvNDB3gK5xXKgAKTCgfdEUjQxYbhk/1nge7Uc1uBIYRdDb0pADuSbLBHd5
	 2ZC47Hc0fRqHG/cE/HdIUdArnnoRUlplotg+uYl7YZwaD9pYuyJB0kxN9GBICTNvK9g==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795788856;
	h=DMARC-Filter:Received:DKIM-Signature:From:To:Cc:Subject:Date:
	 Message-ID:X-Mailer:In-Reply-To:References:MIME-Version:
	 Content-Transfer-Encoding:X-Rspamd-Queue-Id;
	bh=k2fo81eFplJpgtv+AO8ZwoWQN0C7pqxKh2F1qv8H3hQ=;
	b=iqgYNAgUbOX3PUYCHJCGpnMJd0dKowcrEt8SL4bqAHbcga1+qXa1r9eqFPeCwpsUjj/I
	 771EOf7dRzKmhgss34VteZDooaO6iiHwM2NY3lMbc2vf/VsKvS0YuA3lBgUkrHtxyVe4U
	 arFveHLmSc8YpzpI9ZUVlo4luG6aLvq5fyOnCnoeVXPVh1AVgDPWe4cAsolnZkjStbfcm
	 15427xvYG1o4HcIvw08zOp8oS+AwvRONa7ATb+dIZgSK60DjjAxVthtd6DH4vmhezps7U
	 eky+p9Orhflki5wO6ip9nslieneL1sv0b0QARsPoH4/0k9CjNzLFLiqgreSKUzeg4vA==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dmarc=none header.from=mandelbit.com;
	spf=pass smtp.mailfrom=mandelbit.com;
	dkim=pass header.d=mandelbit.com header.i=@mandelbit.com header.a=rsa-sha256 header.s=MBO0001 header.b=plZVvE60;
	arc=none smtp.remote-ip=195.10.208.50
X-Received: from mout-b-105.mailbox.org (mout-b-105.mailbox.org [195.10.208.50])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256)
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id A8B3A606A32A;
	Fri, 27 Nov 2026 14:14:15 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A8B3A606A32A
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=mandelbit.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mandelbit.com
X-Received: from smtp102.mailbox.org (smtp102.mailbox.org [IPv6:2001:67c:2050:b231:465::102])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519MLKEM768 server-signature RSA-PSS (4096 bits) server-digest SHA256)
	(No client certificate requested)
	by mout-b-105.mailbox.org (Postfix) with ESMTPS id 4jbb4z6vZjz9sqc;
	Fri, 27 Nov 2026 15:14:11 +0100 (CET)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mandelbit.com;
	s=MBO0001; t=1795788852;
	h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
	 to:to:cc:cc:mime-version:mime-version:
	 content-transfer-encoding:content-transfer-encoding:
	 in-reply-to:in-reply-to:references:references;
	bh=k2fo81eFplJpgtv+AO8ZwoWQN0C7pqxKh2F1qv8H3hQ=;
	b=plZVvE60NB05P0/X6Pwmazg3UbQHAvZEKIWP66m/nKg5UMUeRwo+cTYmxNElzdGGUHv5/X
	D6aTMB/tml8lq9ulno1qvFrfHMTsM59VvADlHeo+NpfBc2liIgJLl0tMwB31zMPZwlug6h
	CnHWvij9Arousc1I/xvvSiErW56vLJCWdCetQBlKsgWW6Ww+LkUhqoU1our4PgaMUKMs/X
	QShhArtCVbS4cyBJgLzCv68c4RJ3AAHU0uQqImmXnroclvtuIpcPBiO93JwNixKKJ6f7oY
	r7jSJUEJWkvPm5yHBc0e7QMjcJG/+T8/5VYtXDN/g9/reTWXpolgND3/DGZjTA==
X-Authentication-Results: outgoing_mbo_mout;
	dkim=none;
	spf=pass (outgoing_mbo_mout: domain of antonio@mandelbit.com designates 2001:67c:2050:b231:465::102 as permitted sender) smtp.mailfrom=antonio@mandelbit.com
X-Cc: Antonio Quartulli <antonio@mandelbit.com>,
	andrew+netdev@lunn.ch,
	davem@davemloft.net,
	edumazet@kernel.org,
	kuba@kernel.org,
	pabeni@redhat.com,
	ast@kernel.org,
	daniel@iogearbox.net,
	hawk@kernel.org,
	john.fastabend@gmail.com,
	sdf@fomichev.me,
	netdev@vger.kernel.org,
	bpf@vger.kernel.org
X-Mailer: git-send-email 2.56.1
X-In-Reply-To: <20261127141406.651406-1-antonio@mandelbit.com>
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list
X-Rspamd-Queue-Id: 4jbb4z6vZjz9sqc