xref: /linux/fs/dlm/memory.c (revision 116978854427fd7abbeb328768e75b49a7298520)
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 
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 
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 
8552bda2b5SDavid Teigland char *dlm_allocate_lvb(struct dlm_ls *ls)
86e7fd4179SDavid Teigland {
87*11697885SAlexander Aring 	return kzalloc(ls->ls_lvblen, GFP_ATOMIC);
88e7fd4179SDavid Teigland }
89e7fd4179SDavid Teigland 
9052bda2b5SDavid Teigland void dlm_free_lvb(char *p)
91e7fd4179SDavid Teigland {
92e7fd4179SDavid Teigland 	kfree(p);
93e7fd4179SDavid Teigland }
94e7fd4179SDavid Teigland 
95*11697885SAlexander Aring struct dlm_rsb *dlm_allocate_rsb(void)
96e7fd4179SDavid Teigland {
97*11697885SAlexander Aring 	return kmem_cache_zalloc(rsb_cache, GFP_ATOMIC);
98e7fd4179SDavid Teigland }
99e7fd4179SDavid Teigland 
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 
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 
113*11697885SAlexander Aring struct dlm_lkb *dlm_allocate_lkb(void)
114e7fd4179SDavid Teigland {
115*11697885SAlexander Aring 	return kmem_cache_zalloc(lkb_cache, GFP_ATOMIC);
116e7fd4179SDavid Teigland }
117e7fd4179SDavid Teigland 
11852bda2b5SDavid Teigland void dlm_free_lkb(struct dlm_lkb *lkb)
119e7fd4179SDavid Teigland {
1208a39dcd9SAlexander Aring 	if (test_bit(DLM_DFL_USER_BIT, &lkb->lkb_dflags)) {
121597d0caeSDavid Teigland 		struct dlm_user_args *ua;
122d292c0ccSDavid Teigland 		ua = lkb->lkb_ua;
123597d0caeSDavid Teigland 		if (ua) {
124597d0caeSDavid Teigland 			kfree(ua->lksb.sb_lvbptr);
125597d0caeSDavid Teigland 			kfree(ua);
126597d0caeSDavid Teigland 		}
127597d0caeSDavid Teigland 	}
12861bed0baSAlexander Aring 
129e7fd4179SDavid Teigland 	kmem_cache_free(lkb_cache, lkb);
130e7fd4179SDavid Teigland }
131e7fd4179SDavid Teigland 
13298808644SAlexander Aring struct dlm_mhandle *dlm_allocate_mhandle(void)
1336c547f26SAlexander Aring {
13498808644SAlexander Aring 	return kmem_cache_alloc(mhandle_cache, GFP_ATOMIC);
1356c547f26SAlexander Aring }
1366c547f26SAlexander Aring 
1376c547f26SAlexander Aring void dlm_free_mhandle(struct dlm_mhandle *mhandle)
1386c547f26SAlexander Aring {
1396c547f26SAlexander Aring 	kmem_cache_free(mhandle_cache, mhandle);
1406c547f26SAlexander Aring }
1413af2326cSAlexander Aring 
1423af2326cSAlexander Aring struct writequeue_entry *dlm_allocate_writequeue(void)
1433af2326cSAlexander Aring {
1443af2326cSAlexander Aring 	return kmem_cache_alloc(writequeue_cache, GFP_ATOMIC);
1453af2326cSAlexander Aring }
1463af2326cSAlexander Aring 
1473af2326cSAlexander Aring void dlm_free_writequeue(struct writequeue_entry *writequeue)
1483af2326cSAlexander Aring {
1493af2326cSAlexander Aring 	kmem_cache_free(writequeue_cache, writequeue);
1503af2326cSAlexander Aring }
151e4dc81edSAlexander Aring 
15298808644SAlexander Aring struct dlm_msg *dlm_allocate_msg(void)
153e4dc81edSAlexander Aring {
15498808644SAlexander Aring 	return kmem_cache_alloc(msg_cache, GFP_ATOMIC);
155e4dc81edSAlexander Aring }
156e4dc81edSAlexander Aring 
157e4dc81edSAlexander Aring void dlm_free_msg(struct dlm_msg *msg)
158e4dc81edSAlexander Aring {
159e4dc81edSAlexander Aring 	kmem_cache_free(msg_cache, msg);
160e4dc81edSAlexander Aring }
16161bed0baSAlexander Aring 
16261bed0baSAlexander Aring struct dlm_callback *dlm_allocate_cb(void)
16361bed0baSAlexander Aring {
16461bed0baSAlexander Aring 	return kmem_cache_alloc(cb_cache, GFP_ATOMIC);
16561bed0baSAlexander Aring }
16661bed0baSAlexander Aring 
16761bed0baSAlexander Aring void dlm_free_cb(struct dlm_callback *cb)
16861bed0baSAlexander Aring {
16961bed0baSAlexander Aring 	kmem_cache_free(cb_cache, cb);
17061bed0baSAlexander Aring }
171