1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
23d14c5d2SYehuda Sadeh #include <linux/ceph/ceph_debug.h>
33d14c5d2SYehuda Sadeh
43d14c5d2SYehuda Sadeh #include <linux/err.h>
53d14c5d2SYehuda Sadeh #include <linux/sched.h>
63d14c5d2SYehuda Sadeh #include <linux/types.h>
73d14c5d2SYehuda Sadeh #include <linux/vmalloc.h>
83d14c5d2SYehuda Sadeh
9b2aa5d0bSIlya Dryomov #include <linux/ceph/messenger.h>
103d14c5d2SYehuda Sadeh #include <linux/ceph/msgpool.h>
113d14c5d2SYehuda Sadeh
msgpool_alloc(gfp_t gfp_mask,void * arg)125185352cSSage Weil static void *msgpool_alloc(gfp_t gfp_mask, void *arg)
133d14c5d2SYehuda Sadeh {
143d14c5d2SYehuda Sadeh struct ceph_msgpool *pool = arg;
155185352cSSage Weil struct ceph_msg *msg;
163d14c5d2SYehuda Sadeh
17*0d9c1ab3SIlya Dryomov msg = ceph_msg_new2(pool->type, pool->front_len, pool->max_data_items,
18*0d9c1ab3SIlya Dryomov gfp_mask, true);
195185352cSSage Weil if (!msg) {
205185352cSSage Weil dout("msgpool_alloc %s failed\n", pool->name);
215185352cSSage Weil } else {
225185352cSSage Weil dout("msgpool_alloc %s %p\n", pool->name, msg);
235185352cSSage Weil msg->pool = pool;
245185352cSSage Weil }
255185352cSSage Weil return msg;
263d14c5d2SYehuda Sadeh }
273d14c5d2SYehuda Sadeh
msgpool_free(void * element,void * arg)285185352cSSage Weil static void msgpool_free(void *element, void *arg)
293d14c5d2SYehuda Sadeh {
305185352cSSage Weil struct ceph_msgpool *pool = arg;
315185352cSSage Weil struct ceph_msg *msg = element;
325185352cSSage Weil
335185352cSSage Weil dout("msgpool_release %s %p\n", pool->name, msg);
345185352cSSage Weil msg->pool = NULL;
355185352cSSage Weil ceph_msg_put(msg);
363d14c5d2SYehuda Sadeh }
373d14c5d2SYehuda Sadeh
ceph_msgpool_init(struct ceph_msgpool * pool,int type,int front_len,int max_data_items,int size,const char * name)38d50b409fSSage Weil int ceph_msgpool_init(struct ceph_msgpool *pool, int type,
39*0d9c1ab3SIlya Dryomov int front_len, int max_data_items, int size,
40*0d9c1ab3SIlya Dryomov const char *name)
413d14c5d2SYehuda Sadeh {
425185352cSSage Weil dout("msgpool %s init\n", name);
43d50b409fSSage Weil pool->type = type;
443d14c5d2SYehuda Sadeh pool->front_len = front_len;
45*0d9c1ab3SIlya Dryomov pool->max_data_items = max_data_items;
465185352cSSage Weil pool->pool = mempool_create(size, msgpool_alloc, msgpool_free, pool);
473d14c5d2SYehuda Sadeh if (!pool->pool)
483d14c5d2SYehuda Sadeh return -ENOMEM;
493d14c5d2SYehuda Sadeh pool->name = name;
503d14c5d2SYehuda Sadeh return 0;
513d14c5d2SYehuda Sadeh }
523d14c5d2SYehuda Sadeh
ceph_msgpool_destroy(struct ceph_msgpool * pool)533d14c5d2SYehuda Sadeh void ceph_msgpool_destroy(struct ceph_msgpool *pool)
543d14c5d2SYehuda Sadeh {
555185352cSSage Weil dout("msgpool %s destroy\n", pool->name);
563d14c5d2SYehuda Sadeh mempool_destroy(pool->pool);
573d14c5d2SYehuda Sadeh }
583d14c5d2SYehuda Sadeh
ceph_msgpool_get(struct ceph_msgpool * pool,int front_len,int max_data_items)59*0d9c1ab3SIlya Dryomov struct ceph_msg *ceph_msgpool_get(struct ceph_msgpool *pool, int front_len,
60*0d9c1ab3SIlya Dryomov int max_data_items)
613d14c5d2SYehuda Sadeh {
625185352cSSage Weil struct ceph_msg *msg;
635185352cSSage Weil
64*0d9c1ab3SIlya Dryomov if (front_len > pool->front_len ||
65*0d9c1ab3SIlya Dryomov max_data_items > pool->max_data_items) {
66*0d9c1ab3SIlya Dryomov pr_warn_ratelimited("%s need %d/%d, pool %s has %d/%d\n",
67*0d9c1ab3SIlya Dryomov __func__, front_len, max_data_items, pool->name,
68*0d9c1ab3SIlya Dryomov pool->front_len, pool->max_data_items);
693b83f60dSIlya Dryomov WARN_ON_ONCE(1);
703d14c5d2SYehuda Sadeh
713d14c5d2SYehuda Sadeh /* try to alloc a fresh message */
72*0d9c1ab3SIlya Dryomov return ceph_msg_new2(pool->type, front_len, max_data_items,
73*0d9c1ab3SIlya Dryomov GFP_NOFS, false);
743d14c5d2SYehuda Sadeh }
753d14c5d2SYehuda Sadeh
765185352cSSage Weil msg = mempool_alloc(pool->pool, GFP_NOFS);
775185352cSSage Weil dout("msgpool_get %s %p\n", pool->name, msg);
785185352cSSage Weil return msg;
793d14c5d2SYehuda Sadeh }
803d14c5d2SYehuda Sadeh
ceph_msgpool_put(struct ceph_msgpool * pool,struct ceph_msg * msg)813d14c5d2SYehuda Sadeh void ceph_msgpool_put(struct ceph_msgpool *pool, struct ceph_msg *msg)
823d14c5d2SYehuda Sadeh {
835185352cSSage Weil dout("msgpool_put %s %p\n", pool->name, msg);
845185352cSSage Weil
853d14c5d2SYehuda Sadeh /* reset msg front_len; user may have changed it */
863d14c5d2SYehuda Sadeh msg->front.iov_len = pool->front_len;
873d14c5d2SYehuda Sadeh msg->hdr.front_len = cpu_to_le32(pool->front_len);
883d14c5d2SYehuda Sadeh
89*0d9c1ab3SIlya Dryomov msg->data_length = 0;
90*0d9c1ab3SIlya Dryomov msg->num_data_items = 0;
91*0d9c1ab3SIlya Dryomov
923d14c5d2SYehuda Sadeh kref_init(&msg->kref); /* retake single ref */
935185352cSSage Weil mempool_free(msg, pool->pool);
943d14c5d2SYehuda Sadeh }
95