percpu.h 4.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2
#ifndef __LINUX_PERCPU_H
#define __LINUX_PERCPU_H
3

4
#include <linux/mmdebug.h>
5
#include <linux/preempt.h>
L
Linus Torvalds 已提交
6
#include <linux/smp.h>
7
#include <linux/cpumask.h>
T
Tejun Heo 已提交
8
#include <linux/pfn.h>
9
#include <linux/init.h>
10

L
Linus Torvalds 已提交
11 12
#include <asm/percpu.h>

T
Tejun Heo 已提交
13
/* enough to cover all DEFINE_PER_CPUs in modules */
14
#ifdef CONFIG_MODULES
T
Tejun Heo 已提交
15
#define PERCPU_MODULE_RESERVE		(8 << 10)
16
#else
T
Tejun Heo 已提交
17
#define PERCPU_MODULE_RESERVE		0
L
Linus Torvalds 已提交
18 19
#endif

T
Tejun Heo 已提交
20
#ifndef PERCPU_ENOUGH_ROOM
21
#define PERCPU_ENOUGH_ROOM						\
T
Tejun Heo 已提交
22 23 24
	(ALIGN(__per_cpu_end - __per_cpu_start, SMP_CACHE_BYTES) +	\
	 PERCPU_MODULE_RESERVE)
#endif
25

26
/* minimum unit size, also is the maximum supported allocation size */
27
#define PCPU_MIN_UNIT_SIZE		PFN_ALIGN(32 << 10)
28

29 30 31 32 33 34 35 36 37 38
/*
 * Percpu allocator can serve percpu allocations before slab is
 * initialized which allows slab to depend on the percpu allocator.
 * The following two parameters decide how much resource to
 * preallocate for this.  Keep PERCPU_DYNAMIC_RESERVE equal to or
 * larger than PERCPU_DYNAMIC_EARLY_SIZE.
 */
#define PERCPU_DYNAMIC_EARLY_SLOTS	128
#define PERCPU_DYNAMIC_EARLY_SIZE	(12 << 10)

39 40
/*
 * PERCPU_DYNAMIC_RESERVE indicates the amount of free area to piggy
41 42 43
 * back on the first chunk for dynamic percpu allocation if arch is
 * manually allocating and mapping it for faster access (as a part of
 * large page mapping for example).
44
 *
45 46 47 48
 * The following values give between one and two pages of free space
 * after typical minimal boot (2-way SMP, single disk and NIC) with
 * both defconfig and a distro config on x86_64 and 32.  More
 * intelligent way to determine this would be nice.
49
 */
50
#if BITS_PER_LONG > 32
51
#define PERCPU_DYNAMIC_RESERVE		(28 << 10)
52
#else
53
#define PERCPU_DYNAMIC_RESERVE		(20 << 10)
54
#endif
55

56
extern void *pcpu_base_addr;
T
Tejun Heo 已提交
57
extern const unsigned long *pcpu_unit_offsets;
L
Linus Torvalds 已提交
58

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
struct pcpu_group_info {
	int			nr_units;	/* aligned # of units */
	unsigned long		base_offset;	/* base address offset */
	unsigned int		*cpu_map;	/* unit->cpu map, empty
						 * entries contain NR_CPUS */
};

struct pcpu_alloc_info {
	size_t			static_size;
	size_t			reserved_size;
	size_t			dyn_size;
	size_t			unit_size;
	size_t			atom_size;
	size_t			alloc_size;
	size_t			__ai_size;	/* internal, don't use */
	int			nr_groups;	/* 0 if grouping unnecessary */
	struct pcpu_group_info	groups[];
};

78 79 80 81 82 83 84
enum pcpu_fc {
	PCPU_FC_AUTO,
	PCPU_FC_EMBED,
	PCPU_FC_PAGE,

	PCPU_FC_NR,
};
85
extern const char * const pcpu_fc_names[PCPU_FC_NR];
86 87 88

extern enum pcpu_fc pcpu_chosen_fc;

89 90
typedef void * (*pcpu_fc_alloc_fn_t)(unsigned int cpu, size_t size,
				     size_t align);
91 92
typedef void (*pcpu_fc_free_fn_t)(void *ptr, size_t size);
typedef void (*pcpu_fc_populate_pte_fn_t)(unsigned long addr);
93
typedef int (pcpu_fc_cpu_distance_fn_t)(unsigned int from, unsigned int to);
94

95 96 97 98
extern struct pcpu_alloc_info * __init pcpu_alloc_alloc_info(int nr_groups,
							     int nr_units);
extern void __init pcpu_free_alloc_info(struct pcpu_alloc_info *ai);

T
Tejun Heo 已提交
99 100
extern int __init pcpu_setup_first_chunk(const struct pcpu_alloc_info *ai,
					 void *base_addr);
101

102
#ifdef CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK
103
extern int __init pcpu_embed_first_chunk(size_t reserved_size, size_t dyn_size,
104 105 106 107
				size_t atom_size,
				pcpu_fc_cpu_distance_fn_t cpu_distance_fn,
				pcpu_fc_alloc_fn_t alloc_fn,
				pcpu_fc_free_fn_t free_fn);
108
#endif
109

110
#ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK
T
Tejun Heo 已提交
111
extern int __init pcpu_page_first_chunk(size_t reserved_size,
112 113 114
				pcpu_fc_alloc_fn_t alloc_fn,
				pcpu_fc_free_fn_t free_fn,
				pcpu_fc_populate_pte_fn_t populate_pte_fn);
115
#endif
116

R
Rusty Russell 已提交
117
extern void __percpu *__alloc_reserved_percpu(size_t size, size_t align);
118
extern bool is_kernel_percpu_address(unsigned long addr);
L
Linus Torvalds 已提交
119

120
#if !defined(CONFIG_SMP) || !defined(CONFIG_HAVE_SETUP_PER_CPU_AREA)
121 122
extern void __init setup_per_cpu_areas(void);
#endif
123
extern void __init percpu_init_late(void);
124

125
extern void __percpu *__alloc_percpu_gfp(size_t size, size_t align, gfp_t gfp);
126 127 128 129
extern void __percpu *__alloc_percpu(size_t size, size_t align);
extern void free_percpu(void __percpu *__pdata);
extern phys_addr_t per_cpu_ptr_to_phys(void *addr);

130 131 132 133 134 135
#define alloc_percpu_gfp(type, gfp)					\
	(typeof(type) __percpu *)__alloc_percpu_gfp(sizeof(type),	\
						__alignof__(type), gfp)
#define alloc_percpu(type)						\
	(typeof(type) __percpu *)__alloc_percpu(sizeof(type),		\
						__alignof__(type))
L
Linus Torvalds 已提交
136

137 138 139
/* To avoid include hell, as printk can not declare this, we declare it here */
DECLARE_PER_CPU(printk_func_t, printk_func);

L
Linus Torvalds 已提交
140
#endif /* __LINUX_PERCPU_H */