diff options
author | Kent Overstreet <kent.overstreet@gmail.com> | 2022-07-03 21:29:22 -0400 |
---|---|---|
committer | Kent Overstreet <kent.overstreet@gmail.com> | 2022-07-03 23:04:26 -0400 |
commit | 56440899dc06c69f05b59b1e76150de6547a125c (patch) | |
tree | 4f7ef4b4360867db3a01c99023c7180ff7672a36 | |
parent | 0ac0401f035bb278179176bc1d30b048230b453f (diff) |
9p: Add mempools for RPCs9p_mempool
Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
Cc: Eric Van Hensbergen <ericvh@gmail.com>
Cc: Latchesar Ionkov <lucho@ionkov.net>
Cc: Dominique Martinet <asmadeus@codewreck.org>
-rw-r--r-- | include/net/9p/9p.h | 2 | ||||
-rw-r--r-- | include/net/9p/client.h | 12 | ||||
-rw-r--r-- | net/9p/client.c | 70 | ||||
-rw-r--r-- | net/9p/trans_rdma.c | 2 |
4 files changed, 54 insertions, 32 deletions
diff --git a/include/net/9p/9p.h b/include/net/9p/9p.h index 24a509f559ee..0b20ee6854d6 100644 --- a/include/net/9p/9p.h +++ b/include/net/9p/9p.h @@ -539,12 +539,12 @@ struct p9_rstatfs { struct p9_fcall { u32 size; u8 id; + bool used_mempool; u16 tag; size_t offset; size_t capacity; - struct kmem_cache *cache; u8 *sdata; }; diff --git a/include/net/9p/client.h b/include/net/9p/client.h index cb78e0e33332..832dcc866a20 100644 --- a/include/net/9p/client.h +++ b/include/net/9p/client.h @@ -9,6 +9,7 @@ #ifndef NET_9P_CLIENT_H #define NET_9P_CLIENT_H +#include <linux/mempool.h> #include <linux/utsname.h> #include <linux/idr.h> @@ -107,6 +108,14 @@ struct p9_client { void *trans; struct kmem_cache *fcall_cache; + /* + * We need two identical mempools because it's not safe to allocate + * multiple elements from the same pool (without freeing the first); + * that will deadlock if multiple threads need the last element at the + * same time. + */ + mempool_t pools[2]; + union { struct { int rfd; @@ -222,7 +231,8 @@ int p9_client_mkdir_dotl(struct p9_fid *fid, const char *name, int mode, kgid_t gid, struct p9_qid *qid); int p9_client_lock_dotl(struct p9_fid *fid, struct p9_flock *flock, u8 *status); int p9_client_getlock_dotl(struct p9_fid *fid, struct p9_getlock *fl); -void p9_fcall_fini(struct p9_fcall *fc); +void p9_fcall_fini(struct p9_client *c, struct p9_fcall *fc, + int fc_idx); struct p9_req_t *p9_tag_lookup(struct p9_client *c, u16 tag); static inline void p9_req_get(struct p9_req_t *r) diff --git a/net/9p/client.c b/net/9p/client.c index a36a40137caa..e14074d031c6 100644 --- a/net/9p/client.c +++ b/net/9p/client.c @@ -218,23 +218,29 @@ free_and_return: return ret; } -static int p9_fcall_init(struct p9_client *c, struct p9_fcall *fc, - int alloc_msize) +static void p9_fcall_init(struct p9_client *c, struct p9_fcall *fc, + int fc_idx, unsigned alloc_msize) { - if (likely(c->fcall_cache) && alloc_msize == c->msize) { - fc->sdata = kmem_cache_alloc(c->fcall_cache, GFP_NOFS); - fc->cache = c->fcall_cache; - } else { - fc->sdata = kmalloc(alloc_msize, GFP_NOFS); - fc->cache = NULL; - } - if (!fc->sdata) - return -ENOMEM; + gfp_t gfp = GFP_NOFS|__GFP_NOWARN; + + BUG_ON(alloc_msize > c->msize); + + fc->sdata = NULL; + fc->used_mempool = false; fc->capacity = alloc_msize; - return 0; + + if (alloc_msize < c->msize) + fc->sdata = kmalloc(alloc_msize, gfp); + + if (!fc->sdata) { + fc->sdata = mempool_alloc(&c->pools[fc_idx], gfp); + fc->used_mempool = true; + fc->capacity = c->msize; + } } -void p9_fcall_fini(struct p9_fcall *fc) +void p9_fcall_fini(struct p9_client *c, struct p9_fcall *fc, + int fc_idx) { /* sdata can be NULL for interrupted requests in trans_rdma, * and kmem_cache_free does not do NULL-check for us @@ -242,8 +248,8 @@ void p9_fcall_fini(struct p9_fcall *fc) if (unlikely(!fc->sdata)) return; - if (fc->cache) - kmem_cache_free(fc->cache, fc->sdata); + if (fc->used_mempool) + mempool_free(fc->sdata, &c->pools[fc_idx]); else kfree(fc->sdata); } @@ -270,10 +276,8 @@ p9_tag_alloc(struct p9_client *c, int8_t type, unsigned int max_size) if (!req) return ERR_PTR(-ENOMEM); - if (p9_fcall_init(c, &req->tc, alloc_msize)) - goto free_req; - if (p9_fcall_init(c, &req->rc, alloc_msize)) - goto free; + p9_fcall_init(c, &req->tc, 0, alloc_msize); + p9_fcall_init(c, &req->rc, 1, alloc_msize); p9pdu_reset(&req->tc); p9pdu_reset(&req->rc); @@ -310,9 +314,8 @@ p9_tag_alloc(struct p9_client *c, int8_t type, unsigned int max_size) return req; free: - p9_fcall_fini(&req->tc); - p9_fcall_fini(&req->rc); -free_req: + p9_fcall_fini(c, &req->tc, 0); + p9_fcall_fini(c, &req->rc, 1); kmem_cache_free(p9_req_cache, req); return ERR_PTR(-ENOMEM); } @@ -373,8 +376,8 @@ static int p9_tag_remove(struct p9_client *c, struct p9_req_t *r) int p9_req_put(struct p9_client *c, struct p9_req_t *r) { if (refcount_dec_and_test(&r->refcount)) { - p9_fcall_fini(&r->tc); - p9_fcall_fini(&r->rc); + p9_fcall_fini(c, &r->tc, 0); + p9_fcall_fini(c, &r->rc, 1); kmem_cache_free(p9_req_cache, r); return 1; } @@ -999,7 +1002,7 @@ struct p9_client *p9_client_create(const char *dev_name, char *options) char *client_id; err = 0; - clnt = kmalloc(sizeof(*clnt), GFP_KERNEL); + clnt = kzalloc(sizeof(*clnt), GFP_KERNEL); if (!clnt) return ERR_PTR(-ENOMEM); @@ -1050,10 +1053,6 @@ struct p9_client *p9_client_create(const char *dev_name, char *options) goto close_trans; } - err = p9_client_version(clnt); - if (err) - goto close_trans; - /* P9_HDRSZ + 4 is the smallest packet header we can have that is * followed by data accessed from userspace by read */ @@ -1063,6 +1062,15 @@ struct p9_client *p9_client_create(const char *dev_name, char *options) clnt->msize - (P9_HDRSZ + 4), NULL); + err = mempool_init_slab_pool(&clnt->pools[0], 4, clnt->fcall_cache) ?: + mempool_init_slab_pool(&clnt->pools[1], 4, clnt->fcall_cache); + if (err) + goto close_trans; + + err = p9_client_version(clnt); + if (err) + goto close_trans; + return clnt; close_trans: @@ -1070,6 +1078,8 @@ close_trans: put_trans: v9fs_put_trans(clnt->trans_mod); free_client: + mempool_exit(&clnt->pools[1]); + mempool_exit(&clnt->pools[0]); kfree(clnt); return ERR_PTR(err); } @@ -1094,6 +1104,8 @@ void p9_client_destroy(struct p9_client *clnt) p9_tag_cleanup(clnt); + mempool_exit(&clnt->pools[1]); + mempool_exit(&clnt->pools[0]); kmem_cache_destroy(clnt->fcall_cache); kfree(clnt); } diff --git a/net/9p/trans_rdma.c b/net/9p/trans_rdma.c index d817d3745238..99d878d70d56 100644 --- a/net/9p/trans_rdma.c +++ b/net/9p/trans_rdma.c @@ -431,7 +431,7 @@ static int rdma_request(struct p9_client *client, struct p9_req_t *req) if (unlikely(atomic_read(&rdma->excess_rc) > 0)) { if ((atomic_sub_return(1, &rdma->excess_rc) >= 0)) { /* Got one! */ - p9_fcall_fini(&req->rc); + p9_fcall_fini(client, &req->rc, 1); req->rc.sdata = NULL; goto dont_need_post_recv; } else { |