powerpc/rtas: miscellaneous cleanups, user region allocation
cover powerpc/rtas: miscellaneous cleanups, user region allocation
The region exposed to user space for use as work areas passed to sys_rtas() can be incorrectly allocated on radix, leading to failures in users of librtas. Correct this and clean up some of the code visited along the way.
I think the cleanups should be unobjectionable and I've placed them first in the series. Please check my work on the rtas_rmo_buf allocation changes; they are only lightly tested so far (slot add on Power9 PowerVM, and comparison of /memory@0/reg with the contents of /proc/powerpc/rtas/rmo_buf on qemu Power9 w/radix).
I suspect the per-cpu RTAS argument structures for reentrant calls need similar measures, but I can add that to the series once there is consensus on the approach.
Nathan Lynch (6):
powerpc/rtas: improve ppc_rtas_rmo_buf_show documentation powerpc/rtas-proc: remove unused RMO_READ_BUF_MAX powerpc/rtas: remove ibm_suspend_me_token powerpc/rtas: move syscall filter setup into separate function powerpc/rtas: rename RTAS_RMOBUF_MAX to RTAS_USER_REGION_SIZE powerpc/rtas: constrain user region allocation to RMA
arch/powerpc/include/asm/rtas.h | 9 ++- arch/powerpc/kernel/rtas-proc.c | 15 +++-- arch/powerpc/kernel/rtas.c | 108 ++++++++++++++++++++++++-------- 3 files changed, 98 insertions(+), 34 deletions(-)
Signature · 1 line
2.29.2
PATCH 1/6 powerpc/rtas: improve ppc_rtas_rmo_buf_show documentation
Add kerneldoc for ppc_rtas_rmo_buf_show(), the callback for /proc/powerpc/rtas/rmo_buffer, explaining its expected use.
--- arch/powerpc/kernel/rtas-proc.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
arch/powerpc/kernel/rtas-proc.c +10 -1
@@ -757,7 +757,16 @@ static int ppc_rtas_tone_volume_show(struct seq_file *m, void *v)#define RMO_READ_BUF_MAX 30 -/* RTAS Userspace access */ +/** +* ppc_rtas_rmo_buf_show() - Describe RTAS-addressable region for user space. +* +* Base + size description of a range of RTAS-addressable memory set +* aside for user space to use as work area(s) for certain RTAS +* functions. User space accesses this region via /dev/mem. Apart from +* security policies, the kernel does not arbitrate or serialize +* access to this region, and user space must ensure that concurrent +* users do not interfere with each other. +*/ static int ppc_rtas_rmo_buf_show (struct seq_file *m,void *v) {seq_printf (m,"%016lx %x\n" , rtas_rmo_buf,RTAS_RMOBUF_MAX );
Signature · 1 line
2.29.2
Re: [PATCH 1/6] powerpc/rtas: improve ppc_rtas_rmo_buf_show documentation
Quoted 13 lines · Nathan Lynch
Add kerneldoc for
ppc_rtas_rmo_buf_show(), the callback for/proc/powerpc/rtas/rmo_buffer, explaining its expected use.Signed-off-by Nathan Lynch <[email protected]>--- arch/powerpc/kernel/rtas-proc.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)arch/powerpc/kernel/rtas-proc.c +0
-0
arch/powerpc/kernel/rtas-proc.c static int ppc_rtas_tone_volume_show(struct seq_file *m, void *v) · line 765
Quoted 10 lines
arch/powerpc/kernel/rtas-proc.c +6
-1@@ -757,7 +757,16 @@ static int ppc_rtas_tone_volume_show(struct seq_file *m, void *v)#define RMO_READ_BUF_MAX 30 -/* RTAS Userspace access */ +/** +* ppc_rtas_rmo_buf_show() - Describe RTAS-addressable region for user space. +* +* Base + size description of a range of RTAS-addressable memory set +* aside for user space to use as work area(s) for certain RTAS +* functions. User space accesses this region via /dev/mem.
mmm ufff wuuuuut argh^w^w^w^w Thanks for documenting it :)
Quoted 9 lines
Apart from
+ * security policies, the kernel does not arbitrate or serialize
+ * access to this region, and user space must ensure that concurrent
+ * users do not interfere with each other.
+ */
static intppc_rtas_rmo_buf_show(struct seq_file *m, void *v){
seq_printf(m, "%016lx %x\n", rtas_rmo_buf, RTAS_RMOBUF_MAX);
Signature · 1 line
Alexey
Re: [PATCH 1/6] powerpc/rtas: improve ppc_rtas_rmo_buf_show documentation
In reply to Nathan LynchQuoted 4 lines · Nathan Lynch
Add kerneldoc for
ppc_rtas_rmo_buf_show(), the callback for/proc/powerpc/rtas/rmo_buffer, explaining its expected use.Signed-off-by Nathan Lynch <[email protected]>
Looks good.
Signature · 2 lines
Andrew Donnellan OzLabs, ADL Canberra [email protected] IBM Australia Limited
PATCH 2/6 powerpc/rtas-proc: remove unused RMO_READ_BUF_MAX
In reply to Nathan LynchThis constant is unused.
--- arch/powerpc/kernel/rtas-proc.c | 2 -- 1 file changed, 2 deletions(-)
arch/powerpc/kernel/rtas-proc.c +0 -2
@@ -755,8 +755,6 @@ static int ppc_rtas_tone_volume_show(struct seq_file *m, void *v)return 0 ; } -#define RMO_READ_BUF_MAX 30 -/** * ppc_rtas_rmo_buf_show() - Describe RTAS-addressable region for user space. *
Signature · 1 line
2.29.2
Re: [PATCH 2/6] powerpc/rtas-proc: remove unused RMO_READ_BUF_MAX
Quoted 3 lines · Nathan Lynch
This constant is unused.
Signed-off-by Nathan Lynch <[email protected]>
Quoted 18 lines
--- arch/powerpc/kernel/rtas-proc.c | 2 -- 1 file changed, 2 deletions(-)arch/powerpc/kernel/rtas-proc.c +0
-2@@ -755,8 +755,6 @@ static int ppc_rtas_tone_volume_show(struct seq_file *m, void *v)return 0 ; } -#define RMO_READ_BUF_MAX 30 -/** * ppc_rtas_rmo_buf_show() - Describe RTAS-addressable region for user space. *
Signature · 1 line
Alexey
Re: [PATCH 2/6] powerpc/rtas-proc: remove unused RMO_READ_BUF_MAX
In reply to Nathan LynchQuoted 3 lines · Nathan Lynch
This constant is unused.
Signed-off-by Nathan Lynch <[email protected]>
A quick grep agrees.
Quoted 18 lines
--- arch/powerpc/kernel/rtas-proc.c | 2 -- 1 file changed, 2 deletions(-)arch/powerpc/kernel/rtas-proc.c +0
-2@@ -755,8 +755,6 @@ static int ppc_rtas_tone_volume_show(struct seq_file *m, void *v)return 0 ; } -#define RMO_READ_BUF_MAX 30 -/** * ppc_rtas_rmo_buf_show() - Describe RTAS-addressable region for user space. *
Signature · 2 lines
Andrew Donnellan OzLabs, ADL Canberra [email protected] IBM Australia Limited
PATCH 3/6 powerpc/rtas: remove ibm_suspend_me_token
In reply to Nathan LynchThere's not a compelling reason to cache the value of the token for the ibm,suspend-me function. Just look it up when needed in the RTAS syscall's special case for it.
--- arch/powerpc/kernel/rtas.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
arch/powerpc/kernel/rtas.c +2 -5
@@ -828,7 +828,6 @@ void rtas_activate_firmware(void)pr_err ("ibm,activate-firmware failed (%i)\n" , fwrc); } -static int ibm_suspend_me_token =RTAS_UNKNOWN_SERVICE ;#ifdef CONFIG_PPC_PSERIES /** * rtas_call_reentrant() - Used for reentrant rtas calls
@@ -1103,7 +1102,7 @@ SYSCALL_DEFINE1(rtas, struct rtas_args __user *, uargs)return -EINVAL ;/* Need to handle ibm,suspend_me call specially */ -if (token == ibm_suspend_me_token) { +if (token ==rtas_token ("ibm,suspend-me" )) {/* * rtas_ibm_suspend_me assumes the streamid handle is in cpu
@@ -1191,10 +1190,8 @@ void __init rtas_initialize(void)* the stop-self token if any */ #ifdef CONFIG_PPC64 -if (firmware_has_feature (FW_FEATURE_LPAR )) { +if (firmware_has_feature (FW_FEATURE_LPAR )) rtas_region =min (ppc64_rma_size,RTAS_INSTANTIATE_MAX ); - ibm_suspend_me_token =rtas_token ("ibm,suspend-me" ); - }#endif rtas_rmo_buf =memblock_phys_alloc_range (RTAS_RMOBUF_MAX ,PAGE_SIZE ,0 , rtas_region);
Signature · 1 line
2.29.2
Re: [PATCH 3/6] powerpc/rtas: remove ibm_suspend_me_token
Quoted 5 lines · Nathan Lynch
There's not a compelling reason to cache the value of the token for the ibm,suspend-me function. Just look it up when needed in the RTAS syscall's special case for it.
Signed-off-by Nathan Lynch <[email protected]>
Quoted 38 lines
--- arch/powerpc/kernel/rtas.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)arch/powerpc/kernel/rtas.c +2
-5@@ -828,7 +828,6 @@ void rtas_activate_firmware(void)pr_err ("ibm,activate-firmware failed (%i)\n" , fwrc); } -static int ibm_suspend_me_token =RTAS_UNKNOWN_SERVICE ;#ifdef CONFIG_PPC_PSERIES /** * rtas_call_reentrant() - Used for reentrant rtas calls @@ -1103,7 +1102,7 @@ SYSCALL_DEFINE1(rtas, struct rtas_args __user *, uargs)return -EINVAL ;/* Need to handle ibm,suspend_me call specially */ -if (token == ibm_suspend_me_token) { +if (token ==rtas_token ("ibm,suspend-me" )) {/* * rtas_ibm_suspend_me assumes the streamid handle is in cpu @@ -1191,10 +1190,8 @@ void __init rtas_initialize(void)* the stop-self token if any */ #ifdef CONFIG_PPC64 -if (firmware_has_feature (FW_FEATURE_LPAR )) { +if (firmware_has_feature (FW_FEATURE_LPAR )) rtas_region =min (ppc64_rma_size,RTAS_INSTANTIATE_MAX ); - ibm_suspend_me_token =rtas_token ("ibm,suspend-me" ); - }#endif rtas_rmo_buf =memblock_phys_alloc_range (RTAS_RMOBUF_MAX ,PAGE_SIZE ,0 , rtas_region);
Signature · 1 line
Alexey
Re: [PATCH 3/6] powerpc/rtas: remove ibm_suspend_me_token
In reply to Nathan LynchQuoted 5 lines · Nathan Lynch
There's not a compelling reason to cache the value of the token for the ibm,suspend-me function. Just look it up when needed in the RTAS syscall's special case for it.
Signed-off-by Nathan Lynch <[email protected]>
Quoted 38 lines
--- arch/powerpc/kernel/rtas.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)arch/powerpc/kernel/rtas.c +2
-5@@ -828,7 +828,6 @@ void rtas_activate_firmware(void)pr_err ("ibm,activate-firmware failed (%i)\n" , fwrc); } -static int ibm_suspend_me_token =RTAS_UNKNOWN_SERVICE ;#ifdef CONFIG_PPC_PSERIES /** * rtas_call_reentrant() - Used for reentrant rtas calls @@ -1103,7 +1102,7 @@ SYSCALL_DEFINE1(rtas, struct rtas_args __user *, uargs)return -EINVAL ;/* Need to handle ibm,suspend_me call specially */ -if (token == ibm_suspend_me_token) { +if (token ==rtas_token ("ibm,suspend-me" )) {/* * rtas_ibm_suspend_me assumes the streamid handle is in cpu @@ -1191,10 +1190,8 @@ void __init rtas_initialize(void)* the stop-self token if any */ #ifdef CONFIG_PPC64 -if (firmware_has_feature (FW_FEATURE_LPAR )) { +if (firmware_has_feature (FW_FEATURE_LPAR )) rtas_region =min (ppc64_rma_size,RTAS_INSTANTIATE_MAX ); - ibm_suspend_me_token =rtas_token ("ibm,suspend-me" ); - }#endif rtas_rmo_buf =memblock_phys_alloc_range (RTAS_RMOBUF_MAX ,PAGE_SIZE ,0 , rtas_region);
Signature · 2 lines
Andrew Donnellan OzLabs, ADL Canberra [email protected] IBM Australia Limited
PATCH 4/6 powerpc/rtas: move syscall filter setup into separate function
In reply to Nathan LynchReduce conditionally compiled sections within rtas_initialize() by moving the filter table initialization into its own function already guarded by CONFIG_PPC_RTAS_FILTER. No behavior change intended.
--- arch/powerpc/kernel/rtas.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-)
arch/powerpc/kernel/rtas.c +15 -8
@@ -1051,6 +1051,16 @@ static bool block_rtas_call(int token, int nargs,return true; } +static void __init rtas_syscall_filter_init (void ) +{ +unsigned int i; + +for (i =0 ; i <ARRAY_SIZE (rtas_filters); i++) { + rtas_filters[i].token =rtas_token (rtas_filters[i].name); + } + +} +#else static bool block_rtas_call(int token, int nargs,
@@ -1059,6 +1069,10 @@ static bool block_rtas_call(int token, int nargs, return false; } +static void __init rtas_syscall_filter_init (void ) +{ +} +#endif /* CONFIG_PPC_RTAS_FILTER */ /* We assume to be passed big endian arguments */
@@ -1162,9 +1176,6 @@ void __init rtas_initialize(void)unsigned long rtas_region =RTAS_INSTANTIATE_MAX ;u32 base, size, entry;int no_base, no_size, no_entry; -#ifdef CONFIG_PPC_RTAS_FILTER -int i; -#endif /* Get RTAS dev node and fill up our "rtas" structure with infos * about it.
@@ -1203,11 +1214,7 @@ void __init rtas_initialize(void) rtas_last_error_token =rtas_token ("rtas-last-error" );#endif -#ifdef CONFIG_PPC_RTAS_FILTER -for (i =0 ; i <ARRAY_SIZE (rtas_filters); i++) { - rtas_filters[i].token =rtas_token (rtas_filters[i].name); - } -#endif +rtas_syscall_filter_init (); } int __init early_init_dt_scan_rtas(unsigned long node,
Signature · 1 line
2.29.2
Re: [PATCH 4/6] powerpc/rtas: move syscall filter setup into separate function
Quoted 15 lines · Nathan Lynch
Reduce conditionally compiled sections within
rtas_initialize()by moving the filter table initialization into its own function already guarded byCONFIG_PPC_RTAS_FILTER. No behavior change intended.Signed-off-by Nathan Lynch <[email protected]>--- arch/powerpc/kernel/rtas.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-)arch/powerpc/kernel/rtas.c +0
-0@@ -1051,6 +1051,16 @@ static bool block_rtas_call(int token, int nargs,return true;
arch/powerpc/kernel/rtas.c static bool block_rtas_call(int token, int nargs, · line 1061
Quoted 10 lines
arch/powerpc/kernel/rtas.c +8
-0} +static void __init rtas_syscall_filter_init (void ) +{ +unsigned int i; + +for (i =0 ; i <ARRAY_SIZE (rtas_filters); i++) { + rtas_filters[i].token =rtas_token (rtas_filters[i].name); + } +
Unnecessary curly braces (I understand it is cut-n-paste but still) and an empty line. Otherwise:
Quoted 40 lines
arch/powerpc/kernel/rtas.c +7
-8+} +#else static bool block_rtas_call(int token, int nargs,@@ -1059,6 +1069,10 @@ static bool block_rtas_call(int token, int nargs, return false; } +static void __init rtas_syscall_filter_init (void ) +{ +} +#endif /* CONFIG_PPC_RTAS_FILTER */ /* We assume to be passed big endian arguments */ @@ -1162,9 +1176,6 @@ void __init rtas_initialize(void)unsigned long rtas_region =RTAS_INSTANTIATE_MAX ;u32 base, size, entry;int no_base, no_size, no_entry; -#ifdef CONFIG_PPC_RTAS_FILTER -int i; -#endif /* Get RTAS dev node and fill up our "rtas" structure with infos * about it. @@ -1203,11 +1214,7 @@ void __init rtas_initialize(void) rtas_last_error_token =rtas_token ("rtas-last-error" );#endif -#ifdef CONFIG_PPC_RTAS_FILTER -for (i =0 ; i <ARRAY_SIZE (rtas_filters); i++) { - rtas_filters[i].token =rtas_token (rtas_filters[i].name); - } -#endif +rtas_syscall_filter_init (); } int __init early_init_dt_scan_rtas(unsigned long node,
Signature · 1 line
Alexey
Re: [PATCH 4/6] powerpc/rtas: move syscall filter setup into separate function
Quoted 12 lines · Alexey Kardashevskiy
arch/powerpc/kernel/rtas.c static bool block_rtas_call(int token, int nargs, · line 1061
Quoted 8 lines · Nathan Lynch
arch/powerpc/kernel/rtas.c +8
-0+static void __init rtas_syscall_filter_init (void ) +{ +unsigned int i; + +for (i =0 ; i <ARRAY_SIZE (rtas_filters); i++) { + rtas_filters[i].token =rtas_token (rtas_filters[i].name); + } +Unnecessary curly braces (I understand it is cut-n-paste but still) and an empty line.
Yes, will fix in v2, thanks.
Re: [PATCH 4/6] powerpc/rtas: move syscall filter setup into separate function
In reply to Nathan LynchQuoted 5 lines · Nathan Lynch
Reduce conditionally compiled sections within
rtas_initialize()by moving the filter table initialization into its own function already guarded byCONFIG_PPC_RTAS_FILTER. No behavior change intended.Signed-off-by Nathan Lynch <[email protected]>
arch/powerpc/kernel/rtas.c static bool block_rtas_call(int token, int nargs, · line 1062
Quoted 9 lines
arch/powerpc/kernel/rtas.c +9
-0+static void __init rtas_syscall_filter_init (void ) +{ +unsigned int i; + +for (i =0 ; i <ARRAY_SIZE (rtas_filters); i++) { + rtas_filters[i].token =rtas_token (rtas_filters[i].name); + } + +}
Unnecessary empty line, but vitally important braces :)
Signature · 2 lines
Andrew Donnellan OzLabs, ADL Canberra [email protected] IBM Australia Limited
PATCH 5/6 powerpc/rtas: rename RTAS_RMOBUF_MAX to RTAS_USER_REGION_SIZE
In reply to Nathan LynchRTAS_RMOBUF_MAX doesn't actually describe a "maximum" value in any sense. It represents the size of an area of memory set aside for user space to use as work areas for certain RTAS calls.
Rename it to RTAS_USER_REGION, and express the value in terms of the number of work areas allocated.
squash! powerpc/rtas: rename RTAS_RMOBUF_MAX to RTAS_USER_REGION_SIZE
--- arch/powerpc/include/asm/rtas.h | 9 ++++++--- arch/powerpc/kernel/rtas-proc.c | 2 +- arch/powerpc/kernel/rtas.c | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-)
arch/powerpc/include/asm/rtas.h +6 -3
@@ -19,8 +19,11 @@#define RTAS_UNKNOWN_SERVICE (-1)#define RTAS_INSTANTIATE_MAX (1ULL<<30)/* Don't instantiate rtas at/above this value */ -/* Buffer size for ppc_rtas system call. */ -#define RTAS_RMOBUF_MAX (64 * 1024) +/* Work areas shared with RTAS must be 4K, naturally aligned. */ +#define RTAS_WORK_AREA_SIZE 4096 + +/* Work areas allocated for user space access. */ +#define RTAS_USER_REGION_SIZE (RTAS_WORK_AREA_SIZE * 16)/* RTAS return status codes */ #define RTAS_BUSY -2/* RTAS Busy */
@@ -357,7 +360,7 @@ extern void rtas_take_timebase(void);static inline int page_is_rtas_user_buf (unsigned long pfn) {unsigned long paddr = (pfn <<PAGE_SHIFT ); -if (paddr >= rtas_rmo_buf && paddr < (rtas_rmo_buf +RTAS_RMOBUF_MAX )) +if (paddr >= rtas_rmo_buf && paddr < (rtas_rmo_buf +RTAS_USER_REGION_SIZE ))return 1 ;return 0 ; }
arch/powerpc/kernel/rtas-proc.c +1 -1
@@ -767,6 +767,6 @@ static int ppc_rtas_tone_volume_show(struct seq_file *m, void *v)*/ static int ppc_rtas_rmo_buf_show (struct seq_file *m,void *v) { -seq_printf (m,"%016lx %x\n" , rtas_rmo_buf,RTAS_RMOBUF_MAX ); +seq_printf (m,"%016lx %x\n" , rtas_rmo_buf,RTAS_USER_REGION_SIZE );return 0 ; }
arch/powerpc/kernel/rtas.c +1 -1
@@ -1204,7 +1204,7 @@ void __init rtas_initialize(void)if (firmware_has_feature (FW_FEATURE_LPAR )) rtas_region =min (ppc64_rma_size,RTAS_INSTANTIATE_MAX );#endif - rtas_rmo_buf =memblock_phys_alloc_range (RTAS_RMOBUF_MAX ,PAGE_SIZE , + rtas_rmo_buf =memblock_phys_alloc_range (RTAS_USER_REGION_SIZE ,PAGE_SIZE ,0 , rtas_region);if (!rtas_rmo_buf) panic("ERROR: RTAS: Failed to allocate %lx bytes below %pa\n",
Signature · 1 line
2.29.2
Re: [PATCH 5/6] powerpc/rtas: rename RTAS_RMOBUF_MAX to RTAS_USER_REGION_SIZE
Quoted 21 lines · Nathan Lynch
RTAS_RMOBUF_MAXdoesn't actually describe a "maximum" value in any sense. It represents the size of an area of memory set aside for user space to use as work areas for certain RTAS calls.Rename it to
RTAS_USER_REGION, and express the value in terms of the number of work areas allocated.Signed-off-by Nathan Lynch <[email protected]>squash! powerpc/rtas: rename
RTAS_RMOBUF_MAXtoRTAS_USER_REGION_SIZE--- arch/powerpc/include/asm/rtas.h | 9 ++++++--- arch/powerpc/kernel/rtas-proc.c | 2 +- arch/powerpc/kernel/rtas.c | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-)arch/powerpc/include/asm/rtas.h +0
-0
arch/powerpc/include/asm/rtas.h line 22
Quoted 6 lines
arch/powerpc/include/asm/rtas.h +1
-2@@ -19,8 +19,11 @@#define RTAS_UNKNOWN_SERVICE (-1)#define RTAS_INSTANTIATE_MAX (1ULL<<30)/* Don't instantiate rtas at/above this value */ -/* Buffer size for ppc_rtas system call. */ -#define RTAS_RMOBUF_MAX (64 * 1024) +/* Work areas shared with RTAS must be 4K, naturally aligned. */
Why exactly 4K and not (for example) PAGE_SIZE?
arch/powerpc/include/asm/rtas.h line 26
Quoted 4 lines
arch/powerpc/include/asm/rtas.h +4
-0+#define RTAS_WORK_AREA_SIZE 4096 + +/* Work areas allocated for user space access. */ +#define RTAS_USER_REGION_SIZE (RTAS_WORK_AREA_SIZE * 16)
This is still 64K but no clarity why. There is 16 of something, what is it?
Quoted 38 lines
arch/powerpc/include/asm/rtas.h +1
-1/* RTAS return status codes */ #define RTAS_BUSY -2/* RTAS Busy */ @@ -357,7 +360,7 @@ extern void rtas_take_timebase(void);static inline int page_is_rtas_user_buf (unsigned long pfn) {unsigned long paddr = (pfn <<PAGE_SHIFT ); -if (paddr >= rtas_rmo_buf && paddr < (rtas_rmo_buf +RTAS_RMOBUF_MAX )) +if (paddr >= rtas_rmo_buf && paddr < (rtas_rmo_buf +RTAS_USER_REGION_SIZE ))return 1 ;return 0 ; }arch/powerpc/kernel/rtas-proc.c +1
-1@@ -767,6 +767,6 @@ static int ppc_rtas_tone_volume_show(struct seq_file *m, void *v)*/ static int ppc_rtas_rmo_buf_show (struct seq_file *m,void *v) { -seq_printf (m,"%016lx %x\n" , rtas_rmo_buf,RTAS_RMOBUF_MAX ); +seq_printf (m,"%016lx %x\n" , rtas_rmo_buf,RTAS_USER_REGION_SIZE );return 0 ; }arch/powerpc/kernel/rtas.c +1
-1@@ -1204,7 +1204,7 @@ void __init rtas_initialize(void)if (firmware_has_feature (FW_FEATURE_LPAR )) rtas_region =min (ppc64_rma_size,RTAS_INSTANTIATE_MAX );#endif - rtas_rmo_buf =memblock_phys_alloc_range (RTAS_RMOBUF_MAX ,PAGE_SIZE , + rtas_rmo_buf =memblock_phys_alloc_range (RTAS_USER_REGION_SIZE ,PAGE_SIZE ,0 , rtas_region);if (!rtas_rmo_buf) panic("ERROR: RTAS: Failed to allocate %lx bytes below %pa\n",
Signature · 1 line
Alexey
Re: [PATCH 5/6] powerpc/rtas: rename RTAS_RMOBUF_MAX to RTAS_USER_REGION_SIZE
Quoted 9 lines · Alexey Kardashevskiy
Quoted 8 lines · Nathan Lynch
arch/powerpc/include/asm/rtas.h +0
-0@@ -19,8 +19,11 @@#define RTAS_UNKNOWN_SERVICE (-1)#define RTAS_INSTANTIATE_MAX (1ULL<<30)/* Don't instantiate rtas at/above this value */
Quoted 5 lines
arch/powerpc/include/asm/rtas.h line 22
Quoted 3 lines
arch/powerpc/include/asm/rtas.h +1
-2-/* Buffer size for ppc_rtas system call. */ -#define RTAS_RMOBUF_MAX (64 * 1024) +/* Work areas shared with RTAS must be 4K, naturally aligned. */ Why exactly 4K and not (for example)
PAGE_SIZE?
4K is a platform requirement and isn't related to Linux's configured page size. See the PAPR specification for RTAS functions such as ibm,configure-connector, ibm,update-nodes, ibm,update-properties.
There are other calls with work area parameters where alignment isn't specified (e.g. ibm,get-system-parameter) but 4KB alignment is a safe choice for those.
Quoted 7 lines
arch/powerpc/include/asm/rtas.h line 26
Quoted 4 lines
arch/powerpc/include/asm/rtas.h +4
-0+#define RTAS_WORK_AREA_SIZE 4096 + +/* Work areas allocated for user space access. */ +#define RTAS_USER_REGION_SIZE (RTAS_WORK_AREA_SIZE * 16)This is still 64K but no clarity why. There is 16 of something, what is it?
There are 16 4KB work areas in the region. I can name it RTAS_NR_USER_WORK_AREAS or similar.
Re: [PATCH 5/6] powerpc/rtas: rename RTAS_RMOBUF_MAX to RTAS_USER_REGION_SIZE
Quoted 16 lines · Nathan Lynch
Quoted 9 lines · Alexey Kardashevskiy
Quoted 8 lines · Nathan Lynch
arch/powerpc/include/asm/rtas.h +0
-0@@ -19,8 +19,11 @@#define RTAS_UNKNOWN_SERVICE (-1)#define RTAS_INSTANTIATE_MAX (1ULL<<30)/* Don't instantiate rtas at/above this value */ Quoted 5 lines
arch/powerpc/include/asm/rtas.h line 22
Quoted 3 lines
arch/powerpc/include/asm/rtas.h +1
-2-/* Buffer size for ppc_rtas system call. */ -#define RTAS_RMOBUF_MAX (64 * 1024) +/* Work areas shared with RTAS must be 4K, naturally aligned. */ Why exactly 4K and not (for example)
PAGE_SIZE?
Quoted 3 lines
4K is a platform requirement and isn't related to Linux's configured page size. See the PAPR specification for RTAS functions such as ibm,configure-connector, ibm,update-nodes, ibm,update-properties.
Good, since we are documenting things here - add to the comment ("per PAPR")?
Quoted 12 lines
There are other calls with work area parameters where alignment isn't specified (e.g. ibm,get-system-parameter) but 4KB alignment is a safe choice for those.
Quoted 7 lines
arch/powerpc/include/asm/rtas.h line 26
Quoted 4 lines
arch/powerpc/include/asm/rtas.h +4
-0+#define RTAS_WORK_AREA_SIZE 4096 + +/* Work areas allocated for user space access. */ +#define RTAS_USER_REGION_SIZE (RTAS_WORK_AREA_SIZE * 16)This is still 64K but no clarity why. There is 16 of something, what is it?
Quoted 2 lines
There are 16 4KB work areas in the region. I can name it
RTAS_NR_USER_WORK_AREASor similar.
Why 16? PAPR (then add "per PAPR") or we just like 16 ("should be enough")?
Signature · 1 line
Alexey
Re: [PATCH 5/6] powerpc/rtas: rename RTAS_RMOBUF_MAX to RTAS_USER_REGION_SIZE
Quoted 17 lines · Alexey Kardashevskiy
Quoted 16 lines · Nathan Lynch
Quoted 14 lines · Alexey Kardashevskiy
arch/powerpc/include/asm/rtas.h line 22
Quoted 11 lines · Nathan Lynch
arch/powerpc/include/asm/rtas.h +1
-2@@ -19,8 +19,11 @@#define RTAS_UNKNOWN_SERVICE (-1)#define RTAS_INSTANTIATE_MAX (1ULL<<30)/* Don't instantiate rtas at/above this value */ -/* Buffer size for ppc_rtas system call. */ -#define RTAS_RMOBUF_MAX (64 * 1024) +/* Work areas shared with RTAS must be 4K, naturally aligned. */ Why exactly 4K and not (for example)
PAGE_SIZE?
Quoted 6 lines
Quoted 3 lines
4K is a platform requirement and isn't related to Linux's configured page size. See the PAPR specification for RTAS functions such as ibm,configure-connector, ibm,update-nodes, ibm,update-properties.
Good, since we are documenting things here - add to the comment ("per PAPR")?
But almost every constant in this header relates to a specification or requirement in PAPR.
Quoted 12 lines
Quoted 12 lines
There are other calls with work area parameters where alignment isn't specified (e.g. ibm,get-system-parameter) but 4KB alignment is a safe choice for those.
Quoted 7 lines
arch/powerpc/include/asm/rtas.h line 26
Quoted 4 lines
arch/powerpc/include/asm/rtas.h +4
-0+#define RTAS_WORK_AREA_SIZE 4096 + +/* Work areas allocated for user space access. */ +#define RTAS_USER_REGION_SIZE (RTAS_WORK_AREA_SIZE * 16)This is still 64K but no clarity why. There is 16 of something, what is it?
Quoted 6 lines
Quoted 2 lines
There are 16 4KB work areas in the region. I can name it
RTAS_NR_USER_WORK_AREASor similar.Why 16? PAPR (then add "per PAPR") or we just like 16 ("should be enough")?
PAPR doesn't know anything about the user region; it's a Linux construct. It's been 64KB since pre-git days and I'm not sure what the original reason is. At this point, maintaining a kernel-user ABI seems like enough justification for the value.
Re: [PATCH 5/6] powerpc/rtas: rename RTAS_RMOBUF_MAX to RTAS_USER_REGION_SIZE
Quoted 22 lines · Nathan Lynch
Quoted 21 lines · Alexey Kardashevskiy
Quoted 19 lines · Nathan Lynch
Quoted 9 lines · Alexey Kardashevskiy
Quoted 8 lines · Nathan Lynch
arch/powerpc/include/asm/rtas.h +0
-0@@ -19,8 +19,11 @@#define RTAS_UNKNOWN_SERVICE (-1)#define RTAS_INSTANTIATE_MAX (1ULL<<30)/* Don't instantiate rtas at/above this value */ Quoted 5 lines
arch/powerpc/include/asm/rtas.h line 22
Quoted 3 lines
arch/powerpc/include/asm/rtas.h +1
-2-/* Buffer size for ppc_rtas system call. */ -#define RTAS_RMOBUF_MAX (64 * 1024) +/* Work areas shared with RTAS must be 4K, naturally aligned. */ Why exactly 4K and not (for example)
PAGE_SIZE?4K is a platform requirement and isn't related to Linux's configured page size. See the PAPR specification for RTAS functions such as ibm,configure-connector, ibm,update-nodes, ibm,update-properties.
Quoted 5 lines
Quoted 2 lines
Good, since we are documenting things here - add to the comment ("per PAPR")?
But almost every constant in this header relates to a specification or requirement in PAPR.
Yup, "almost".
Quoted 17 lines
Quoted 17 lines
Quoted 15 lines
There are other calls with work area parameters where alignment isn't specified (e.g. ibm,get-system-parameter) but 4KB alignment is a safe choice for those.
Quoted 7 lines
arch/powerpc/include/asm/rtas.h line 26
Quoted 4 lines
arch/powerpc/include/asm/rtas.h +4
-0+#define RTAS_WORK_AREA_SIZE 4096 + +/* Work areas allocated for user space access. */ +#define RTAS_USER_REGION_SIZE (RTAS_WORK_AREA_SIZE * 16)This is still 64K but no clarity why. There is 16 of something, what is it?
There are 16 4KB work areas in the region. I can name it
RTAS_NR_USER_WORK_AREASor similar.
Quoted 7 lines
Quoted 2 lines
Why 16? PAPR (then add "per PAPR") or we just like 16 ("should be enough")?
PAPR doesn't know anything about the user region; it's a Linux construct. It's been 64KB since pre-git days and I'm not sure what the original reason is. At this point, maintaining a kernel-user ABI seems like enough justification for the value.
I am not arguing keeping the numbers but you are replacing one magic number with another and for neither it is horribly obvious where they came from. Is 16 the max number of concurrently running sys_rtas system calls? Does the userspace ensure there is no more than 16? btw where is that userspace code? I thought https://github.com/power-ras/ppc64-diag.git but no. Thanks,
Signature · 1 line
Alexey
Re: [PATCH 5/6] powerpc/rtas: rename RTAS_RMOBUF_MAX to RTAS_USER_REGION_SIZE
Quoted 20 lines · Alexey Kardashevskiy
Quoted 19 lines · Nathan Lynch
Quoted 17 lines · Alexey Kardashevskiy
Quoted 10 lines · Nathan Lynch
Quoted 8 lines · Alexey Kardashevskiy
Review
Quoted 4 lines · Nathan Lynch
(quoted diff) +4
-0+#define RTAS_WORK_AREA_SIZE 4096 + +/* Work areas allocated for user space access. */ +#define RTAS_USER_REGION_SIZE (RTAS_WORK_AREA_SIZE * 16)This is still 64K but no clarity why. There is 16 of something, what is it?
Quoted 2 lines
There are 16 4KB work areas in the region. I can name it
RTAS_NR_USER_WORK_AREASor similar.Why 16? PAPR (then add "per PAPR") or we just like 16 ("should be enough")?
Quoted 8 lines
Quoted 4 lines
PAPR doesn't know anything about the user region; it's a Linux construct. It's been 64KB since pre-git days and I'm not sure what the original reason is. At this point, maintaining a kernel-user ABI seems like enough justification for the value.
I am not arguing keeping the numbers but you are replacing one magic number with another and for neither it is horribly obvious where they came from.
When I wrote it I viewed it as changing one of the factors in (64 * 1024) to a named constant that better expresses how the region is used and adjusting the remaining factor to arrive at the same end result. I considered it a net improvement even if we're not sure how 64K was arrived at in the first place, although I suspect it was chosen to support multiple concurrent users, and to be compatible with both 4K and 64K page sizes. Then again 64K pages came a bit after this was introduced.
The change that introduced RTAS_RMOBUF_MAX (here renamed to RTAS_USER_REGION_SIZE) does not explain how the value was derived:
================
Author: Andrew Morton <[email protected]> Date: Sun Jan 18 18:17:30 2004 -0800
[PATCH] ppc64: add rtas syscall, from John Rose
From: Anton Blanchard <[email protected]>
Added RTAS syscall. Reserved lowmem rtas_rmo_buf for userspace use. Created "rmo_buffer" proc file to export bounds of rtas_rmo_buf.
[...]
include/asm-ppc64/rtas.h +3 -0
@@ -19,6 +19,9 @@#define RTAS_UNKNOWN_SERVICE (-1)#define RTAS_INSTANTIATE_MAX (1UL<<30)/* Don't instantiate rtas at/above this value */ +/* Buffer size for ppc_rtas system call. */ +#define RTAS_RMOBUF_MAX (64 * 1024) +
================
The comment "Buffer size for ppc_rtas system call" (removed by my change) is not really appropriate because 1. not all sys_rtas invocations use the buffer, and 2. no callers use the entire buffer.
Quoted 2 lines
Is 16 the max number of concurrently running
sys_rtassystem calls? Does the userspace ensure there is no more than 16?
No and no; not all calls to sys_rtas need to use a work area. However, librtas uses record locking to arbitrate access to the user region, and the unit of allocation is 4KB. This is a reasonable choice: many RTAS calls which take a work area require 4KB alignment. But some do not (ibm,get-system-parameter), and librtas conceivably could be made to perform finer-grained allocations.
It's not the kernel's concern how librtas partitions the user region, so I'm inclined to leave the (64 * 1024) expression alone now. Thanks for your review.
Quoted 2 lines
btw where is that userspace code? I thought
https://github.com/power-ras/ppc64-diag.git but no. Thanks,
librtas, of which ppc64-diag and powerpc-utils are users:
Re: [PATCH 5/6] powerpc/rtas: rename RTAS_RMOBUF_MAX to RTAS_USER_REGION_SIZE
In reply to Nathan LynchQuoted 10 lines · Nathan Lynch
RTAS_RMOBUF_MAXdoesn't actually describe a "maximum" value in any sense. It represents the size of an area of memory set aside for user space to use as work areas for certain RTAS calls.Rename it to
RTAS_USER_REGION, and express the value in terms of the number of work areas allocated.Signed-off-by Nathan Lynch <[email protected]>squash! powerpc/rtas: rename
RTAS_RMOBUF_MAXtoRTAS_USER_REGION_SIZE
I think you meant to get rid of this line...
Signature · 2 lines
Andrew Donnellan OzLabs, ADL Canberra [email protected] IBM Australia Limited
kernel test robot
build botRe: [PATCH 5/6] powerpc/rtas: rename RTAS_RMOBUF_MAX to RTAS_USER_REGION_SIZE
In reply to Nathan LynchThank you for the patch! Yet something to improve:
Hi Nathan,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on powerpc/next]
[also build test ERROR on v5.11-rc3 next-20210115]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Nathan-Lynch/powerpc-rtas-miscellaneous-cleanups-user-region-allocation/20210115-113045 base: https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git next config: powerpc-allyesconfig (attached as .config) compiler: powerpc64-linux-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/656368735841abab983fa593b2ddae52a6273a71
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Nathan-Lynch/powerpc-rtas-miscellaneous-cleanups-user-region-allocation/20210115-113045 git checkout 656368735841abab983fa593b2ddae52a6273a71
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=powerpc
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
arch/powerpc/kernel/rtas.c: In function 'in_rmo_buf':
>> arch/powerpc/kernel/rtas.c:990:26: error: 'RTAS_RMOBUF_MAX' undeclared (first use in this function)
990 | base < (rtas_rmo_buf + RTAS_RMOBUF_MAX) &&
| ^~~~~~~~~~~~~~~
arch/powerpc/kernel/rtas.c:990:26: note: each undeclared identifier is reported only once for each function it appears in
arch/powerpc/kernel/rtas.c:994:1: error: control reaches end of non-void function [-Werror=return-type]
994 | }
| ^cc1: some warnings being treated as errors
vim +/RTAS_RMOBUF_MAX +990 arch/powerpc/kernel/rtas.c
bd59380c5ba4147d Andrew Donnellan 2020-08-20 986
bd59380c5ba4147d Andrew Donnellan 2020-08-20 987 static bool in_rmo_buf(u32 base, u32 end)
bd59380c5ba4147d Andrew Donnellan 2020-08-20 988 {
bd59380c5ba4147d Andrew Donnellan 2020-08-20 989 return base >= rtas_rmo_buf &&
bd59380c5ba4147d Andrew Donnellan 2020-08-20 @990 base < (rtas_rmo_buf + RTAS_RMOBUF_MAX) &&
bd59380c5ba4147d Andrew Donnellan 2020-08-20 991 base <= end &&
bd59380c5ba4147d Andrew Donnellan 2020-08-20 992 end >= rtas_rmo_buf &&
bd59380c5ba4147d Andrew Donnellan 2020-08-20 993 end < (rtas_rmo_buf + RTAS_RMOBUF_MAX);
bd59380c5ba4147d Andrew Donnellan 2020-08-20 994 }bd59380c5ba4147d Andrew Donnellan 2020-08-20 995
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]
PATCH 6/6 powerpc/rtas: constrain user region allocation to RMA
In reply to Nathan LynchMemory locations passed as arguments from the OS to RTAS usually need to be addressable in 32-bit mode and must reside in the Real Mode Area. On PAPR guests, the RMA starts at logical address 0 and is the first logical memory block reported in the LPAR’s device tree.
On powerpc targets with RTAS, Linux makes available to user space a region of memory suitable for arguments to be passed to RTAS via sys_rtas(). This region (rtas_rmo_buf) is allocated via the memblock API during boot in order to ensure that it satisfies the requirements described above.
With radix MMU, the upper limit supplied to the memblock allocation can exceed the bounds of the first logical memory block, since ppc64_rma_size is ULONG_MAX and RTAS_INSTANTIATE_MAX is 1GB. (512MB is a common size of the first memory block according to a small sample of LPARs I have checked.) This leads to failures when user space invokes an RTAS function that uses a work area, such as ibm,configure-connector.
Alter the determination of the upper limit for rtas_rmo_buf's allocation to consult the device tree directly, ensuring placement within the RMA regardless of the MMU in use.
--- arch/powerpc/kernel/rtas.c | 80 +++++++++++++++++++++++++++++++------- 1 file changed, 65 insertions(+), 15 deletions(-)
arch/powerpc/kernel/rtas.c +65 -15
@@ -1166,6 +1166,70 @@ SYSCALL_DEFINE1(rtas, struct rtas_args __user *, uargs)return 0 ; } +/* +* Memory locations passed to RTAS must be in the RMA as described by +* the range in /memory@0. +*/ +static phys_addr_t rtas_arg_addr_limit (void ) +{ +unsigned int addr_cells; +unsigned int size_cells; +struct device_node *np; +const __be32 *prop; +u64 limit; +u64 base; + +/* RTAS is instantiated in 32-bit mode. */ + limit =1ULL <<32 ; + +/* Account for mem=. */ +if (memory_limit !=0 ) + limit =min (limit, memory_limit); + + np =of_find_node_by_path ("/memory@0" ); +if (!np) + goto out; + + prop =of_get_property (np,"reg" ,NULL ); +if (!prop) + goto put; + + addr_cells =of_n_addr_cells (np); + base =of_read_number (prop, addr_cells); + prop += addr_cells; + size_cells =of_n_size_cells (np); + limit =min (limit,of_read_number (prop, size_cells)); +put: +of_node_put (np); +out: +pr_debug ("%s: base = %#llx limit = %#llx" , __func__, base, limit); + +return limit; +} + +static void __init rtas_user_region_setup (void ) +{ +phys_addr_t limit, align, size; + + limit =rtas_arg_addr_limit (); + size =RTAS_USER_REGION_SIZE ; + +/* +* Although work areas need only 4KB alignment, user space +* accesses this region via mmap so it must be placed on a +* page boundary. +*/ + align =PAGE_SIZE ; + + rtas_rmo_buf =memblock_phys_alloc_range (size, align,0 , limit); +if (rtas_rmo_buf ==0 ) { +panic ("Failed to allocate %llu bytes for user region below %pa\n" , + size, &limit); + } + +pr_debug ("RTAS user region allocated at %pa\n" , &rtas_rmo_buf); +} +/* * Call early during boot, before mem init, to retrieve the RTAS * information from the device-tree and allocate the RMO buffer for userland
@@ -1173,7 +1237,6 @@ SYSCALL_DEFINE1(rtas, struct rtas_args __user *, uargs)*/ void __init rtas_initialize (void ) { -unsigned long rtas_region =RTAS_INSTANTIATE_MAX ;u32 base, size, entry;int no_base, no_size, no_entry;
@@ -1197,23 +1260,10 @@ void __init rtas_initialize(void) no_entry =of_property_read_u32 (rtas.dev,"linux,rtas-entry" , &entry); rtas.entry = no_entry ? rtas.base : entry; -/* If RTAS was found, allocate the RMO buffer for it and look for -* the stop-self token if any -*/ -#ifdef CONFIG_PPC64 -if (firmware_has_feature (FW_FEATURE_LPAR )) - rtas_region =min (ppc64_rma_size,RTAS_INSTANTIATE_MAX ); -#endif - rtas_rmo_buf =memblock_phys_alloc_range (RTAS_USER_REGION_SIZE ,PAGE_SIZE , -0 , rtas_region); -if (!rtas_rmo_buf) -panic ("ERROR: RTAS: Failed to allocate %lx bytes below %pa\n" , -PAGE_SIZE , &rtas_region); -#ifdef CONFIG_RTAS_ERROR_LOGGING rtas_last_error_token =rtas_token ("rtas-last-error" );#endif - +rtas_user_region_setup ();rtas_syscall_filter_init (); }
Signature · 1 line
2.29.2
Re: [PATCH 6/6] powerpc/rtas: constrain user region allocation to RMA
Quoted 19 lines · Nathan Lynch
Memory locations passed as arguments from the OS to RTAS usually need to be addressable in 32-bit mode and must reside in the Real Mode Area. On PAPR guests, the RMA starts at logical address 0 and is the first logical memory block reported in the LPAR’s device tree.
On powerpc targets with RTAS, Linux makes available to user space a region of memory suitable for arguments to be passed to RTAS via
sys_rtas(). This region (rtas_rmo_buf) is allocated via the memblock API during boot in order to ensure that it satisfies the requirements described above.With radix MMU, the upper limit supplied to the memblock allocation can exceed the bounds of the first logical memory block, since
ppc64_rma_sizeisULONG_MAXandRTAS_INSTANTIATE_MAXis 1GB. (512MB is a common size of the first memory block according to a small sample of LPARs I have checked.) This leads to failures when user space invokes an RTAS function that uses a work area, such as ibm,configure-connector.
Quoted 3 lines
Alter the determination of the upper limit for rtas_rmo_buf's allocation to consult the device tree directly, ensuring placement within the RMA regardless of the MMU in use.
Can we tie this with RTAS (which also needs to be in RMA) and simply add extra 64K in prom_instantiate_rtas() and advertise this address (ALIGH_UP(rtas-base + rtas-size, PAGE_SIZE)) to the user space? We do not need this RMO area before that point.
And probably do the same with per-cpu RTAS argument structures mentioned in the cover letter?
Quoted 115 lines
Signed-off-by Nathan Lynch <[email protected]>--- arch/powerpc/kernel/rtas.c | 80 +++++++++++++++++++++++++++++++------- 1 file changed, 65 insertions(+), 15 deletions(-)arch/powerpc/kernel/rtas.c +65
-15@@ -1166,6 +1166,70 @@ SYSCALL_DEFINE1(rtas, struct rtas_args __user *, uargs)return 0 ; } +/* +* Memory locations passed to RTAS must be in the RMA as described by +* the range in /memory@0. +*/ +static phys_addr_t rtas_arg_addr_limit (void ) +{ +unsigned int addr_cells; +unsigned int size_cells; +struct device_node *np; +const __be32 *prop; +u64 limit; +u64 base; + +/* RTAS is instantiated in 32-bit mode. */ + limit =1ULL <<32 ; + +/* Account for mem=. */ +if (memory_limit !=0 ) + limit =min (limit, memory_limit); + + np =of_find_node_by_path ("/memory@0" ); +if (!np) + goto out; + + prop =of_get_property (np,"reg" ,NULL ); +if (!prop) + goto put; + + addr_cells =of_n_addr_cells (np); + base =of_read_number (prop, addr_cells); + prop += addr_cells; + size_cells =of_n_size_cells (np); + limit =min (limit,of_read_number (prop, size_cells)); +put: +of_node_put (np); +out: +pr_debug ("%s: base = %#llx limit = %#llx" , __func__, base, limit); + +return limit; +} + +static void __init rtas_user_region_setup (void ) +{ +phys_addr_t limit, align, size; + + limit =rtas_arg_addr_limit (); + size =RTAS_USER_REGION_SIZE ; + +/* +* Although work areas need only 4KB alignment, user space +* accesses this region via mmap so it must be placed on a +* page boundary. +*/ + align =PAGE_SIZE ; + + rtas_rmo_buf =memblock_phys_alloc_range (size, align,0 , limit); +if (rtas_rmo_buf ==0 ) { +panic ("Failed to allocate %llu bytes for user region below %pa\n" , + size, &limit); + } + +pr_debug ("RTAS user region allocated at %pa\n" , &rtas_rmo_buf); +} +/* * Call early during boot, before mem init, to retrieve the RTAS * information from the device-tree and allocate the RMO buffer for userland @@ -1173,7 +1237,6 @@ SYSCALL_DEFINE1(rtas, struct rtas_args __user *, uargs)*/ void __init rtas_initialize (void ) { -unsigned long rtas_region =RTAS_INSTANTIATE_MAX ;u32 base, size, entry;int no_base, no_size, no_entry;@@ -1197,23 +1260,10 @@ void __init rtas_initialize(void) no_entry =of_property_read_u32 (rtas.dev,"linux,rtas-entry" , &entry); rtas.entry = no_entry ? rtas.base : entry; -/* If RTAS was found, allocate the RMO buffer for it and look for -* the stop-self token if any -*/ -#ifdef CONFIG_PPC64 -if (firmware_has_feature (FW_FEATURE_LPAR )) - rtas_region =min (ppc64_rma_size,RTAS_INSTANTIATE_MAX ); -#endif - rtas_rmo_buf =memblock_phys_alloc_range (RTAS_USER_REGION_SIZE ,PAGE_SIZE , -0 , rtas_region); -if (!rtas_rmo_buf) -panic ("ERROR: RTAS: Failed to allocate %lx bytes below %pa\n" , -PAGE_SIZE , &rtas_region); -#ifdef CONFIG_RTAS_ERROR_LOGGING rtas_last_error_token =rtas_token ("rtas-last-error" );#endif - +rtas_user_region_setup ();rtas_syscall_filter_init (); }
Signature · 1 line
Alexey
Re: [PATCH 6/6] powerpc/rtas: constrain user region allocation to RMA
Quoted 20 lines · Alexey Kardashevskiy
Quoted 19 lines · Nathan Lynch
Memory locations passed as arguments from the OS to RTAS usually need to be addressable in 32-bit mode and must reside in the Real Mode Area. On PAPR guests, the RMA starts at logical address 0 and is the first logical memory block reported in the LPAR’s device tree.
On powerpc targets with RTAS, Linux makes available to user space a region of memory suitable for arguments to be passed to RTAS via
sys_rtas(). This region (rtas_rmo_buf) is allocated via the memblock API during boot in order to ensure that it satisfies the requirements described above.With radix MMU, the upper limit supplied to the memblock allocation can exceed the bounds of the first logical memory block, since
ppc64_rma_sizeisULONG_MAXandRTAS_INSTANTIATE_MAXis 1GB. (512MB is a common size of the first memory block according to a small sample of LPARs I have checked.) This leads to failures when user space invokes an RTAS function that uses a work area, such as ibm,configure-connector.
Quoted 8 lines
Quoted 3 lines
Alter the determination of the upper limit for rtas_rmo_buf's allocation to consult the device tree directly, ensuring placement within the RMA regardless of the MMU in use.
Can we tie this with RTAS (which also needs to be in RMA) and simply add extra 64K in
prom_instantiate_rtas()and advertise this address (ALIGH_UP(rtas-base + rtas-size, PAGE_SIZE)) to the user space? We do not need this RMO area before that point.
Can you explain more about what advantage that would bring? I'm not seeing it. It's a more significant change than what I've written here. Would it interact well with kexec?
Quoted 2 lines
And probably do the same with per-cpu RTAS argument structures mentioned in the cover letter?
I don't think so, since those need to be allocated with the pacas and limited to the maximum possible CPUs, which is discovered by the kernel much later.
But maybe I misunderstand what you're suggesting.
Re: [PATCH 6/6] powerpc/rtas: constrain user region allocation to RMA
Quoted 25 lines · Nathan Lynch
Quoted 24 lines · Alexey Kardashevskiy
Quoted 22 lines · Nathan Lynch
Memory locations passed as arguments from the OS to RTAS usually need to be addressable in 32-bit mode and must reside in the Real Mode Area. On PAPR guests, the RMA starts at logical address 0 and is the first logical memory block reported in the LPAR’s device tree.
On powerpc targets with RTAS, Linux makes available to user space a region of memory suitable for arguments to be passed to RTAS via
sys_rtas(). This region (rtas_rmo_buf) is allocated via the memblock API during boot in order to ensure that it satisfies the requirements described above.With radix MMU, the upper limit supplied to the memblock allocation can exceed the bounds of the first logical memory block, since
ppc64_rma_sizeisULONG_MAXandRTAS_INSTANTIATE_MAXis 1GB. (512MB is a common size of the first memory block according to a small sample of LPARs I have checked.) This leads to failures when user space invokes an RTAS function that uses a work area, such as ibm,configure-connector.Alter the determination of the upper limit for rtas_rmo_buf's allocation to consult the device tree directly, ensuring placement within the RMA regardless of the MMU in use.
Quoted 8 lines
Quoted 4 lines
Can we tie this with RTAS (which also needs to be in RMA) and simply add extra 64K in
prom_instantiate_rtas()and advertise this address (ALIGH_UP(rtas-base + rtas-size, PAGE_SIZE)) to the user space? We do not need this RMO area before that point.Can you explain more about what advantage that would bring? I'm not seeing it. It's a more significant change than what I've written here.
We already allocate space for RTAS and (like RMO) it needs to be in RMA, and RMO is useless without RTAS. We can reuse RTAS allocation code for RMO like this:
===
arch/powerpc/kernel/prom_init.c +2 -1
@@ -1821,7 +1821,8 @@ static void __init prom_instantiate_rtas(void)if (size ==0 )return ; - base =alloc_down (size,PAGE_SIZE ,0 ); +/* One page for RTAS, one for RMO */ + base =alloc_down (size,PAGE_SIZE +PAGE_SIZE ,0 );if (base ==0 )prom_panic ("Could not allocate memory for RTAS\n" );
arch/powerpc/kernel/rtas.c +0 -0
@@ -1186,6 +1186,7 @@ void __init rtas_initialize(void) rtas.size = size; no_entry = of_property_read_u32(rtas.dev, "linux,rtas-entry",
&entry);
rtas.entry = no_entry ? rtas.base : entry;+ rtas_rmo_buf = rtas.base + PAGE_SIZE;
/* If RTAS was found, allocate the RMO buffer for it and look for
* the stop-self token if any
@@ -1196,11 +1197,6 @@ void __init rtas_initialize(void)
ibm_suspend_me_token = rtas_token("ibm,suspend-me");
} #endif
- rtas_rmo_buf = memblock_phys_alloc_range(RTAS_RMOBUF_MAX, PAGE_SIZE,
- 0, rtas_region);
- if (!rtas_rmo_buf)
- panic("ERROR: RTAS: Failed to allocate %lx bytes below %pa\n",
- PAGE_SIZE, &rtas_region);
===
May be store in the FDT as "linux,rmo-base" next to "linux,rtas-base", for clarity, as sharing symbols between prom and main kernel is a bit tricky.
The benefit is that we do not do the same thing (== find 64K in RMA) in 2 different ways and if the RMO allocated my way is broken - we'll know it much sooner as RTAS itself will break too.
Quoted 1 line
Would it interact well with kexec?
Good point. For this, the easiest will be setting rtas-size in the FDT to the allocated RTAS space (PAGE_SIZE*2 with the hunk above applied). Probably.
Quoted 6 lines
Quoted 2 lines
And probably do the same with per-cpu RTAS argument structures mentioned in the cover letter?
I don't think so, since those need to be allocated with the pacas and limited to the maximum possible CPUs, which is discovered by the kernel much later.
The first cell of /proc/device-tree/cpus/ibm,drc-indexes is the number of cores, it is there when RTAS is instantiated, we know SMT after "ibm,client-architecture-support" (if I remember correctly).
Quoted 1 line
But maybe I misunderstand what you're suggesting.
Usually it is me missing the bigger picture :)
Signature · 1 line
Alexey
Re: [PATCH 6/6] powerpc/rtas: constrain user region allocation to RMA
Quoted 31 lines · Alexey Kardashevskiy
Quoted 30 lines · Nathan Lynch
Quoted 28 lines · Alexey Kardashevskiy
Quoted 19 lines · Nathan Lynch
Memory locations passed as arguments from the OS to RTAS usually need to be addressable in 32-bit mode and must reside in the Real Mode Area. On PAPR guests, the RMA starts at logical address 0 and is the first logical memory block reported in the LPAR’s device tree.
On powerpc targets with RTAS, Linux makes available to user space a region of memory suitable for arguments to be passed to RTAS via
sys_rtas(). This region (rtas_rmo_buf) is allocated via the memblock API during boot in order to ensure that it satisfies the requirements described above.With radix MMU, the upper limit supplied to the memblock allocation can exceed the bounds of the first logical memory block, since
ppc64_rma_sizeisULONG_MAXandRTAS_INSTANTIATE_MAXis 1GB. (512MB is a common size of the first memory block according to a small sample of LPARs I have checked.) This leads to failures when user space invokes an RTAS function that uses a work area, such as ibm,configure-connector.Quoted 3 lines
Alter the determination of the upper limit for rtas_rmo_buf's allocation to consult the device tree directly, ensuring placement within the RMA regardless of the MMU in use.
Can we tie this with RTAS (which also needs to be in RMA) and simply add extra 64K in
prom_instantiate_rtas()and advertise this address (ALIGH_UP(rtas-base + rtas-size, PAGE_SIZE)) to the user space? We do not need this RMO area before that point.
Quoted 8 lines
Quoted 3 lines
Can you explain more about what advantage that would bring? I'm not seeing it. It's a more significant change than what I've written here.
We already allocate space for RTAS and (like RMO) it needs to be in RMA, and RMO is useless without RTAS. We can reuse RTAS allocation code for RMO like this:
When you say RMO I assume you are referring to rtas_rmo_buf? (I don't think it is well-named.)
arch/powerpc/kernel/prom_init.c static void __init prom_instantiate_rtas(void) · line 1824
Quoted 12 lines
===
arch/powerpc/kernel/prom_init.c +1
-1@@ -1821,7 +1821,8 @@ static void __init prom_instantiate_rtas(void)if (size ==0 )return ; - base =alloc_down (size,PAGE_SIZE ,0 ); +/* One page for RTAS, one for RMO */
One page for RTAS? RTAS is ~20MB on LPARs I've checked:
# lsprop /proc/device-tree/rtas/{rtas-size,linux,rtas-base}
/proc/device-tree/rtas/rtas-size
01370000 (20381696)arch/powerpc/kernel/prom_init.c static void __init prom_instantiate_rtas(void) · line 1825
Quoted 1 line
arch/powerpc/kernel/prom_init.c +1
-0+ base =alloc_down (size,PAGE_SIZE +PAGE_SIZE ,0 );
This changes the alignment but not the size of the allocation.
arch/powerpc/kernel/rtas.c void __init rtas_initialize(void) · line 1190
Quoted 13 lines
arch/powerpc/kernel/prom_init.c +0
-0if (base ==0 )prom_panic ("Could not allocate memory for RTAS\n" );arch/powerpc/kernel/rtas.c +1
-0@@ -1186,6 +1186,7 @@ void __init rtas_initialize(void) rtas.size = size; no_entry =of_property_read_u32 (rtas.dev,"linux,rtas-entry" , &entry); rtas.entry = no_entry ? rtas.base : entry; + rtas_rmo_buf = rtas.base +PAGE_SIZE ;
I think this would overlay the user region on top of the RTAS private data area, allowing user space to corrupt it.
Quoted 15 lines
arch/powerpc/kernel/rtas.c +0
-5/* If RTAS was found, allocate the RMO buffer for it and look for * the stop-self token if any @@ -1196,11 +1197,6 @@ void __init rtas_initialize(void) ibm_suspend_me_token = rtas_token("ibm,suspend-me"); }#endif - rtas_rmo_buf =memblock_phys_alloc_range (RTAS_RMOBUF_MAX ,PAGE_SIZE , -0 , rtas_region); -if (!rtas_rmo_buf) - panic("ERROR: RTAS: Failed to allocate %lx bytes below %pa\n", - PAGE_SIZE, &rtas_region);===
Quoted 7 lines
May be store in the FDT as "linux,rmo-base" next to "linux,rtas-base", for clarity, as sharing symbols between prom and main kernel is a bit tricky.
The benefit is that we do not do the same thing (== find 64K in RMA)
in 2 different ways and if the RMO allocated my way is broken - we'll know it much sooner as RTAS itself will break too.
Implementation details aside... I'll grant that combining the allocations into one in prom_init reduces some duplication in the sense that both are subject to the same constraints (mostly - the RTAS data area must not cross a 256MB boundary, while the user region may). But they really are distinct concerns. The RTAS private data area is specified in the platform architecture, the OS is obligated to allocate it and pass it to instantiate-rtas, etc etc. However the user region (rtas_rmo_buf) is purely a Linux construct which is there to support sys_rtas.
Now, there are multiple sites in the kernel proper that must allocate memory suitable for passing to RTAS. Obviously there is value in consolidating the logic for that purpose in one place, so I'll work on adding that in v2. OK?
Re: [PATCH 6/6] powerpc/rtas: constrain user region allocation to RMA
Quoted 55 lines · Nathan Lynch
Quoted 31 lines · Alexey Kardashevskiy
Quoted 30 lines · Nathan Lynch
Quoted 28 lines · Alexey Kardashevskiy
Quoted 19 lines · Nathan Lynch
Memory locations passed as arguments from the OS to RTAS usually need to be addressable in 32-bit mode and must reside in the Real Mode Area. On PAPR guests, the RMA starts at logical address 0 and is the first logical memory block reported in the LPAR’s device tree.
On powerpc targets with RTAS, Linux makes available to user space a region of memory suitable for arguments to be passed to RTAS via
sys_rtas(). This region (rtas_rmo_buf) is allocated via the memblock API during boot in order to ensure that it satisfies the requirements described above.With radix MMU, the upper limit supplied to the memblock allocation can exceed the bounds of the first logical memory block, since
ppc64_rma_sizeisULONG_MAXandRTAS_INSTANTIATE_MAXis 1GB. (512MB is a common size of the first memory block according to a small sample of LPARs I have checked.) This leads to failures when user space invokes an RTAS function that uses a work area, such as ibm,configure-connector.Quoted 3 lines
Alter the determination of the upper limit for rtas_rmo_buf's allocation to consult the device tree directly, ensuring placement within the RMA regardless of the MMU in use.
Can we tie this with RTAS (which also needs to be in RMA) and simply add extra 64K in
prom_instantiate_rtas()and advertise this address (ALIGH_UP(rtas-base + rtas-size, PAGE_SIZE)) to the user space? We do not need this RMO area before that point.Quoted 8 lines
Quoted 3 lines
Can you explain more about what advantage that would bring? I'm not seeing it. It's a more significant change than what I've written here.
We already allocate space for RTAS and (like RMO) it needs to be in RMA, and RMO is useless without RTAS. We can reuse RTAS allocation code for RMO like this:
When you say RMO I assume you are referring to
rtas_rmo_buf? (I don't think it is well-named.)Quoted 10 lines
===
arch/powerpc/kernel/prom_init.c +0
-0@@ -1821,7 +1821,8 @@ static void __init prom_instantiate_rtas(void)if (size ==0 )return ;
Quoted 8 lines
arch/powerpc/kernel/prom_init.c static void __init prom_instantiate_rtas(void) · line 1824
Quoted 2 lines
arch/powerpc/kernel/prom_init.c +1
-1- base =alloc_down (size,PAGE_SIZE ,0 ); +/* One page for RTAS, one for RMO */ One page for RTAS? RTAS is ~20MB on LPARs I've checked:
# lsprop /proc/device-tree/rtas/{rtas-size,linux,rtas-base} /proc/device-tree/rtas/rtas-size 01370000 (20381696)
You are right, I did not sleep well when replied, sorry about that :) I tried it with KVM where RTAS is just a few KBs (20 constant bytes + MCE log, depends on cpu number) so it worked for me.
Quoted 4 lines
arch/powerpc/kernel/prom_init.c static void __init prom_instantiate_rtas(void) · line 1825
Quoted 2 lines
arch/powerpc/kernel/prom_init.c +1
-0+ base =alloc_down (size,PAGE_SIZE +PAGE_SIZE ,0 );This changes the alignment but not the size of the allocation.
Should be:
base = alloc_down(ALIGN_UP(size, PAGE_SIZE) + PAGE_SIZE, PAGE_SIZE, 0);
Quoted 5 lines
Quoted 5 lines
arch/powerpc/kernel/prom_init.c +0
-0if (base ==0 )prom_panic ("Could not allocate memory for RTAS\n" );
Quoted 13 lines
arch/powerpc/kernel/rtas.c void __init rtas_initialize(void) · line 1190
Quoted 10 lines
arch/powerpc/kernel/rtas.c +1
-0@@ -1186,6 +1186,7 @@ void __init rtas_initialize(void) rtas.size = size; no_entry =of_property_read_u32 (rtas.dev,"linux,rtas-entry" , &entry); rtas.entry = no_entry ? rtas.base : entry; + rtas_rmo_buf = rtas.base +PAGE_SIZE ;I think this would overlay the user region on top of the RTAS private data area, allowing user space to corrupt it.
Right, my bad. Should be:
rtas_rmo_buf = ALIGN_UP(rtas.base + rtas.size, PAGE_SIZE);
Quoted 20 lines
Quoted 20 lines
arch/powerpc/kernel/rtas.c +0
-5/* If RTAS was found, allocate the RMO buffer for it and look for * the stop-self token if any @@ -1196,11 +1197,6 @@ void __init rtas_initialize(void) ibm_suspend_me_token = rtas_token("ibm,suspend-me"); }#endif - rtas_rmo_buf =memblock_phys_alloc_range (RTAS_RMOBUF_MAX ,PAGE_SIZE , -0 , rtas_region); -if (!rtas_rmo_buf) - panic("ERROR: RTAS: Failed to allocate %lx bytes below %pa\n", - PAGE_SIZE, &rtas_region);===
May be store in the FDT as "linux,rmo-base" next to "linux,rtas-base", for clarity, as sharing symbols between prom and main kernel is a bit tricky.
Quoted 13 lines
Quoted 3 lines
The benefit is that we do not do the same thing (== find 64K in RMA)
in 2 different ways and if the RMO allocated my way is broken - we'll know it much sooner as RTAS itself will break too.Implementation details aside... I'll grant that combining the allocations into one in
prom_initreduces some duplication in the sense that both are subject to the same constraints (mostly - the RTAS data area must not cross a 256MB boundary, while the user region may). But they really are distinct concerns. The RTAS private data area is specified in the platform architecture, the OS is obligated to allocate it and pass it to instantiate-rtas, etc etc. However the user region (rtas_rmo_buf) is purely a Linux construct which is there to supportsys_rtas.
Not purely - it should be an address which RTAS accepts. Cannot argue with the rest though, it all sounds correct.
Quoted 4 lines
Now, there are multiple sites in the kernel proper that must allocate memory suitable for passing to RTAS. Obviously there is value in consolidating the logic for that purpose in one place, so I'll work on adding that in v2. OK?
Sure.
Signature · 1 line
Alexey
Michael Ellerman
reviewerRe: [PATCH 6/6] powerpc/rtas: constrain user region allocation to RMA
In reply to Nathan LynchQuoted 37 lines · Nathan Lynch
Quoted 36 lines · Alexey Kardashevskiy
Quoted 33 lines · Nathan Lynch
Quoted 20 lines · Alexey Kardashevskiy
Quoted 19 lines · Nathan Lynch
Memory locations passed as arguments from the OS to RTAS usually need to be addressable in 32-bit mode and must reside in the Real Mode Area. On PAPR guests, the RMA starts at logical address 0 and is the first logical memory block reported in the LPAR’s device tree.
On powerpc targets with RTAS, Linux makes available to user space a region of memory suitable for arguments to be passed to RTAS via
sys_rtas(). This region (rtas_rmo_buf) is allocated via the memblock API during boot in order to ensure that it satisfies the requirements described above.With radix MMU, the upper limit supplied to the memblock allocation can exceed the bounds of the first logical memory block, since
ppc64_rma_sizeisULONG_MAXandRTAS_INSTANTIATE_MAXis 1GB. (512MB is a common size of the first memory block according to a small sample of LPARs I have checked.) This leads to failures when user space invokes an RTAS function that uses a work area, such as ibm,configure-connector.Quoted 8 lines
Quoted 3 lines
Alter the determination of the upper limit for rtas_rmo_buf's allocation to consult the device tree directly, ensuring placement within the RMA regardless of the MMU in use.
Can we tie this with RTAS (which also needs to be in RMA) and simply add extra 64K in
prom_instantiate_rtas()and advertise this address (ALIGH_UP(rtas-base + rtas-size, PAGE_SIZE)) to the user space? We do not need this RMO area before that point.Can you explain more about what advantage that would bring? I'm not seeing it. It's a more significant change than what I've written here.
Quoted 6 lines
Quoted 3 lines
We already allocate space for RTAS and (like RMO) it needs to be in RMA, and RMO is useless without RTAS. We can reuse RTAS allocation code for RMO like this:
When you say RMO I assume you are referring to
rtas_rmo_buf? (I don't think it is well-named.)
...
RMO (Real mode offset) is the old term we used to use to refer to what is now called the RMA (Real mode area). There are still many references to RMO in Linux, but they almost certainly all refer to what we now call the RMA.
Quoted 18 lines
Quoted 7 lines
May be store in the FDT as "linux,rmo-base" next to "linux,rtas-base", for clarity, as sharing symbols between prom and main kernel is a bit tricky.
The benefit is that we do not do the same thing (== find 64K in RMA)
in 2 different ways and if the RMO allocated my way is broken - we'll know it much sooner as RTAS itself will break too.Implementation details aside... I'll grant that combining the allocations into one in
prom_initreduces some duplication in the sense that both are subject to the same constraints (mostly - the RTAS data area must not cross a 256MB boundary, while the user region may). But they really are distinct concerns. The RTAS private data area is specified in the platform architecture, the OS is obligated to allocate it and pass it to instantiate-rtas, etc etc. However the user region (rtas_rmo_buf) is purely a Linux construct which is there to supportsys_rtas.
Quoted 4 lines
Now, there are multiple sites in the kernel proper that must allocate memory suitable for passing to RTAS. Obviously there is value in consolidating the logic for that purpose in one place, so I'll work on adding that in v2. OK?
I don't think we want to move any allocations into prom_init.c unless we have to.
It's best thought of as a trampoline, that runs before the kernel proper, to transition from live OF to a flat DT environment. One thing that must be done as part of that is instantiating RTAS, because it's basically a runtime copy of the live OF. But any other allocs are for Linux to handle later, IMHO.
cheers
Re: [PATCH 6/6] powerpc/rtas: constrain user region allocation to RMA
Quoted 42 lines · Michael Ellerman
Quoted 41 lines · Nathan Lynch
Quoted 39 lines · Alexey Kardashevskiy
Quoted 25 lines · Nathan Lynch
Quoted 24 lines · Alexey Kardashevskiy
Quoted 22 lines · Nathan Lynch
Memory locations passed as arguments from the OS to RTAS usually need to be addressable in 32-bit mode and must reside in the Real Mode Area. On PAPR guests, the RMA starts at logical address 0 and is the first logical memory block reported in the LPAR’s device tree.
On powerpc targets with RTAS, Linux makes available to user space a region of memory suitable for arguments to be passed to RTAS via
sys_rtas(). This region (rtas_rmo_buf) is allocated via the memblock API during boot in order to ensure that it satisfies the requirements described above.With radix MMU, the upper limit supplied to the memblock allocation can exceed the bounds of the first logical memory block, since
ppc64_rma_sizeisULONG_MAXandRTAS_INSTANTIATE_MAXis 1GB. (512MB is a common size of the first memory block according to a small sample of LPARs I have checked.) This leads to failures when user space invokes an RTAS function that uses a work area, such as ibm,configure-connector.Alter the determination of the upper limit for rtas_rmo_buf's allocation to consult the device tree directly, ensuring placement within the RMA regardless of the MMU in use.
Quoted 8 lines
Quoted 4 lines
Can we tie this with RTAS (which also needs to be in RMA) and simply add extra 64K in
prom_instantiate_rtas()and advertise this address (ALIGH_UP(rtas-base + rtas-size, PAGE_SIZE)) to the user space? We do not need this RMO area before that point.Can you explain more about what advantage that would bring? I'm not seeing it. It's a more significant change than what I've written here.
We already allocate space for RTAS and (like RMO) it needs to be in RMA, and RMO is useless without RTAS. We can reuse RTAS allocation code for RMO like this:
Quoted 8 lines
Quoted 2 lines
When you say RMO I assume you are referring to
rtas_rmo_buf? (I don't think it is well-named.)...
RMO (Real mode offset) is the old term we used to use to refer to what is now called the RMA (Real mode area). There are still many references to RMO in Linux, but they almost certainly all refer to what we now call the RMA.
Yes... but I think in this discussion Alexey was using RMO to stand in for rtas_rmo_buf, which was what I was trying to clarify.
Quoted 23 lines
Quoted 18 lines
Quoted 7 lines
May be store in the FDT as "linux,rmo-base" next to "linux,rtas-base", for clarity, as sharing symbols between prom and main kernel is a bit tricky.
The benefit is that we do not do the same thing (== find 64K in RMA)
in 2 different ways and if the RMO allocated my way is broken - we'll know it much sooner as RTAS itself will break too.Implementation details aside... I'll grant that combining the allocations into one in
prom_initreduces some duplication in the sense that both are subject to the same constraints (mostly - the RTAS data area must not cross a 256MB boundary, while the user region may). But they really are distinct concerns. The RTAS private data area is specified in the platform architecture, the OS is obligated to allocate it and pass it to instantiate-rtas, etc etc. However the user region (rtas_rmo_buf) is purely a Linux construct which is there to supportsys_rtas.Quoted 4 lines
Now, there are multiple sites in the kernel proper that must allocate memory suitable for passing to RTAS. Obviously there is value in consolidating the logic for that purpose in one place, so I'll work on adding that in v2. OK?
Quoted 8 lines
I don't think we want to move any allocations into
prom_init.cunless we have to.It's best thought of as a trampoline, that runs before the kernel proper, to transition from live OF to a flat DT environment. One thing that must be done as part of that is instantiating RTAS, because it's basically a runtime copy of the live OF. But any other allocs are for Linux to handle later, IMHO.
Agreed.
Re: [PATCH 6/6] powerpc/rtas: constrain user region allocation to RMA
Quoted 47 lines · Nathan Lynch
Quoted 46 lines · Michael Ellerman
Quoted 43 lines · Nathan Lynch
Quoted 31 lines · Alexey Kardashevskiy
Quoted 30 lines · Nathan Lynch
Quoted 28 lines · Alexey Kardashevskiy
Quoted 19 lines · Nathan Lynch
Memory locations passed as arguments from the OS to RTAS usually need to be addressable in 32-bit mode and must reside in the Real Mode Area. On PAPR guests, the RMA starts at logical address 0 and is the first logical memory block reported in the LPAR’s device tree.
On powerpc targets with RTAS, Linux makes available to user space a region of memory suitable for arguments to be passed to RTAS via
sys_rtas(). This region (rtas_rmo_buf) is allocated via the memblock API during boot in order to ensure that it satisfies the requirements described above.With radix MMU, the upper limit supplied to the memblock allocation can exceed the bounds of the first logical memory block, since
ppc64_rma_sizeisULONG_MAXandRTAS_INSTANTIATE_MAXis 1GB. (512MB is a common size of the first memory block according to a small sample of LPARs I have checked.) This leads to failures when user space invokes an RTAS function that uses a work area, such as ibm,configure-connector.Quoted 3 lines
Alter the determination of the upper limit for rtas_rmo_buf's allocation to consult the device tree directly, ensuring placement within the RMA regardless of the MMU in use.
Can we tie this with RTAS (which also needs to be in RMA) and simply add extra 64K in
prom_instantiate_rtas()and advertise this address (ALIGH_UP(rtas-base + rtas-size, PAGE_SIZE)) to the user space? We do not need this RMO area before that point.Quoted 8 lines
Quoted 3 lines
Can you explain more about what advantage that would bring? I'm not seeing it. It's a more significant change than what I've written here.
We already allocate space for RTAS and (like RMO) it needs to be in RMA, and RMO is useless without RTAS. We can reuse RTAS allocation code for RMO like this:
When you say RMO I assume you are referring to
rtas_rmo_buf? (I don't think it is well-named.)...
Quoted 7 lines
Quoted 4 lines
RMO (Real mode offset) is the old term we used to use to refer to what is now called the RMA (Real mode area). There are still many references to RMO in Linux, but they almost certainly all refer to what we now call the RMA.
Yes... but I think in this discussion Alexey was using RMO to stand in for
rtas_rmo_buf, which was what I was trying to clarify.
Correct. Thanks for the clarification, appreciated.
Quoted 27 lines
Quoted 27 lines
Quoted 19 lines
Quoted 8 lines
May be store in the FDT as "linux,rmo-base" next to "linux,rtas-base", for clarity, as sharing symbols between prom and main kernel is a bit tricky.
The benefit is that we do not do the same thing (== find 64K in RMA)
in 2 different ways and if the RMO allocated my way is broken - we'll know it much sooner as RTAS itself will break too.Implementation details aside... I'll grant that combining the allocations into one in
prom_initreduces some duplication in the sense that both are subject to the same constraints (mostly - the RTAS data area must not cross a 256MB boundary, while the user region may). But they really are distinct concerns. The RTAS private data area is specified in the platform architecture, the OS is obligated to allocate it and pass it to instantiate-rtas, etc etc. However the user region (rtas_rmo_buf) is purely a Linux construct which is there to supportsys_rtas.Quoted 4 lines
Now, there are multiple sites in the kernel proper that must allocate memory suitable for passing to RTAS. Obviously there is value in consolidating the logic for that purpose in one place, so I'll work on adding that in v2. OK?
I don't think we want to move any allocations into
prom_init.cunless we have to.
Quoted 7 lines
Quoted 5 lines
It's best thought of as a trampoline, that runs before the kernel proper, to transition from live OF to a flat DT environment. One thing that must be done as part of that is instantiating RTAS, because it's basically a runtime copy of the live OF. But any other allocs are for Linux to handle later, IMHO.
Agreed.
Then the only comment I have left is may be use of_address_to_resource() + resource_size() instead of of_n_addr_cells()/of_n_size_cells() (like pseries_memory_block_size()). And now I shut up :) Thanks,
Signature · 1 line
Alexey
Michael Ellerman
reviewerRe: [PATCH 6/6] powerpc/rtas: constrain user region allocation to RMA
In reply to Nathan LynchQuoted 11 lines · Nathan Lynch
Memory locations passed as arguments from the OS to RTAS usually need to be addressable in 32-bit mode and must reside in the Real Mode Area. On PAPR guests, the RMA starts at logical address 0 and is the first logical memory block reported in the LPAR’s device tree.
On powerpc targets with RTAS, Linux makes available to user space a region of memory suitable for arguments to be passed to RTAS via
sys_rtas(). This region (rtas_rmo_buf) is allocated via the memblock API during boot in order to ensure that it satisfies the requirements described above.
Quoted 3 lines
With radix MMU, the upper limit supplied to the memblock allocation can exceed the bounds of the first logical memory block, since
ppc64_rma_sizeisULONG_MAXandRTAS_INSTANTIATE_MAXis 1GB.
Why does the size of the first memory block matter for radix?
The 1GB limit is sufficient to make it accessible by 32-bit code.
Quoted 2 lines
(512MB is a common size of the first memory block according to a small sample of LPARs I have checked.)
That's the minimum we request, see prom_init.c:
/* option vector 2: Open Firmware options supported */
.vec2 = {
.byte1 = OV2_REAL_MODE,
.reserved = 0,
.real_base = cpu_to_be32(0xffffffff),
.real_size = cpu_to_be32(0xffffffff),
.virt_base = cpu_to_be32(0xffffffff),
.virt_size = cpu_to_be32(0xffffffff),
.load_base = cpu_to_be32(0xffffffff),
.min_rma = cpu_to_be32(512), /* 512MB min RMA */Since v4.12 in 2017.
cheers
Re: [PATCH 6/6] powerpc/rtas: constrain user region allocation to RMA
Quoted 12 lines · Michael Ellerman
Quoted 11 lines · Nathan Lynch
Memory locations passed as arguments from the OS to RTAS usually need to be addressable in 32-bit mode and must reside in the Real Mode Area. On PAPR guests, the RMA starts at logical address 0 and is the first logical memory block reported in the LPAR’s device tree.
On powerpc targets with RTAS, Linux makes available to user space a region of memory suitable for arguments to be passed to RTAS via
sys_rtas(). This region (rtas_rmo_buf) is allocated via the memblock API during boot in order to ensure that it satisfies the requirements described above.
Quoted 5 lines
Quoted 3 lines
With radix MMU, the upper limit supplied to the memblock allocation can exceed the bounds of the first logical memory block, since
ppc64_rma_sizeisULONG_MAXandRTAS_INSTANTIATE_MAXis 1GB.Why does the size of the first memory block matter for radix?
Here is my understanding: in the platform architecture, the size of the first memory block equals the RMA, regardless of the MMU mode. It just so happens that when using radix, Linux can pass ibm,configure-connector a work area address outside of the RMA because the allocation constraints for the work area are computed differently. It would be wrong of the OS to pass RTAS arguments outside of this region with hash MMU as well.
Quoted 1 line
The 1GB limit is sufficient to make it accessible by 32-bit code.
But the requirement is that memory arguments passed to RTAS reside in the RMA, which may be less than 1GB.
Quoted 16 lines
Quoted 2 lines
(512MB is a common size of the first memory block according to a small sample of LPARs I have checked.)
That's the minimum we request, see
prom_init.c:/* option vector 2: Open Firmware options supported */ .vec2 = { .byte1 = OV2_REAL_MODE, .reserved = 0, .real_base = cpu_to_be32(0xffffffff), .real_size = cpu_to_be32(0xffffffff), .virt_base = cpu_to_be32(0xffffffff), .virt_size = cpu_to_be32(0xffffffff), .load_base = cpu_to_be32(0xffffffff), .min_rma = cpu_to_be32(512), /* 512MB min RMA */
Quoted 1 line
Since v4.12 in 2017.
Thanks for the pointer, I had been trying to find this without success.
Michael Ellerman
reviewerRe: [PATCH 6/6] powerpc/rtas: constrain user region allocation to RMA
Quoted 19 lines · Nathan Lynch
Quoted 12 lines · Michael Ellerman
Quoted 11 lines · Nathan Lynch
Memory locations passed as arguments from the OS to RTAS usually need to be addressable in 32-bit mode and must reside in the Real Mode Area. On PAPR guests, the RMA starts at logical address 0 and is the first logical memory block reported in the LPAR’s device tree.
On powerpc targets with RTAS, Linux makes available to user space a region of memory suitable for arguments to be passed to RTAS via
sys_rtas(). This region (rtas_rmo_buf) is allocated via the memblock API during boot in order to ensure that it satisfies the requirements described above.Quoted 5 lines
Quoted 3 lines
With radix MMU, the upper limit supplied to the memblock allocation can exceed the bounds of the first logical memory block, since
ppc64_rma_sizeisULONG_MAXandRTAS_INSTANTIATE_MAXis 1GB.Why does the size of the first memory block matter for radix?
Quoted 7 lines
Here is my understanding: in the platform architecture, the size of the first memory block equals the RMA, regardless of the MMU mode. It just so happens that when using radix, Linux can pass ibm,configure-connector a work area address outside of the RMA because the allocation constraints for the work area are computed differently. It would be wrong of the OS to pass RTAS arguments outside of this region with hash MMU as well.
If that's the requirement then shouldn't we be adjusting ppc64_rma_size? Otherwise aren't other uses of ppc64_rma_size going to run into similar problems.
Or does the RMA only apply for RTAS calls when using radix?
cheers
Re: [PATCH 6/6] powerpc/rtas: constrain user region allocation to RMA
Quoted 28 lines · Michael Ellerman
Quoted 19 lines · Nathan Lynch
Quoted 12 lines · Michael Ellerman
Quoted 11 lines · Nathan Lynch
Memory locations passed as arguments from the OS to RTAS usually need to be addressable in 32-bit mode and must reside in the Real Mode Area. On PAPR guests, the RMA starts at logical address 0 and is the first logical memory block reported in the LPAR’s device tree.
On powerpc targets with RTAS, Linux makes available to user space a region of memory suitable for arguments to be passed to RTAS via
sys_rtas(). This region (rtas_rmo_buf) is allocated via the memblock API during boot in order to ensure that it satisfies the requirements described above.Quoted 5 lines
Quoted 3 lines
With radix MMU, the upper limit supplied to the memblock allocation can exceed the bounds of the first logical memory block, since
ppc64_rma_sizeisULONG_MAXandRTAS_INSTANTIATE_MAXis 1GB.Why does the size of the first memory block matter for radix?
Quoted 7 lines
Here is my understanding: in the platform architecture, the size of the first memory block equals the RMA, regardless of the MMU mode. It just so happens that when using radix, Linux can pass ibm,configure-connector a work area address outside of the RMA because the allocation constraints for the work area are computed differently. It would be wrong of the OS to pass RTAS arguments outside of this region with hash MMU as well.
Quoted 3 lines
If that's the requirement then shouldn't we be adjusting
ppc64_rma_size? Otherwise aren't other uses ofppc64_rma_sizegoing to run into similar problems.
Not all allocations limited by ppc64_rma_size set up memory that is passed to RTAS though, do they? e.g. emergency_stack_init and init_fallback_flush? Those shouldn't be confined to the first LMB unnecessarily.
That's why I'm thinking what I've written here should be generalized a bit and placed in an early allocator function that can be used to set up the user region and the per-cpu reentrant RTAS argument buffers (see allocate_paca_ptrs/new_rtas_args). So far those two sites are the only ones I'm convinced need attention.