blob: 5e76af742c807afd3630e630156ab7962c26acdb [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef __LINUX_PERCPU_H
3#define __LINUX_PERCPU_H
Martin Peschke7ff6f082006-09-25 23:31:21 -07004
Sasha Levin309381fea2014-01-23 15:52:54 -08005#include <linux/mmdebug.h>
Robert P. J. Day0a3021f42007-07-15 23:39:57 -07006#include <linux/preempt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/smp.h>
Martin Peschke7ff6f082006-09-25 23:31:21 -07008#include <linux/cpumask.h>
Steven Rostedt (Red Hat)04b74b22014-11-21 09:16:58 -05009#include <linux/printk.h>
Tejun Heo6a242902009-03-06 14:33:58 +090010#include <linux/pfn.h>
Tejun Heode380b52010-03-24 17:06:43 +090011#include <linux/init.h>
Martin Peschke7ff6f082006-09-25 23:31:21 -070012
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <asm/percpu.h>
14
Tejun Heo6a242902009-03-06 14:33:58 +090015/* enough to cover all DEFINE_PER_CPUs in modules */
Jeremy Fitzhardingeb00742d32007-05-02 19:27:11 +020016#ifdef CONFIG_MODULES
Tejun Heo6a242902009-03-06 14:33:58 +090017#define PERCPU_MODULE_RESERVE (8 << 10)
Jeremy Fitzhardingeb00742d32007-05-02 19:27:11 +020018#else
Tejun Heo6a242902009-03-06 14:33:58 +090019#define PERCPU_MODULE_RESERVE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#endif
21
Tejun Heo8d408b42009-02-24 11:57:21 +090022/* minimum unit size, also is the maximum supported allocation size */
Tejun Heo6abad5a2010-09-03 18:22:47 +020023#define PCPU_MIN_UNIT_SIZE PFN_ALIGN(32 << 10)
Tejun Heo8d408b42009-02-24 11:57:21 +090024
Dennis Zhou (Facebook)d2f3c382017-07-24 19:02:09 -040025/* minimum allocation size and shift in bytes */
26#define PCPU_MIN_ALLOC_SHIFT 2
27#define PCPU_MIN_ALLOC_SIZE (1 << PCPU_MIN_ALLOC_SHIFT)
28
Tejun Heo8d408b42009-02-24 11:57:21 +090029/*
Dennis Zhoub239f7d2019-02-13 11:10:30 -080030 * The PCPU_BITMAP_BLOCK_SIZE must be the same size as PAGE_SIZE as the
31 * updating of hints is used to manage the nr_empty_pop_pages in both
32 * the chunk and globally.
Dennis Zhou (Facebook)ca460b32017-07-24 19:02:12 -040033 */
34#define PCPU_BITMAP_BLOCK_SIZE PAGE_SIZE
35#define PCPU_BITMAP_BLOCK_BITS (PCPU_BITMAP_BLOCK_SIZE >> \
36 PCPU_MIN_ALLOC_SHIFT)
37
38/*
Tejun Heo099a19d2010-06-27 18:50:00 +020039 * Percpu allocator can serve percpu allocations before slab is
40 * initialized which allows slab to depend on the percpu allocator.
41 * The following two parameters decide how much resource to
42 * preallocate for this. Keep PERCPU_DYNAMIC_RESERVE equal to or
43 * larger than PERCPU_DYNAMIC_EARLY_SIZE.
44 */
45#define PERCPU_DYNAMIC_EARLY_SLOTS 128
46#define PERCPU_DYNAMIC_EARLY_SIZE (12 << 10)
47
48/*
Tejun Heo8d408b42009-02-24 11:57:21 +090049 * PERCPU_DYNAMIC_RESERVE indicates the amount of free area to piggy
Tejun Heo6b19b0c2009-03-06 14:33:59 +090050 * back on the first chunk for dynamic percpu allocation if arch is
51 * manually allocating and mapping it for faster access (as a part of
52 * large page mapping for example).
Tejun Heo8d408b42009-02-24 11:57:21 +090053 *
Tejun Heo6b19b0c2009-03-06 14:33:59 +090054 * The following values give between one and two pages of free space
55 * after typical minimal boot (2-way SMP, single disk and NIC) with
56 * both defconfig and a distro config on x86_64 and 32. More
57 * intelligent way to determine this would be nice.
Tejun Heo8d408b42009-02-24 11:57:21 +090058 */
Tejun Heo6b19b0c2009-03-06 14:33:59 +090059#if BITS_PER_LONG > 32
Tejun Heo1a4d7602014-09-02 14:46:05 -040060#define PERCPU_DYNAMIC_RESERVE (28 << 10)
Tejun Heo6b19b0c2009-03-06 14:33:59 +090061#else
Tejun Heo1a4d7602014-09-02 14:46:05 -040062#define PERCPU_DYNAMIC_RESERVE (20 << 10)
Tejun Heo6b19b0c2009-03-06 14:33:59 +090063#endif
Tejun Heo8d408b42009-02-24 11:57:21 +090064
Tejun Heofbf59bc2009-02-20 16:29:08 +090065extern void *pcpu_base_addr;
Tejun Heofb435d52009-08-14 15:00:51 +090066extern const unsigned long *pcpu_unit_offsets;
Tejun Heofbf59bc2009-02-20 16:29:08 +090067
Tejun Heofd1e8a12009-08-14 15:00:51 +090068struct pcpu_group_info {
69 int nr_units; /* aligned # of units */
70 unsigned long base_offset; /* base address offset */
71 unsigned int *cpu_map; /* unit->cpu map, empty
72 * entries contain NR_CPUS */
73};
74
75struct pcpu_alloc_info {
76 size_t static_size;
77 size_t reserved_size;
78 size_t dyn_size;
79 size_t unit_size;
80 size_t atom_size;
81 size_t alloc_size;
82 size_t __ai_size; /* internal, don't use */
83 int nr_groups; /* 0 if grouping unnecessary */
84 struct pcpu_group_info groups[];
85};
86
Tejun Heof58dc012009-08-14 15:00:50 +090087enum pcpu_fc {
88 PCPU_FC_AUTO,
89 PCPU_FC_EMBED,
90 PCPU_FC_PAGE,
Tejun Heof58dc012009-08-14 15:00:50 +090091
92 PCPU_FC_NR,
93};
Andi Kleen17f36092012-10-04 17:12:07 -070094extern const char * const pcpu_fc_names[PCPU_FC_NR];
Tejun Heof58dc012009-08-14 15:00:50 +090095
96extern enum pcpu_fc pcpu_chosen_fc;
97
Tejun Heo3cbc8562009-08-14 15:00:50 +090098typedef void * (*pcpu_fc_alloc_fn_t)(unsigned int cpu, size_t size,
99 size_t align);
Tejun Heod4b95f82009-07-04 08:10:59 +0900100typedef void (*pcpu_fc_free_fn_t)(void *ptr, size_t size);
101typedef void (*pcpu_fc_populate_pte_fn_t)(unsigned long addr);
Tejun Heoa530b792009-07-04 08:11:00 +0900102typedef int (pcpu_fc_cpu_distance_fn_t)(unsigned int from, unsigned int to);
Tejun Heofbf59bc2009-02-20 16:29:08 +0900103
Tejun Heofd1e8a12009-08-14 15:00:51 +0900104extern struct pcpu_alloc_info * __init pcpu_alloc_alloc_info(int nr_groups,
105 int nr_units);
106extern void __init pcpu_free_alloc_info(struct pcpu_alloc_info *ai);
Tejun Heo033e48f2009-08-14 15:00:51 +0900107
Kefeng Wang163fa232019-07-03 16:25:52 +0800108extern void __init pcpu_setup_first_chunk(const struct pcpu_alloc_info *ai,
Tejun Heofb435d52009-08-14 15:00:51 +0900109 void *base_addr);
Tejun Heo8d408b42009-02-24 11:57:21 +0900110
Tejun Heo08fc4582009-08-14 15:00:49 +0900111#ifdef CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK
Tejun Heo4ba6ce22010-06-27 18:49:59 +0200112extern int __init pcpu_embed_first_chunk(size_t reserved_size, size_t dyn_size,
Tejun Heoc8826dd2009-08-14 15:00:52 +0900113 size_t atom_size,
114 pcpu_fc_cpu_distance_fn_t cpu_distance_fn,
115 pcpu_fc_alloc_fn_t alloc_fn,
116 pcpu_fc_free_fn_t free_fn);
Tejun Heo08fc4582009-08-14 15:00:49 +0900117#endif
Tejun Heo66c3a752009-03-10 16:27:48 +0900118
Tejun Heo08fc4582009-08-14 15:00:49 +0900119#ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK
Tejun Heofb435d52009-08-14 15:00:51 +0900120extern int __init pcpu_page_first_chunk(size_t reserved_size,
Tejun Heod4b95f82009-07-04 08:10:59 +0900121 pcpu_fc_alloc_fn_t alloc_fn,
122 pcpu_fc_free_fn_t free_fn,
123 pcpu_fc_populate_pte_fn_t populate_pte_fn);
Tejun Heo08fc4582009-08-14 15:00:49 +0900124#endif
Tejun Heod4b95f82009-07-04 08:10:59 +0900125
Rusty Russelle0fdb0e2009-10-29 22:34:15 +0900126extern void __percpu *__alloc_reserved_percpu(size_t size, size_t align);
Thomas Gleixner383776f2017-02-27 15:37:36 +0100127extern bool __is_kernel_percpu_address(unsigned long addr, unsigned long *can_addr);
Tejun Heo10fad5e2010-03-10 18:57:54 +0900128extern bool is_kernel_percpu_address(unsigned long addr);
Tejun Heof2a82052009-02-20 16:29:08 +0900129
Tejun Heobbddff02010-09-03 18:22:48 +0200130#if !defined(CONFIG_SMP) || !defined(CONFIG_HAVE_SETUP_PER_CPU_AREA)
Tejun Heoe74e3962009-03-30 19:07:44 +0900131extern void __init setup_per_cpu_areas(void);
132#endif
133
Tejun Heo5835d962014-09-02 14:46:04 -0400134extern void __percpu *__alloc_percpu_gfp(size_t size, size_t align, gfp_t gfp);
Tejun Heode380b52010-03-24 17:06:43 +0900135extern void __percpu *__alloc_percpu(size_t size, size_t align);
136extern void free_percpu(void __percpu *__pdata);
137extern phys_addr_t per_cpu_ptr_to_phys(void *addr);
138
Tejun Heo5835d962014-09-02 14:46:04 -0400139#define alloc_percpu_gfp(type, gfp) \
140 (typeof(type) __percpu *)__alloc_percpu_gfp(sizeof(type), \
141 __alignof__(type), gfp)
142#define alloc_percpu(type) \
143 (typeof(type) __percpu *)__alloc_percpu(sizeof(type), \
144 __alignof__(type))
Tejun Heof2a82052009-02-20 16:29:08 +0900145
Dennis Zhou (Facebook)7e8a6302018-08-21 21:53:58 -0700146extern unsigned long pcpu_nr_pages(void);
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148#endif /* __LINUX_PERCPU_H */