blob: e3ecb80cd18216347d17c5e109d311febd20b770 [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
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +020017 msg = ceph_msg_new2(pool->type, pool->front_len, pool->max_data_items,
18 gfp_mask, true);
Sage Weil51853522011-08-09 14:48:11 -070019 if (!msg) {
20 dout("msgpool_alloc %s failed\n", pool->name);
21 } else {
22 dout("msgpool_alloc %s %p\n", pool->name, msg);
23 msg->pool = pool;
24 }
25 return msg;
Sage Weild52f8472010-04-01 15:23:14 -070026}
27
Sage Weil51853522011-08-09 14:48:11 -070028static void msgpool_free(void *element, void *arg)
Sage Weild52f8472010-04-01 15:23:14 -070029{
Sage Weil51853522011-08-09 14:48:11 -070030 struct ceph_msgpool *pool = arg;
31 struct ceph_msg *msg = element;
32
33 dout("msgpool_release %s %p\n", pool->name, msg);
34 msg->pool = NULL;
35 ceph_msg_put(msg);
Sage Weil8fc91fd2009-10-06 11:31:13 -070036}
37
Sage Weild50b4092012-07-09 14:22:34 -070038int ceph_msgpool_init(struct ceph_msgpool *pool, int type,
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +020039 int front_len, int max_data_items, int size,
40 const char *name)
Sage Weil8fc91fd2009-10-06 11:31:13 -070041{
Sage Weil51853522011-08-09 14:48:11 -070042 dout("msgpool %s init\n", name);
Sage Weild50b4092012-07-09 14:22:34 -070043 pool->type = type;
Sage Weil8fc91fd2009-10-06 11:31:13 -070044 pool->front_len = front_len;
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +020045 pool->max_data_items = max_data_items;
Sage Weil51853522011-08-09 14:48:11 -070046 pool->pool = mempool_create(size, msgpool_alloc, msgpool_free, pool);
Sage Weild52f8472010-04-01 15:23:14 -070047 if (!pool->pool)
48 return -ENOMEM;
Sage Weil4f482802010-04-24 09:56:35 -070049 pool->name = name;
Sage Weild52f8472010-04-01 15:23:14 -070050 return 0;
Sage Weil8fc91fd2009-10-06 11:31:13 -070051}
52
53void ceph_msgpool_destroy(struct ceph_msgpool *pool)
54{
Sage Weil51853522011-08-09 14:48:11 -070055 dout("msgpool %s destroy\n", pool->name);
Sage Weild52f8472010-04-01 15:23:14 -070056 mempool_destroy(pool->pool);
Sage Weil8fc91fd2009-10-06 11:31:13 -070057}
58
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +020059struct ceph_msg *ceph_msgpool_get(struct ceph_msgpool *pool, int front_len,
60 int max_data_items)
Sage Weil8fc91fd2009-10-06 11:31:13 -070061{
Sage Weil51853522011-08-09 14:48:11 -070062 struct ceph_msg *msg;
63
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +020064 if (front_len > pool->front_len ||
65 max_data_items > pool->max_data_items) {
66 pr_warn_ratelimited("%s need %d/%d, pool %s has %d/%d\n",
67 __func__, front_len, max_data_items, pool->name,
68 pool->front_len, pool->max_data_items);
Ilya Dryomov3b83f602018-10-11 17:04:33 +020069 WARN_ON_ONCE(1);
Sage Weil8f3bc052009-10-14 17:36:07 -070070
71 /* try to alloc a fresh message */
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +020072 return ceph_msg_new2(pool->type, front_len, max_data_items,
73 GFP_NOFS, false);
Sage Weil8f3bc052009-10-14 17:36:07 -070074 }
75
Sage Weil51853522011-08-09 14:48:11 -070076 msg = mempool_alloc(pool->pool, GFP_NOFS);
77 dout("msgpool_get %s %p\n", pool->name, msg);
78 return msg;
Sage Weil8fc91fd2009-10-06 11:31:13 -070079}
80
81void ceph_msgpool_put(struct ceph_msgpool *pool, struct ceph_msg *msg)
82{
Sage Weil51853522011-08-09 14:48:11 -070083 dout("msgpool_put %s %p\n", pool->name, msg);
84
Sage Weild52f8472010-04-01 15:23:14 -070085 /* reset msg front_len; user may have changed it */
86 msg->front.iov_len = pool->front_len;
87 msg->hdr.front_len = cpu_to_le32(pool->front_len);
Sage Weil3ca02ef2010-03-01 15:25:00 -080088
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +020089 msg->data_length = 0;
90 msg->num_data_items = 0;
91
Sage Weild52f8472010-04-01 15:23:14 -070092 kref_init(&msg->kref); /* retake single ref */
Sage Weil51853522011-08-09 14:48:11 -070093 mempool_free(msg, pool->pool);
Sage Weil8fc91fd2009-10-06 11:31:13 -070094}