Skip to content

Commit fbf59bc

Browse files
committed
percpu: implement new dynamic percpu allocator
Impact: new scalable dynamic percpu allocator which allows dynamic percpu areas to be accessed the same way as static ones Implement scalable dynamic percpu allocator which can be used for both static and dynamic percpu areas. This will allow static and dynamic areas to share faster direct access methods. This feature is optional and enabled only when CONFIG_HAVE_DYNAMIC_PER_CPU_AREA is defined by arch. Please read comment on top of mm/percpu.c for details. Signed-off-by: Tejun Heo <tj@kernel.org> Cc: Andrew Morton <akpm@linux-foundation.org>
1 parent 8fc4898 commit fbf59bc

4 files changed

Lines changed: 943 additions & 4 deletions

File tree

‎include/linux/percpu.h‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,23 +76,37 @@
7676

7777
#ifdef CONFIG_SMP
7878

79-
struct percpu_data {
80-
void *ptrs[1];
81-
};
79+
#ifdef CONFIG_HAVE_DYNAMIC_PER_CPU_AREA
8280

83-
#define __percpu_disguise(pdata) (struct percpu_data *)~(unsigned long)(pdata)
81+
extern void *pcpu_base_addr;
8482

83+
typedef void (*pcpu_populate_pte_fn_t)(unsigned long addr);
84+
85+
extern size_t __init pcpu_setup_static(pcpu_populate_pte_fn_t populate_pte_fn,
86+
struct page **pages, size_t cpu_size);
8587
/*
8688
* Use this to get to a cpu's version of the per-cpu object
8789
* dynamically allocated. Non-atomic access to the current CPU's
8890
* version should probably be combined with get_cpu()/put_cpu().
8991
*/
92+
#define per_cpu_ptr(ptr, cpu) SHIFT_PERCPU_PTR((ptr), per_cpu_offset((cpu)))
93+
94+
#else /* CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
95+
96+
struct percpu_data {
97+
void *ptrs[1];
98+
};
99+
100+
#define __percpu_disguise(pdata) (struct percpu_data *)~(unsigned long)(pdata)
101+
90102
#define per_cpu_ptr(ptr, cpu) \
91103
({ \
92104
struct percpu_data *__p = __percpu_disguise(ptr); \
93105
(__typeof__(ptr))__p->ptrs[(cpu)]; \
94106
})
95107

108+
#endif /* CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
109+
96110
extern void *__alloc_percpu(size_t size, size_t align);
97111
extern void free_percpu(void *__pdata);
98112

‎kernel/module.c‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include <linux/tracepoint.h>
5252
#include <linux/ftrace.h>
5353
#include <linux/async.h>
54+
#include <linux/percpu.h>
5455

5556
#if 0
5657
#define DEBUGP printk
@@ -366,6 +367,34 @@ static struct module *find_module(const char *name)
366367
}
367368

368369
#ifdef CONFIG_SMP
370+
371+
#ifdef CONFIG_HAVE_DYNAMIC_PER_CPU_AREA
372+
373+
static void *percpu_modalloc(unsigned long size, unsigned long align,
374+
const char *name)
375+
{
376+
void *ptr;
377+
378+
if (align > PAGE_SIZE) {
379+
printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
380+
name, align, PAGE_SIZE);
381+
align = PAGE_SIZE;
382+
}
383+
384+
ptr = __alloc_percpu(size, align);
385+
if (!ptr)
386+
printk(KERN_WARNING
387+
"Could not allocate %lu bytes percpu data\n", size);
388+
return ptr;
389+
}
390+
391+
static void percpu_modfree(void *freeme)
392+
{
393+
free_percpu(freeme);
394+
}
395+
396+
#else /* ... !CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
397+
369398
/* Number of blocks used and allocated. */
370399
static unsigned int pcpu_num_used, pcpu_num_allocated;
371400
/* Size of each block. -ve means used. */
@@ -499,6 +528,8 @@ static int percpu_modinit(void)
499528
}
500529
__initcall(percpu_modinit);
501530

531+
#endif /* CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
532+
502533
static unsigned int find_pcpusec(Elf_Ehdr *hdr,
503534
Elf_Shdr *sechdrs,
504535
const char *secstrings)

‎mm/Makefile‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ obj-$(CONFIG_FAILSLAB) += failslab.o
3030
obj-$(CONFIG_MEMORY_HOTPLUG) += memory_hotplug.o
3131
obj-$(CONFIG_FS_XIP) += filemap_xip.o
3232
obj-$(CONFIG_MIGRATION) += migrate.o
33+
ifdef CONFIG_HAVE_DYNAMIC_PER_CPU_AREA
34+
obj-$(CONFIG_SMP) += percpu.o
35+
else
3336
obj-$(CONFIG_SMP) += allocpercpu.o
37+
endif
3438
obj-$(CONFIG_QUICKLIST) += quicklist.o
3539
obj-$(CONFIG_CGROUP_MEM_RES_CTLR) += memcontrol.o page_cgroup.o

0 commit comments

Comments
 (0)