allocpercpu.c 4.1 KB
Newer Older
1 2 3
/*
 * linux/mm/allocpercpu.c
 *
C
Christoph Lameter 已提交
4
 * Separated from slab.c August 11, 2006 Christoph Lameter
5 6 7 8
 */
#include <linux/mm.h>
#include <linux/module.h>

9 10 11 12
#ifndef cache_line_size
#define cache_line_size()	L1_CACHE_BYTES
#endif

13 14 15 16 17 18 19 20
/**
 * percpu_depopulate - depopulate per-cpu data for given cpu
 * @__pdata: per-cpu data to depopulate
 * @cpu: depopulate per-cpu data for this cpu
 *
 * Depopulating per-cpu data for a cpu going offline would be a typical
 * use case. You need to register a cpu hotplug handler for that purpose.
 */
21
static void percpu_depopulate(void *__pdata, int cpu)
22 23
{
	struct percpu_data *pdata = __percpu_disguise(__pdata);
24 25 26

	kfree(pdata->ptrs[cpu]);
	pdata->ptrs[cpu] = NULL;
27 28 29 30 31 32 33
}

/**
 * percpu_depopulate_mask - depopulate per-cpu data for some cpu's
 * @__pdata: per-cpu data to depopulate
 * @mask: depopulate per-cpu data for cpu's selected through mask bits
 */
34
static void __percpu_depopulate_mask(void *__pdata, cpumask_t *mask)
35 36
{
	int cpu;
37
	for_each_cpu_mask_nr(cpu, *mask)
38 39
		percpu_depopulate(__pdata, cpu);
}
40 41 42

#define percpu_depopulate_mask(__pdata, mask) \
	__percpu_depopulate_mask((__pdata), &(mask))
43 44 45 46 47 48 49 50 51 52 53 54

/**
 * percpu_populate - populate per-cpu data for given cpu
 * @__pdata: per-cpu data to populate further
 * @size: size of per-cpu object
 * @gfp: may sleep or not etc.
 * @cpu: populate per-data for this cpu
 *
 * Populating per-cpu data for a cpu coming online would be a typical
 * use case. You need to register a cpu hotplug handler for that purpose.
 * Per-cpu object is populated with zeroed buffer.
 */
55
static void *percpu_populate(void *__pdata, size_t size, gfp_t gfp, int cpu)
56 57 58 59
{
	struct percpu_data *pdata = __percpu_disguise(__pdata);
	int node = cpu_to_node(cpu);

60 61 62 63 64
	/*
	 * We should make sure each CPU gets private memory.
	 */
	size = roundup(size, cache_line_size());

65
	BUG_ON(pdata->ptrs[cpu]);
66 67 68
	if (node_online(node))
		pdata->ptrs[cpu] = kmalloc_node(size, gfp|__GFP_ZERO, node);
	else
69 70 71 72 73 74 75 76 77 78 79 80 81
		pdata->ptrs[cpu] = kzalloc(size, gfp);
	return pdata->ptrs[cpu];
}

/**
 * percpu_populate_mask - populate per-cpu data for more cpu's
 * @__pdata: per-cpu data to populate further
 * @size: size of per-cpu object
 * @gfp: may sleep or not etc.
 * @mask: populate per-cpu data for cpu's selected through mask bits
 *
 * Per-cpu objects are populated with zeroed buffers.
 */
82 83
static int __percpu_populate_mask(void *__pdata, size_t size, gfp_t gfp,
				  cpumask_t *mask)
84
{
85
	cpumask_t populated;
86 87
	int cpu;

88
	cpus_clear(populated);
89
	for_each_cpu_mask_nr(cpu, *mask)
90 91 92 93 94 95 96
		if (unlikely(!percpu_populate(__pdata, size, gfp, cpu))) {
			__percpu_depopulate_mask(__pdata, &populated);
			return -ENOMEM;
		} else
			cpu_set(cpu, populated);
	return 0;
}
97 98 99

#define percpu_populate_mask(__pdata, size, gfp, mask) \
	__percpu_populate_mask((__pdata), (size), (gfp), &(mask))
100 101

/**
102
 * alloc_percpu - initial setup of per-cpu data
103
 * @size: size of per-cpu object
104
 * @align: alignment
105
 *
106 107
 * Allocate dynamic percpu area.  Percpu objects are populated with
 * zeroed buffers.
108
 */
109
void *__alloc_percpu(size_t size, size_t align)
110
{
111 112 113 114
	/*
	 * We allocate whole cache lines to avoid false sharing
	 */
	size_t sz = roundup(nr_cpu_ids * sizeof(void *), cache_line_size());
115
	void *pdata = kzalloc(sz, GFP_KERNEL);
116 117
	void *__pdata = __percpu_disguise(pdata);

118 119 120 121 122 123 124
	/*
	 * Can't easily make larger alignment work with kmalloc.  WARN
	 * on it.  Larger alignment should only be used for module
	 * percpu sections on SMP for which this path isn't used.
	 */
	WARN_ON_ONCE(align > __alignof__(unsigned long long));

125 126
	if (unlikely(!pdata))
		return NULL;
127 128
	if (likely(!__percpu_populate_mask(__pdata, size, GFP_KERNEL,
					   &cpu_possible_map)))
129 130 131 132
		return __pdata;
	kfree(pdata);
	return NULL;
}
133
EXPORT_SYMBOL_GPL(__alloc_percpu);
134 135

/**
136
 * free_percpu - final cleanup of per-cpu data
137 138 139 140 141
 * @__pdata: object to clean up
 *
 * We simply clean up any per-cpu object left. No need for the client to
 * track and specify through a bis mask which per-cpu objects are to free.
 */
142
void free_percpu(void *__pdata)
143
{
144 145
	if (unlikely(!__pdata))
		return;
146 147 148
	__percpu_depopulate_mask(__pdata, &cpu_possible_map);
	kfree(__percpu_disguise(__pdata));
}
149
EXPORT_SYMBOL_GPL(free_percpu);