blob: 72571535883fdd15d43bcb9628bb858bd5cc713d [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002#include <linux/ceph/ceph_debug.h>
Sage Weil8fc91fd2009-10-06 11:31:13 -07003
4#include <linux/err.h>
5#include <linux/sched.h>
6#include <linux/types.h>
7#include <linux/vmalloc.h>
8
Ilya Dryomovb2aa5d02016-06-07 21:57:15 +02009#include <linux/ceph/messenger.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070010#include <linux/ceph/msgpool.h>
Sage Weil8fc91fd2009-10-06 11:31:13 -070011
Sage Weil51853522011-08-09 14:48:11 -070012static void *msgpool_alloc(gfp_t gfp_mask, void *arg)
Sage Weil8fc91fd2009-10-06 11:31:13 -070013{
Sage Weild52f8472010-04-01 15:23:14 -070014 struct ceph_msgpool *pool = arg;
Sage Weil51853522011-08-09 14:48:11 -070015 struct ceph_msg *msg;
Sage Weil8fc91fd2009-10-06 11:31:13 -070016
Sage Weild50b4092012-07-09 14:22:34 -070017 msg = ceph_msg_new(pool->type, pool->front_len, gfp_mask, true);
Sage Weil51853522011-08-09 14:48:11 -070018 if (!msg) {
19 dout("msgpool_alloc %s failed\n", pool->name);
20 } else {
21 dout("msgpool_alloc %s %p\n", pool->name, msg);
22 msg->pool = pool;
23 }
24 return msg;
Sage Weild52f8472010-04-01 15:23:14 -070025}
26
Sage Weil51853522011-08-09 14:48:11 -070027static void msgpool_free(void *element, void *arg)
Sage Weild52f8472010-04-01 15:23:14 -070028{
Sage Weil51853522011-08-09 14:48:11 -070029 struct ceph_msgpool *pool = arg;
30 struct ceph_msg *msg = element;
31
32 dout("msgpool_release %s %p\n", pool->name, msg);
33 msg->pool = NULL;
34 ceph_msg_put(msg);
Sage Weil8fc91fd2009-10-06 11:31:13 -070035}
36
Sage Weild50b4092012-07-09 14:22:34 -070037int ceph_msgpool_init(struct ceph_msgpool *pool, int type,
Sage Weil4f482802010-04-24 09:56:35 -070038 int front_len, int size, bool blocking, const char *name)
Sage Weil8fc91fd2009-10-06 11:31:13 -070039{
Sage Weil51853522011-08-09 14:48:11 -070040 dout("msgpool %s init\n", name);
Sage Weild50b4092012-07-09 14:22:34 -070041 pool->type = type;
Sage Weil8fc91fd2009-10-06 11:31:13 -070042 pool->front_len = front_len;
Sage Weil51853522011-08-09 14:48:11 -070043 pool->pool = mempool_create(size, msgpool_alloc, msgpool_free, pool);
Sage Weild52f8472010-04-01 15:23:14 -070044 if (!pool->pool)
45 return -ENOMEM;
Sage Weil4f482802010-04-24 09:56:35 -070046 pool->name = name;
Sage Weild52f8472010-04-01 15:23:14 -070047 return 0;
Sage Weil8fc91fd2009-10-06 11:31:13 -070048}
49
50void ceph_msgpool_destroy(struct ceph_msgpool *pool)
51{
Sage Weil51853522011-08-09 14:48:11 -070052 dout("msgpool %s destroy\n", pool->name);
Sage Weild52f8472010-04-01 15:23:14 -070053 mempool_destroy(pool->pool);
Sage Weil8fc91fd2009-10-06 11:31:13 -070054}
55
Sage Weild52f8472010-04-01 15:23:14 -070056struct ceph_msg *ceph_msgpool_get(struct ceph_msgpool *pool,
57 int front_len)
Sage Weil8fc91fd2009-10-06 11:31:13 -070058{
Sage Weil51853522011-08-09 14:48:11 -070059 struct ceph_msg *msg;
60
Sage Weild52f8472010-04-01 15:23:14 -070061 if (front_len > pool->front_len) {
Sage Weil51853522011-08-09 14:48:11 -070062 dout("msgpool_get %s need front %d, pool size is %d\n",
Sage Weil4f482802010-04-24 09:56:35 -070063 pool->name, front_len, pool->front_len);
Sage Weil8f3bc052009-10-14 17:36:07 -070064 WARN_ON(1);
65
66 /* try to alloc a fresh message */
Sage Weild50b4092012-07-09 14:22:34 -070067 return ceph_msg_new(pool->type, front_len, GFP_NOFS, false);
Sage Weil8f3bc052009-10-14 17:36:07 -070068 }
69
Sage Weil51853522011-08-09 14:48:11 -070070 msg = mempool_alloc(pool->pool, GFP_NOFS);
71 dout("msgpool_get %s %p\n", pool->name, msg);
72 return msg;
Sage Weil8fc91fd2009-10-06 11:31:13 -070073}
74
75void ceph_msgpool_put(struct ceph_msgpool *pool, struct ceph_msg *msg)
76{
Sage Weil51853522011-08-09 14:48:11 -070077 dout("msgpool_put %s %p\n", pool->name, msg);
78
Sage Weild52f8472010-04-01 15:23:14 -070079 /* reset msg front_len; user may have changed it */
80 msg->front.iov_len = pool->front_len;
81 msg->hdr.front_len = cpu_to_le32(pool->front_len);
Sage Weil3ca02ef2010-03-01 15:25:00 -080082
Sage Weild52f8472010-04-01 15:23:14 -070083 kref_init(&msg->kref); /* retake single ref */
Sage Weil51853522011-08-09 14:48:11 -070084 mempool_free(msg, pool->pool);
Sage Weil8fc91fd2009-10-06 11:31:13 -070085}