🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
5 messages
5 total messages Started by Kyuwon Cho Thu, 26 Nov 2026 02:07
[PATCH net v3] xsk: keep the charged page count when pinning a umem fails
#22847
Author: 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
Re: [PATCH net v3] xsk: keep the charged page count when pinning a umem fails
#22916
Author: Björn Töpel
Date: Thu, 26 Nov 2026 06:33
23 lines
9006 bytes
Kyuwon Cho <kyuwoncho18@gmail.com> writes:

> 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>

Reviewed-by: Björn Töpel <bjorn@kernel.org>
Re: [PATCH net v3] xsk: keep the charged page count when pinning a umem fails
#23020
Author: Maciej Fijalkowski
Date: Thu, 26 Nov 2026 10:29
141 lines
24839 bytes
On Thu, Nov 26, 2026 at 11:07:42AM +0900, Kyuwon Cho wrote:
> 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

Thanks for the follow-up!
Acked-by: Maciej Fijalkowski <maciej.fijalkowski@intel.com>

Regarding removing Bjorn's tag, please have some common sense here, I know
AI might be telling this to you but this change was nothing major.

> 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
> 
Re: [PATCH net v3] xsk: keep the charged page count when pinning a umem fails
#23294
Author: Jason Xing
Date: Fri, 27 Nov 2026 02:30
27 lines
13198 bytes
On Thu, Nov 26, 2026 at 10:08 AM Kyuwon Cho <kyuwoncho18@gmail.com> wrote:
>
> 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>

LGTM, thanks!

Reviewed-by: Jason Xing <kerneljasonxing@gmail.com>
Re: [PATCH net v3] xsk: keep the charged page count when pinning a umem fails
#25568
Author: patchwork-bot+netdevbpf
Date: Wed, 02 Dec 2026 02:40
28 lines
9104 bytes
Hello:

This patch was applied to netdev/net.git (main)
by Willem de Bruijn <willemb@google.com>:

On Thu, 26 Nov 2026 11:07:42 +0900 you wrote:
> 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.
> 
> [...]

Here is the summary with links:
  - [net,v3] xsk: keep the charged page count when pinning a umem fails
    https://git.kernel.org/netdev/net/c/1bff9e356e99

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads