ceph: fix memory leak due to possible dentry init race
[deliverable/linux.git] / fs / ceph / msgpool.c
CommitLineData
8fc91fd8
SW
1#include "ceph_debug.h"
2
3#include <linux/err.h>
4#include <linux/sched.h>
5#include <linux/types.h>
6#include <linux/vmalloc.h>
7
8#include "msgpool.h"
9
d52f847a
SW
10static void *alloc_fn(gfp_t gfp_mask, void *arg)
11{
12 struct ceph_msgpool *pool = arg;
8fc91fd8 13
bb257664 14 return ceph_msg_new(0, pool->front_len);
d52f847a 15}
8fc91fd8 16
d52f847a 17static void free_fn(void *element, void *arg)
8fc91fd8 18{
d52f847a 19 ceph_msg_put(element);
8fc91fd8
SW
20}
21
22int ceph_msgpool_init(struct ceph_msgpool *pool,
d52f847a 23 int front_len, int size, bool blocking)
8fc91fd8 24{
8fc91fd8 25 pool->front_len = front_len;
d52f847a
SW
26 pool->pool = mempool_create(size, alloc_fn, free_fn, pool);
27 if (!pool->pool)
28 return -ENOMEM;
29 return 0;
8fc91fd8
SW
30}
31
32void ceph_msgpool_destroy(struct ceph_msgpool *pool)
33{
d52f847a 34 mempool_destroy(pool->pool);
8fc91fd8
SW
35}
36
d52f847a
SW
37struct ceph_msg *ceph_msgpool_get(struct ceph_msgpool *pool,
38 int front_len)
8fc91fd8 39{
d52f847a 40 if (front_len > pool->front_len) {
8f3bc053
SW
41 pr_err("msgpool_get pool %p need front %d, pool size is %d\n",
42 pool, front_len, pool->front_len);
43 WARN_ON(1);
44
45 /* try to alloc a fresh message */
bb257664 46 return ceph_msg_new(0, front_len);
8f3bc053
SW
47 }
48
d52f847a 49 return mempool_alloc(pool->pool, GFP_NOFS);
8fc91fd8
SW
50}
51
52void ceph_msgpool_put(struct ceph_msgpool *pool, struct ceph_msg *msg)
53{
d52f847a
SW
54 /* reset msg front_len; user may have changed it */
55 msg->front.iov_len = pool->front_len;
56 msg->hdr.front_len = cpu_to_le32(pool->front_len);
3ca02ef9 57
d52f847a 58 kref_init(&msg->kref); /* retake single ref */
8fc91fd8 59}
This page took 0.041146 seconds and 5 git commands to generate.