riscv: Add numa support for riscv64 platform
v2 cover riscv: Add numa support for riscv64 platform
riscv: Add numa support for riscv64 platform
This implementation is based on arm64 porting. It is tested with qemu-system-riscv64, SiFive Unleashed board and OmniXtend FPGA platform.
There will be two nodes in /sys/devices/system/node if it is described in dts and CONFIG_NUMA is enabled. We can use numastat/numactl/numademo to see its status.
Changes in v2:
- split this patch to more patches to be more readable
- set cpu->hotplugable to 0 since it is not supported yet
- add more explanation for moving unflatten_device_tree() to paging_init()
Greentime Hu (4):
riscv: Add support pte_protnone and pmd_protnone if
CONFIG_NUMA_BALANCING
riscv: Move unflatten_device_tree() to paging_init() because
riscv_numa_init() needs the dt information.
riscv: Use variable this_cpu instead of smp_processor_id()
riscv: Add numa support for riscv64 platform
arch/riscv/Kconfig | 30 ++- arch/riscv/include/asm/mmzone.h | 13 ++ arch/riscv/include/asm/numa.h | 46 ++++ arch/riscv/include/asm/pci.h | 10 + arch/riscv/include/asm/pgtable.h | 20 ++ arch/riscv/kernel/setup.c | 26 ++- arch/riscv/kernel/smpboot.c | 20 +- arch/riscv/mm/Makefile | 1 + arch/riscv/mm/init.c | 3 + arch/riscv/mm/numa.c | 372 +++++++++++++++++++++++++++++++ 10 files changed, 536 insertions(+), 5 deletions(-) create mode 100644 arch/riscv/include/asm/mmzone.h create mode 100644 arch/riscv/include/asm/numa.h create mode 100644 arch/riscv/mm/numa.c
Signature · 1 line
2.17.1
RFC PATCH v2 1/4 riscv: Add support pte_protnone and pmd_protnone if CONFIG_NUMA_BALANCING
These two functions are used to distinguish between PROT_NONENUMA protections and hinting fault protections.
--- arch/riscv/include/asm/pgtable.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
arch/riscv/include/asm/pgtable.h +20 -0
@@ -166,6 +166,11 @@ static inline unsigned long pmd_page_vaddr(pmd_t pmd)return (unsigned long )pfn_to_virt (pmd_val (pmd) >> _PAGE_PFN_SHIFT); } +static inline pte_t pmd_pte (pmd_t pmd) +{ +return __pte (pmd_val (pmd)); +} +/* Yields the page frame number (PFN) of a page table entry */ static inline unsigned long pte_pfn (pte_t pte) {
@@ -279,6 +284,21 @@ static inline pte_t pte_mkhuge(pte_t pte)return pte; } +#ifdef CONFIG_NUMA_BALANCING +/* +* See the comment in include/asm-generic/pgtable.h +*/ +static inline int pte_protnone (pte_t pte) +{ +return (pte_val (pte) & (_PAGE_PRESENT | _PAGE_PROT_NONE)) == _PAGE_PROT_NONE; +} + +static inline int pmd_protnone (pmd_t pmd) +{ +return pte_protnone (pmd_pte (pmd)); +} +#endif +/* Modify page protection bits */ static inline pte_t pte_modify (pte_t pte,pgprot_t newprot) {
Signature · 1 line
2.17.1
RFC PATCH v2 2/4 riscv: Move unflatten_device_tree() to paging_init() because riscv_numa_init() needs the dt information.
In reply to Greentime HuIt is moved to paging_init() is because that of_numa_init() will use of_numa_parse_cpu_nodes() and of_numa_parse_memory_nodes(). We have to unflatten_device_tree() first then we can call riscv_numa_init(), but riscv_numa_init() shall be called before memblocks_present() because the node information will be used in memblocks_present().
So the calling order will be looked like this. unflatten_device_tree(); //To get dt information for memory and nodes riscv_numa_init(); //It can use of_numa_parse_* and set the nodes information memblocks_present(); //The node information can be used now
--- arch/riscv/kernel/setup.c | 1 - arch/riscv/mm/init.c | 1 + 2 files changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kernel/setup.c +0 -1
@@ -67,7 +67,6 @@ void __init setup_arch(char **cmdline_p)setup_bootmem ();paging_init (); -unflatten_device_tree ();clint_init_boot_cpu ();#ifdef CONFIG_SWIOTLB
arch/riscv/mm/init.c +1 -0
@@ -491,6 +491,7 @@ static inline void setup_vm_final(void)void __init paging_init (void ) {setup_vm_final (); +unflatten_device_tree ();memblocks_present ();sparse_init ();setup_zero_page ();
Signature · 1 line
2.17.1
RFC PATCH v2 3/4 riscv: Use variable this_cpu instead of smp_processor_id()
In reply to Greentime HuTo clean up codes in smpboot.c by using a variable this_cpu to prevent too many calls for smp_processor_id()
--- arch/riscv/kernel/smpboot.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
arch/riscv/kernel/smpboot.c +9 -3
@@ -46,6 +46,9 @@ void __init smp_prepare_boot_cpu(void)void __init smp_prepare_cpus (unsigned int max_cpus) {int cpuid; +unsigned int this_cpu; + + this_cpu =smp_processor_id ();/* This covers non-smp usecase mandated by "nosmp" option */ if (max_cpus ==0 )
@@ -137,6 +140,9 @@ void __init smp_cpus_done(unsigned int max_cpus) asmlinkage __visible void __init smp_callin(void) {struct mm_struct *mm = &init_mm; +unsigned int this_cpu; + + this_cpu =smp_processor_id ();if (!IS_ENABLED (CONFIG_RISCV_SBI ))clint_clear_ipi (cpuid_to_hartid_map (smp_processor_id ()));
@@ -146,9 +152,9 @@ asmlinkage __visible void __init smp_callin(void) current->active_mm = mm;trap_init (); -notify_cpu_starting (smp_processor_id ()); -update_siblings_masks (smp_processor_id ()); -set_cpu_online (smp_processor_id (),1 ); +notify_cpu_starting (this_cpu); +update_siblings_masks (this_cpu); +set_cpu_online (this_cpu, true);/* * Remote TLB flushes are ignored while the CPU is offline, so emit * a local TLB flush right now just in case.
Signature · 1 line
2.17.1
RFC PATCH v2 4/4 riscv: Add numa support for riscv64 platform
In reply to Greentime HuThis implementation is based on arm64 porting. It is tested with qemu-system-riscv64, SiFive Unleashed board and OmniXtend FPGA platform.
There will be two nodes in /sys/devices/system/node if it is described in dts and CONFIG_NUMA is enabled. We can use numastat/numactl/numademo to see its status.
Changes in v2:
- split this patch to more patches to be more readable
- set cpu->hotplugable to 0 since it is not supported yet
- add more explanation for moving unflatten_device_tree() to paging_init()
--- arch/riscv/Kconfig | 30 ++- arch/riscv/include/asm/mmzone.h | 13 ++ arch/riscv/include/asm/numa.h | 46 ++++ arch/riscv/include/asm/pci.h | 10 + arch/riscv/kernel/setup.c | 25 +++ arch/riscv/kernel/smpboot.c | 8 + arch/riscv/mm/Makefile | 1 + arch/riscv/mm/init.c | 2 + arch/riscv/mm/numa.c | 372 ++++++++++++++++++++++++++++++++ 9 files changed, 506 insertions(+), 1 deletion(-) create mode 100644 arch/riscv/include/asm/mmzone.h create mode 100644 arch/riscv/include/asm/numa.h create mode 100644 arch/riscv/mm/numa.c
arch/riscv/Kconfig +29 -1
@@ -22,7 +22,6 @@ config RISCV select CLONE_BACKWARDS select COMMON_CLK select GENERIC_CLOCKEVENTS - select GENERIC_CPU_DEVICES select GENERIC_IRQ_SHOW select GENERIC_PCI_IOMAP select GENERIC_SCHED_CLOCK
@@ -253,6 +252,35 @@ config TUNE_GENERIC bool "generic" endchoice +# Common NUMA Features +config NUMA + bool "Numa Memory Allocation and Scheduler Support" + select OF_NUMA + select ARCH_SUPPORTS_NUMA_BALANCING + depends on SPARSEMEM + help + Enable NUMA (Non-Uniform Memory Access) support. + + The kernel will try to allocate memory used by a CPU on the + local memory of the CPU and add some more + NUMA awareness to the kernel. + +config NODES_SHIFT + int "Maximum NUMA Nodes (as a power of 2)" + range 1 10 + default "2" + depends on NEED_MULTIPLE_NODES + help + Specify the maximum number of NUMA Nodes available on the target + system. Increases memory reserved to accommodate various tables. + +config USE_PERCPU_NUMA_NODE_ID + def_bool y + depends on NUMA + +config NEED_PER_CPU_EMBED_FIRST_CHUNK + def_bool y + depends on NUMA config RISCV_ISA_C bool "Emit compressed instructions when building Linux"
arch/riscv/include/asm/mmzone.h new file +13 -0
@@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __ASM_MMZONE_H +#define __ASM_MMZONE_H + +#ifdef CONFIG_NUMA + +#include <asm/numa.h> + +extern struct pglist_data *node_data[]; +#define NODE_DATA (nid) (node_data[(nid)]) + +#endif /* CONFIG_NUMA */ +#endif /* __ASM_MMZONE_H */
arch/riscv/include/asm/numa.h new file +46 -0
46 lines not on this page · raw message
arch/riscv/include/asm/pci.h +10 -0
@@ -32,6 +32,16 @@ static inline int pci_proc_domain(struct pci_bus *bus)/* always show the domain in /proc */ return 1 ; } + +#ifdef CONFIG_NUMA +int pcibus_to_node (struct pci_bus *bus); +#ifndef cpumask_of_pcibus +#define cpumask_of_pcibus (bus) (pcibus_to_node(bus) == -1 ? \ + cpu_all_mask : \ + cpumask_of_node(pcibus_to_node(bus))) +#endif +#endif /* CONFIG_NUMA */ +#endif /* CONFIG_PCI */ #endif /* _ASM_RISCV_PCI_H */
arch/riscv/kernel/setup.c +25 -0
@@ -54,6 +54,31 @@ void __init parse_dtb(void)#endif } +static DEFINE_PER_CPU(struct cpu, cpu_devices); + +static int __init topology_init (void ) +{ +int i, ret; + +#ifdef CONFIG_NEED_MULTIPLE_NODES +for_each_online_node (i) +register_one_node (i); +#endif + +for_each_possible_cpu (i) { +struct cpu *cpu = &per_cpu (cpu_devices, i); + + cpu->hotpluggable =0 ; + ret =register_cpu (cpu, i); +if (unlikely (ret)) +pr_warn ("Warning: %s: register_cpu %d failed (%d)\n" , + __func__, i, ret); + } + +return 0 ; +} +subsys_initcall (topology_init); +void __init setup_arch (char **cmdline_p) { init_mm.start_code = (unsigned long ) _stext;
arch/riscv/kernel/smpboot.c +8 -0
@@ -27,6 +27,7 @@#include <asm/clint.h> #include <asm/irq.h> #include <asm/mmu_context.h> +#include <asm/numa.h> #include <asm/tlbflush.h> #include <asm/sections.h> #include <asm/sbi.h>
@@ -49,6 +50,8 @@ void __init smp_prepare_cpus(unsigned int max_cpus)unsigned int this_cpu; this_cpu =smp_processor_id (); +numa_store_cpu_info (this_cpu); +numa_add_cpu (this_cpu);/* This covers non-smp usecase mandated by "nosmp" option */ if (max_cpus ==0 )
@@ -58,6 +61,7 @@ void __init smp_prepare_cpus(unsigned int max_cpus)if (cpuid ==smp_processor_id ())continue ;set_cpu_present (cpuid, true); +numa_store_cpu_info (cpuid); } }
@@ -76,6 +80,7 @@ void __init setup_smp(void)if (hart ==cpuid_to_hartid_map (0 )) {BUG_ON (found_boot_cpu); found_boot_cpu =1 ; +early_map_cpu_to_node (0 ,of_node_to_nid (dn));continue ; }if (cpuid >=NR_CPUS ) {
@@ -85,6 +90,7 @@ void __init setup_smp(void) }cpuid_to_hartid_map (cpuid) = hart; +early_map_cpu_to_node (cpuid,of_node_to_nid (dn)); cpuid++; }
@@ -153,6 +159,8 @@ asmlinkage __visible void __init smp_callin(void)trap_init ();notify_cpu_starting (this_cpu); +numa_store_cpu_info (this_cpu); +numa_add_cpu (this_cpu);update_siblings_masks (this_cpu);set_cpu_online (this_cpu, true);/*
arch/riscv/mm/Makefile +1 -0
@@ -15,3 +15,4 @@ ifeq ($(CONFIG_MMU),y) obj-$(CONFIG_SMP) += tlbflush.o endif obj-$(CONFIG_HUGETLB_PAGE) += hugetlbpage.o +obj-$(CONFIG_NUMA) += numa.o
arch/riscv/mm/init.c +2 -0
@@ -18,6 +18,7 @@#include <asm/sections.h> #include <asm/pgtable.h> #include <asm/io.h> +#include <asm/numa.h> #include "../kernel/head.h"
@@ -492,6 +493,7 @@ void __init paging_init(void) {setup_vm_final ();unflatten_device_tree (); +riscv_numa_init ();memblocks_present ();sparse_init ();setup_zero_page ();
arch/riscv/mm/numa.c new file +372 -0
372 lines not on this page · raw message
Signature · 1 line
2.17.1