Whamcloud - gitweb
LU-14187 osd-ldiskfs: fix locking in write commit
[fs/lustre-release.git] / libcfs / libcfs / libcfs_mem.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 021110-1307, USA
20  *
21  * GPL HEADER END
22  */
23 /*
24  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
25  * Copyright (c) 2012, Intel Corporation.
26  */
27 /*
28  * This file is part of Lustre, http://www.lustre.org/
29  * Lustre is a trademark of Sun Microsystems, Inc.
30  *
31  * Author: liang@whamcloud.com
32  */
33
34 #define DEBUG_SUBSYSTEM S_LNET
35
36 #include <linux/workqueue.h>
37 #include <libcfs/libcfs.h>
38 #include <lustre_compat.h>
39
40 struct cfs_var_array {
41         unsigned int            va_count;       /* # of buffers */
42         unsigned int            va_size;        /* size of each var */
43         struct cfs_cpt_table    *va_cptab;      /* cpu partition table */
44         void                    *va_ptrs[0];    /* buffer addresses */
45 };
46
47 /*
48  * free per-cpu data, see more detail in cfs_percpt_free
49  */
50 void
51 cfs_percpt_free(void *vars)
52 {
53         struct  cfs_var_array *arr;
54         int     i;
55
56         arr = container_of(vars, struct cfs_var_array, va_ptrs[0]);
57
58         for (i = 0; i < arr->va_count; i++) {
59                 if (arr->va_ptrs[i] != NULL)
60                         LIBCFS_FREE(arr->va_ptrs[i], arr->va_size);
61         }
62
63         LIBCFS_FREE(arr, offsetof(struct cfs_var_array,
64                                   va_ptrs[arr->va_count]));
65 }
66 EXPORT_SYMBOL(cfs_percpt_free);
67
68 /*
69  * allocate per cpu-partition variables, returned value is an array of pointers,
70  * variable can be indexed by CPU partition ID, i.e:
71  *
72  *      arr = cfs_percpt_alloc(cfs_cpu_pt, size);
73  *      then caller can access memory block for CPU 0 by arr[0],
74  *      memory block for CPU 1 by arr[1]...
75  *      memory block for CPU N by arr[N]...
76  *
77  * cacheline aligned.
78  */
79 void *
80 cfs_percpt_alloc(struct cfs_cpt_table *cptab, unsigned int size)
81 {
82         struct cfs_var_array    *arr;
83         int                     count;
84         int                     i;
85
86         count = cfs_cpt_number(cptab);
87
88         LIBCFS_ALLOC(arr, offsetof(struct cfs_var_array, va_ptrs[count]));
89         if (arr == NULL)
90                 return NULL;
91
92         arr->va_size    = size = L1_CACHE_ALIGN(size);
93         arr->va_count   = count;
94         arr->va_cptab   = cptab;
95
96         for (i = 0; i < count; i++) {
97                 LIBCFS_CPT_ALLOC(arr->va_ptrs[i], cptab, i, size);
98                 if (arr->va_ptrs[i] == NULL) {
99                         cfs_percpt_free((void *)&arr->va_ptrs[0]);
100                         return NULL;
101                 }
102         }
103
104         return (void *)&arr->va_ptrs[0];
105 }
106 EXPORT_SYMBOL(cfs_percpt_alloc);
107
108 /*
109  * return number of CPUs (or number of elements in per-cpu data)
110  * according to cptab of @vars
111  */
112 int
113 cfs_percpt_number(void *vars)
114 {
115         struct cfs_var_array *arr;
116
117         arr = container_of(vars, struct cfs_var_array, va_ptrs[0]);
118
119         return arr->va_count;
120 }
121 EXPORT_SYMBOL(cfs_percpt_number);
122
123 /*
124  * free variable array, see more detail in cfs_array_alloc
125  */
126 void
127 cfs_array_free(void *vars)
128 {
129         struct cfs_var_array    *arr;
130         int                     i;
131
132         arr = container_of(vars, struct cfs_var_array, va_ptrs[0]);
133
134         for (i = 0; i < arr->va_count; i++) {
135                 if (arr->va_ptrs[i] == NULL)
136                         continue;
137
138                 LIBCFS_FREE(arr->va_ptrs[i], arr->va_size);
139         }
140         LIBCFS_FREE(arr, offsetof(struct cfs_var_array,
141                                   va_ptrs[arr->va_count]));
142 }
143 EXPORT_SYMBOL(cfs_array_free);
144
145 /*
146  * allocate a variable array, returned value is an array of pointers.
147  * Caller can specify length of array by @count, @size is size of each
148  * memory block in array.
149  */
150 void *
151 cfs_array_alloc(int count, unsigned int size)
152 {
153         struct cfs_var_array    *arr;
154         int                     i;
155
156         LIBCFS_ALLOC(arr, offsetof(struct cfs_var_array, va_ptrs[count]));
157         if (arr == NULL)
158                 return NULL;
159
160         arr->va_count   = count;
161         arr->va_size    = size;
162
163         for (i = 0; i < count; i++) {
164                 LIBCFS_ALLOC(arr->va_ptrs[i], size);
165
166                 if (arr->va_ptrs[i] == NULL) {
167                         cfs_array_free((void *)&arr->va_ptrs[0]);
168                         return NULL;
169                 }
170         }
171
172         return (void *)&arr->va_ptrs[0];
173 }
174 EXPORT_SYMBOL(cfs_array_alloc);
175
176 /*
177  * This is opencoding of vfree_atomic from Linux kernel added in 4.10 with
178  * minimum changes needed to work on older kernels too.
179  */
180
181 #ifndef llist_for_each_safe
182 #define llist_for_each_safe(pos, n, node)                       \
183         for ((pos) = (node); (pos) && ((n) = (pos)->next, true); (pos) = (n))
184 #endif
185
186 struct vfree_deferred {
187         struct llist_head list;
188         struct work_struct wq;
189 };
190 static DEFINE_PER_CPU(struct vfree_deferred, vfree_deferred);
191
192 static void free_work(struct work_struct *w)
193 {
194         struct vfree_deferred *p = container_of(w, struct vfree_deferred, wq);
195         struct llist_node *t, *llnode;
196
197         llist_for_each_safe(llnode, t, llist_del_all(&p->list))
198                 vfree((void *)llnode);
199 }
200
201 void libcfs_vfree_atomic(const void *addr)
202 {
203         struct vfree_deferred *p = raw_cpu_ptr(&vfree_deferred);
204
205         if (!addr)
206                 return;
207
208         if (llist_add((struct llist_node *)addr, &p->list))
209                 schedule_work(&p->wq);
210 }
211 EXPORT_SYMBOL(libcfs_vfree_atomic);
212
213 void __init init_libcfs_vfree_atomic(void)
214 {
215         int i;
216
217         for_each_possible_cpu(i) {
218                 struct vfree_deferred *p;
219
220                 p = &per_cpu(vfree_deferred, i);
221                 init_llist_head(&p->list);
222                 INIT_WORK(&p->wq, free_work);
223         }
224 }
225
226 void __exit exit_libcfs_vfree_atomic(void)
227 {
228         flush_scheduled_work();
229 }