mm: Reimplementation of alloc_percpu - move qsort from xfs/support to linux/lib
Subsystems (4)
PATCH 1/2 mm: Reimplementation of alloc_percpu - move qsort from xfs/support to linux/lib
Here's a patch to move qsort.c from fs/xfs/support/ to linux/lib so that alloc_percpu reimplementation can use it.
Thanks,
Kiran
Patch to move qsort routine from the xfs file system to linux/lib so that other subsystems can make use of it.
---
fs/xfs/Makefile | 1 fs/xfs/linux-2.6/xfs_linux.h | 2 fs/xfs/support/qsort.c | 155 ------------------------------------------- fs/xfs/support/qsort.h | 41 ----------- include/linux/qsort.h | 41 +++++++++++ lib/Makefile | 3 lib/qsort.c | 155 +++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 199 insertions(+), 199 deletions(-)
fs/xfs/linux-2.6/xfs_linux.h +1 -1
@@ -64,7 +64,6 @@#include <sema.h> #include <time.h> -#include <support/qsort.h> #include <support/ktrace.h> #include <support/debug.h> #include <support/move.h>
@@ -88,6 +87,7 @@#include <linux/list.h> #include <linux/proc_fs.h> #include <linux/version.h> +#include <linux/qsort.h> #include <asm/page.h> #include <asm/div64.h>
fs/xfs/Makefile +0 -1
@@ -143,7 +143,6 @@ xfs-y += $(addprefix support/, \ debug.o \ move.o \ - qsort.o \ uuid.o) xfs-$(CONFIG_XFS_TRACE) += support/ktrace.o
fs/xfs/support/qsort.c +0 -155
155 lines not on this page · raw message
fs/xfs/support/qsort.h +0 -41
41 lines not on this page · raw message
include/linux/qsort.h +41 -0
41 lines not on this page · raw message
lib/Makefile +2 -1
@@ -5,7 +5,8 @@ lib-y := errno.o ctype.o string.o vsprintf.o cmdline.o \ bust_spinlocks.o rbtree.o radix-tree.o dump_stack.o \ kobject.o kref.o idr.o div64.o parser.o int_sqrt.o \ - bitmap.o extable.o kobject_uevent.o prio_tree.o + bitmap.o extable.o kobject_uevent.o prio_tree.o \ + qsort.o ifeq ($(CONFIG_DEBUG_KOBJECT),y) CFLAGS_kobject.o += -DDEBUG
lib/qsort.c +155 -0
155 lines not on this page · raw message
Re: [patch 2/2] mm: Reimplementation of alloc_percpu
Here's the alloc_percpu reimplementation changed to
- Use qsort
- Use GFP_KERNEL|__GFP_HIGHMEM|__GFP_ZERO for BLOCK_MANAGEMENT_PAGES (GFP_HIGHZERO would have been ideal)
- Changed currency size to sizeof (int) from sizeof (void *) for better utilization for small objects
The allocator can be easily modified to use __per_cpu_offset[] table at a later stage by:
1. Allocating ALIGN(__per_cpu_end - __per_cpu_start, PAGE_SIZE) for the static percpu areas and populating __per_cpu_offset[] offset table
2. Making PCPU_BLKSIZE same as the static per cpu area size above
3. Serving dynamic percpu requests from modules etc from blocks by returning ret -= __per_cpu_offset[0] from a percpu block. This way modules need not have a limit on static percpu areas.
Thanks,
Kiran
---
The following patch re-implements the linux dynamic percpu memory allocator so that:
1. Percpu memory dereference is faster
- One less memory reference compared to existing simple alloc_percpu
- As fast as with static percpu areas, one mem ref less actually.
2. Better memory usage
- Doesn't need a NR_CPUS pointer array for each allocation
- Interlaces objects making better utilization of memory/cachelines
- Userspace tests show 98% utilization with random sized allocations after repeated random frees. Utilization with small sized allocations (counters) expected to be better than random sized allocations.
3. Provides truly node local allocation
- The percpu memory with existing alloc_percpu does node local allocation, but the NR_CPUS place holder is not node local. This problem doesn't exist with the new implementation.
Design:
We have "blocks" of memory akin to slabs. Each block has
(percpu blocksize) * NR_CPUS + (block management data structures) of kernel VA space allocated to it. Node local pages are allocated and mapped against the corresponding cpus' VA space. Pages are allocated for block management data structures and mapped to their corresponding VA. These reside at (percpu blocksize) * NR_CPUS offset from the beginning of the block. The allocator maintains a circular linked list of blocks, sorted in descending order of block utilization. On requests for objects, allocator tries to serve the request from the most utilized block.
The allocator allocates memory in multiples of a fixed currency size for a request. The allocator returns address of the percpu
object corresponding to cpu0. The cpu local variable for any given cpu can be obtained by simple arithmetic:obj_address + cpu_id * PCPU_BLKSIZE.
Testing:
The block allocator has undergone some userspace stress testing with small counter sized objects as well as a large number of random sized objects.
Changes:
- Allocator uses qsort now
- Use GFP_KERNEL|__GFP_HIGHMEM|__GFP_ZERO for block management pages
- Changed currency size to sizeof (int) from sizeof (void *)
---
include/linux/kernel.h | 2 include/linux/percpu.h | 18 - mm/Makefile | 1 mm/percpu.c | 686 +++++++++++++++++++++++++++++++++++++++++++++++++ mm/slab.c | 70 ----- 5 files changed, 696 insertions(+), 81 deletions(-)
include/linux/kernel.h +2 -0
@@ -29,6 +29,8 @@#define ARRAY_SIZE (x) (sizeof(x) / sizeof((x)[0]))#define ALIGN (x,a) (((x)+(a)-1)&~((a)-1)) +#define IS_ALIGNED (x,a) (!(((a) - 1) & (x))) +#define IS_POWEROFTWO (x) (!(((x) - 1) & (x)))#define KERN_EMERG "<0>"/* system is unusable */ #define KERN_ALERT "<1>"/* action must be taken immediately */
include/linux/percpu.h +7 -11
@@ -15,23 +15,19 @@#define get_cpu_var (var) (*({ preempt_disable(); &__get_cpu_var(var); }))#define put_cpu_var (var) preempt_enable() -#ifdef CONFIG_SMP - -struct percpu_data { -void *ptrs[NR_CPUS ]; -void *blkp; -}; +/* This is the upper bound for an object using alloc_percpu */ +#define PCPU_BLKSIZE (PAGE_SIZE << 1) +#define PCPU_CURR_SIZE (sizeof (int)) +#ifdef CONFIG_SMP /* * Use this to get to a cpu's version of the per-cpu object allocated using * alloc_percpu. Non-atomic access to the current CPU's version should * probably be combined with get_cpu()/put_cpu(). */ #define per_cpu_ptr (ptr, cpu) \ -({ \ - struct percpu_data *__p = (struct percpu_data *)~(unsigned long)(ptr); \ - (__typeof__(ptr))__p->ptrs[(cpu)]; \ -}) + ((__typeof__(ptr)) \ + (RELOC_HIDE(ptr, PCPU_BLKSIZE * cpu)))extern void *__alloc_percpu (size_t size,size_t align);extern void free_percpu (const void *);
@@ -56,6 +52,6 @@/* Simple wrapper for the common case: zeros memory. */ #define alloc_percpu (type) \ - ((type *)(__alloc_percpu(sizeof(type), __alignof__(type)))) + ((type *)(__alloc_percpu(ALIGN(sizeof (type), PCPU_CURR_SIZE), __alignof__(type))))#endif /* __LINUX_PERCPU_H */
mm/Makefile +1 -0
@@ -17,4 +17,5 @@ obj-$(CONFIG_NUMA) += mempolicy.o obj-$(CONFIG_SHMEM) += shmem.o obj-$(CONFIG_TINY_SHMEM) += tiny-shmem.o +obj-$(CONFIG_SMP) += percpu.o
mm/percpu.c +686 -0
686 lines not on this page · raw message
mm/slab.c +0 -70
82 lines not on this page · raw message
Re: [patch 2/2] mm: Reimplementation of alloc_percpu
Quoted 7 lines · Ravikiran G Thirumalai
Here's the
alloc_percpureimplementation changed to
- Use qsort
- Use GFP_KERNEL|__GFP_HIGHMEM|__GFP_ZERO forBLOCK_MANAGEMENT_PAGES(GFP_HIGHZEROwould have been ideal)
- Changed currency size to sizeof (int) from sizeof (void *) for better utilization for small objects
Quoted 8 lines
The allocator can be easily modified to use __per_cpu_offset[] table at a later stage by:
1. AllocatingALIGN(__per_cpu_end - __per_cpu_start, PAGE_SIZE)for the static percpu areas and populating __per_cpu_offset[] offset table
2. MakingPCPU_BLKSIZEsame as the static per cpu area size above
3. Serving dynamic percpu requests from modules etc from blocks by returning ret -= __per_cpu_offset[0] from a percpu block. This way modules need not have a limit on static percpu areas.
Unfortunately ia64 breaks (3). They have pinned TLB entries covering 64k, which they put the static per-cpu data into. This is used for local_inc, etc, and David Mosberger loved that trick (this is why my version allocated from that first reserved block for modules' static per-cpu vars).
Hope that clarifies,
Rusty.
Signature · 1 line
A bad analogy is like a leaky screwdriver -- Richard Braakman
Re: [patch 2/2] mm: Reimplementation of alloc_percpu
Quoted 11 lines · Rusty Russell
Quoted 9 lines · Ravikiran G Thirumalai
...
The allocator can be easily modified to use __per_cpu_offset[] table at a later stage by:
1. AllocatingALIGN(__per_cpu_end - __per_cpu_start, PAGE_SIZE)for the static percpu areas and populating __per_cpu_offset[] offset table
2. MakingPCPU_BLKSIZEsame as the static per cpu area size above
3. Serving dynamic percpu requests from modules etc from blocks by returning ret -= __per_cpu_offset[0] from a percpu block. This way modules need not have a limit on static percpu areas.
Quoted 5 lines
Unfortunately ia64 breaks (3). They have pinned TLB entries covering 64k, which they put the static per-cpu data into. This is used for
local_inc, etc, and David Mosberger loved that trick (this is why my version allocated from that first reserved block for modules' static per-cpu vars).
Hmmm... then if we change (1) to allocate PERCPU_ENOUGH_ROOM, then the math will work out? We will still have a limit on static per-cpu areas in modules, but alloc_percpu can use the same __per_cpu_offset table[]. Will this work?
But, what I am concerned is about arches like x86_64 which currently do not maintain the relation:
__per_cpu_offset[n] = __per_cpu_offset[0] + static_percpu_size * n ---> (A) correct me if I am wrong, but both our methods for alloc_percpu to use per_cpu_offset depend on the static per-cpu areas being virtually contiguous (with relation (A) above being maintained).
If arches cannot sew up node local pages to form a virtually contiguous block, maybe because setup_per_cpu_areas happens early during boot, then we will have a problem.
So a common solution could be:
Declare a dynamic percpu offset table 'alloc_percpu_offset' or something like that, make it a static per-cpu variable. Then, the blocks can be of any uniform size, we just have to fill alloc_percpu_offset and use
(RELOC_HIDE(ptr, per_cpu(alloc_percpu_offset, cpu))))
to get to to the cpu local versions. I think dereference speeds can be fast with this method too since we use __per_cpu_offset[] indirectly. Of course this is not needed if all arches can do node local allocation and maintain relation (A) for static per-cpu areas.
Thanks,
Kiran
Re: [patch 2/2] mm: Reimplementation of alloc_percpu
Quoted 18 lines · Ravikiran G Thirumalai
Quoted 11 lines · Rusty Russell
Quoted 9 lines · Ravikiran G Thirumalai
...
The allocator can be easily modified to use __per_cpu_offset[] table at a later stage by:
1. AllocatingALIGN(__per_cpu_end - __per_cpu_start, PAGE_SIZE)for the static percpu areas and populating __per_cpu_offset[] offset table
2. MakingPCPU_BLKSIZEsame as the static per cpu area size above
3. Serving dynamic percpu requests from modules etc from blocks by returning ret -= __per_cpu_offset[0] from a percpu block. This way modules need not have a limit on static percpu areas.Quoted 5 lines
Unfortunately ia64 breaks (3). They have pinned TLB entries covering 64k, which they put the static per-cpu data into. This is used for
local_inc, etc, and David Mosberger loved that trick (this is why my version allocated from that first reserved block for modules' static per-cpu vars).
Quoted 4 lines
Hmmm... then if we change (1) to allocate
PERCPU_ENOUGH_ROOM, then the math will work out? We will still have a limit on static per-cpu areas in modules, butalloc_percpucan use the same__per_cpu_offsettable[]. Will this work?
I think so.
Quoted 9 lines
But, what I am concerned is about arches like
x86_64which currently do not maintain the relation:
__per_cpu_offset[n] = __per_cpu_offset[0] +static_percpu_size* n ---> (A) correct me if I am wrong, but both our methods foralloc_percputo useper_cpu_offsetdepend on the static per-cpu areas being virtually contiguous (with relation (A) above being maintained).
If arches cannot sew up node local pages to form a virtually contiguous block, maybe becausesetup_per_cpu_areashappens early during boot, then we will have a problem.
They don't actually have to be contiguous, although that makes it easier. They can reserve virtual address space to extend their per-cpu areas. I think this is a worthwhile tradeoff if they want to do this.
Cheers,
Rusty.
Signature · 1 line
A bad analogy is like a leaky screwdriver -- Richard Braakman