blob: 4bc6dafb703eaa6ec3d0f1d6deff3cbb82a82f06 [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
24/*
Tejun Heo099a19d2010-06-27 18:50:00 +020025 * Percpu allocator can serve percpu allocations before slab is
26 * initialized which allows slab to depend on the percpu allocator.
27 * The following two parameters decide how much resource to
28 * preallocate for this. Keep PERCPU_DYNAMIC_RESERVE equal to or
29 * larger than PERCPU_DYNAMIC_EARLY_SIZE.
30 */
31#define PERCPU_DYNAMIC_EARLY_SLOTS 128
32#define PERCPU_DYNAMIC_EARLY_SIZE (12 << 10)
33
34/*
Tejun Heo8d408b42009-02-24 11:57:21 +090035 * PERCPU_DYNAMIC_RESERVE indicates the amount of free area to piggy
Tejun Heo6b19b0c2009-03-06 14:33:59 +090036 * back on the first chunk for dynamic percpu allocation if arch is
37 * manually allocating and mapping it for faster access (as a part of
38 * large page mapping for example).
Tejun Heo8d408b42009-02-24 11:57:21 +090039 *
Tejun Heo6b19b0c2009-03-06 14:33:59 +090040 * The following values give between one and two pages of free space
41 * after typical minimal boot (2-way SMP, single disk and NIC) with
42 * both defconfig and a distro config on x86_64 and 32. More
43 * intelligent way to determine this would be nice.
Tejun Heo8d408b42009-02-24 11:57:21 +090044 */
Tejun Heo6b19b0c2009-03-06 14:33:59 +090045#if BITS_PER_LONG > 32
Tejun Heo1a4d7602014-09-02 14:46:05 -040046#define PERCPU_DYNAMIC_RESERVE (28 << 10)
Tejun Heo6b19b0c2009-03-06 14:33:59 +090047#else
Tejun Heo1a4d7602014-09-02 14:46:05 -040048#define PERCPU_DYNAMIC_RESERVE (20 << 10)
Tejun Heo6b19b0c2009-03-06 14:33:59 +090049#endif
Tejun Heo8d408b42009-02-24 11:57:21 +090050
Tejun Heofbf59bc2009-02-20 16:29:08 +090051extern void *pcpu_base_addr;
Tejun Heofb435d52009-08-14 15:00:51 +090052extern const unsigned long *pcpu_unit_offsets;
Tejun Heofbf59bc2009-02-20 16:29:08 +090053
Tejun Heofd1e8a12009-08-14 15:00:51 +090054struct pcpu_group_info {
55 int nr_units; /* aligned # of units */
56 unsigned long base_offset; /* base address offset */
57 unsigned int *cpu_map; /* unit->cpu map, empty
58 * entries contain NR_CPUS */
59};
60
61struct pcpu_alloc_info {
62 size_t static_size;
63 size_t reserved_size;
64 size_t dyn_size;
65 size_t unit_size;
66 size_t atom_size;
67 size_t alloc_size;
68 size_t __ai_size; /* internal, don't use */
69 int nr_groups; /* 0 if grouping unnecessary */
70 struct pcpu_group_info groups[];
71};
72
Tejun Heof58dc012009-08-14 15:00:50 +090073enum pcpu_fc {
74 PCPU_FC_AUTO,
75 PCPU_FC_EMBED,
76 PCPU_FC_PAGE,
Tejun Heof58dc012009-08-14 15:00:50 +090077
78 PCPU_FC_NR,
79};
Andi Kleen17f36092012-10-04 17:12:07 -070080extern const char * const pcpu_fc_names[PCPU_FC_NR];
Tejun Heof58dc012009-08-14 15:00:50 +090081
82extern enum pcpu_fc pcpu_chosen_fc;
83
Tejun Heo3cbc8562009-08-14 15:00:50 +090084typedef void * (*pcpu_fc_alloc_fn_t)(unsigned int cpu, size_t size,
85 size_t align);
Tejun Heod4b95f82009-07-04 08:10:59 +090086typedef void (*pcpu_fc_free_fn_t)(void *ptr, size_t size);
87typedef void (*pcpu_fc_populate_pte_fn_t)(unsigned long addr);
Tejun Heoa530b792009-07-04 08:11:00 +090088typedef int (pcpu_fc_cpu_distance_fn_t)(unsigned int from, unsigned int to);
Tejun Heofbf59bc2009-02-20 16:29:08 +090089
Tejun Heofd1e8a12009-08-14 15:00:51 +090090extern struct pcpu_alloc_info * __init pcpu_alloc_alloc_info(int nr_groups,
91 int nr_units);
92extern void __init pcpu_free_alloc_info(struct pcpu_alloc_info *ai);
Tejun Heo033e48f2009-08-14 15:00:51 +090093
Tejun Heofb435d52009-08-14 15:00:51 +090094extern int __init pcpu_setup_first_chunk(const struct pcpu_alloc_info *ai,
95 void *base_addr);
Tejun Heo8d408b42009-02-24 11:57:21 +090096
Tejun Heo08fc4582009-08-14 15:00:49 +090097#ifdef CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK
Tejun Heo4ba6ce22010-06-27 18:49:59 +020098extern int __init pcpu_embed_first_chunk(size_t reserved_size, size_t dyn_size,
Tejun Heoc8826dd2009-08-14 15:00:52 +090099 size_t atom_size,
100 pcpu_fc_cpu_distance_fn_t cpu_distance_fn,
101 pcpu_fc_alloc_fn_t alloc_fn,
102 pcpu_fc_free_fn_t free_fn);
Tejun Heo08fc4582009-08-14 15:00:49 +0900103#endif
Tejun Heo66c3a752009-03-10 16:27:48 +0900104
Tejun Heo08fc4582009-08-14 15:00:49 +0900105#ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK
Tejun Heofb435d52009-08-14 15:00:51 +0900106extern int __init pcpu_page_first_chunk(size_t reserved_size,
Tejun Heod4b95f82009-07-04 08:10:59 +0900107 pcpu_fc_alloc_fn_t alloc_fn,
108 pcpu_fc_free_fn_t free_fn,
109 pcpu_fc_populate_pte_fn_t populate_pte_fn);
Tejun Heo08fc4582009-08-14 15:00:49 +0900110#endif
Tejun Heod4b95f82009-07-04 08:10:59 +0900111
Rusty Russelle0fdb0e2009-10-29 22:34:15 +0900112extern void __percpu *__alloc_reserved_percpu(size_t size, size_t align);
Tejun Heo10fad5e2010-03-10 18:57:54 +0900113extern bool is_kernel_percpu_address(unsigned long addr);
Tejun Heof2a82052009-02-20 16:29:08 +0900114
Tejun Heobbddff02010-09-03 18:22:48 +0200115#if !defined(CONFIG_SMP) || !defined(CONFIG_HAVE_SETUP_PER_CPU_AREA)
Tejun Heoe74e3962009-03-30 19:07:44 +0900116extern void __init setup_per_cpu_areas(void);
117#endif
Tejun Heo099a19d2010-06-27 18:50:00 +0200118extern void __init percpu_init_late(void);
Tejun Heoe74e3962009-03-30 19:07:44 +0900119
Tejun Heo5835d962014-09-02 14:46:04 -0400120extern void __percpu *__alloc_percpu_gfp(size_t size, size_t align, gfp_t gfp);
Tejun Heode380b52010-03-24 17:06:43 +0900121extern void __percpu *__alloc_percpu(size_t size, size_t align);
122extern void free_percpu(void __percpu *__pdata);
123extern phys_addr_t per_cpu_ptr_to_phys(void *addr);
124
Tejun Heo5835d962014-09-02 14:46:04 -0400125#define alloc_percpu_gfp(type, gfp) \
126 (typeof(type) __percpu *)__alloc_percpu_gfp(sizeof(type), \
127 __alignof__(type), gfp)
128#define alloc_percpu(type) \
129 (typeof(type) __percpu *)__alloc_percpu(sizeof(type), \
130 __alignof__(type))
Tejun Heof2a82052009-02-20 16:29:08 +0900131
Steven Rostedt (Red Hat)afdc34a2014-06-19 17:33:31 -0400132/* To avoid include hell, as printk can not declare this, we declare it here */
133DECLARE_PER_CPU(printk_func_t, printk_func);
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#endif /* __LINUX_PERCPU_H */