Christoph Lameter | d00bcc9 | 2006-09-25 23:31:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/mm/allocpercpu.c |
| 3 | * |
| 4 | * Separated from slab.c August 11, 2006 Christoph Lameter <clameter@sgi.com> |
| 5 | */ |
| 6 | #include <linux/mm.h> |
| 7 | #include <linux/module.h> |
| 8 | |
| 9 | /** |
| 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 |
| 13 | * |
| 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. |
| 16 | */ |
| 17 | void percpu_depopulate(void *__pdata, int cpu) |
| 18 | { |
| 19 | struct percpu_data *pdata = __percpu_disguise(__pdata); |
| 20 | if (pdata->ptrs[cpu]) { |
| 21 | kfree(pdata->ptrs[cpu]); |
| 22 | pdata->ptrs[cpu] = NULL; |
| 23 | } |
| 24 | } |
| 25 | EXPORT_SYMBOL_GPL(percpu_depopulate); |
| 26 | |
| 27 | /** |
| 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 |
| 31 | */ |
| 32 | void __percpu_depopulate_mask(void *__pdata, cpumask_t *mask) |
| 33 | { |
| 34 | int cpu; |
| 35 | for_each_cpu_mask(cpu, *mask) |
| 36 | percpu_depopulate(__pdata, cpu); |
| 37 | } |
| 38 | EXPORT_SYMBOL_GPL(__percpu_depopulate_mask); |
| 39 | |
| 40 | /** |
| 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 |
| 46 | * |
| 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. |
| 50 | */ |
| 51 | void *percpu_populate(void *__pdata, size_t size, gfp_t gfp, int cpu) |
| 52 | { |
| 53 | struct percpu_data *pdata = __percpu_disguise(__pdata); |
| 54 | int node = cpu_to_node(cpu); |
| 55 | |
| 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); |
| 60 | if (pdata->ptrs[cpu]) |
| 61 | memset(pdata->ptrs[cpu], 0, size); |
| 62 | } else |
| 63 | pdata->ptrs[cpu] = kzalloc(size, gfp); |
| 64 | return pdata->ptrs[cpu]; |
| 65 | } |
| 66 | EXPORT_SYMBOL_GPL(percpu_populate); |
| 67 | |
| 68 | /** |
| 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 |
| 74 | * |
| 75 | * Per-cpu objects are populated with zeroed buffers. |
| 76 | */ |
| 77 | int __percpu_populate_mask(void *__pdata, size_t size, gfp_t gfp, |
| 78 | cpumask_t *mask) |
| 79 | { |
| 80 | cpumask_t populated = CPU_MASK_NONE; |
| 81 | int cpu; |
| 82 | |
| 83 | for_each_cpu_mask(cpu, *mask) |
| 84 | if (unlikely(!percpu_populate(__pdata, size, gfp, cpu))) { |
| 85 | __percpu_depopulate_mask(__pdata, &populated); |
| 86 | return -ENOMEM; |
| 87 | } else |
| 88 | cpu_set(cpu, populated); |
| 89 | return 0; |
| 90 | } |
| 91 | EXPORT_SYMBOL_GPL(__percpu_populate_mask); |
| 92 | |
| 93 | /** |
| 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 |
| 98 | * |
| 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. |
| 102 | */ |
| 103 | void *__percpu_alloc_mask(size_t size, gfp_t gfp, cpumask_t *mask) |
| 104 | { |
| 105 | void *pdata = kzalloc(sizeof(struct percpu_data), gfp); |
| 106 | void *__pdata = __percpu_disguise(pdata); |
| 107 | |
| 108 | if (unlikely(!pdata)) |
| 109 | return NULL; |
| 110 | if (likely(!__percpu_populate_mask(__pdata, size, gfp, mask))) |
| 111 | return __pdata; |
| 112 | kfree(pdata); |
| 113 | return NULL; |
| 114 | } |
| 115 | EXPORT_SYMBOL_GPL(__percpu_alloc_mask); |
| 116 | |
| 117 | /** |
| 118 | * percpu_free - final cleanup of per-cpu data |
| 119 | * @__pdata: object to clean up |
| 120 | * |
| 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. |
| 123 | */ |
| 124 | void percpu_free(void *__pdata) |
| 125 | { |
| 126 | __percpu_depopulate_mask(__pdata, &cpu_possible_map); |
| 127 | kfree(__percpu_disguise(__pdata)); |
| 128 | } |
| 129 | EXPORT_SYMBOL_GPL(percpu_free); |