Blame view

net/ceph/msgpool.c 2.33 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
2
  #include <linux/ceph/ceph_debug.h>
8fc91fd85   Sage Weil   ceph: message pools
3
4
5
6
7
  
  #include <linux/err.h>
  #include <linux/sched.h>
  #include <linux/types.h>
  #include <linux/vmalloc.h>
b2aa5d0bc   Ilya Dryomov   libceph: fix some...
8
  #include <linux/ceph/messenger.h>
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
9
  #include <linux/ceph/msgpool.h>
8fc91fd85   Sage Weil   ceph: message pools
10

5185352c1   Sage Weil   libceph: fix msgpool
11
  static void *msgpool_alloc(gfp_t gfp_mask, void *arg)
d52f847a8   Sage Weil   ceph: rewrite msg...
12
13
  {
  	struct ceph_msgpool *pool = arg;
5185352c1   Sage Weil   libceph: fix msgpool
14
  	struct ceph_msg *msg;
8fc91fd85   Sage Weil   ceph: message pools
15

0d9c1ab3b   Ilya Dryomov   libceph: prealloc...
16
17
  	msg = ceph_msg_new2(pool->type, pool->front_len, pool->max_data_items,
  			    gfp_mask, true);
5185352c1   Sage Weil   libceph: fix msgpool
18
19
20
21
22
23
24
25
26
  	if (!msg) {
  		dout("msgpool_alloc %s failed
  ", pool->name);
  	} else {
  		dout("msgpool_alloc %s %p
  ", pool->name, msg);
  		msg->pool = pool;
  	}
  	return msg;
d52f847a8   Sage Weil   ceph: rewrite msg...
27
  }
8fc91fd85   Sage Weil   ceph: message pools
28

5185352c1   Sage Weil   libceph: fix msgpool
29
  static void msgpool_free(void *element, void *arg)
8fc91fd85   Sage Weil   ceph: message pools
30
  {
5185352c1   Sage Weil   libceph: fix msgpool
31
32
33
34
35
36
37
  	struct ceph_msgpool *pool = arg;
  	struct ceph_msg *msg = element;
  
  	dout("msgpool_release %s %p
  ", pool->name, msg);
  	msg->pool = NULL;
  	ceph_msg_put(msg);
8fc91fd85   Sage Weil   ceph: message pools
38
  }
d50b409fb   Sage Weil   libceph: initiali...
39
  int ceph_msgpool_init(struct ceph_msgpool *pool, int type,
0d9c1ab3b   Ilya Dryomov   libceph: prealloc...
40
41
  		      int front_len, int max_data_items, int size,
  		      const char *name)
8fc91fd85   Sage Weil   ceph: message pools
42
  {
5185352c1   Sage Weil   libceph: fix msgpool
43
44
  	dout("msgpool %s init
  ", name);
d50b409fb   Sage Weil   libceph: initiali...
45
  	pool->type = type;
8fc91fd85   Sage Weil   ceph: message pools
46
  	pool->front_len = front_len;
0d9c1ab3b   Ilya Dryomov   libceph: prealloc...
47
  	pool->max_data_items = max_data_items;
5185352c1   Sage Weil   libceph: fix msgpool
48
  	pool->pool = mempool_create(size, msgpool_alloc, msgpool_free, pool);
d52f847a8   Sage Weil   ceph: rewrite msg...
49
50
  	if (!pool->pool)
  		return -ENOMEM;
4f48280ee   Sage Weil   ceph: name msgpoo...
51
  	pool->name = name;
d52f847a8   Sage Weil   ceph: rewrite msg...
52
  	return 0;
8fc91fd85   Sage Weil   ceph: message pools
53
54
55
56
  }
  
  void ceph_msgpool_destroy(struct ceph_msgpool *pool)
  {
5185352c1   Sage Weil   libceph: fix msgpool
57
58
  	dout("msgpool %s destroy
  ", pool->name);
d52f847a8   Sage Weil   ceph: rewrite msg...
59
  	mempool_destroy(pool->pool);
8fc91fd85   Sage Weil   ceph: message pools
60
  }
0d9c1ab3b   Ilya Dryomov   libceph: prealloc...
61
62
  struct ceph_msg *ceph_msgpool_get(struct ceph_msgpool *pool, int front_len,
  				  int max_data_items)
8fc91fd85   Sage Weil   ceph: message pools
63
  {
5185352c1   Sage Weil   libceph: fix msgpool
64
  	struct ceph_msg *msg;
0d9c1ab3b   Ilya Dryomov   libceph: prealloc...
65
66
67
68
69
70
  	if (front_len > pool->front_len ||
  	    max_data_items > pool->max_data_items) {
  		pr_warn_ratelimited("%s need %d/%d, pool %s has %d/%d
  ",
  		    __func__, front_len, max_data_items, pool->name,
  		    pool->front_len, pool->max_data_items);
3b83f60da   Ilya Dryomov   libceph: enable f...
71
  		WARN_ON_ONCE(1);
8f3bc053c   Sage Weil   ceph: warn on all...
72
73
  
  		/* try to alloc a fresh message */
0d9c1ab3b   Ilya Dryomov   libceph: prealloc...
74
75
  		return ceph_msg_new2(pool->type, front_len, max_data_items,
  				     GFP_NOFS, false);
8f3bc053c   Sage Weil   ceph: warn on all...
76
  	}
5185352c1   Sage Weil   libceph: fix msgpool
77
78
79
80
  	msg = mempool_alloc(pool->pool, GFP_NOFS);
  	dout("msgpool_get %s %p
  ", pool->name, msg);
  	return msg;
8fc91fd85   Sage Weil   ceph: message pools
81
82
83
84
  }
  
  void ceph_msgpool_put(struct ceph_msgpool *pool, struct ceph_msg *msg)
  {
5185352c1   Sage Weil   libceph: fix msgpool
85
86
  	dout("msgpool_put %s %p
  ", pool->name, msg);
d52f847a8   Sage Weil   ceph: rewrite msg...
87
88
89
  	/* reset msg front_len; user may have changed it */
  	msg->front.iov_len = pool->front_len;
  	msg->hdr.front_len = cpu_to_le32(pool->front_len);
3ca02ef96   Sage Weil   ceph: reset front...
90

0d9c1ab3b   Ilya Dryomov   libceph: prealloc...
91
92
  	msg->data_length = 0;
  	msg->num_data_items = 0;
d52f847a8   Sage Weil   ceph: rewrite msg...
93
  	kref_init(&msg->kref);  /* retake single ref */
5185352c1   Sage Weil   libceph: fix msgpool
94
  	mempool_free(msg, pool->pool);
8fc91fd85   Sage Weil   ceph: message pools
95
  }