xref: /linux/fs/dlm/memory.c (revision b5314f2c6654a3616fd72777deb1ca766cc50618)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /******************************************************************************
3 *******************************************************************************
4 **
5 **  Copyright (C) Sistina Software, Inc.  1997-2003  All rights reserved.
6 **  Copyright (C) 2004-2007 Red Hat, Inc.  All rights reserved.
7 **
8 **
9 *******************************************************************************
10 ******************************************************************************/
11 
12 #include "dlm_internal.h"
13 #include "midcomms.h"
14 #include "lowcomms.h"
15 #include "config.h"
16 #include "memory.h"
17 #include "ast.h"
18 
19 static struct kmem_cache *writequeue_cache;
20 static struct kmem_cache *mhandle_cache;
21 static struct kmem_cache *msg_cache;
22 static struct kmem_cache *lkb_cache;
23 static struct kmem_cache *rsb_cache;
24 static struct kmem_cache *cb_cache;
25 
26 
27 int __init dlm_memory_init(void)
28 {
29 	writequeue_cache = dlm_lowcomms_writequeue_cache_create();
30 	if (!writequeue_cache)
31 		goto out;
32 
33 	mhandle_cache = dlm_midcomms_cache_create();
34 	if (!mhandle_cache)
35 		goto mhandle;
36 
37 	lkb_cache = kmem_cache_create("dlm_lkb", sizeof(struct dlm_lkb),
38 				__alignof__(struct dlm_lkb), 0, NULL);
39 	if (!lkb_cache)
40 		goto lkb;
41 
42 	msg_cache = dlm_lowcomms_msg_cache_create();
43 	if (!msg_cache)
44 		goto msg;
45 
46 	rsb_cache = kmem_cache_create("dlm_rsb", sizeof(struct dlm_rsb),
47 				__alignof__(struct dlm_rsb), 0, NULL);
48 	if (!rsb_cache)
49 		goto rsb;
50 
51 	cb_cache = kmem_cache_create_usercopy("dlm_cb", sizeof(struct dlm_callback),
52 				     __alignof__(struct dlm_callback), 0,
53 					 offsetof(struct dlm_callback, lvbptr),
54 					 sizeof_field(struct dlm_callback, lvbptr),
55 				     NULL);
56 	if (!cb_cache)
57 		goto cb;
58 
59 	return 0;
60 
61 cb:
62 	kmem_cache_destroy(rsb_cache);
63 rsb:
64 	kmem_cache_destroy(msg_cache);
65 msg:
66 	kmem_cache_destroy(lkb_cache);
67 lkb:
68 	kmem_cache_destroy(mhandle_cache);
69 mhandle:
70 	kmem_cache_destroy(writequeue_cache);
71 out:
72 	return -ENOMEM;
73 }
74 
75 void dlm_memory_exit(void)
76 {
77 	rcu_barrier();
78 
79 	kmem_cache_destroy(writequeue_cache);
80 	kmem_cache_destroy(mhandle_cache);
81 	kmem_cache_destroy(msg_cache);
82 	kmem_cache_destroy(lkb_cache);
83 	kmem_cache_destroy(rsb_cache);
84 	kmem_cache_destroy(cb_cache);
85 }
86 
87 char *dlm_allocate_lvb(struct dlm_ls *ls)
88 {
89 	return kzalloc(ls->ls_lvblen, GFP_ATOMIC);
90 }
91 
92 void dlm_free_lvb(char *p)
93 {
94 	kfree(p);
95 }
96 
97 struct dlm_rsb *dlm_allocate_rsb(void)
98 {
99 	return kmem_cache_zalloc(rsb_cache, GFP_ATOMIC);
100 }
101 
102 static void __free_rsb_rcu(struct rcu_head *rcu)
103 {
104 	struct dlm_rsb *r = container_of(rcu, struct dlm_rsb, rcu);
105 	if (r->res_lvbptr)
106 		dlm_free_lvb(r->res_lvbptr);
107 	kmem_cache_free(rsb_cache, r);
108 }
109 
110 void dlm_free_rsb(struct dlm_rsb *r)
111 {
112 	call_rcu(&r->rcu, __free_rsb_rcu);
113 }
114 
115 struct dlm_lkb *dlm_allocate_lkb(void)
116 {
117 	return kmem_cache_zalloc(lkb_cache, GFP_ATOMIC);
118 }
119 
120 static void __free_lkb_rcu(struct rcu_head *rcu)
121 {
122 	struct dlm_lkb *lkb = container_of(rcu, struct dlm_lkb, rcu);
123 
124 	if (test_bit(DLM_DFL_USER_BIT, &lkb->lkb_dflags)) {
125 		struct dlm_user_args *ua;
126 		ua = lkb->lkb_ua;
127 		if (ua) {
128 			kfree(ua->lksb.sb_lvbptr);
129 			kfree(ua);
130 		}
131 	}
132 
133 	kmem_cache_free(lkb_cache, lkb);
134 }
135 
136 void dlm_free_lkb(struct dlm_lkb *lkb)
137 {
138 	call_rcu(&lkb->rcu, __free_lkb_rcu);
139 }
140 
141 struct dlm_mhandle *dlm_allocate_mhandle(void)
142 {
143 	return kmem_cache_alloc(mhandle_cache, GFP_ATOMIC);
144 }
145 
146 void dlm_free_mhandle(struct dlm_mhandle *mhandle)
147 {
148 	kmem_cache_free(mhandle_cache, mhandle);
149 }
150 
151 struct writequeue_entry *dlm_allocate_writequeue(void)
152 {
153 	return kmem_cache_alloc(writequeue_cache, GFP_ATOMIC);
154 }
155 
156 void dlm_free_writequeue(struct writequeue_entry *writequeue)
157 {
158 	kmem_cache_free(writequeue_cache, writequeue);
159 }
160 
161 struct dlm_msg *dlm_allocate_msg(void)
162 {
163 	return kmem_cache_alloc(msg_cache, GFP_ATOMIC);
164 }
165 
166 void dlm_free_msg(struct dlm_msg *msg)
167 {
168 	kmem_cache_free(msg_cache, msg);
169 }
170 
171 struct dlm_callback *dlm_allocate_cb(void)
172 {
173 	return kmem_cache_alloc(cb_cache, GFP_ATOMIC);
174 }
175 
176 void dlm_free_cb(struct dlm_callback *cb)
177 {
178 	kmem_cache_free(cb_cache, cb);
179 }
180