xref: /linux/fs/dlm/memory.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
12522fe45SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2e7fd4179SDavid Teigland /******************************************************************************
3e7fd4179SDavid Teigland *******************************************************************************
4e7fd4179SDavid Teigland **
5e7fd4179SDavid Teigland **  Copyright (C) Sistina Software, Inc.  1997-2003  All rights reserved.
652bda2b5SDavid Teigland **  Copyright (C) 2004-2007 Red Hat, Inc.  All rights reserved.
7e7fd4179SDavid Teigland **
8e7fd4179SDavid Teigland **
9e7fd4179SDavid Teigland *******************************************************************************
10e7fd4179SDavid Teigland ******************************************************************************/
11e7fd4179SDavid Teigland 
12e7fd4179SDavid Teigland #include "dlm_internal.h"
136c547f26SAlexander Aring #include "midcomms.h"
143af2326cSAlexander Aring #include "lowcomms.h"
15e7fd4179SDavid Teigland #include "config.h"
16e7fd4179SDavid Teigland #include "memory.h"
1761bed0baSAlexander Aring #include "ast.h"
18e7fd4179SDavid Teigland 
193af2326cSAlexander Aring static struct kmem_cache *writequeue_cache;
206c547f26SAlexander Aring static struct kmem_cache *mhandle_cache;
21e4dc81edSAlexander Aring static struct kmem_cache *msg_cache;
22e18b890bSChristoph Lameter static struct kmem_cache *lkb_cache;
233881ac04SDavid Teigland static struct kmem_cache *rsb_cache;
2461bed0baSAlexander Aring static struct kmem_cache *cb_cache;
25e7fd4179SDavid Teigland 
26e7fd4179SDavid Teigland 
dlm_memory_init(void)2730727174SDenis Cheng int __init dlm_memory_init(void)
28e7fd4179SDavid Teigland {
293af2326cSAlexander Aring 	writequeue_cache = dlm_lowcomms_writequeue_cache_create();
303af2326cSAlexander Aring 	if (!writequeue_cache)
313af2326cSAlexander Aring 		goto out;
323af2326cSAlexander Aring 
336c547f26SAlexander Aring 	mhandle_cache = dlm_midcomms_cache_create();
346c547f26SAlexander Aring 	if (!mhandle_cache)
353af2326cSAlexander Aring 		goto mhandle;
366c547f26SAlexander Aring 
37e7fd4179SDavid Teigland 	lkb_cache = kmem_cache_create("dlm_lkb", sizeof(struct dlm_lkb),
3820c2df83SPaul Mundt 				__alignof__(struct dlm_lkb), 0, NULL);
39e7fd4179SDavid Teigland 	if (!lkb_cache)
406c547f26SAlexander Aring 		goto lkb;
413881ac04SDavid Teigland 
42e4dc81edSAlexander Aring 	msg_cache = dlm_lowcomms_msg_cache_create();
43e4dc81edSAlexander Aring 	if (!msg_cache)
44e4dc81edSAlexander Aring 		goto msg;
45e4dc81edSAlexander Aring 
463881ac04SDavid Teigland 	rsb_cache = kmem_cache_create("dlm_rsb", sizeof(struct dlm_rsb),
473881ac04SDavid Teigland 				__alignof__(struct dlm_rsb), 0, NULL);
486c547f26SAlexander Aring 	if (!rsb_cache)
496c547f26SAlexander Aring 		goto rsb;
503881ac04SDavid Teigland 
5161bed0baSAlexander Aring 	cb_cache = kmem_cache_create("dlm_cb", sizeof(struct dlm_callback),
5261bed0baSAlexander Aring 				     __alignof__(struct dlm_callback), 0,
5361bed0baSAlexander Aring 				     NULL);
548113aa91SYang Yingliang 	if (!cb_cache)
5561bed0baSAlexander Aring 		goto cb;
5661bed0baSAlexander Aring 
5775af271eSDan Carpenter 	return 0;
586c547f26SAlexander Aring 
5961bed0baSAlexander Aring cb:
6061bed0baSAlexander Aring 	kmem_cache_destroy(rsb_cache);
616c547f26SAlexander Aring rsb:
62e4dc81edSAlexander Aring 	kmem_cache_destroy(msg_cache);
63e4dc81edSAlexander Aring msg:
646c547f26SAlexander Aring 	kmem_cache_destroy(lkb_cache);
656c547f26SAlexander Aring lkb:
666c547f26SAlexander Aring 	kmem_cache_destroy(mhandle_cache);
673af2326cSAlexander Aring mhandle:
683af2326cSAlexander Aring 	kmem_cache_destroy(writequeue_cache);
696c547f26SAlexander Aring out:
706c547f26SAlexander Aring 	return -ENOMEM;
71e7fd4179SDavid Teigland }
72e7fd4179SDavid Teigland 
dlm_memory_exit(void)73e7fd4179SDavid Teigland void dlm_memory_exit(void)
74e7fd4179SDavid Teigland {
7589b01913SAlexander Aring 	rcu_barrier();
7689b01913SAlexander Aring 
773af2326cSAlexander Aring 	kmem_cache_destroy(writequeue_cache);
786c547f26SAlexander Aring 	kmem_cache_destroy(mhandle_cache);
79e4dc81edSAlexander Aring 	kmem_cache_destroy(msg_cache);
80e7fd4179SDavid Teigland 	kmem_cache_destroy(lkb_cache);
813881ac04SDavid Teigland 	kmem_cache_destroy(rsb_cache);
8261bed0baSAlexander Aring 	kmem_cache_destroy(cb_cache);
83e7fd4179SDavid Teigland }
84e7fd4179SDavid Teigland 
dlm_allocate_lvb(struct dlm_ls * ls)8552bda2b5SDavid Teigland char *dlm_allocate_lvb(struct dlm_ls *ls)
86e7fd4179SDavid Teigland {
8711697885SAlexander Aring 	return kzalloc(ls->ls_lvblen, GFP_ATOMIC);
88e7fd4179SDavid Teigland }
89e7fd4179SDavid Teigland 
dlm_free_lvb(char * p)9052bda2b5SDavid Teigland void dlm_free_lvb(char *p)
91e7fd4179SDavid Teigland {
92e7fd4179SDavid Teigland 	kfree(p);
93e7fd4179SDavid Teigland }
94e7fd4179SDavid Teigland 
dlm_allocate_rsb(void)9511697885SAlexander Aring struct dlm_rsb *dlm_allocate_rsb(void)
96e7fd4179SDavid Teigland {
9711697885SAlexander Aring 	return kmem_cache_zalloc(rsb_cache, GFP_ATOMIC);
98e7fd4179SDavid Teigland }
99e7fd4179SDavid Teigland 
__free_rsb_rcu(struct rcu_head * rcu)10001fdeca1SAlexander Aring static void __free_rsb_rcu(struct rcu_head *rcu)
101e7fd4179SDavid Teigland {
10201fdeca1SAlexander Aring 	struct dlm_rsb *r = container_of(rcu, struct dlm_rsb, rcu);
103e7fd4179SDavid Teigland 	if (r->res_lvbptr)
10452bda2b5SDavid Teigland 		dlm_free_lvb(r->res_lvbptr);
1053881ac04SDavid Teigland 	kmem_cache_free(rsb_cache, r);
106e7fd4179SDavid Teigland }
107e7fd4179SDavid Teigland 
dlm_free_rsb(struct dlm_rsb * r)10801fdeca1SAlexander Aring void dlm_free_rsb(struct dlm_rsb *r)
10901fdeca1SAlexander Aring {
11001fdeca1SAlexander Aring 	call_rcu(&r->rcu, __free_rsb_rcu);
11101fdeca1SAlexander Aring }
11201fdeca1SAlexander Aring 
dlm_allocate_lkb(void)11311697885SAlexander Aring struct dlm_lkb *dlm_allocate_lkb(void)
114e7fd4179SDavid Teigland {
11511697885SAlexander Aring 	return kmem_cache_zalloc(lkb_cache, GFP_ATOMIC);
116e7fd4179SDavid Teigland }
117e7fd4179SDavid Teigland 
__free_lkb_rcu(struct rcu_head * rcu)118*c846f732SAlexander Aring static void __free_lkb_rcu(struct rcu_head *rcu)
119e7fd4179SDavid Teigland {
120*c846f732SAlexander Aring 	struct dlm_lkb *lkb = container_of(rcu, struct dlm_lkb, rcu);
121*c846f732SAlexander Aring 
1228a39dcd9SAlexander Aring 	if (test_bit(DLM_DFL_USER_BIT, &lkb->lkb_dflags)) {
123597d0caeSDavid Teigland 		struct dlm_user_args *ua;
124d292c0ccSDavid Teigland 		ua = lkb->lkb_ua;
125597d0caeSDavid Teigland 		if (ua) {
126597d0caeSDavid Teigland 			kfree(ua->lksb.sb_lvbptr);
127597d0caeSDavid Teigland 			kfree(ua);
128597d0caeSDavid Teigland 		}
129597d0caeSDavid Teigland 	}
13061bed0baSAlexander Aring 
131e7fd4179SDavid Teigland 	kmem_cache_free(lkb_cache, lkb);
132e7fd4179SDavid Teigland }
133e7fd4179SDavid Teigland 
dlm_free_lkb(struct dlm_lkb * lkb)134*c846f732SAlexander Aring void dlm_free_lkb(struct dlm_lkb *lkb)
135*c846f732SAlexander Aring {
136*c846f732SAlexander Aring 	call_rcu(&lkb->rcu, __free_lkb_rcu);
137*c846f732SAlexander Aring }
138*c846f732SAlexander Aring 
dlm_allocate_mhandle(void)13998808644SAlexander Aring struct dlm_mhandle *dlm_allocate_mhandle(void)
1406c547f26SAlexander Aring {
14198808644SAlexander Aring 	return kmem_cache_alloc(mhandle_cache, GFP_ATOMIC);
1426c547f26SAlexander Aring }
1436c547f26SAlexander Aring 
dlm_free_mhandle(struct dlm_mhandle * mhandle)1446c547f26SAlexander Aring void dlm_free_mhandle(struct dlm_mhandle *mhandle)
1456c547f26SAlexander Aring {
1466c547f26SAlexander Aring 	kmem_cache_free(mhandle_cache, mhandle);
1476c547f26SAlexander Aring }
1483af2326cSAlexander Aring 
dlm_allocate_writequeue(void)1493af2326cSAlexander Aring struct writequeue_entry *dlm_allocate_writequeue(void)
1503af2326cSAlexander Aring {
1513af2326cSAlexander Aring 	return kmem_cache_alloc(writequeue_cache, GFP_ATOMIC);
1523af2326cSAlexander Aring }
1533af2326cSAlexander Aring 
dlm_free_writequeue(struct writequeue_entry * writequeue)1543af2326cSAlexander Aring void dlm_free_writequeue(struct writequeue_entry *writequeue)
1553af2326cSAlexander Aring {
1563af2326cSAlexander Aring 	kmem_cache_free(writequeue_cache, writequeue);
1573af2326cSAlexander Aring }
158e4dc81edSAlexander Aring 
dlm_allocate_msg(void)15998808644SAlexander Aring struct dlm_msg *dlm_allocate_msg(void)
160e4dc81edSAlexander Aring {
16198808644SAlexander Aring 	return kmem_cache_alloc(msg_cache, GFP_ATOMIC);
162e4dc81edSAlexander Aring }
163e4dc81edSAlexander Aring 
dlm_free_msg(struct dlm_msg * msg)164e4dc81edSAlexander Aring void dlm_free_msg(struct dlm_msg *msg)
165e4dc81edSAlexander Aring {
166e4dc81edSAlexander Aring 	kmem_cache_free(msg_cache, msg);
167e4dc81edSAlexander Aring }
16861bed0baSAlexander Aring 
dlm_allocate_cb(void)16961bed0baSAlexander Aring struct dlm_callback *dlm_allocate_cb(void)
17061bed0baSAlexander Aring {
17161bed0baSAlexander Aring 	return kmem_cache_alloc(cb_cache, GFP_ATOMIC);
17261bed0baSAlexander Aring }
17361bed0baSAlexander Aring 
dlm_free_cb(struct dlm_callback * cb)17461bed0baSAlexander Aring void dlm_free_cb(struct dlm_callback *cb)
17561bed0baSAlexander Aring {
17661bed0baSAlexander Aring 	kmem_cache_free(cb_cache, cb);
17761bed0baSAlexander Aring }
178