Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0003 
0004 #include <linux/kernel.h>
0005 #include <linux/export.h>
0006 #include <linux/init.h>
0007 #include <linux/memblock.h>
0008 #include <linux/percpu.h>
0009 #include <linux/kexec.h>
0010 #include <linux/crash_dump.h>
0011 #include <linux/smp.h>
0012 #include <linux/topology.h>
0013 #include <linux/pfn.h>
0014 #include <asm/sections.h>
0015 #include <asm/processor.h>
0016 #include <asm/desc.h>
0017 #include <asm/setup.h>
0018 #include <asm/mpspec.h>
0019 #include <asm/apicdef.h>
0020 #include <asm/highmem.h>
0021 #include <asm/proto.h>
0022 #include <asm/cpumask.h>
0023 #include <asm/cpu.h>
0024 #include <asm/stackprotector.h>
0025 
0026 DEFINE_PER_CPU_READ_MOSTLY(int, cpu_number);
0027 EXPORT_PER_CPU_SYMBOL(cpu_number);
0028 
0029 #ifdef CONFIG_X86_64
0030 #define BOOT_PERCPU_OFFSET ((unsigned long)__per_cpu_load)
0031 #else
0032 #define BOOT_PERCPU_OFFSET 0
0033 #endif
0034 
0035 DEFINE_PER_CPU_READ_MOSTLY(unsigned long, this_cpu_off) = BOOT_PERCPU_OFFSET;
0036 EXPORT_PER_CPU_SYMBOL(this_cpu_off);
0037 
0038 unsigned long __per_cpu_offset[NR_CPUS] __ro_after_init = {
0039     [0 ... NR_CPUS-1] = BOOT_PERCPU_OFFSET,
0040 };
0041 EXPORT_SYMBOL(__per_cpu_offset);
0042 
0043 /*
0044  * On x86_64 symbols referenced from code should be reachable using
0045  * 32bit relocations.  Reserve space for static percpu variables in
0046  * modules so that they are always served from the first chunk which
0047  * is located at the percpu segment base.  On x86_32, anything can
0048  * address anywhere.  No need to reserve space in the first chunk.
0049  */
0050 #ifdef CONFIG_X86_64
0051 #define PERCPU_FIRST_CHUNK_RESERVE  PERCPU_MODULE_RESERVE
0052 #else
0053 #define PERCPU_FIRST_CHUNK_RESERVE  0
0054 #endif
0055 
0056 #ifdef CONFIG_X86_32
0057 /**
0058  * pcpu_need_numa - determine percpu allocation needs to consider NUMA
0059  *
0060  * If NUMA is not configured or there is only one NUMA node available,
0061  * there is no reason to consider NUMA.  This function determines
0062  * whether percpu allocation should consider NUMA or not.
0063  *
0064  * RETURNS:
0065  * true if NUMA should be considered; otherwise, false.
0066  */
0067 static bool __init pcpu_need_numa(void)
0068 {
0069 #ifdef CONFIG_NUMA
0070     pg_data_t *last = NULL;
0071     unsigned int cpu;
0072 
0073     for_each_possible_cpu(cpu) {
0074         int node = early_cpu_to_node(cpu);
0075 
0076         if (node_online(node) && NODE_DATA(node) &&
0077             last && last != NODE_DATA(node))
0078             return true;
0079 
0080         last = NODE_DATA(node);
0081     }
0082 #endif
0083     return false;
0084 }
0085 #endif
0086 
0087 static int __init pcpu_cpu_distance(unsigned int from, unsigned int to)
0088 {
0089 #ifdef CONFIG_NUMA
0090     if (early_cpu_to_node(from) == early_cpu_to_node(to))
0091         return LOCAL_DISTANCE;
0092     else
0093         return REMOTE_DISTANCE;
0094 #else
0095     return LOCAL_DISTANCE;
0096 #endif
0097 }
0098 
0099 static int __init pcpu_cpu_to_node(int cpu)
0100 {
0101     return early_cpu_to_node(cpu);
0102 }
0103 
0104 void __init pcpu_populate_pte(unsigned long addr)
0105 {
0106     populate_extra_pte(addr);
0107 }
0108 
0109 static inline void setup_percpu_segment(int cpu)
0110 {
0111 #ifdef CONFIG_X86_32
0112     struct desc_struct d = GDT_ENTRY_INIT(0x8092, per_cpu_offset(cpu),
0113                           0xFFFFF);
0114 
0115     write_gdt_entry(get_cpu_gdt_rw(cpu), GDT_ENTRY_PERCPU, &d, DESCTYPE_S);
0116 #endif
0117 }
0118 
0119 void __init setup_per_cpu_areas(void)
0120 {
0121     unsigned int cpu;
0122     unsigned long delta;
0123     int rc;
0124 
0125     pr_info("NR_CPUS:%d nr_cpumask_bits:%d nr_cpu_ids:%u nr_node_ids:%u\n",
0126         NR_CPUS, nr_cpumask_bits, nr_cpu_ids, nr_node_ids);
0127 
0128     /*
0129      * Allocate percpu area.  Embedding allocator is our favorite;
0130      * however, on NUMA configurations, it can result in very
0131      * sparse unit mapping and vmalloc area isn't spacious enough
0132      * on 32bit.  Use page in that case.
0133      */
0134 #ifdef CONFIG_X86_32
0135     if (pcpu_chosen_fc == PCPU_FC_AUTO && pcpu_need_numa())
0136         pcpu_chosen_fc = PCPU_FC_PAGE;
0137 #endif
0138     rc = -EINVAL;
0139     if (pcpu_chosen_fc != PCPU_FC_PAGE) {
0140         const size_t dyn_size = PERCPU_MODULE_RESERVE +
0141             PERCPU_DYNAMIC_RESERVE - PERCPU_FIRST_CHUNK_RESERVE;
0142         size_t atom_size;
0143 
0144         /*
0145          * On 64bit, use PMD_SIZE for atom_size so that embedded
0146          * percpu areas are aligned to PMD.  This, in the future,
0147          * can also allow using PMD mappings in vmalloc area.  Use
0148          * PAGE_SIZE on 32bit as vmalloc space is highly contended
0149          * and large vmalloc area allocs can easily fail.
0150          */
0151 #ifdef CONFIG_X86_64
0152         atom_size = PMD_SIZE;
0153 #else
0154         atom_size = PAGE_SIZE;
0155 #endif
0156         rc = pcpu_embed_first_chunk(PERCPU_FIRST_CHUNK_RESERVE,
0157                         dyn_size, atom_size,
0158                         pcpu_cpu_distance,
0159                         pcpu_cpu_to_node);
0160         if (rc < 0)
0161             pr_warn("%s allocator failed (%d), falling back to page size\n",
0162                 pcpu_fc_names[pcpu_chosen_fc], rc);
0163     }
0164     if (rc < 0)
0165         rc = pcpu_page_first_chunk(PERCPU_FIRST_CHUNK_RESERVE,
0166                        pcpu_cpu_to_node);
0167     if (rc < 0)
0168         panic("cannot initialize percpu area (err=%d)", rc);
0169 
0170     /* alrighty, percpu areas up and running */
0171     delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
0172     for_each_possible_cpu(cpu) {
0173         per_cpu_offset(cpu) = delta + pcpu_unit_offsets[cpu];
0174         per_cpu(this_cpu_off, cpu) = per_cpu_offset(cpu);
0175         per_cpu(cpu_number, cpu) = cpu;
0176         setup_percpu_segment(cpu);
0177         /*
0178          * Copy data used in early init routines from the
0179          * initial arrays to the per cpu data areas.  These
0180          * arrays then become expendable and the *_early_ptr's
0181          * are zeroed indicating that the static arrays are
0182          * gone.
0183          */
0184 #ifdef CONFIG_X86_LOCAL_APIC
0185         per_cpu(x86_cpu_to_apicid, cpu) =
0186             early_per_cpu_map(x86_cpu_to_apicid, cpu);
0187         per_cpu(x86_bios_cpu_apicid, cpu) =
0188             early_per_cpu_map(x86_bios_cpu_apicid, cpu);
0189         per_cpu(x86_cpu_to_acpiid, cpu) =
0190             early_per_cpu_map(x86_cpu_to_acpiid, cpu);
0191 #endif
0192 #ifdef CONFIG_X86_32
0193         per_cpu(x86_cpu_to_logical_apicid, cpu) =
0194             early_per_cpu_map(x86_cpu_to_logical_apicid, cpu);
0195 #endif
0196 #ifdef CONFIG_NUMA
0197         per_cpu(x86_cpu_to_node_map, cpu) =
0198             early_per_cpu_map(x86_cpu_to_node_map, cpu);
0199         /*
0200          * Ensure that the boot cpu numa_node is correct when the boot
0201          * cpu is on a node that doesn't have memory installed.
0202          * Also cpu_up() will call cpu_to_node() for APs when
0203          * MEMORY_HOTPLUG is defined, before per_cpu(numa_node) is set
0204          * up later with c_init aka intel_init/amd_init.
0205          * So set them all (boot cpu and all APs).
0206          */
0207         set_cpu_numa_node(cpu, early_cpu_to_node(cpu));
0208 #endif
0209         /*
0210          * Up to this point, the boot CPU has been using .init.data
0211          * area.  Reload any changed state for the boot CPU.
0212          */
0213         if (!cpu)
0214             switch_to_new_gdt(cpu);
0215     }
0216 
0217     /* indicate the early static arrays will soon be gone */
0218 #ifdef CONFIG_X86_LOCAL_APIC
0219     early_per_cpu_ptr(x86_cpu_to_apicid) = NULL;
0220     early_per_cpu_ptr(x86_bios_cpu_apicid) = NULL;
0221     early_per_cpu_ptr(x86_cpu_to_acpiid) = NULL;
0222 #endif
0223 #ifdef CONFIG_X86_32
0224     early_per_cpu_ptr(x86_cpu_to_logical_apicid) = NULL;
0225 #endif
0226 #ifdef CONFIG_NUMA
0227     early_per_cpu_ptr(x86_cpu_to_node_map) = NULL;
0228 #endif
0229 
0230     /* Setup node to cpumask map */
0231     setup_node_to_cpumask_map();
0232 
0233     /* Setup cpu initialized, callin, callout masks */
0234     setup_cpu_local_masks();
0235 
0236     /*
0237      * Sync back kernel address range again.  We already did this in
0238      * setup_arch(), but percpu data also needs to be available in
0239      * the smpboot asm and arch_sync_kernel_mappings() doesn't sync to
0240      * swapper_pg_dir on 32-bit. The per-cpu mappings need to be available
0241      * there too.
0242      *
0243      * FIXME: Can the later sync in setup_cpu_entry_areas() replace
0244      * this call?
0245      */
0246     sync_initial_page_table();
0247 }