🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #22847

[PATCH net v3] xsk: keep the charged page count when pinning a umem fails

#22847
From: Kyuwon Cho
Date: Thu, 26 Nov 2026 02:07
132 lines
15179 bytes
xdp_umem_reg() charges umem->npgs pages to the user's locked_vm before
it pins the umem. When pin_user_pages() pins fewer pages than asked,
xdp_umem_pin_pages() overwrites umem->npgs with the pinned count so that
xdp_umem_unpin_pages() releases only those, and xdp_umem_unaccount_pages()
then gives back only that many. The rest of the charge is never returned.

A short pin happens when the tail of the umem area is not mapped. With
RLIMIT_MEMLOCK at 16 pages, three registrations of an 8-page area whose
last 4 pages are unmapped leave the user able to register only 4 pages,
for as long as the user has a process running.

Unpin the pinned pages directly and leave umem->npgs as charged.

Fixes: c0c77d8fb787 ("xsk: add user memory registration support sockopt")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/all/20260421172951.A7052C2BCB5@smtp.kernel.org/
Signed-off-by: Kyuwon Cho <kyuwoncho18@gmail.com>
---
v3: simpler error path without @err, as Maciej suggested; Bjorn's
    Reviewed-by is not carried over, as the code changed
v2: https://lore.kernel.org/all/179556786851.2007362.7849536379916971930@gmail.com/
    target net instead of bpf (Alexei)
v1: https://lore.kernel.org/all/179552079829.1290967.12345053263834529704@gmail.com/

The issue was found by an LLM-guided test generator that we are
developing as a research prototype; Sashiko had flagged the same leak
in its review of an AUTOSEL backport. A reproducer, which runs as any
user:

#define _GNU_SOURCE
#include <errno.h>
#include <linux/if_xdp.h>
#include <sched.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <unistd.h>

static int reg(char *addr, long len)
{
	struct xdp_umem_reg mr = {
		.addr = (unsigned long)addr, .len = len, .chunk_size = 2048,
	};
	int fd = socket(AF_XDP, SOCK_RAW, 0);
	int ret = setsockopt(fd, SOL_XDP, XDP_UMEM_REG, &mr, sizeof(mr)) ? -errno : 0;

	close(fd);
	return ret;
}

int main(void)
{
	long pg = sysconf(_SC_PAGESIZE);
	struct rlimit rl = { 16 * pg, 16 * pg };
	char *a;

	/* no CAP_IPC_LOCK, CAP_NET_RAW for AF_XDP */
	if (unshare(CLONE_NEWUSER | CLONE_NEWNET) || setrlimit(RLIMIT_MEMLOCK, &rl))
		return perror("setup"), 1;

	for (int i = 0; i < 3; i++) {
		/* 8 pages, the last 4 unmapped: pin_user_pages() pins 4 */
		a = mmap(NULL, 8 * pg, PROT_READ | PROT_WRITE,
			 MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
		munmap(a + 4 * pg, 4 * pg);
		printf("short umem: %s\n", strerror(-reg(a, 8 * pg)));
		munmap(a, 4 * pg);
	}

	a = mmap(NULL, 16 * pg, PROT_READ | PROT_WRITE,
		 MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
	printf("16-page umem: %s\n", strerror(-reg(a, 16 * pg)));
	return 0;
}

Without the patch, the 16-page registration fails with ENOBUFS: the
three short ones leave 12 of the 16 pages charged. With it, it
succeeds.

The patch was written with LLM assistance; we reviewed and tested it.

 net/xdp/xdp_umem.c | 22 +++++++---------------
 1 file changed, 7 insertions(+), 15 deletions(-)

diff --git a/net/xdp/xdp_umem.c b/net/xdp/xdp_umem.c
index cd8643360eb3..ad72bd96905c 100644
--- a/net/xdp/xdp_umem.c
+++ b/net/xdp/xdp_umem.c
@@ -95,7 +95,6 @@ static int xdp_umem_pin_pages(struct xdp_umem *umem, unsigned long address)
 {
 	unsigned int gup_flags = FOLL_WRITE;
 	long npgs;
-	int err;
 
 	umem->pgs = kvzalloc_objs(*umem->pgs, umem->npgs,
 				  GFP_KERNEL | __GFP_NOWARN);
@@ -107,23 +106,16 @@ static int xdp_umem_pin_pages(struct xdp_umem *umem, unsigned long address)
 			      gup_flags | FOLL_LONGTERM, &umem->pgs[0]);
 	mmap_read_unlock(current->mm);
 
-	if (npgs != umem->npgs) {
-		if (npgs >= 0) {
-			umem->npgs = npgs;
-			err = -ENOMEM;
-			goto out_pin;
-		}
-		err = npgs;
-		goto out_pgs;
-	}
-	return 0;
+	if (npgs == umem->npgs)
+		return 0;
+
+	if (npgs > 0)
+		unpin_user_pages(umem->pgs, npgs);
 
-out_pin:
-	xdp_umem_unpin_pages(umem);
-out_pgs:
 	kvfree(umem->pgs);
 	umem->pgs = NULL;
-	return err;
+
+	return npgs < 0 ? npgs : -ENOMEM;
 }
 
 static int xdp_umem_account_pages(struct xdp_umem *umem)
-- 
2.43.0
Technical Details
Message-ID: <179562059260.3381969.13546710565371160648@gmail.com>
Path: mbox-mo1.pugleaf.net!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-371809-linux-kernel-netdev=dewy.ru

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-371809-linux-kernel-netdev=dewy.ru
From: kyuwoncho18@gmail.com (Kyuwon Cho)
Newsgroups: linux.kernel.netdev
Subject: [PATCH net v3] xsk: keep the charged page count when pinning a umem fails
Date: Thu, 26 Nov 2026 11:07:42 +0900
Organization: Usenet Network
Message-ID: <179562059260.3381969.13546710565371160648@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="358084"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:2Jagdw1LuPaIP+Xs5huLPEyw1GM= sha256:DfLg1TGB0yeM7LnLvZ+91x6MNuNy+fFIuNYWDCYec6g=
	sha1:gSKokchBbbbw9BL0zz30Pqbpq24= sha256:T5zXjLknEudC1FWmD1JSTE9thrtW4bYM4s5APDCs5ZY=
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 A0E9B125EAD; Thu, 26 Nov 2026 05:08:19 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.049
X-Spam-Status: No, score=-3.049 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, FREEMAIL_FORGED_FROMDOMAIN=0.001,
	FREEMAIL_FROM=0.001, 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=gmail.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 8c4RzgfEEXHs;
	Thu, 26 Nov 2026 05:08:12 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-371809-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=gmail.com
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
	by mail.dewy.ru (Postfix) with ESMTPS id 8D909125EA3; Thu, 26 Nov 2026 05:08:02 +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 3A1283F58B; Thu, 26 Nov 2026 02:07:51 +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=1795658869;
	b=R5Jpv2jIwy7zfacyY+eus6p0HN9sWGlBo7Zy5sjxIvepDOFTK0TLCrQTbPCSVnknrDus
	 cJKr9/gSmVWxLTTvUaacA55Ype/WDjnGy5uaRZLCXyExvOwB1/dY9kNZa16WteZ+elCNl
	 81Dx4/kJ23ADd7uLLs34YIt5t4vFEepWwSGmNrk2/WXztq8kyjkpHfwMt4mZqsitcD/YH
	 DpkfAISmqPG8vgehd8q6YPKo5Vd7FnHpu7VmupKTfgXZHNJKIr3pHXdGKpsZ1/S1I88WZ
	 h2XVadnNozzW4/1MGPlALFfktqjjY29NmNsP/6Azu54fJfKeOGu+uPgmdKyDnhKlM6w==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795658869;
	h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:
	 X-Google-DKIM-Signature:X-Forwarded-Encrypted:X-Gm-Message-State:
	 X-Gm-Gg:X-Received:Received:From:To:Cc:Subject:Date:Message-ID:
	 X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe:
	 List-Unsubscribe:List-Owner:Precedence:MIME-Version:
	 Content-Transfer-Encoding;
	bh=WkAms7KA42Baas5n6wZEyMjVEZ8N2pj3aqq6PvvQjdE=;
	b=JfUR1JVbHWbH9rjEhh5cRRw2JuWWPIokRZE8z62ugpMb7XDkgGJNZAaGs2UVjV4Qz7sy
	 Nnzb73MhMP4XXf90LWl1Bg0pD79AJzIc7WzkW2OQ+KjmDlp7FfDr/51aicJhPADR6fixE
	 SUSZ42mETtldB4r3FuS2aeAq4XsOgVJUSBDrzsgHB8cPFJGOXhyKDsCtdEMld4SwDJzxU
	 1rX6WEtDevH8lVw+06HLw3kAT45SVMSprO04o4Fsjd8/CmdryXd719JlEwLDA1PtkkyT6
	 BD75WudjKsCNpOAmMERrzHjrbngbd/RvmO9tDrbt3Bnqsp1VZlRAeKCq+ByHVpgCm2w==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=povKuKh2;
	dmarc=pass header.from=gmail.com;
	spf=pass smtp.mailfrom=gmail.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 2A42F606A68D;
	Thu, 26 Nov 2026 02:07:49 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=povKuKh2
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.169
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1795658868;
	b=n0V3uBCvYt4ozmAoRE+WrqCKxhpr+1zntzxGsrEBO09rr50mPPyyRMPSDCjySjw6mmlJ
	 sDm5atPQYWjvjYp7oBT+UXL9sQh23AxQZLIoLp+GDS9XSxdtcS5mP5Qg6J19PLfZWGZMF
	 kmhVpO0Nm+PwybxUqMSNs4BaYQddCDsPBadmABCV/cOrU22WCQiPXBqkNhh9kNm6egqRG
	 xYmv+C8M2eHvZmQk0GmZVxuwhjs9TClAsI18PL8iYgsWUpJ6/0OOXrBn4dHuqDpfdt8DH
	 u3erXqUxjbHl/GkXeVcJC1G816ol/MIy1R10w8XffnRsty7Z8jtCxzO/MEtuKeRffig==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795658868;
	h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature:
	 X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received:
	 From:To:Cc:Subject:Date:Message-ID:MIME-Version:
	 Content-Transfer-Encoding;
	bh=WkAms7KA42Baas5n6wZEyMjVEZ8N2pj3aqq6PvvQjdE=;
	b=M9APyLQaeb5EmjR8qwuvupVxVSoylFUHKNBl1IJlXsKwB6ljWsDjIzssgzrZnUg63oCL
	 nVXOavMui8xERS3ZnJnC60MdwiY0NE77LnHFj47aAW+yHoIX2PpWkv7MPGK6aGr9HKSW2
	 ay6qGfa8ipE9SkTxXhStreUVG6/KERUr9SeWvOI2v3OzfcYhAk5EVSNRtxD7rPgS9OBYB
	 Z4nxBijn3YvHl6CJ8EM07Lkxicu4PIXnYMlHmd5HL/+R5rfRy43eIzr9P2s2+o8H4MNeC
	 5hpiQvfVSOc5wM+oiYp6rW15UgnFcEi9pG6qjX1oTPHfIf/KyuAOw1h8tlE2gpDArvg==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dmarc=pass header.from=gmail.com;
	spf=pass smtp.mailfrom=gmail.com;
	dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=povKuKh2;
	arc=none smtp.remote-ip=209.85.214.169
X-Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169])
	(using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256)
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id EA425606A32D
	for <netdev@vger.kernel.org>; Thu, 26 Nov 2026 02:07:47 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org EA425606A32D
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com
X-Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-2f0464dd6b6so40285395ad.1
        for <netdev@vger.kernel.org>; Wed, 25 Nov 2026 18:07:47 -0800 (PST)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20260707; t=1795658867; x=1796263667; 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=WkAms7KA42Baas5n6wZEyMjVEZ8N2pj3aqq6PvvQjdE=;
        b=povKuKh2H6D2wLFvEqnB2T8NXUi566C8IhNc2LNF7XF5xNAcQPsYSsSfd0VkOp7Jxs
         WROFL3uX6OL9vmEUoSUtnvvhcQDFI4rQqOAnY0/ecwRlpYKolkNepVHQuINeEoi9jUHJ
         a6X64fiydSLPsroytYNKtNYqrQZ5fa6vCUDHo6M3UiRC3lCWWPfZlMXgnQ+UJmIJHE20
         DaZPYoZvUQZSKaaguAWJwabMv5QSumxhDxkkK7aYxS7nO6K9RXKulGx6a03mb4lfzHMQ
         RFmV5ddqWtI0V/zGXPwRxcLpILZHiimz0E1WBC5f8IbWWJXiVn+MG4rkAUc0jWr3qrHw
         tQOw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20260707; t=1795658867; x=1796263667;
        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=WkAms7KA42Baas5n6wZEyMjVEZ8N2pj3aqq6PvvQjdE=;
        b=b0fNWxg4btENM1rAFS9V3dGmEkdGcZI5IqcgSvWPz8erLMSDb1rckOcAIPcF91Xyk2
         yn9hltmzW5UoTOGgHE1T18B6V/HajDdWb84QDj77gSB+KTeeipphKSrrdTNLoi+aC9OM
         c+1L3v/ZV9p/obe5JQPMlDvJ40KhmW1k4kvW62/WTu+30DqDs07ize/vLaecGqDhW4Ej
         5W2hud4/5sTNEvZRycs5YyI3c9GRXqE0Ff5QGXpmux152raepiff6KAFRSv6+ygk9Esv
         M0EC/ItvcXNmELGywttImZQjZLihjYrWMzwebSPDUB8Rj8lru3gxx6j4IMtKCrIv3riq
         EJ1w==
X-Forwarded-Encrypted: i=1; AL+hErGY0E9pZBTESZVZNLKmoVWKgiLgWM+AieTllafFxJSaqF/vLVklI1v+VWyU4o2CrsjGw7nhtlY=@vger.kernel.org
X-Gm-Message-State: APuU4Z8ljPHNKKFrPUnSTSOnuGSvERcq7bZeavJGMR04/312heXvylVx
	rxx9BiqssbvHDuZXTHK/E65xiegceSZizA70ceqJmOkmOmNwDoTk+Pvy
X-Gm-Gg: AZFdTZeM2jtCF5ppw50it90qICcEw3JiIrFgITafvEPj9B5mIjbPEdG39jHgIp0x1GJ
	dKtZB/hrraClodbXy2eUaVQ8KPaL+3Ei6RHqXSiI7UTVXNA13pNb+FWP2kvYxthzc5LRQshD2kf
	MAla5uwiu8JPUtREj2oF/2cp4vjcrc1o8jrBU1XHXz7kYRKWfKgLST9K2kwb9/Sm3wae6xMOOZU
	D2ys0AlQi6L5FbBOuzCUkAd+OuoHtfLv0VjYm32SftjLczf5lREPlx9m3ZUUxKoOoOtPXJ8aJB5
	Yiex/Ehhg1toS3GSVNtgWJWaM4EfNZUYPya8CTKQxClY7mlZTQmU9SbfZ8S0s6WczxVn3P0GYpx
	bTD6HRDVYF1gY4DsGzO/pAc7E9TroCn0qwYN9gJ7fiv/cGfzSSd7ADV6N89MnsbpUoJuSZTzEZF
	DgdzmmKkBQx3P4d+NmN7eXnt7GHEtrhdEvAaaLozdq+rrMmP7vyoBhy/rlPKi4TIVMEJZJrljUd
	6OWGoOVAwPMfWVpEGlrQCXWZde8eeghdrGLIZfrNqssxcKuyoPJZuYYGToD6o/HejudL8TWwrsK
	e7JRlBbj7WvD/Xf4ZxQyCTgk2WvESMbjIjDb3+tptk7uA5qGQqh4lYcdpX65xziWKKhufgAxNdU
	Va7VDltLc+lbk
X-Received: by 2002:a17:903:2f87:b0:2ef:e285:7085 with SMTP id d9443c01a7336-2f03f1b1946mr192913505ad.44.1795658867206;
        Wed, 25 Nov 2026 18:07:47 -0800 (PST)
X-Received: from d20be28ee783.tail196be.ts.net (vldb.skku.ac.kr. [115.145.173.180])
        by smtp.gmail.com with ESMTPSA id d9443c01a7336-2f03f0c6e50sm95550735ad.13.2026.11.25.18.07.44
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Wed, 25 Nov 2026 18:07:46 -0800 (PST)
X-Cc: Stanislav Fomichev <sdf@fomichev.me>,
	Björn Töpel <bjorn@kernel.org>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>,
	Paolo Abeni <pabeni@redhat.com>,
	Willem de Bruijn <willemb@google.com>,
	Simon Horman <horms@kernel.org>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Jesper Dangaard Brouer <hawk@kernel.org>,
	John Fastabend <john.fastabend@gmail.com>,
	netdev@vger.kernel.org,
	bpf@vger.kernel.org
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