Skip to contentSkip to conversation

9p: Use kvmalloc for message buffers

PATCH Superseded Pierre Barre 4 participants 17 messages Jul 30, 2025 – Nov 3, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 9p: Use kvmalloc for message buffers

While developing a 9P server (https://github.com/Barre/ZeroFS) and testing it under high-load, I was running into allocation failures. The failures occur even with plenty of free memory available because kmalloc requires contiguous physical memory.

This results in errors like:
ls: page allocation failure: order:7, mode:0x40c40(GFP_NOFS|__GFP_COMP)

---
 net/9p/client.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/9p/client.c +2 -2
index 5c1ca57ccd28..f82b5674057c 100644
@@ -230,7 +230,7 @@ static int p9_fcall_init(struct p9_client *c, struct p9_fcall *fc,
 		fc->sdata = kmem_cache_alloc(c->fcall_cache, GFP_NOFS);
 		fc->cache = c->fcall_cache;
 	} else {
-		fc->sdata = kmalloc(alloc_msize, GFP_NOFS);
+		fc->sdata = kvmalloc(alloc_msize, GFP_NOFS);
 		fc->cache = NULL;
 	}
 	if (!fc->sdata)
@@ -252,7 +252,7 @@ void p9_fcall_fini(struct p9_fcall *fc)
 	if (fc->cache)
 		kmem_cache_free(fc->cache, fc->sdata);
 	else
-		kfree(fc->sdata);
+		kvfree(fc->sdata);
 }
 EXPORT_SYMBOL(p9_fcall_fini);
 
Signature · 1 line
2.39.5 (Apple Git-154)
#1 lore raw
Quoted 4 lines · CEST Pierre Barre

While developing a 9P server (https://github.com/Barre/ZeroFS) and testing it under high-load, I was running into allocation failures. The failures occur even with plenty of free memory available because kmalloc requires contiguous physical memory.

This results in errors like:
ls: page allocation failure: order:7, mode:0x40c40(GFP_NOFS|__GFP_COMP)

What was msize?

Quoted 10 lines
---
 net/9p/client.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/9p/client.c +0 -0
index 5c1ca57ccd28..f82b5674057c 100644

net/9p/client.c static int p9_fcall_init(struct p9_client *c, struct p9_fcall *fc, · line 233

Quoted 5 lines
net/9p/client.c +1 -1
@@ -230,7 +230,7 @@ static int p9_fcall_init(struct p9_client *c, struct p9_fcall *fc,
 		fc->sdata = kmem_cache_alloc(c->fcall_cache, GFP_NOFS);
 		fc->cache = c->fcall_cache;
 	} else {
-		fc->sdata = kmalloc(alloc_msize, GFP_NOFS);
+		fc->sdata = kvmalloc(alloc_msize, GFP_NOFS);

That would work with certain transports like fd I guess, but not via virtio-pci transport for instance, since PCI-DMA requires physical pages. Same applies to Xen transport I guess.

Quoted 13 lines
net/9p/client.c +1 -1
 		fc->cache = NULL;
 	}
 	if (!fc->sdata)
@@ -252,7 +252,7 @@ void p9_fcall_fini(struct p9_fcall *fc)
 	if (fc->cache)
 		kmem_cache_free(fc->cache, fc->sdata);
 	else
-		kfree(fc->sdata);
+		kvfree(fc->sdata);
 }
 EXPORT_SYMBOL(p9_fcall_fini);
 
#2 lore raw

Thank you for your email.

Quoted 1 line

What was msize?

I was mounting the filesystem using:

trans=tcp,port=5564,version=9p2000.L,msize=1048576,cache=mmap,access=user

Quoted 3 lines

That would work with certain transports like fd I guess, but not via virtio-pci transport for instance, since PCI-DMA requires physical pages. Same applies to Xen transport I guess.

Would it be acceptable to add a mount option (like nocontig or loosealloc?) that enables kvmalloc?

Best,
Pierre

Quoted 41 lines · Christian Schoenebeck
Quoted 4 lines · CEST Pierre Barre

While developing a 9P server (https://github.com/Barre/ZeroFS) and testing it under high-load, I was running into allocation failures. The failures occur even with plenty of free memory available because kmalloc requires contiguous physical memory.

This results in errors like:
ls: page allocation failure: order:7, mode:0x40c40(GFP_NOFS|__GFP_COMP)

What was msize?

Quoted 10 lines
---
 net/9p/client.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/9p/client.c +0 -0
index 5c1ca57ccd28..f82b5674057c 100644

net/9p/client.c static int p9_fcall_init(struct p9_client *c, struct p9_fcall *fc, · line 233

Quoted 5 lines
net/9p/client.c +1 -1
@@ -230,7 +230,7 @@ static int p9_fcall_init(struct p9_client *c, struct p9_fcall *fc,
 		fc->sdata = kmem_cache_alloc(c->fcall_cache, GFP_NOFS);
 		fc->cache = c->fcall_cache;
 	} else {
-		fc->sdata = kmalloc(alloc_msize, GFP_NOFS);
+		fc->sdata = kvmalloc(alloc_msize, GFP_NOFS);

That would work with certain transports like fd I guess, but not via virtio-pci transport for instance, since PCI-DMA requires physical pages. Same applies to Xen transport I guess.

Quoted 13 lines
net/9p/client.c +1 -1
 		fc->cache = NULL;
 	}
 	if (!fc->sdata)
@@ -252,7 +252,7 @@ void p9_fcall_fini(struct p9_fcall *fc)
 	if (fc->cache)
 		kmem_cache_free(fc->cache, fc->sdata);
 	else
-		kfree(fc->sdata);
+		kvfree(fc->sdata);
 }
 EXPORT_SYMBOL(p9_fcall_fini);
 
#3 lore raw
Quoted 7 lines · CEST Pierre Barre

Thank you for your email.

Quoted 1 line

What was msize?

I was mounting the filesystem using:

trans=tcp,port=5564,version=9p2000.L,msize=1048576,cache=mmap,access=user

Yeah, that explains both why you were triggering this issue, as 1M msize will likely cause kmalloc() failures under heavy load, and why your patch was working for you due to chosen tcp transport.

Quoted 5 lines
Quoted 3 lines

That would work with certain transports like fd I guess, but not via virtio-pci transport for instance, since PCI-DMA requires physical pages. Same applies to Xen transport I guess.

Would it be acceptable to add a mount option (like nocontig or loosealloc?) that enables kvmalloc?

Dominique's call obviously, I'm just giving my few cents here. To me it would make sense to fix the root cause instead of shorting a symptom:

Right now 9p filesystem code (under fs/9p) requires a linear buffer, whereas some 9p transports (under net/9p) require physical pages, and the latter is not going to change.

One solution therefore might be changing fs/9p code to work on a scatter/ gather list instead of a simple linear buffer. But I guess that would be too much work.

So a more reasonable solution instead might be using kvmalloc(), as suggested by you, and adjusting the individual transports such that they translate a virtual memory address to a list of physical addresses via e.g. vmalloc_to_page() if needed.

/Christian

#4 lore raw

As a middle-ground, would it be acceptable to add a transport capability flag indicating whether the transport requires contiguous memory for DMA?

1. Add a P9_TRANS_REQUIRES_CONTIGUOUS flag to struct p9_trans_module
2. Set this flag for virtio, xen, and rdma transports
3. Modify p9_fcall_init to check the flag:

     if (c->trans_mod->caps & P9_TRANS_REQUIRES_CONTIGUOUS)
        fc->sdata = kmalloc(alloc_msize, GFP_NOFS);
     else
        fc->sdata = kvmalloc(alloc_msize, GFP_NOFS);

I can prepare a patch implementing this approach if you agree.

Best,
Pierre

Quoted 36 lines · Christian Schoenebeck
Quoted 7 lines · CEST Pierre Barre

Thank you for your email.

Quoted 1 line

What was msize?

I was mounting the filesystem using:

trans=tcp,port=5564,version=9p2000.L,msize=1048576,cache=mmap,access=user

Yeah, that explains both why you were triggering this issue, as 1M msize will likely cause kmalloc() failures under heavy load, and why your patch was working for you due to chosen tcp transport.

Quoted 5 lines
Quoted 3 lines

That would work with certain transports like fd I guess, but not via virtio-pci transport for instance, since PCI-DMA requires physical pages. Same applies to Xen transport I guess.

Would it be acceptable to add a mount option (like nocontig or loosealloc?) that enables kvmalloc?

Dominique's call obviously, I'm just giving my few cents here. To me it would make sense to fix the root cause instead of shorting a symptom:

Right now 9p filesystem code (under fs/9p) requires a linear buffer, whereas some 9p transports (under net/9p) require physical pages, and the latter is not going to change.

One solution therefore might be changing fs/9p code to work on a scatter/ gather list instead of a simple linear buffer. But I guess that would be too much work.

So a more reasonable solution instead might be using kvmalloc(), as suggested by you, and adjusting the individual transports such that they translate a virtual memory address to a list of physical addresses via e.g. vmalloc_to_page() if needed.

/Christian

#5 lore raw

(Added Willy in To, if you have time to advise on what's appropriate wrt. memory allocation here to use either as a contiguous virtual memory buffer or a scatterlist for various kind of dma used by transports that'd be appreciated)

First - thanks for starting this thread, large contiguous allocations has been a problem reported regularly in the past, and no-one has had the energy to address it yet, but it's definitely something worth tackling.

Pierre Barre wrote on Wed, Jul 30, 2025 at 10:16:13PM +0200:

Quoted 3 lines

As a middle-ground, would it be acceptable to add a transport capability flag indicating whether the transport requires contiguous memory for DMA?

I think such a flag will be needed even if we do what Christian suggested:

Quoted 4 lines
Quoted 4 lines

So a more reasonable solution instead might be using kvmalloc(), as suggested by you, and adjusting the individual transports such that they translate a virtual memory address to a list of physical addresses via e.g. vmalloc_to_page() if needed.

Start with a requires contiguous flag set for all transports except trans_fd (I'm not sure about usb, but I'm pretty sure all the rest need it); then each transport can figure out how to get a scatterlist or something they need from the allocation, so we don't need to have a big bang that breaks everything at the same time.

I'm sure virtio's implem will come soon enough, but I don't see anyone working on RDMA or xen so fast.

Quoted 2 lines

1. Add a P9_TRANS_REQUIRES_CONTIGUOUS flag to struct p9_trans_module
2. Set this flag for virtio, xen, and rdma transports

This is a nit but I'd rather the flag go the other way around, e.g. no flag means requires contiguous and it's only set after confirming the transport works
(I just looked at usb and struct usb_request makes me think some drivers use dma, so, yeah..)

We can always turn it around later if the majority of transports handle it.

Quoted 5 lines

3. Modify p9_fcall_init to check the flag:

     if (c->trans_mod->caps & P9_TRANS_REQUIRES_CONTIGUOUS)
        fc->sdata = kmalloc(alloc_msize, GFP_NOFS);
     else
        fc->sdata = kvmalloc(alloc_msize, GFP_NOFS);

I'm also curious if there's something more appropriate than kvmalloc wrt using either as a contiguous virtual memory buffer or a scatterlist, but I think this is fine for now unless someone knows of something more appropriate.

Thanks,

Signature · 1 line
Dominique Martinet | Asmadeus
#6 lore raw
Quoted 4 lines

First - thanks for starting this thread, large contiguous allocations has been a problem reported regularly in the past, and no-one has had the energy to address it yet, but it's definitely something worth tackling.

Glad that I didn't just create noise. I implemented 9P in addition to NFS in my server because fsyncing on NFS didn't give me what I wanted (fsync doesn't call commit when write is communicated to be "durable" by the server, but that's a story for an other day...).

I am very grateful that this client implementation exists.

Quoted 60 lines · On Thu, Jul 31, 2025, at 00:07, [email protected] wrote:

(Added Willy in To, if you have time to advise on what's appropriate wrt. memory allocation here to use either as a contiguous virtual memory buffer or a scatterlist for various kind of dma used by transports that'd be appreciated)

First - thanks for starting this thread, large contiguous allocations has been a problem reported regularly in the past, and no-one has had the energy to address it yet, but it's definitely something worth tackling.

Pierre Barre wrote on Wed, Jul 30, 2025 at 10:16:13PM +0200:

Quoted 3 lines

As a middle-ground, would it be acceptable to add a transport capability flag indicating whether the transport requires contiguous memory for DMA?

I think such a flag will be needed even if we do what Christian suggested:

Quoted 4 lines
Quoted 4 lines

So a more reasonable solution instead might be using kvmalloc(), as suggested by you, and adjusting the individual transports such that they translate a virtual memory address to a list of physical addresses via e.g. vmalloc_to_page() if needed.

Start with a requires contiguous flag set for all transports except trans_fd (I'm not sure about usb, but I'm pretty sure all the rest need it); then each transport can figure out how to get a scatterlist or something they need from the allocation, so we don't need to have a big bang that breaks everything at the same time.

I'm sure virtio's implem will come soon enough, but I don't see anyone working on RDMA or xen so fast.

Quoted 2 lines

1. Add a P9_TRANS_REQUIRES_CONTIGUOUS flag to struct p9_trans_module
2. Set this flag for virtio, xen, and rdma transports

This is a nit but I'd rather the flag go the other way around, e.g. no flag means requires contiguous and it's only set after confirming the transport works
(I just looked at usb and struct usb_request makes me think some drivers use dma, so, yeah..)

We can always turn it around later if the majority of transports handle it.

Quoted 5 lines

3. Modify p9_fcall_init to check the flag:

     if (c->trans_mod->caps & P9_TRANS_REQUIRES_CONTIGUOUS)
        fc->sdata = kmalloc(alloc_msize, GFP_NOFS);
     else
        fc->sdata = kvmalloc(alloc_msize, GFP_NOFS);

I'm also curious if there's something more appropriate than kvmalloc wrt using either as a contiguous virtual memory buffer or a scatterlist, but I think this is fine for now unless someone knows of something more appropriate.

Thanks,

Signature · 1 line
Dominique Martinet | Asmadeus
#7 lore raw

Hi Everyone,

If I submit a patch based on what has been discussed above, is it likely to be accepted? Unfortunately, in my current setup, I am encountering this issue quite frequently, and users of my servers are having a hard time making sense of the error.

Thank you.

Best,
Pierre

Quoted 73 lines · Pierre Barre
Quoted 4 lines

First - thanks for starting this thread, large contiguous allocations has been a problem reported regularly in the past, and no-one has had the energy to address it yet, but it's definitely something worth tackling.

Glad that I didn't just create noise. I implemented 9P in addition to NFS in my server because fsyncing on NFS didn't give me what I wanted (fsync doesn't call commit when write is communicated to be "durable" by the server, but that's a story for an other day...).

I am very grateful that this client implementation exists.

Quoted 60 lines · On Thu, Jul 31, 2025, at 00:07, [email protected] wrote:

(Added Willy in To, if you have time to advise on what's appropriate wrt. memory allocation here to use either as a contiguous virtual memory buffer or a scatterlist for various kind of dma used by transports that'd be appreciated)

First - thanks for starting this thread, large contiguous allocations has been a problem reported regularly in the past, and no-one has had the energy to address it yet, but it's definitely something worth tackling.

Pierre Barre wrote on Wed, Jul 30, 2025 at 10:16:13PM +0200:

Quoted 3 lines

As a middle-ground, would it be acceptable to add a transport capability flag indicating whether the transport requires contiguous memory for DMA?

I think such a flag will be needed even if we do what Christian suggested:

Quoted 4 lines
Quoted 4 lines

So a more reasonable solution instead might be using kvmalloc(), as suggested by you, and adjusting the individual transports such that they translate a virtual memory address to a list of physical addresses via e.g. vmalloc_to_page() if needed.

Start with a requires contiguous flag set for all transports except trans_fd (I'm not sure about usb, but I'm pretty sure all the rest need it); then each transport can figure out how to get a scatterlist or something they need from the allocation, so we don't need to have a big bang that breaks everything at the same time.

I'm sure virtio's implem will come soon enough, but I don't see anyone working on RDMA or xen so fast.

Quoted 2 lines

1. Add a P9_TRANS_REQUIRES_CONTIGUOUS flag to struct p9_trans_module
2. Set this flag for virtio, xen, and rdma transports

This is a nit but I'd rather the flag go the other way around, e.g. no flag means requires contiguous and it's only set after confirming the transport works
(I just looked at usb and struct usb_request makes me think some drivers use dma, so, yeah..)

We can always turn it around later if the majority of transports handle it.

Quoted 5 lines

3. Modify p9_fcall_init to check the flag:

     if (c->trans_mod->caps & P9_TRANS_REQUIRES_CONTIGUOUS)
        fc->sdata = kmalloc(alloc_msize, GFP_NOFS);
     else
        fc->sdata = kvmalloc(alloc_msize, GFP_NOFS);

I'm also curious if there's something more appropriate than kvmalloc wrt using either as a contiguous virtual memory buffer or a scatterlist, but I think this is fine for now unless someone knows of something more appropriate.

Thanks,

Signature · 1 line
Dominique Martinet | Asmadeus
#8 lore raw

Pierre Barre wrote on Wed, Aug 06, 2025 at 05:50:42PM +0200:

Quoted 4 lines

If I submit a patch based on what has been discussed above, is it likely to be accepted? Unfortunately, in my current setup, I am encountering this issue quite frequently, and users of my servers are having a hard time making sense of the error.

Yes, sorry it wasn't clear.

I still have no idea what's the "best" allocation method that we'll be able to use as either a vmalloc buffer or split into a scatterlist, but there's little point in worrying too much about it, so please go ahead.

If it's restricted to trans_fd and there's a chance we can make use of it with (at least) virtio later I think everyone will be happy :)

Signature · 1 line
Dominique
#9 lore raw
Quoted 7 lines · CEST Dominique Martinet

Pierre Barre wrote on Wed, Aug 06, 2025 at 05:50:42PM +0200:

Quoted 4 lines

If I submit a patch based on what has been discussed above, is it likely to be accepted? Unfortunately, in my current setup, I am encountering this issue quite frequently, and users of my servers are having a hard time making sense of the error.

Quoted 8 lines

Yes, sorry it wasn't clear.

I still have no idea what's the "best" allocation method that we'll be able to use as either a vmalloc buffer or split into a scatterlist, but there's little point in worrying too much about it, so please go ahead.

If it's restricted to trans_fd and there's a chance we can make use of it with (at least) virtio later I think everyone will be happy :)

Yes, sounds like a viable plan.

Pierre, one more thing to note: kmem_cache_alloc() might still fail though. So maybe it would make sense to add a separate patch that would check the result of kmem_cache_alloc() and if it fails, falling back to your kvmalloc() call (if enabled by the discussed transport mechanism of course).

/Christian

#10 lore raw

PATCH v2 1/1 9p: Use kvmalloc for message buffers on supported transports

While developing a 9P server (https://github.com/Barre/ZeroFS) and testing it under high-load, I was running into allocation failures. The failures occur even with plenty of free memory available because kmalloc requires contiguous physical memory.

This results in errors like:
ls: page allocation failure: order:7, mode:0x40c40(GFP_NOFS|__GFP_COMP)

This patch introduces a transport capability flag (supports_vmalloc) that indicates whether a transport can work with vmalloc'd buffers (non-physically contiguous memory). Transports requiring DMA should leave this flag as false.

The fd-based transports (tcp, unix, fd) set this flag to true, and p9_fcall_init will use kvmalloc instead of kmalloc for these transports. This allows the allocator to fall back to vmalloc when contiguous physical memory is not available.

Additionally, if kmem_cache_alloc fails, the code falls back to kvmalloc for transports that support it.

---

 include/net/9p/transport.h |  4 ++++
 net/9p/client.c            | 11 +++++++++--
 net/9p/trans_fd.c          |  3 +++
 3 files changed, 16 insertions(+), 2 deletions(-)
include/net/9p/transport.h +4 -0
index 766ec07c9599..f0981515148d 100644
@@ -24,6 +24,9 @@
  *                   we're less flexible when choosing the response message
  *                   size in this case
  * @def: set if this transport should be considered the default
+ * @supports_vmalloc: set if this transport can work with vmalloc'd buffers
+ *                    (non-physically contiguous memory). Transports requiring
+ *                    DMA should leave this as false.
  * @create: member function to create a new connection on this transport
  * @close: member function to discard a connection on this transport
  * @request: member function to issue a request to the transport
@@ -44,6 +47,7 @@ struct p9_trans_module {
 	int maxsize;		/* max message size of transport */
 	bool pooled_rbuffers;
 	int def;		/* this transport should be default */
+	bool supports_vmalloc;	/* can work with vmalloc'd buffers */
 	struct module *owner;
 	int (*create)(struct p9_client *client,
 		      const char *devname, char *args);
net/9p/client.c +9 -2
index 5c1ca57ccd28..2a4884c880c1 100644
@@ -229,8 +229,15 @@ static int p9_fcall_init(struct p9_client *c, struct p9_fcall *fc,
 	if (likely(c->fcall_cache) && alloc_msize == c->msize) {
 		fc->sdata = kmem_cache_alloc(c->fcall_cache, GFP_NOFS);
 		fc->cache = c->fcall_cache;
+		if (!fc->sdata && c->trans_mod->supports_vmalloc) {
+			fc->sdata = kvmalloc(alloc_msize, GFP_NOFS);
+			fc->cache = NULL;
+		}
 	} else {
-		fc->sdata = kmalloc(alloc_msize, GFP_NOFS);
+		if (c->trans_mod->supports_vmalloc)
+			fc->sdata = kvmalloc(alloc_msize, GFP_NOFS);
+		else
+			fc->sdata = kmalloc(alloc_msize, GFP_NOFS);
 		fc->cache = NULL;
 	}
 	if (!fc->sdata)
@@ -252,7 +259,7 @@ void p9_fcall_fini(struct p9_fcall *fc)
 	if (fc->cache)
 		kmem_cache_free(fc->cache, fc->sdata);
 	else
-		kfree(fc->sdata);
+		kvfree(fc->sdata);
 }
 EXPORT_SYMBOL(p9_fcall_fini);
 
net/9p/trans_fd.c +3 -0
index a516745f732f..e7334033eba5 100644
@@ -1101,6 +1101,7 @@ static struct p9_trans_module p9_tcp_trans = {
 	.maxsize = MAX_SOCK_BUF,
 	.pooled_rbuffers = false,
 	.def = 0,
+	.supports_vmalloc = true,
 	.create = p9_fd_create_tcp,
 	.close = p9_fd_close,
 	.request = p9_fd_request,
@@ -1115,6 +1116,7 @@ static struct p9_trans_module p9_unix_trans = {
 	.name = "unix",
 	.maxsize = MAX_SOCK_BUF,
 	.def = 0,
+	.supports_vmalloc = true,
 	.create = p9_fd_create_unix,
 	.close = p9_fd_close,
 	.request = p9_fd_request,
@@ -1129,6 +1131,7 @@ static struct p9_trans_module p9_fd_trans = {
 	.name = "fd",
 	.maxsize = MAX_SOCK_BUF,
 	.def = 0,
+	.supports_vmalloc = true,
 	.create = p9_fd_create,
 	.close = p9_fd_close,
 	.request = p9_fd_request,
Signature · 1 line
2.39.5 (Apple Git-154)
Quoted 25 lines · Christian Schoenebeck
Quoted 7 lines · CEST Dominique Martinet

Pierre Barre wrote on Wed, Aug 06, 2025 at 05:50:42PM +0200:

Quoted 4 lines

If I submit a patch based on what has been discussed above, is it likely to be accepted? Unfortunately, in my current setup, I am encountering this issue quite frequently, and users of my servers are having a hard time making sense of the error.

Quoted 8 lines

Yes, sorry it wasn't clear.

I still have no idea what's the "best" allocation method that we'll be able to use as either a vmalloc buffer or split into a scatterlist, but there's little point in worrying too much about it, so please go ahead.

If it's restricted to trans_fd and there's a chance we can make use of it with (at least) virtio later I think everyone will be happy :)

Yes, sounds like a viable plan.

Pierre, one more thing to note: kmem_cache_alloc() might still fail though. So maybe it would make sense to add a separate patch that would check the result of kmem_cache_alloc() and if it fails, falling back to your kvmalloc() call (if enabled by the discussed transport mechanism of course).

/Christian

#11 lore raw

Re: [PATCH v2] 9p: Use kvmalloc for message buffers on supported transports

Pierre Barre wrote on Thu, Oct 16, 2025 at 09:01:56AM +0200:

Quoted 13 lines

While developing a 9P server (https://github.com/Barre/ZeroFS) and testing it under high-load, I was running into allocation failures. The failures occur even with plenty of free memory available because kmalloc requires contiguous physical memory.

This results in errors like:
ls: page allocation failure: order:7, mode:0x40c40(GFP_NOFS|__GFP_COMP)

This patch introduces a transport capability flag (supports_vmalloc) that indicates whether a transport can work with vmalloc'd buffers (non-physically contiguous memory). Transports requiring DMA should leave this flag as false.

Quoted 7 lines

The fd-based transports (tcp, unix, fd) set this flag to true, and p9_fcall_init will use kvmalloc instead of kmalloc for these transports. This allows the allocator to fall back to vmalloc when contiguous physical memory is not available.

Additionally, if kmem_cache_alloc fails, the code falls back to kvmalloc for transports that support it.

Looks good to me, I'll pick this up for -next when time allows and it'll get in 6.19 in a few months

Now to get someone to make this work with virtio... :)

net/9p/client.c void p9_fcall_fini(struct p9_fcall *fc) · line 262

Quoted 6 lines
net/9p/client.c +1 -1
@@ -252,7 +259,7 @@ void p9_fcall_fini(struct p9_fcall *fc)
 	if (fc->cache)
 		kmem_cache_free(fc->cache, fc->sdata);
 	else
-		kfree(fc->sdata);
+		kvfree(fc->sdata);

(Oh, it's ok to kvfree() something allocated with either kmalloc or kvmalloc, neat.)

Signature · 1 line
Dominique Martinet | Asmadeus
reviewer Reviewed-by on v2 1/1
#12 lore raw

Re: [PATCH v2] 9p: Use kvmalloc for message buffers on supported transports

In reply to Pierre Barre
Quoted 102 lines · CEST Pierre Barre

While developing a 9P server (https://github.com/Barre/ZeroFS) and testing it under high-load, I was running into allocation failures. The failures occur even with plenty of free memory available because kmalloc requires contiguous physical memory.

This results in errors like:
ls: page allocation failure: order:7, mode:0x40c40(GFP_NOFS|__GFP_COMP)

This patch introduces a transport capability flag (supports_vmalloc) that indicates whether a transport can work with vmalloc'd buffers (non-physically contiguous memory). Transports requiring DMA should leave this flag as false.

The fd-based transports (tcp, unix, fd) set this flag to true, and p9_fcall_init will use kvmalloc instead of kmalloc for these transports. This allows the allocator to fall back to vmalloc when contiguous physical memory is not available.

Additionally, if kmem_cache_alloc fails, the code falls back to kvmalloc for transports that support it.

---

 include/net/9p/transport.h |  4 ++++
 net/9p/client.c            | 11 +++++++++--
 net/9p/trans_fd.c          |  3 +++
 3 files changed, 16 insertions(+), 2 deletions(-)
include/net/9p/transport.h +2 -0
index 766ec07c9599..f0981515148d 100644
@@ -24,6 +24,9 @@
  *                   we're less flexible when choosing the response message
 *                   size in this case
  * @def: set if this transport should be considered the default
+ * @supports_vmalloc: set if this transport can work with vmalloc'd buffers
+ *                    (non-physically contiguous memory). Transports

requiring + * DMA should leave this as false.
* @create: member function to create a new connection on this transport
* @close: member function to discard a connection on this transport
* @request: member function to issue a request to the transport

include/net/9p/transport.h +1 -0
@@ -44,6 +47,7 @@ struct p9_trans_module {
 	int maxsize;		/* max message size of transport */
 	bool pooled_rbuffers;
 	int def;		/* this transport should be default */
+	bool supports_vmalloc;	/* can work with vmalloc'd buffers */
 	struct module *owner;
 	int (*create)(struct p9_client *client,
 		      const char *devname, char *args);
net/9p/client.c +9 -2
index 5c1ca57ccd28..2a4884c880c1 100644
@@ -229,8 +229,15 @@ static int p9_fcall_init(struct p9_client *c, struct
 p9_fcall *fc, if (likely(c->fcall_cache) && alloc_msize == c->msize) {
 		fc->sdata = kmem_cache_alloc(c->fcall_cache, GFP_NOFS);
 		fc->cache = c->fcall_cache;
+		if (!fc->sdata && c->trans_mod->supports_vmalloc) {
+			fc->sdata = kvmalloc(alloc_msize, GFP_NOFS);
+			fc->cache = NULL;
+		}
 	} else {
-		fc->sdata = kmalloc(alloc_msize, GFP_NOFS);
+		if (c->trans_mod->supports_vmalloc)
+			fc->sdata = kvmalloc(alloc_msize, GFP_NOFS);
+		else
+			fc->sdata = kmalloc(alloc_msize, GFP_NOFS);
 		fc->cache = NULL;
 	}
 	if (!fc->sdata)
@@ -252,7 +259,7 @@ void p9_fcall_fini(struct p9_fcall *fc)
 	if (fc->cache)
 		kmem_cache_free(fc->cache, fc->sdata);
 	else
-		kfree(fc->sdata);
+		kvfree(fc->sdata);
 }
 EXPORT_SYMBOL(p9_fcall_fini);
 
net/9p/trans_fd.c +2 -0
index a516745f732f..e7334033eba5 100644
@@ -1101,6 +1101,7 @@ static struct p9_trans_module p9_tcp_trans = {
 	.maxsize = MAX_SOCK_BUF,
 	.pooled_rbuffers = false,
 	.def = 0,
+	.supports_vmalloc = true,
 	.create = p9_fd_create_tcp,
 	.close = p9_fd_close,n
 	.request = p9_fd_request,
@@ -1115,6 +1116,7 @@ static struct p9_trans_module p9_unix_trans = {
 	.name = "unix",
 	.maxsize = MAX_SOCK_BUF,
 	.def = 0,
+	.supports_vmalloc = true,
 	.create = p9_fd_create_unix,
 	.close = p9_fd_close,
 	.request = p9_fd_request,

net/9p/trans_fd.c static struct p9_trans_module p9_fd_trans = { · line 1137

Quoted 7 lines
net/9p/trans_fd.c +1 -0
@@ -1129,6 +1131,7 @@ static struct p9_trans_module p9_fd_trans = {
 	.name = "fd",
 	.maxsize = MAX_SOCK_BUF,
 	.def = 0,
+	.supports_vmalloc = true,
 	.create = p9_fd_create,
 	.close = p9_fd_close,
 	.request = p9_fd_request,

Just for clarity I would add .supports_vmalloc = false to the other transports. Except of that:

Thanks!

/Christian

#13 lore raw

Re: [PATCH v3] 9p: Use kvmalloc for message buffers on supported transports

While developing a 9P server (https://github.com/Barre/ZeroFS) and testing it under high-load, I was running into allocation failures. The failures occur even with plenty of free memory available because kmalloc requires contiguous physical memory.

This results in errors like:
ls: page allocation failure: order:7, mode:0x40c40(GFP_NOFS|__GFP_COMP)

This patch introduces a transport capability flag (supports_vmalloc) that indicates whether a transport can work with vmalloc'd buffers (non-physically contiguous memory). Transports requiring DMA should leave this flag as false.

The fd-based transports (tcp, unix, fd) set this flag to true, and p9_fcall_init will use kvmalloc instead of kmalloc for these transports. This allows the allocator to fall back to vmalloc when contiguous physical memory is not available.

Additionally, if kmem_cache_alloc fails, the code falls back to kvmalloc for transports that support it.

---
Changes in v3:
- Added explicit .supports_vmalloc = false to DMA-based transports (suggested by Christian Schoenebeck)

 include/net/9p/transport.h |  4 ++++
 net/9p/client.c            | 11 +++++++++--
 net/9p/trans_fd.c          |  3 +++
 net/9p/trans_rdma.c        |  1 +
 net/9p/trans_usbg.c        |  1 +
 net/9p/trans_virtio.c      |  1 +
 net/9p/trans_xen.c         |  1 +
 7 files changed, 20 insertions(+), 2 deletions(-)
include/net/9p/transport.h +4 -0
index 766ec07c9599..f0981515148d 100644
@@ -24,6 +24,9 @@
  *                   we're less flexible when choosing the response message
  *                   size in this case
  * @def: set if this transport should be considered the default
+ * @supports_vmalloc: set if this transport can work with vmalloc'd buffers
+ *                    (non-physically contiguous memory). Transports requiring
+ *                    DMA should leave this as false.
  * @create: member function to create a new connection on this transport
  * @close: member function to discard a connection on this transport
  * @request: member function to issue a request to the transport
@@ -44,6 +47,7 @@ struct p9_trans_module {
 	int maxsize;		/* max message size of transport */
 	bool pooled_rbuffers;
 	int def;		/* this transport should be default */
+	bool supports_vmalloc;	/* can work with vmalloc'd buffers */
 	struct module *owner;
 	int (*create)(struct p9_client *client,
 		      const char *devname, char *args);
net/9p/client.c +9 -2
index 5c1ca57ccd28..2a4884c880c1 100644
@@ -229,8 +229,15 @@ static int p9_fcall_init(struct p9_client *c, struct p9_fcall *fc,
 	if (likely(c->fcall_cache) && alloc_msize == c->msize) {
 		fc->sdata = kmem_cache_alloc(c->fcall_cache, GFP_NOFS);
 		fc->cache = c->fcall_cache;
+		if (!fc->sdata && c->trans_mod->supports_vmalloc) {
+			fc->sdata = kvmalloc(alloc_msize, GFP_NOFS);
+			fc->cache = NULL;
+		}
 	} else {
-		fc->sdata = kmalloc(alloc_msize, GFP_NOFS);
+		if (c->trans_mod->supports_vmalloc)
+			fc->sdata = kvmalloc(alloc_msize, GFP_NOFS);
+		else
+			fc->sdata = kmalloc(alloc_msize, GFP_NOFS);
 		fc->cache = NULL;
 	}
 	if (!fc->sdata)
@@ -252,7 +259,7 @@ void p9_fcall_fini(struct p9_fcall *fc)
 	if (fc->cache)
 		kmem_cache_free(fc->cache, fc->sdata);
 	else
-		kfree(fc->sdata);
+		kvfree(fc->sdata);
 }
 EXPORT_SYMBOL(p9_fcall_fini);
 
net/9p/trans_fd.c +3 -0
index a516745f732f..e7334033eba5 100644
@@ -1101,6 +1101,7 @@ static struct p9_trans_module p9_tcp_trans = {
 	.maxsize = MAX_SOCK_BUF,
 	.pooled_rbuffers = false,
 	.def = 0,
+	.supports_vmalloc = true,
 	.create = p9_fd_create_tcp,
 	.close = p9_fd_close,
 	.request = p9_fd_request,
@@ -1115,6 +1116,7 @@ static struct p9_trans_module p9_unix_trans = {
 	.name = "unix",
 	.maxsize = MAX_SOCK_BUF,
 	.def = 0,
+	.supports_vmalloc = true,
 	.create = p9_fd_create_unix,
 	.close = p9_fd_close,
 	.request = p9_fd_request,
@@ -1129,6 +1131,7 @@ static struct p9_trans_module p9_fd_trans = {
 	.name = "fd",
 	.maxsize = MAX_SOCK_BUF,
 	.def = 0,
+	.supports_vmalloc = true,
 	.create = p9_fd_create,
 	.close = p9_fd_close,
 	.request = p9_fd_request,
net/9p/trans_rdma.c +1 -0
index b84748baf9cb..6c5ad232c194 100644
@@ -749,6 +749,7 @@ static struct p9_trans_module p9_rdma_trans = {
 	.maxsize = P9_RDMA_MAXSIZE,
 	.pooled_rbuffers = true,
 	.def = 0,
+	.supports_vmalloc = false,
 	.owner = THIS_MODULE,
 	.create = rdma_create_trans,
 	.close = rdma_close,
net/9p/trans_usbg.c +1 -0
index 468f7e8f0277..2542ef099233 100644
@@ -514,6 +514,7 @@ static struct p9_trans_module p9_usbg_trans = {
 	.close = p9_usbg_close,
 	.request = p9_usbg_request,
 	.cancel = p9_usbg_cancel,
+	.supports_vmalloc = false,
 	.owner = THIS_MODULE,
 };
 
net/9p/trans_virtio.c +1 -0
index 0b8086f58ad5..12806207f4f0 100644
@@ -803,6 +803,7 @@ static struct p9_trans_module p9_virtio_trans = {
 	.maxsize = PAGE_SIZE * (VIRTQUEUE_NUM - 3),
 	.pooled_rbuffers = false,
 	.def = 1,
+	.supports_vmalloc = false,
 	.owner = THIS_MODULE,
 };
 
net/9p/trans_xen.c +1 -0
index b9ff69c7522a..4b1cec0ab829 100644
@@ -258,6 +258,7 @@ static struct p9_trans_module p9_xen_trans = {
 	.maxsize = 1 << (XEN_9PFS_RING_ORDER + XEN_PAGE_SHIFT - 2),
 	.pooled_rbuffers = false,
 	.def = 1,
+	.supports_vmalloc = false,
 	.create = p9_xen_create,
 	.close = p9_xen_close,
 	.request = p9_xen_request,
Signature · 1 line
2.39.5 (Apple Git-154)
Quoted 119 lines · Christian Schoenebeck
Quoted 102 lines · CEST Pierre Barre

While developing a 9P server (https://github.com/Barre/ZeroFS) and testing it under high-load, I was running into allocation failures. The failures occur even with plenty of free memory available because kmalloc requires contiguous physical memory.

This results in errors like:
ls: page allocation failure: order:7, mode:0x40c40(GFP_NOFS|__GFP_COMP)

This patch introduces a transport capability flag (supports_vmalloc) that indicates whether a transport can work with vmalloc'd buffers (non-physically contiguous memory). Transports requiring DMA should leave this flag as false.

The fd-based transports (tcp, unix, fd) set this flag to true, and p9_fcall_init will use kvmalloc instead of kmalloc for these transports. This allows the allocator to fall back to vmalloc when contiguous physical memory is not available.

Additionally, if kmem_cache_alloc fails, the code falls back to kvmalloc for transports that support it.

---

 include/net/9p/transport.h |  4 ++++
 net/9p/client.c            | 11 +++++++++--
 net/9p/trans_fd.c          |  3 +++
 3 files changed, 16 insertions(+), 2 deletions(-)
include/net/9p/transport.h +2 -0
index 766ec07c9599..f0981515148d 100644
@@ -24,6 +24,9 @@
  *                   we're less flexible when choosing the response message
 *                   size in this case
  * @def: set if this transport should be considered the default
+ * @supports_vmalloc: set if this transport can work with vmalloc'd buffers
+ *                    (non-physically contiguous memory). Transports

requiring + * DMA should leave this as false.
* @create: member function to create a new connection on this transport
* @close: member function to discard a connection on this transport
* @request: member function to issue a request to the transport

include/net/9p/transport.h +1 -0
@@ -44,6 +47,7 @@ struct p9_trans_module {
 	int maxsize;		/* max message size of transport */
 	bool pooled_rbuffers;
 	int def;		/* this transport should be default */
+	bool supports_vmalloc;	/* can work with vmalloc'd buffers */
 	struct module *owner;
 	int (*create)(struct p9_client *client,
 		      const char *devname, char *args);
net/9p/client.c +9 -2
index 5c1ca57ccd28..2a4884c880c1 100644
@@ -229,8 +229,15 @@ static int p9_fcall_init(struct p9_client *c, struct
 p9_fcall *fc, if (likely(c->fcall_cache) && alloc_msize == c->msize) {
 		fc->sdata = kmem_cache_alloc(c->fcall_cache, GFP_NOFS);
 		fc->cache = c->fcall_cache;
+		if (!fc->sdata && c->trans_mod->supports_vmalloc) {
+			fc->sdata = kvmalloc(alloc_msize, GFP_NOFS);
+			fc->cache = NULL;
+		}
 	} else {
-		fc->sdata = kmalloc(alloc_msize, GFP_NOFS);
+		if (c->trans_mod->supports_vmalloc)
+			fc->sdata = kvmalloc(alloc_msize, GFP_NOFS);
+		else
+			fc->sdata = kmalloc(alloc_msize, GFP_NOFS);
 		fc->cache = NULL;
 	}
 	if (!fc->sdata)
@@ -252,7 +259,7 @@ void p9_fcall_fini(struct p9_fcall *fc)
 	if (fc->cache)
 		kmem_cache_free(fc->cache, fc->sdata);
 	else
-		kfree(fc->sdata);
+		kvfree(fc->sdata);
 }
 EXPORT_SYMBOL(p9_fcall_fini);
 
net/9p/trans_fd.c +2 -0
index a516745f732f..e7334033eba5 100644
@@ -1101,6 +1101,7 @@ static struct p9_trans_module p9_tcp_trans = {
 	.maxsize = MAX_SOCK_BUF,
 	.pooled_rbuffers = false,
 	.def = 0,
+	.supports_vmalloc = true,
 	.create = p9_fd_create_tcp,
 	.close = p9_fd_close,n
 	.request = p9_fd_request,
@@ -1115,6 +1116,7 @@ static struct p9_trans_module p9_unix_trans = {
 	.name = "unix",
 	.maxsize = MAX_SOCK_BUF,
 	.def = 0,
+	.supports_vmalloc = true,
 	.create = p9_fd_create_unix,
 	.close = p9_fd_close,
 	.request = p9_fd_request,

net/9p/trans_fd.c static struct p9_trans_module p9_fd_trans = { · line 1137

Quoted 7 lines
net/9p/trans_fd.c +1 -0
@@ -1129,6 +1131,7 @@ static struct p9_trans_module p9_fd_trans = {
 	.name = "fd",
 	.maxsize = MAX_SOCK_BUF,
 	.def = 0,
+	.supports_vmalloc = true,
 	.create = p9_fd_create,
 	.close = p9_fd_close,
 	.request = p9_fd_request,

Just for clarity I would add .supports_vmalloc = false to the other transports. Except of that:

Thanks!

/Christian

#14 lore raw

Re: [PATCH v3] 9p: Use kvmalloc for message buffers on supported transports

Le 16/10/2025 à 15:58, Pierre Barre a écrit :

Quoted 40 lines

While developing a 9P server (https://github.com/Barre/ZeroFS) and testing it under high-load, I was running into allocation failures. The failures occur even with plenty of free memory available because kmalloc requires contiguous physical memory.

This results in errors like:
ls: page allocation failure: order:7, mode:0x40c40(GFP_NOFS|__GFP_COMP)

This patch introduces a transport capability flag (supports_vmalloc) that indicates whether a transport can work with vmalloc'd buffers (non-physically contiguous memory). Transports requiring DMA should leave this flag as false.

The fd-based transports (tcp, unix, fd) set this flag to true, and p9_fcall_init will use kvmalloc instead of kmalloc for these transports. This allows the allocator to fall back to vmalloc when contiguous physical memory is not available.

Additionally, if kmem_cache_alloc fails, the code falls back to kvmalloc for transports that support it.

---
Changes in v3:
- Added explicit .supports_vmalloc = false to DMA-based transports (suggested by Christian Schoenebeck)

  include/net/9p/transport.h |  4 ++++
  net/9p/client.c            | 11 +++++++++--
  net/9p/trans_fd.c          |  3 +++
  net/9p/trans_rdma.c        |  1 +
  net/9p/trans_usbg.c        |  1 +
  net/9p/trans_virtio.c      |  1 +
  net/9p/trans_xen.c         |  1 +
  7 files changed, 20 insertions(+), 2 deletions(-)
include/net/9p/transport.h +0 -0
index 766ec07c9599..f0981515148d 100644

...

include/net/9p/transport.h struct p9_trans_module { · line 49

Quoted 4 lines
include/net/9p/transport.h +0 -0
@@ -44,6 +47,7 @@ struct p9_trans_module {
  	int maxsize;		/* max message size of transport */
  	bool pooled_rbuffers;
  	int def;		/* this transport should be default */

Unrelated to this patch, but it looks like 'def' could be a bool. This would save a few bytes in the structure, should it matter.

CJ

Quoted 4 lines
include/net/9p/transport.h +1 -0
+	bool supports_vmalloc;	/* can work with vmalloc'd buffers */
  	struct module *owner;
  	int (*create)(struct p9_client *client,
  		      const char *devname, char *args);
maintainer
#15 lore raw

Re: [PATCH v3] 9p: Use kvmalloc for message buffers on supported transports

In reply to Pierre Barre

Pierre Barre wrote on Thu, Oct 16, 2025 at 03:58:36PM +0200:

Quoted 18 lines

While developing a 9P server (https://github.com/Barre/ZeroFS) and testing it under high-load, I was running into allocation failures. The failures occur even with plenty of free memory available because kmalloc requires contiguous physical memory.

This results in errors like:
ls: page allocation failure: order:7, mode:0x40c40(GFP_NOFS|__GFP_COMP)

This patch introduces a transport capability flag (supports_vmalloc) that indicates whether a transport can work with vmalloc'd buffers (non-physically contiguous memory). Transports requiring DMA should leave this flag as false.

The fd-based transports (tcp, unix, fd) set this flag to true, and p9_fcall_init will use kvmalloc instead of kmalloc for these transports. This allows the allocator to fall back to vmalloc when contiguous physical memory is not available.

Quoted 4 lines

Additionally, if kmem_cache_alloc fails, the code falls back to kvmalloc for transports that support it.

Thanks, it's now picked up and queued in -next -- will send to Linus in a couple of months.

FWIW, I prefer (and I think it's the norm in the linux world) if patches new versions aren't sent as a reply to previous version, it confuses tools like b4 that fetch the patch thread for version operations. If you send patches again please just send later versions without using --in-reply-to :)

Cheers,

Signature · 1 line
Dominique Martinet | Asmadeus
#16 lore raw

Re: [PATCH v3] 9p: Use kvmalloc for message buffers on supported transports

Quoted 28 lines · asmadeus

Pierre Barre wrote on Thu, Oct 16, 2025 at 03:58:36PM +0200:

Quoted 18 lines

While developing a 9P server (https://github.com/Barre/ZeroFS) and testing it under high-load, I was running into allocation failures. The failures occur even with plenty of free memory available because kmalloc requires contiguous physical memory.

This results in errors like:
ls: page allocation failure: order:7, mode:0x40c40(GFP_NOFS|__GFP_COMP)

This patch introduces a transport capability flag (supports_vmalloc) that indicates whether a transport can work with vmalloc'd buffers (non-physically contiguous memory). Transports requiring DMA should leave this flag as false.

The fd-based transports (tcp, unix, fd) set this flag to true, and p9_fcall_init will use kvmalloc instead of kmalloc for these transports. This allows the allocator to fall back to vmalloc when contiguous physical memory is not available.

Quoted 4 lines

Additionally, if kmem_cache_alloc fails, the code falls back to kvmalloc for transports that support it.

Quoted 8 lines

Thanks, it's now picked up and queued in -next -- will send to Linus in a couple of months.

FWIW, I prefer (and I think it's the norm in the linux world) if patches new versions aren't sent as a reply to previous version, it confuses tools like b4 that fetch the patch thread for version operations. If you send patches again please just send later versions without using --in-reply-to :)

Got it, thank you!

Best,
Pierre

Quoted 4 lines

Cheers,

Signature · 1 line
Dominique Martinet | Asmadeus