4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
24 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright (c) 2012, Intel Corporation.
28 * This file is part of Lustre, http://www.lustre.org/
30 * Author: liang@whamcloud.com
33 #define DEBUG_SUBSYSTEM S_LNET
35 #include <linux/workqueue.h>
36 #include <libcfs/libcfs.h>
37 #include <lustre_compat.h>
39 struct cfs_var_array {
40 unsigned int va_count; /* # of buffers */
41 unsigned int va_size; /* size of each var */
42 struct cfs_cpt_table *va_cptab; /* cpu partition table */
43 void *va_ptrs[0]; /* buffer addresses */
47 * free per-cpu data, see more detail in cfs_percpt_free
50 cfs_percpt_free(void *vars)
52 struct cfs_var_array *arr;
55 arr = container_of(vars, struct cfs_var_array, va_ptrs[0]);
57 for (i = 0; i < arr->va_count; i++) {
58 if (arr->va_ptrs[i] != NULL)
59 LIBCFS_FREE(arr->va_ptrs[i], arr->va_size);
62 LIBCFS_FREE(arr, offsetof(struct cfs_var_array,
63 va_ptrs[arr->va_count]));
65 EXPORT_SYMBOL(cfs_percpt_free);
68 * allocate per cpu-partition variables, returned value is an array of pointers,
69 * variable can be indexed by CPU partition ID, i.e:
71 * arr = cfs_percpt_alloc(cfs_cpu_pt, size);
72 * then caller can access memory block for CPU 0 by arr[0],
73 * memory block for CPU 1 by arr[1]...
74 * memory block for CPU N by arr[N]...
79 cfs_percpt_alloc(struct cfs_cpt_table *cptab, unsigned int size)
81 struct cfs_var_array *arr;
85 count = cfs_cpt_number(cptab);
87 LIBCFS_ALLOC(arr, offsetof(struct cfs_var_array, va_ptrs[count]));
91 arr->va_size = size = L1_CACHE_ALIGN(size);
92 arr->va_count = count;
93 arr->va_cptab = cptab;
95 for (i = 0; i < count; i++) {
96 LIBCFS_CPT_ALLOC(arr->va_ptrs[i], cptab, i, size);
97 if (arr->va_ptrs[i] == NULL) {
98 cfs_percpt_free((void *)&arr->va_ptrs[0]);
103 return (void *)&arr->va_ptrs[0];
105 EXPORT_SYMBOL(cfs_percpt_alloc);
108 * return number of CPUs (or number of elements in per-cpu data)
109 * according to cptab of @vars
112 cfs_percpt_number(void *vars)
114 struct cfs_var_array *arr;
116 arr = container_of(vars, struct cfs_var_array, va_ptrs[0]);
118 return arr->va_count;
120 EXPORT_SYMBOL(cfs_percpt_number);
124 * This is opencoding of vfree_atomic from Linux kernel added in 4.10 with
125 * minimum changes needed to work on older kernels too.
128 #ifndef llist_for_each_safe
129 #define llist_for_each_safe(pos, n, node) \
130 for ((pos) = (node); (pos) && ((n) = (pos)->next, true); (pos) = (n))
133 struct vfree_deferred {
134 struct llist_head list;
135 struct work_struct wq;
137 static DEFINE_PER_CPU(struct vfree_deferred, vfree_deferred);
139 static void free_work(struct work_struct *w)
141 struct vfree_deferred *p = container_of(w, struct vfree_deferred, wq);
142 struct llist_node *t, *llnode;
144 llist_for_each_safe(llnode, t, llist_del_all(&p->list))
145 vfree((void *)llnode);
148 void libcfs_vfree_atomic(const void *addr)
150 struct vfree_deferred *p = raw_cpu_ptr(&vfree_deferred);
155 if (llist_add((struct llist_node *)addr, &p->list))
156 schedule_work(&p->wq);
158 EXPORT_SYMBOL(libcfs_vfree_atomic);
160 void __init init_libcfs_vfree_atomic(void)
164 for_each_possible_cpu(i) {
165 struct vfree_deferred *p;
167 p = &per_cpu(vfree_deferred, i);
168 init_llist_head(&p->list);
169 INIT_WORK(&p->wq, free_work);
173 void __exit exit_libcfs_vfree_atomic(void)
175 flush_scheduled_work();