Skip to content

Commit e21d451

Browse files
Barremartinetd
authored andcommitted
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. Signed-off-by: Pierre Barre <pierre@barre.sh> Reviewed-by: Christian Schoenebeck <linux_oss@crudebyte.com> Message-ID: <d2017c29-11fb-44a5-bd0f-4204329bbefb@app.fastmail.com> Signed-off-by: Dominique Martinet <asmadeus@codewreck.org>
1 parent 43c36a5 commit e21d451

7 files changed

Lines changed: 20 additions & 2 deletions

File tree

include/net/9p/transport.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
* we're less flexible when choosing the response message
2525
* size in this case
2626
* @def: set if this transport should be considered the default
27+
* @supports_vmalloc: set if this transport can work with vmalloc'd buffers
28+
* (non-physically contiguous memory). Transports requiring
29+
* DMA should leave this as false.
2730
* @create: member function to create a new connection on this transport
2831
* @close: member function to discard a connection on this transport
2932
* @request: member function to issue a request to the transport
@@ -44,6 +47,7 @@ struct p9_trans_module {
4447
int maxsize; /* max message size of transport */
4548
bool pooled_rbuffers;
4649
int def; /* this transport should be default */
50+
bool supports_vmalloc; /* can work with vmalloc'd buffers */
4751
struct module *owner;
4852
int (*create)(struct p9_client *client,
4953
const char *devname, char *args);

net/9p/client.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,15 @@ static int p9_fcall_init(struct p9_client *c, struct p9_fcall *fc,
229229
if (likely(c->fcall_cache) && alloc_msize == c->msize) {
230230
fc->sdata = kmem_cache_alloc(c->fcall_cache, GFP_NOFS);
231231
fc->cache = c->fcall_cache;
232+
if (!fc->sdata && c->trans_mod->supports_vmalloc) {
233+
fc->sdata = kvmalloc(alloc_msize, GFP_NOFS);
234+
fc->cache = NULL;
235+
}
232236
} else {
233-
fc->sdata = kmalloc(alloc_msize, GFP_NOFS);
237+
if (c->trans_mod->supports_vmalloc)
238+
fc->sdata = kvmalloc(alloc_msize, GFP_NOFS);
239+
else
240+
fc->sdata = kmalloc(alloc_msize, GFP_NOFS);
234241
fc->cache = NULL;
235242
}
236243
if (!fc->sdata)
@@ -252,7 +259,7 @@ void p9_fcall_fini(struct p9_fcall *fc)
252259
if (fc->cache)
253260
kmem_cache_free(fc->cache, fc->sdata);
254261
else
255-
kfree(fc->sdata);
262+
kvfree(fc->sdata);
256263
}
257264
EXPORT_SYMBOL(p9_fcall_fini);
258265

net/9p/trans_fd.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,7 @@ static struct p9_trans_module p9_tcp_trans = {
11011101
.maxsize = MAX_SOCK_BUF,
11021102
.pooled_rbuffers = false,
11031103
.def = 0,
1104+
.supports_vmalloc = true,
11041105
.create = p9_fd_create_tcp,
11051106
.close = p9_fd_close,
11061107
.request = p9_fd_request,
@@ -1115,6 +1116,7 @@ static struct p9_trans_module p9_unix_trans = {
11151116
.name = "unix",
11161117
.maxsize = MAX_SOCK_BUF,
11171118
.def = 0,
1119+
.supports_vmalloc = true,
11181120
.create = p9_fd_create_unix,
11191121
.close = p9_fd_close,
11201122
.request = p9_fd_request,
@@ -1129,6 +1131,7 @@ static struct p9_trans_module p9_fd_trans = {
11291131
.name = "fd",
11301132
.maxsize = MAX_SOCK_BUF,
11311133
.def = 0,
1134+
.supports_vmalloc = true,
11321135
.create = p9_fd_create,
11331136
.close = p9_fd_close,
11341137
.request = p9_fd_request,

net/9p/trans_rdma.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,7 @@ static struct p9_trans_module p9_rdma_trans = {
749749
.maxsize = P9_RDMA_MAXSIZE,
750750
.pooled_rbuffers = true,
751751
.def = 0,
752+
.supports_vmalloc = false,
752753
.owner = THIS_MODULE,
753754
.create = rdma_create_trans,
754755
.close = rdma_close,

net/9p/trans_usbg.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ static struct p9_trans_module p9_usbg_trans = {
514514
.close = p9_usbg_close,
515515
.request = p9_usbg_request,
516516
.cancel = p9_usbg_cancel,
517+
.supports_vmalloc = false,
517518
.owner = THIS_MODULE,
518519
};
519520

net/9p/trans_virtio.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,7 @@ static struct p9_trans_module p9_virtio_trans = {
803803
.maxsize = PAGE_SIZE * (VIRTQUEUE_NUM - 3),
804804
.pooled_rbuffers = false,
805805
.def = 1,
806+
.supports_vmalloc = false,
806807
.owner = THIS_MODULE,
807808
};
808809

net/9p/trans_xen.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ static struct p9_trans_module p9_xen_trans = {
258258
.maxsize = 1 << (XEN_9PFS_RING_ORDER + XEN_PAGE_SHIFT - 2),
259259
.pooled_rbuffers = false,
260260
.def = 1,
261+
.supports_vmalloc = false,
261262
.create = p9_xen_create,
262263
.close = p9_xen_close,
263264
.request = p9_xen_request,

0 commit comments

Comments
 (0)