blob: 31795e619273e439ad3d869e63c42a6b153c39a1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __LINUX_PERCPU_H
2#define __LINUX_PERCPU_H
Martin Peschke7ff6f082006-09-25 23:31:21 -07003
Sasha Levin309381fea2014-01-23 15:52:54 -08004#include <linux/mmdebug.h>
Robert P. J. Day0a3021f2007-07-15 23:39:57 -07005#include <linux/preempt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/smp.h>
Martin Peschke7ff6f082006-09-25 23:31:21 -07007#include <linux/cpumask.h>
Steven Rostedt (Red Hat)04b74b22014-11-21 09:16:58 -05008#include <linux/printk.h>
Tejun Heo6a242902009-03-06 14:33:58 +09009#include <linux/pfn.h>
Tejun Heode380b52010-03-24 17:06:43 +090010#include <linux/init.h>
Martin Peschke7ff6f082006-09-25 23:31:21 -070011
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <asm/percpu.h>
13
Tejun Heo6a242902009-03-06 14:33:58 +090014/* enough to cover all DEFINE_PER_CPUs in modules */
Jeremy Fitzhardingeb00742d32007-05-02 19:27:11 +020015#ifdef CONFIG_MODULES
Tejun Heo6a242902009-03-06 14:33:58 +090016#define PERCPU_MODULE_RESERVE (8 << 10)
Jeremy Fitzhardingeb00742d32007-05-02 19:27:11 +020017#else
Tejun Heo6a242902009-03-06 14:33:58 +090018#define PERCPU_MODULE_RESERVE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#endif
20
Tejun Heo8d408b42009-02-24 11:57:21 +090021/* minimum unit size, also is the maximum supported allocation size */
Tejun Heo6abad5a2010-09-03 18:22:47 +020022#define PCPU_MIN_UNIT_SIZE PFN_ALIGN(32 << 10)
Tejun Heo8d408b42009-02-24 11:57:21 +090023
Dennis Zhou (Facebook)d2f3c382017-07-24 19:02:09 -040024/* minimum allocation size and shift in bytes */
25#define PCPU_MIN_ALLOC_SHIFT 2
26#define PCPU_MIN_ALLOC_SIZE (1 << PCPU_MIN_ALLOC_SHIFT)
27
Tejun Heo8d408b42009-02-24 11:57:21 +090028/*
Dennis Zhou (Facebook)ca460b32017-07-24 19:02:12 -040029 * This determines the size of each metadata block. There are several subtle
30 * constraints around this constant. The reserved region must be a multiple of
31 * PCPU_BITMAP_BLOCK_SIZE. Additionally, PCPU_BITMAP_BLOCK_SIZE must be a
32 * multiple of PAGE_SIZE or PAGE_SIZE must be a multiple of
33 * PCPU_BITMAP_BLOCK_SIZE to align with the populated page map. The unit_size
34 * also has to be a multiple of PCPU_BITMAP_BLOCK_SIZE to ensure full blocks.
35 */
36#define PCPU_BITMAP_BLOCK_SIZE PAGE_SIZE
37#define PCPU_BITMAP_BLOCK_BITS (PCPU_BITMAP_BLOCK_SIZE >> \
38 PCPU_MIN_ALLOC_SHIFT)
39
40/*
Tejun Heo099a19d2010-06-27 18:50:00 +020041 * Percpu allocator can serve percpu allocations before slab is
42 * initialized which allows slab to depend on the percpu allocator.
43 * The following two parameters decide how much resource to
44 * preallocate for this. Keep PERCPU_DYNAMIC_RESERVE equal to or
45 * larger than PERCPU_DYNAMIC_EARLY_SIZE.
46 */
47#define PERCPU_DYNAMIC_EARLY_SLOTS 128
48#define PERCPU_DYNAMIC_EARLY_SIZE (12 << 10)
49
50/*
Tejun Heo8d408b42009-02-24 11:57:21 +090051 * PERCPU_DYNAMIC_RESERVE indicates the amount of free area to piggy
Tejun Heo6b19b0c2009-03-06 14:33:59 +090052 * back on the first chunk for dynamic percpu allocation if arch is
53 * manually allocating and mapping it for faster access (as a part of
54 * large page mapping for example).
Tejun Heo8d408b42009-02-24 11:57:21 +090055 *
Tejun Heo6b19b0c2009-03-06 14:33:59 +090056 * The following values give between one and two pages of free space
57 * after typical minimal boot (2-way SMP, single disk and NIC) with
58 * both defconfig and a distro config on x86_64 and 32. More
59 * intelligent way to determine this would be nice.
Tejun Heo8d408b42009-02-24 11:57:21 +090060 */
Tejun Heo6b19b0c2009-03-06 14:33:59 +090061#if BITS_PER_LONG > 32
Tejun Heo1a4d7602014-09-02 14:46:05 -040062#define PERCPU_DYNAMIC_RESERVE (28 << 10)
Tejun Heo6b19b0c2009-03-06 14:33:59 +090063#else
Tejun Heo1a4d7602014-09-02 14:46:05 -040064#define PERCPU_DYNAMIC_RESERVE (20 << 10)
Tejun Heo6b19b0c2009-03-06 14:33:59 +090065#endif
Tejun Heo8d408b42009-02-24 11:57:21 +090066
Tejun Heofbf59bc2009-02-20 16:29:08 +090067extern void *pcpu_base_addr;
Tejun Heofb435d52009-08-14 15:00:51 +090068extern const unsigned long *pcpu_unit_offsets;
Tejun Heofbf59bc2009-02-20 16:29:08 +090069
Tejun Heofd1e8a12009-08-14 15:00:51 +090070struct pcpu_group_info {
71 int nr_units; /* aligned # of units */
72 unsigned long base_offset; /* base address offset */
73 unsigned int *cpu_map; /* unit->cpu map, empty
74 * entries contain NR_CPUS */
75};
76
77struct pcpu_alloc_info {
78 size_t static_size;
79 size_t reserved_size;
80 size_t dyn_size;
81 size_t unit_size;
82 size_t atom_size;
83 size_t alloc_size;
84 size_t __ai_size; /* internal, don't use */
85 int nr_groups; /* 0 if grouping unnecessary */
86 struct pcpu_group_info groups[];
87};
88
Tejun Heof58dc012009-08-14 15:00:50 +090089enum pcpu_fc {
90 PCPU_FC_AUTO,
91 PCPU_FC_EMBED,
92 PCPU_FC_PAGE,
Tejun Heof58dc012009-08-14 15:00:50 +090093
94 PCPU_FC_NR,
95};
Andi Kleen17f36092012-10-04 17:12:07 -070096extern const char * const pcpu_fc_names[PCPU_FC_NR];
Tejun Heof58dc012009-08-14 15:00:50 +090097
98extern enum pcpu_fc pcpu_chosen_fc;
99
Tejun Heo3cbc8562009-08-14 15:00:50 +0900100typedef void * (*pcpu_fc_alloc_fn_t)(unsigned int cpu, size_t size,
101 size_t align);
Tejun Heod4b95f82009-07-04 08:10:59 +0900102typedef void (*pcpu_fc_free_fn_t)(void *ptr, size_t size);
103typedef void (*pcpu_fc_populate_pte_fn_t)(unsigned long addr);
Tejun Heoa530b792009-07-04 08:11:00 +0900104typedef int (pcpu_fc_cpu_distance_fn_t)(unsigned int from, unsigned int to);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900105
Tejun Heofd1e8a12009-08-14 15:00:51 +0900106extern struct pcpu_alloc_info * __init pcpu_alloc_alloc_info(int nr_groups,
107 int nr_units);
108extern void __init pcpu_free_alloc_info(struct pcpu_alloc_info *ai);
Tejun Heo033e48f2009-08-14 15:00:51 +0900109
Tejun Heofb435d52009-08-14 15:00:51 +0900110extern int __init pcpu_setup_first_chunk(const struct pcpu_alloc_info *ai,
111 void *base_addr);
Tejun Heo8d408b42009-02-24 11:57:21 +0900112
Tejun Heo08fc4582009-08-14 15:00:49 +0900113#ifdef CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK
Tejun Heo4ba6ce22010-06-27 18:49:59 +0200114extern int __init pcpu_embed_first_chunk(size_t reserved_size, size_t dyn_size,
Tejun Heoc8826dd2009-08-14 15:00:52 +0900115 size_t atom_size,
116 pcpu_fc_cpu_distance_fn_t cpu_distance_fn,
117 pcpu_fc_alloc_fn_t alloc_fn,
118 pcpu_fc_free_fn_t free_fn);
Tejun Heo08fc4582009-08-14 15:00:49 +0900119#endif
Tejun Heo66c3a752009-03-10 16:27:48 +0900120
Tejun Heo08fc4582009-08-14 15:00:49 +0900121#ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK
Tejun Heofb435d52009-08-14 15:00:51 +0900122extern int __init pcpu_page_first_chunk(size_t reserved_size,
Tejun Heod4b95f82009-07-04 08:10:59 +0900123 pcpu_fc_alloc_fn_t alloc_fn,
124 pcpu_fc_free_fn_t free_fn,
125 pcpu_fc_populate_pte_fn_t populate_pte_fn);
Tejun Heo08fc4582009-08-14 15:00:49 +0900126#endif
Tejun Heod4b95f82009-07-04 08:10:59 +0900127
Rusty Russelle0fdb0e2009-10-29 22:34:15 +0900128extern void __percpu *__alloc_reserved_percpu(size_t size, size_t align);
Thomas Gleixner383776f2017-02-27 15:37:36 +0100129extern bool __is_kernel_percpu_address(unsigned long addr, unsigned long *can_addr);
Tejun Heo10fad5e2010-03-10 18:57:54 +0900130extern bool is_kernel_percpu_address(unsigned long addr);
Tejun Heof2a82052009-02-20 16:29:08 +0900131
Tejun Heobbddff02010-09-03 18:22:48 +0200132#if !defined(CONFIG_SMP) || !defined(CONFIG_HAVE_SETUP_PER_CPU_AREA)
Tejun Heoe74e3962009-03-30 19:07:44 +0900133extern void __init setup_per_cpu_areas(void);
134#endif
135
Tejun Heo5835d962014-09-02 14:46:04 -0400136extern void __percpu *__alloc_percpu_gfp(size_t size, size_t align, gfp_t gfp);
Tejun Heode380b52010-03-24 17:06:43 +0900137extern void __percpu *__alloc_percpu(size_t size, size_t align);
138extern void free_percpu(void __percpu *__pdata);
139extern phys_addr_t per_cpu_ptr_to_phys(void *addr);
140
Tejun Heo5835d962014-09-02 14:46:04 -0400141#define alloc_percpu_gfp(type, gfp) \
142 (typeof(type) __percpu *)__alloc_percpu_gfp(sizeof(type), \
143 __alignof__(type), gfp)
144#define alloc_percpu(type) \
145 (typeof(type) __percpu *)__alloc_percpu(sizeof(type), \
146 __alignof__(type))
Tejun Heof2a82052009-02-20 16:29:08 +0900147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148#endif /* __LINUX_PERCPU_H */