2 * linux/mm/allocpercpu.c
4 * Separated from slab.c August 11, 2006 Christoph Lameter <clameter@sgi.com>
7 #include <linux/module.h>
10 * percpu_depopulate - depopulate per-cpu data for given cpu
11 * @__pdata: per-cpu data to depopulate
12 * @cpu: depopulate per-cpu data for this cpu
14 * Depopulating per-cpu data for a cpu going offline would be a typical
15 * use case. You need to register a cpu hotplug handler for that purpose.
17 void percpu_depopulate(void *__pdata, int cpu)
19 struct percpu_data *pdata = __percpu_disguise(__pdata);
20 if (pdata->ptrs[cpu]) {
21 kfree(pdata->ptrs[cpu]);
22 pdata->ptrs[cpu] = NULL;
25 EXPORT_SYMBOL_GPL(percpu_depopulate);
28 * percpu_depopulate_mask - depopulate per-cpu data for some cpu's
29 * @__pdata: per-cpu data to depopulate
30 * @mask: depopulate per-cpu data for cpu's selected through mask bits
32 void __percpu_depopulate_mask(void *__pdata, cpumask_t *mask)
35 for_each_cpu_mask(cpu, *mask)
36 percpu_depopulate(__pdata, cpu);
38 EXPORT_SYMBOL_GPL(__percpu_depopulate_mask);
41 * percpu_populate - populate per-cpu data for given cpu
42 * @__pdata: per-cpu data to populate further
43 * @size: size of per-cpu object
44 * @gfp: may sleep or not etc.
45 * @cpu: populate per-data for this cpu
47 * Populating per-cpu data for a cpu coming online would be a typical
48 * use case. You need to register a cpu hotplug handler for that purpose.
49 * Per-cpu object is populated with zeroed buffer.
51 void *percpu_populate(void *__pdata, size_t size, gfp_t gfp, int cpu)
53 struct percpu_data *pdata = __percpu_disguise(__pdata);
54 int node = cpu_to_node(cpu);
56 BUG_ON(pdata->ptrs[cpu]);
57 if (node_online(node)) {
58 /* FIXME: kzalloc_node(size, gfp, node) */
59 pdata->ptrs[cpu] = kmalloc_node(size, gfp, node);
61 memset(pdata->ptrs[cpu], 0, size);
63 pdata->ptrs[cpu] = kzalloc(size, gfp);
64 return pdata->ptrs[cpu];
66 EXPORT_SYMBOL_GPL(percpu_populate);
69 * percpu_populate_mask - populate per-cpu data for more cpu's
70 * @__pdata: per-cpu data to populate further
71 * @size: size of per-cpu object
72 * @gfp: may sleep or not etc.
73 * @mask: populate per-cpu data for cpu's selected through mask bits
75 * Per-cpu objects are populated with zeroed buffers.
77 int __percpu_populate_mask(void *__pdata, size_t size, gfp_t gfp,
80 cpumask_t populated = CPU_MASK_NONE;
83 for_each_cpu_mask(cpu, *mask)
84 if (unlikely(!percpu_populate(__pdata, size, gfp, cpu))) {
85 __percpu_depopulate_mask(__pdata, &populated);
88 cpu_set(cpu, populated);
91 EXPORT_SYMBOL_GPL(__percpu_populate_mask);
94 * percpu_alloc_mask - initial setup of per-cpu data
95 * @size: size of per-cpu object
96 * @gfp: may sleep or not etc.
97 * @mask: populate per-data for cpu's selected through mask bits
99 * Populating per-cpu data for all online cpu's would be a typical use case,
100 * which is simplified by the percpu_alloc() wrapper.
101 * Per-cpu objects are populated with zeroed buffers.
103 void *__percpu_alloc_mask(size_t size, gfp_t gfp, cpumask_t *mask)
105 void *pdata = kzalloc(sizeof(struct percpu_data), gfp);
106 void *__pdata = __percpu_disguise(pdata);
108 if (unlikely(!pdata))
110 if (likely(!__percpu_populate_mask(__pdata, size, gfp, mask)))
115 EXPORT_SYMBOL_GPL(__percpu_alloc_mask);
118 * percpu_free - final cleanup of per-cpu data
119 * @__pdata: object to clean up
121 * We simply clean up any per-cpu object left. No need for the client to
122 * track and specify through a bis mask which per-cpu objects are to free.
124 void percpu_free(void *__pdata)
126 __percpu_depopulate_mask(__pdata, &cpu_possible_map);
127 kfree(__percpu_disguise(__pdata));
129 EXPORT_SYMBOL_GPL(percpu_free);