Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / mm / allocpercpu.c
blobdadf01b7072eaf59bb03089368ad5c1376476a1c
1 /*
2 * linux/mm/allocpercpu.c
4 * Separated from slab.c August 11, 2006 Christoph Lameter <clameter@sgi.com>
5 */
6 #include <linux/mm.h>
7 #include <linux/module.h>
9 <<<<<<< HEAD:mm/allocpercpu.c
10 =======
11 #ifndef cache_line_size
12 #define cache_line_size() L1_CACHE_BYTES
13 #endif
15 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:mm/allocpercpu.c
16 /**
17 * percpu_depopulate - depopulate per-cpu data for given cpu
18 * @__pdata: per-cpu data to depopulate
19 * @cpu: depopulate per-cpu data for this cpu
21 * Depopulating per-cpu data for a cpu going offline would be a typical
22 * use case. You need to register a cpu hotplug handler for that purpose.
24 void percpu_depopulate(void *__pdata, int cpu)
26 struct percpu_data *pdata = __percpu_disguise(__pdata);
28 kfree(pdata->ptrs[cpu]);
29 pdata->ptrs[cpu] = NULL;
31 EXPORT_SYMBOL_GPL(percpu_depopulate);
33 /**
34 * percpu_depopulate_mask - depopulate per-cpu data for some cpu's
35 * @__pdata: per-cpu data to depopulate
36 * @mask: depopulate per-cpu data for cpu's selected through mask bits
38 void __percpu_depopulate_mask(void *__pdata, cpumask_t *mask)
40 int cpu;
41 for_each_cpu_mask(cpu, *mask)
42 percpu_depopulate(__pdata, cpu);
44 EXPORT_SYMBOL_GPL(__percpu_depopulate_mask);
46 /**
47 * percpu_populate - populate per-cpu data for given cpu
48 * @__pdata: per-cpu data to populate further
49 * @size: size of per-cpu object
50 * @gfp: may sleep or not etc.
51 * @cpu: populate per-data for this cpu
53 * Populating per-cpu data for a cpu coming online would be a typical
54 * use case. You need to register a cpu hotplug handler for that purpose.
55 * Per-cpu object is populated with zeroed buffer.
57 void *percpu_populate(void *__pdata, size_t size, gfp_t gfp, int cpu)
59 struct percpu_data *pdata = __percpu_disguise(__pdata);
60 int node = cpu_to_node(cpu);
62 <<<<<<< HEAD:mm/allocpercpu.c
63 =======
65 * We should make sure each CPU gets private memory.
67 size = roundup(size, cache_line_size());
69 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:mm/allocpercpu.c
70 BUG_ON(pdata->ptrs[cpu]);
71 if (node_online(node))
72 pdata->ptrs[cpu] = kmalloc_node(size, gfp|__GFP_ZERO, node);
73 else
74 pdata->ptrs[cpu] = kzalloc(size, gfp);
75 return pdata->ptrs[cpu];
77 EXPORT_SYMBOL_GPL(percpu_populate);
79 /**
80 * percpu_populate_mask - populate per-cpu data for more cpu's
81 * @__pdata: per-cpu data to populate further
82 * @size: size of per-cpu object
83 * @gfp: may sleep or not etc.
84 * @mask: populate per-cpu data for cpu's selected through mask bits
86 * Per-cpu objects are populated with zeroed buffers.
88 int __percpu_populate_mask(void *__pdata, size_t size, gfp_t gfp,
89 cpumask_t *mask)
91 cpumask_t populated = CPU_MASK_NONE;
92 int cpu;
94 for_each_cpu_mask(cpu, *mask)
95 if (unlikely(!percpu_populate(__pdata, size, gfp, cpu))) {
96 __percpu_depopulate_mask(__pdata, &populated);
97 return -ENOMEM;
98 } else
99 cpu_set(cpu, populated);
100 return 0;
102 EXPORT_SYMBOL_GPL(__percpu_populate_mask);
105 * percpu_alloc_mask - initial setup of per-cpu data
106 * @size: size of per-cpu object
107 * @gfp: may sleep or not etc.
108 * @mask: populate per-data for cpu's selected through mask bits
110 * Populating per-cpu data for all online cpu's would be a typical use case,
111 * which is simplified by the percpu_alloc() wrapper.
112 * Per-cpu objects are populated with zeroed buffers.
114 void *__percpu_alloc_mask(size_t size, gfp_t gfp, cpumask_t *mask)
116 <<<<<<< HEAD:mm/allocpercpu.c
117 void *pdata = kzalloc(nr_cpu_ids * sizeof(void *), gfp);
118 =======
120 * We allocate whole cache lines to avoid false sharing
122 size_t sz = roundup(nr_cpu_ids * sizeof(void *), cache_line_size());
123 void *pdata = kzalloc(sz, gfp);
124 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:mm/allocpercpu.c
125 void *__pdata = __percpu_disguise(pdata);
127 if (unlikely(!pdata))
128 return NULL;
129 if (likely(!__percpu_populate_mask(__pdata, size, gfp, mask)))
130 return __pdata;
131 kfree(pdata);
132 return NULL;
134 EXPORT_SYMBOL_GPL(__percpu_alloc_mask);
137 * percpu_free - final cleanup of per-cpu data
138 * @__pdata: object to clean up
140 * We simply clean up any per-cpu object left. No need for the client to
141 * track and specify through a bis mask which per-cpu objects are to free.
143 void percpu_free(void *__pdata)
145 if (unlikely(!__pdata))
146 return;
147 __percpu_depopulate_mask(__pdata, &cpu_possible_map);
148 kfree(__percpu_disguise(__pdata));
150 EXPORT_SYMBOL_GPL(percpu_free);