powerpc/pseries: New character devices for system parameters and VPD
Subsystems (3)
v6 cover powerpc/pseries: New character devices for system parameters and VPD
Add character devices that expose PAPR-specific system parameters and VPD to user space.
The problem: important platform features are enabled on Linux VMs through the powerpc-specific rtas() syscall in combination with writeable mappings of /dev/mem. In typical usage, this is encapsulated behind APIs provided by the librtas library. This paradigm is incompatible with lockdown, which prohibits /dev/mem access. It also is too low-level in many cases: a single logical operation may require multiple sys_rtas() calls in succession to complete. This carries the risk that a process may exit while leaving an operation unfinished. It also means that callers must coordinate their use of the syscall for functions that cannot tolerate multiple concurrent clients, such as ibm,get-vpd.
These are the general problems, but it's difficult to formulate a similarly general solution in the form of a single replacement ABI for sys_rtas(). Instead, each platform facility we expose to user space needs a specific interface that forms the kernel-user interactions at a higher level than individual RTAS calls.
I've identified system parameter support as a high priority for this effort, since the software that communicates with the management console relies on it and therefore does not work at all with lockdown enabled on current kernels. VPD retrieval is also important (for licensing/entitlement things I think?), and serves as a good initial example of encapsulating sequence-based RTAS calls. So this series proposes a model for incrementally solving these issues by introducing a small pseries-specific "driver" for each of these platform functions. The new drivers expose these facilities to user space in ways that are compatible with lockdown, and they require no coordination between their clients.
In preparation, per-function mutexes are added to the core RTAS code to serialize access to sequence-based RTAS functions. These prevent kernel-based sequences from interfering with each other, and they prevent sys_rtas() users from disrupting kernel-based users. The RTAS core enforces correct lock usage when lockdep is enabled.
Both drivers could potentially support poll() methods to notify clients of changes to parameters or VPD that happen due to partition migration and other events. But that should be safe to leave for later, assuming there's any interest.
I have made changes to librtas to prefer the new interfaces and verified that existing clients work correctly with the new code. A draft PR for that work is here:
https://github.com/ibm-power-utilities/librtas/pull/36
The user-space ABI has not changed since v1 of this series.
I expect to propose at least one more small driver in this style for platform dump retrieval in a separate submission in the future. Other facilities may follow as needs are identified.
---
Changes in v6:
- Require FMODE_WRITE for setting system parameters in papr-sysparm
and add a corresponding testcase.
- Update existing sysparm testcases to open the device writable when
attempting to set parameters.
- Link to v5: https://lore.kernel.org/r/20231207-papr-sys_rtas-vs-lockdown-v5-0-2ce965636a58@linux.ibm.com
Changes in v5:
- Add to the front of the queue another fix for a latent bug where
sys_rtas() users can trigger a spurious warning backtrace.
- Merge "powerpc/uapi: Export papr-miscdev.h header" into "Add
papr-vpd character driver for VPD retrieval" so we don't temporarily
expose the PAPR_MISCDEV_IOC_ID constant via UAPI without also
updating the ioctl-number documentation. (Michael Ellerman)
- Drop rtas_function_{un}lock() in favor of more explicit use of the new
per-function mutexes; make rtas_ibm_get_vpd_lock extern for use by
papr-vpd.
- Assert that rtas_ibm_get_vpd_lock is held in rtas_ibm_get_vpd().
- Add example usage to "powerpc/rtas: Facilitate high-level call
sequences". (Aneesh Kumar K.V)
- Drop the now-unnecessary 04/13 "powerpc/rtas: Factor out function
descriptor lookup".
- Include document version when citing the PAPR+ specification
throughout. (Michael Ellerman)
- Add missing include directives to papr-vpd and papr-sysparm. (Michal
Suchánek)
- Fix spurious testcase failure in environments without a working ibm,set-system-parameter RTAS function. (Michael Ellerman)
- Link to v4: https://lore.kernel.org/r/20231117-papr-sys_rtas-vs-lockdown-v4-0-b794d8cb8502@linux.ibm.com
Changes in v4:
- Fix latent issue in rtas_token_to_function() which causes boot-time
crashes.
- More small preparatory changes: a function table iterator and
additional symbolic constants for RTAS function return values.
- Use symbolic constants for ibm,get-vpd statuses in papr-vpd.c.
- Add commentary to papr_vpd_ioc_create_handle() explaining choice to
retrieve all VPD at file handle creation time instead of deferring
it to the read handler.
- Rebase on current powerpc/next.
- Link to v3: https://lore.kernel.org/r/20231025-papr-sys_rtas-vs-lockdown-v3-0-5eb04559e7d8@linux.ibm.com
Changes in v3:
- Add new rtas_function_lock()/unlock() APIs and convert existing code
to use them.
- Convert papr-vpd to use rtas_function_lock()/unlock() instead of
having sys_rtas() obtain a driver-private mutex.
- Rebase on current powerpc/next.
- Link to v2: https://lore.kernel.org/r/20231013-papr-sys_rtas-vs-lockdown-v2-0-ead01ce01722@linux.ibm.com
Changes in v2:
- Fix unused-but-set variable warning in papr-sysparm code.
- Rebase on powerpc/next branch.
- Link to v1: https://lore.kernel.org/r/20231006-papr-sys_rtas-vs-lockdown-v1-0-3a36bfb66e2e@linux.ibm.com
Changes in v1 vs initial RFC:
- Add papr-sysparm driver and tests.
- Add a papr-miscdev.h uapi header.
- Prevent sys_rtas() from interfering with papr-vpd call sequences.
- Handle -4 ("VPD changed") status in papr-vpd.
- Include string_helpers.h in papr-vpd.c, per Michal Suchánek
- Link to RFC: https://lore.kernel.org/r/20230822-papr-sys_rtas-vs-lockdown-v1-0-932623cf3c7b@linux.ibm.com
---
Nathan Lynch (13):
powerpc/rtas: Avoid warning on invalid token argument to sys_rtas() powerpc/rtas: Add for_each_rtas_function() iterator powerpc/rtas: Fall back to linear search on failed token->function lookup powerpc/rtas: Add function return status constants powerpc/rtas: Move token validation from block_rtas_call() to sys_rtas() powerpc/rtas: Facilitate high-level call sequences powerpc/rtas: Serialize firmware activation sequences powerpc/rtas: Warn if per-function lock isn't held powerpc/pseries: Add papr-vpd character driver for VPD retrieval powerpc/pseries/papr-sysparm: Validate buffer object lengths powerpc/pseries/papr-sysparm: Expose character device to user space powerpc/selftests: Add test for papr-vpd
powerpc/selftests: Add test for papr-sysparm
Documentation/userspace-api/ioctl/ioctl-number.rst | 4 + arch/powerpc/include/asm/papr-sysparm.h | 17 +- arch/powerpc/include/asm/rtas.h | 28 +- arch/powerpc/include/uapi/asm/papr-miscdev.h | 9 + arch/powerpc/include/uapi/asm/papr-sysparm.h | 58 +++ arch/powerpc/include/uapi/asm/papr-vpd.h | 22 + arch/powerpc/kernel/rtas.c | 184 +++++-- arch/powerpc/platforms/pseries/Makefile | 1 + arch/powerpc/platforms/pseries/papr-sysparm.c | 205 +++++++- arch/powerpc/platforms/pseries/papr-vpd.c | 541 +++++++++++++++++++++ tools/testing/selftests/powerpc/Makefile | 2 + .../selftests/powerpc/papr_sysparm/.gitignore | 1 + .../selftests/powerpc/papr_sysparm/Makefile | 12 + .../selftests/powerpc/papr_sysparm/papr_sysparm.c | 196 ++++++++ .../testing/selftests/powerpc/papr_vpd/.gitignore | 1 + tools/testing/selftests/powerpc/papr_vpd/Makefile | 12 + .../testing/selftests/powerpc/papr_vpd/papr_vpd.c | 352 ++++++++++++++ 17 files changed, 1596 insertions(+), 49 deletions(-)
---
base-commit: 27951e1d8274e9f9a2925b069e4492939a3f2099
change-id: 20230817-papr-sys_rtas-vs-lockdown-5c54505db792
Best regards,
Signature · 1 line
Nathan Lynch <[email protected]>
PATCH v6 1/13 powerpc/rtas: Avoid warning on invalid token argument to sys_rtas()
rtas_token_to_function() WARNs when passed an invalid token; it's meant to catch bugs in kernel-based users of RTAS functions. However, user space controls the token value passed to rtas_token_to_function() by block_rtas_call(), so user space with sufficient privilege to use sys_rtas() can trigger the warnings at will:
unexpected failed lookup for token 2048
WARNING: CPU: 20 PID: 2247 at arch/powerpc/kernel/rtas.c:556
rtas_token_to_function+0xfc/0x110
...
NIP rtas_token_to_function+0xfc/0x110
LR rtas_token_to_function+0xf8/0x110
Call Trace:
rtas_token_to_function+0xf8/0x110 (unreliable)
sys_rtas+0x188/0x880
system_call_exception+0x268/0x530
system_call_common+0x160/0x2c4It's desirable to continue warning on bogus tokens in rtas_token_to_function(). Currently it is used to look up RTAS function descriptors when tracing, where we know there has to have been a successful descriptor lookup by different means already, and it would be a serious inconsistency for the reverse lookup to fail.
So instead of weakening rtas_token_to_function()'s contract by removing the warnings, introduce rtas_token_to_function_untrusted(), which has no opinion on failed lookups. Convert block_rtas_call() and rtas_token_to_function() to use it.
--- arch/powerpc/kernel/rtas.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-)
arch/powerpc/kernel/rtas.c +17 -2
@@ -544,6 +544,21 @@ static int __init rtas_token_to_function_xarray_init(void) }arch_initcall (rtas_token_to_function_xarray_init); +/* +* For use by sys_rtas(), where the token value is provided by user +* space and we don't want to warn on failed lookups. +*/ +static const struct rtas_function *rtas_token_to_function_untrusted (s32 token) +{ +return xa_load (&rtas_token_to_function_xarray, token); +} + +/* +* Reverse lookup for deriving the function descriptor from a +* known-good token value in contexts where the former is not already +* available. @token must be valid, e.g. derived from the result of a +* prior lookup against the function table. +*/ static const struct rtas_function *rtas_token_to_function (s32 token) {const struct rtas_function *func;
@@ -551,7 +566,7 @@ static const struct rtas_function *rtas_token_to_function(s32 token)if (WARN_ONCE (token <0 ,"invalid token %d" , token))return NULL ; - func =xa_load (&rtas_token_to_function_xarray, token); + func =rtas_token_to_function_untrusted (token);if (WARN_ONCE (!func,"unexpected failed lookup for token %d" , token))return NULL ;
@@ -1721,7 +1736,7 @@ static bool block_rtas_call(int token, int nargs,* If this token doesn't correspond to a function the kernel * understands, you're not allowed to call it. */ - func =rtas_token_to_function (token); + func =rtas_token_to_function_untrusted (token);if (!func) goto err;/*
Signature · 1 line
2.41.0
PATCH v6 2/13 powerpc/rtas: Add for_each_rtas_function() iterator
In reply to Nathan LynchAdd a convenience macro for iterating over every element of the internal function table and convert the one site that can use it. An additional user of the macro is anticipated in changes to follow.
--- arch/powerpc/kernel/rtas.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
arch/powerpc/kernel/rtas.c +7 -2
@@ -454,6 +454,11 @@ static struct rtas_function rtas_function_table[] __ro_after_init = { }, }; +#define for_each_rtas_function (funcp) \ + for (funcp = &rtas_function_table[0]; \ + funcp < &rtas_function_table[ARRAY_SIZE(rtas_function_table)]; \ + ++funcp) +/* * Nearly all RTAS calls need to be serialized. All uses of the * default rtas_args block must hold rtas_lock.
@@ -525,10 +530,10 @@ static DEFINE_XARRAY(rtas_token_to_function_xarray);static int __init rtas_token_to_function_xarray_init (void ) { +const struct rtas_function *func;int err =0 ; -for (size_t i =0 ; i <ARRAY_SIZE (rtas_function_table); ++i) { -const struct rtas_function *func = &rtas_function_table[i]; +for_each_rtas_function (func) {const s32 token = func->token;if (token ==RTAS_UNKNOWN_SERVICE )
Signature · 1 line
2.41.0
PATCH v6 3/13 powerpc/rtas: Fall back to linear search on failed token->function lookup
In reply to Nathan LynchEnabling any of the powerpc:rtas_* tracepoints at boot is likely to result in an oops on RTAS platforms. For example, booting a QEMU pseries model with 'trace_event=powerpc:rtas_input' in the command line leads to:
BUG: Kernel NULL pointer dereference on read at 0x00000008
Oops: Kernel access of bad area, sig: 7 [#1]
NIP [c00000000004231c] do_enter_rtas+0x1bc/0x460
LR [c00000000004231c] do_enter_rtas+0x1bc/0x460
Call Trace:
do_enter_rtas+0x1bc/0x460 (unreliable)
rtas_call+0x22c/0x4a0
rtas_get_boot_time+0x80/0x14c
read_persistent_clock64+0x124/0x150
read_persistent_wall_and_boot_offset+0x28/0x58
timekeeping_init+0x70/0x348
start_kernel+0xa0c/0xc1c
start_here_common+0x1c/0x20(This is preceded by a warning for the failed lookup in rtas_token_to_function().)
This happens when __do_enter_rtas_trace() attempts a token to function descriptor lookup before the xarray containing the mappings has been set up.
Fall back to linear scan of the table if rtas_token_to_function_xarray is empty.
--- arch/powerpc/kernel/rtas.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)
arch/powerpc/kernel/rtas.c +14 -4
@@ -572,11 +572,21 @@ static const struct rtas_function *rtas_token_to_function(s32 token)return NULL ; func =rtas_token_to_function_untrusted (token); +if (func) +return func; +/* +* Fall back to linear scan in case the reverse mapping hasn't +* been initialized yet. +*/ +if (xa_empty (&rtas_token_to_function_xarray)) { +for_each_rtas_function (func) { +if (func->token == token) +return func; + } + } -if (WARN_ONCE (!func,"unexpected failed lookup for token %d" , token)) -return NULL ; - -return func; +WARN_ONCE (true,"unexpected failed lookup for token %d" , token); +return NULL ; }/* This is here deliberately so it's only used in this file */
Signature · 1 line
2.41.0
PATCH v6 4/13 powerpc/rtas: Add function return status constants
In reply to Nathan LynchNot all of the generic RTAS function statuses specified in PAPR have symbolic constants and descriptions in rtas.h. Fix this, providing a little more background, slightly updating the existing wording, and improving the formatting.
--- arch/powerpc/include/asm/rtas.h | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-)
arch/powerpc/include/asm/rtas.h +19 -6
@@ -201,12 +201,25 @@ typedef struct {/* Memory set aside for sys_rtas to use with calls that need a work area. */ #define RTAS_USER_REGION_SIZE (64 * 1024) -/* RTAS return status codes */ -#define RTAS_HARDWARE_ERROR -1/* Hardware Error */ -#define RTAS_BUSY -2/* RTAS Busy */ -#define RTAS_INVALID_PARAMETER -3/* Invalid indicator/domain/sensor etc. */ -#define RTAS_EXTENDED_DELAY_MIN 9900 -#define RTAS_EXTENDED_DELAY_MAX 9905 +/* +* Common RTAS function return values, derived from the table "RTAS +* Status Word Values" in PAPR+ v2.13 7.2.8: "Return Codes". If a +* function can return a value in this table then generally it has the +* meaning listed here. More extended commentary in the documentation +* for rtas_call(). +* +* RTAS functions may use negative and positive numbers not in this +* set for function-specific error and success conditions, +* respectively. +*/ +#define RTAS_SUCCESS 0/* Success. */ +#define RTAS_HARDWARE_ERROR -1/* Hardware or other unspecified error. */ +#define RTAS_BUSY -2/* Retry immediately. */ +#define RTAS_INVALID_PARAMETER -3/* Invalid indicator/domain/sensor etc. */ +#define RTAS_UNEXPECTED_STATE_CHANGE -7/* Seems limited to EEH and slot reset. */ +#define RTAS_EXTENDED_DELAY_MIN 9900/* Retry after delaying for ~1ms. */ +#define RTAS_EXTENDED_DELAY_MAX 9905/* Retry after delaying for ~100s. */ +#define RTAS_ML_ISOLATION_ERROR -9000/* Multi-level isolation error. */ /* statuses specific to ibm,suspend-me */ #define RTAS_SUSPEND_ABORTED 9000/* Suspension aborted */
Signature · 1 line
2.41.0
PATCH v6 5/13 powerpc/rtas: Move token validation from block_rtas_call() to sys_rtas()
In reply to Nathan LynchThe rtas system call handler sys_rtas() delegates certain input validation steps to a helper function: block_rtas_call(). One of these steps ensures that the user-supplied token value maps to a known RTAS function. This is done by performing a "reverse" token-to-function lookup via rtas_token_to_function_untrusted() to obtain an rtas_function object.
In changes to come, sys_rtas() itself will need the function descriptor for the token. To prepare:
* Move the lookup and validation up into sys_rtas() and pass the resulting rtas_function pointer to block_rtas_call(), which is otherwise unconcerned with the token value.
* Change block_rtas_call() to report the RTAS function name instead of the token value on validation failures, since it can now rely on having a valid function descriptor.
One behavior change is that sys_rtas() now silently errors out when passed a bad token, before calling block_rtas_call(). So we will no longer log "RTAS call blocked - exploit attempt?" on invalid tokens. This is consistent with how sys_rtas() currently handles other "metadata" (nargs and nret), while block_rtas_call() is primarily concerned with validating the arguments to be passed to specific RTAS functions.
--- arch/powerpc/kernel/rtas.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-)
arch/powerpc/kernel/rtas.c +16 -16
@@ -1738,24 +1738,18 @@ static bool in_rmo_buf(u32 base, u32 end) end < (rtas_rmo_buf +RTAS_USER_REGION_SIZE ); } -static bool block_rtas_call (int token,int nargs, +static bool block_rtas_call (const struct rtas_function *func,int nargs,struct rtas_args *args) { -const struct rtas_function *func;const struct rtas_filter *f; -const bool is_platform_dump = token ==rtas_function_token (RTAS_FN_IBM_PLATFORM_DUMP ); -const bool is_config_conn = token ==rtas_function_token (RTAS_FN_IBM_CONFIGURE_CONNECTOR ); +const bool is_platform_dump = + func == &rtas_function_table[RTAS_FNIDX__IBM_PLATFORM_DUMP ]; +const bool is_config_conn = + func == &rtas_function_table[RTAS_FNIDX__IBM_CONFIGURE_CONNECTOR ];u32 base, size, end;/* -* If this token doesn't correspond to a function the kernel -* understands, you're not allowed to call it. -*/ - func =rtas_token_to_function_untrusted (token); -if (!func) - goto err; -/* -* And only functions with filters attached are allowed. +* Only functions with filters attached are allowed. */ f = func->filter;if (!f)
@@ -1812,14 +1806,15 @@ static bool block_rtas_call(int token, int nargs,return false; err:pr_err_ratelimited ("sys_rtas: RTAS call blocked - exploit attempt?\n" ); -pr_err_ratelimited ("sys_rtas: token=0x%x, nargs=%d (called by %s)\n" , - token, nargs, current->comm); +pr_err_ratelimited ("sys_rtas: %s nargs=%d (called by %s)\n" , + func->name, nargs, current->comm);return true; }/* We assume to be passed big endian arguments */ SYSCALL_DEFINE1(rtas, struct rtas_args __user *, uargs) { +const struct rtas_function *func;struct pin_cookie cookie;struct rtas_args args;unsigned long flags;
@@ -1849,13 +1844,18 @@ SYSCALL_DEFINE1(rtas, struct rtas_args __user *, uargs) nargs * sizeof(rtas_arg_t)) != 0)return -EFAULT ; -if (token ==RTAS_UNKNOWN_SERVICE ) +/* +* If this token doesn't correspond to a function the kernel +* understands, you're not allowed to call it. +*/ + func =rtas_token_to_function_untrusted (token); +if (!func)return -EINVAL ; args.rets = &args.args[nargs];memset (args.rets,0 , nret *sizeof (rtas_arg_t)); -if (block_rtas_call (token, nargs, &args)) +if (block_rtas_call (func, nargs, &args))return -EINVAL ;if (token_is_restricted_errinjct (token)) {
Signature · 1 line
2.41.0
PATCH v6 6/13 powerpc/rtas: Facilitate high-level call sequences
In reply to Nathan LynchOn RTAS platforms there is a general restriction that the OS must not enter RTAS on more than one CPU at a time. This low-level serialization requirement is satisfied by holding a spin lock (rtas_lock) across most RTAS function invocations.
However, some pseries RTAS functions require multiple successive calls to complete a logical operation. Beginning a new call sequence for such a function may disrupt any other sequences of that function already in progress. Safe and reliable use of these functions effectively requires higher-level serialization beyond what is already done at the level of RTAS entry and exit.
Where a sequence-based RTAS function is invoked only through sys_rtas(), with no in-kernel users, there is no issue as far as the kernel is concerned. User space is responsible for appropriately serializing its call sequences. (Whether user space code actually takes measures to prevent sequence interleaving is another matter.) Examples of such functions currently include ibm,platform-dump and ibm,get-vpd.
But where a sequence-based RTAS function has both user space and in-kernel uesrs, there is a hazard. Even if the in-kernel call sites of such a function serialize their sequences correctly, a user of sys_rtas() can invoke the same function at any time, potentially disrupting a sequence in progress.
So in order to prevent disruption of kernel-based RTAS call sequences, they must serialize not only with themselves but also with sys_rtas() users, somehow. Preferably without adding more function-specific hacks to sys_rtas(). This is a prerequisite for adding an in-kernel call sequence of ibm,get-vpd, which is in a change to follow.
Note that it has never been feasible for the kernel to prevent sys_rtas()-based sequences from being disrupted because control returns to user space on every call. sys_rtas()-based users of these functions have always been, and continue to be, responsible for coordinating their call sequences with other users, even those which may invoke the RTAS functions through less direct means than sys_rtas(). This is an unavoidable consequence of exposing sequence-based RTAS functions through sys_rtas().
* Add an optional mutex member to struct rtas_function.
* Statically define a mutex for each RTAS function with known call sequence serialization requirements, and assign its address to the .lock member of the corresponding function table entry, along with justifying commentary.
* In sys_rtas(), if the table entry for the RTAS function being called has a populated lock member, acquire it before taking rtas_lock and entering RTAS.
* Kernel-based RTAS call sequences are expected to access the appropriate mutex explicitly by name. For example, a user of the ibm,activate-firmware RTAS function would do:
int token = rtas_function_token(RTAS_FN_IBM_ACTIVATE_FIRMWARE);
int fwrc;
mutex_lock(&rtas_ibm_activate_firmware_lock);
do {
fwrc = rtas_call(token, 0, 1, NULL);
} while (rtas_busy_delay(fwrc));
mutex_unlock(&rtas_ibm_activate_firmware_lock);There should be no perceivable change introduced here except that concurrent callers of the same RTAS function via sys_rtas() may block on a mutex instead of spinning on rtas_lock.
--- arch/powerpc/include/asm/rtas.h | 3 ++ arch/powerpc/kernel/rtas.c | 83 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+)
arch/powerpc/include/asm/rtas.h +3 -0
@@ -3,6 +3,7 @@#define _POWERPC_RTAS_H#ifdef __KERNEL__ +#include <linux/mutex.h> #include <linux/spinlock.h> #include <asm/page.h> #include <asm/rtas-types.h>
@@ -512,6 +513,8 @@ extern char rtas_data_buf[RTAS_DATA_BUF_SIZE];/* RMO buffer reserved for user-space RTAS use */ extern unsigned long rtas_rmo_buf; +extern struct mutex rtas_ibm_get_vpd_lock; +#define GLOBAL_INTERRUPT_QUEUE 9005/**
arch/powerpc/kernel/rtas.c +83 -0
163 lines not on this page · raw message
Signature · 1 line
2.41.0
PATCH v6 7/13 powerpc/rtas: Serialize firmware activation sequences
In reply to Nathan LynchUse rtas_ibm_activate_firmware_lock to prevent interleaving call sequences of the ibm,activate-firmware RTAS function, which typically requires multiple calls to complete the update. While the spec does not specifically prohibit interleaved sequences, there's almost certainly no advantage to allowing them.
--- arch/powerpc/kernel/rtas.c | 4 ++++ 1 file changed, 4 insertions(+)
arch/powerpc/kernel/rtas.c +4 -0
@@ -1734,10 +1734,14 @@ void rtas_activate_firmware(void)return ; } +mutex_lock (&rtas_ibm_activate_firmware_lock); +do { fwrc =rtas_call (token,0 ,1 ,NULL ); }while (rtas_busy_delay (fwrc)); +mutex_unlock (&rtas_ibm_activate_firmware_lock); +if (fwrc)pr_err ("ibm,activate-firmware failed (%i)\n" , fwrc); }
Signature · 1 line
2.41.0
PATCH v6 8/13 powerpc/rtas: Warn if per-function lock isn't held
In reply to Nathan LynchIf the function descriptor has a populated lock member, then callers are required to hold it across calls. Now that the firmware activation sequence is appropriately guarded, we can warn when the requirement isn't satisfied.
__do_enter_rtas_trace() gets reorganized a bit as a result of performing the function descriptor lookup unconditionally now.
--- arch/powerpc/kernel/rtas.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-)
arch/powerpc/kernel/rtas.c +9 -12
@@ -671,28 +671,25 @@ static void __do_enter_rtas(struct rtas_args *args)static void __do_enter_rtas_trace (struct rtas_args *args) { -const char *name =NULL ; +const struct rtas_function *func =rtas_token_to_function (be32_to_cpu (args->token)); -if (args == &rtas_args) -lockdep_assert_held (&rtas_lock);/* -* If the tracepoints that consume the function name aren't -* active, avoid the lookup. +* If there is a per-function lock, it must be held by the +* caller. */ -if ((trace_rtas_input_enabled () ||trace_rtas_output_enabled ())) { -const s32 token =be32_to_cpu (args->token); -const struct rtas_function *func =rtas_token_to_function (token); +if (func->lock) +lockdep_assert_held (func->lock); - name = func->name; - } +if (args == &rtas_args) +lockdep_assert_held (&rtas_lock); -trace_rtas_input (args, name); +trace_rtas_input (args, func->name);trace_rtas_ll_entry (args);__do_enter_rtas (args);trace_rtas_ll_exit (args); -trace_rtas_output (args, name); +trace_rtas_output (args, func->name); }static void do_enter_rtas (struct rtas_args *args)
Signature · 1 line
2.41.0
PATCH v6 9/13 powerpc/pseries: Add papr-vpd character driver for VPD retrieval
In reply to Nathan LynchPowerVM LPARs may retrieve Vital Product Data (VPD) for system components using the ibm,get-vpd RTAS function.
We can expose this to user space with a /dev/papr-vpd character device, where the programming model is:
struct papr_location_code plc = { .str = "", }; /* obtain all VPD */
int devfd = open("/dev/papr-vpd", O_RDONLY);
int vpdfd = ioctl(devfd, PAPR_VPD_CREATE_HANDLE, &plc);
size_t size = lseek(vpdfd, 0, SEEK_END);
char *buf = malloc(size);
pread(devfd, buf, size, 0);When a file descriptor is obtained from ioctl(PAPR_VPD_CREATE_HANDLE), the file contains the result of a complete ibm,get-vpd sequence. The file contents are immutable from the POV of user space. To get a new view of the VPD, the client must create a new handle.
This design choice insulates user space from most of the complexities that ibm,get-vpd brings:
* ibm,get-vpd must be called more than once to obtain complete results.
* Only one ibm,get-vpd call sequence should be in progress at a time; interleaved sequences will disrupt each other. Callers must have a protocol for serializing their use of the function.
* A call sequence in progress may receive a "VPD changed, try again" status, requiring the client to abandon the sequence and start over.
The memory required for the VPD buffers seems acceptable, around 20KB for all VPD on one of my systems. And the value of the /rtas/ibm,vpd-size DT property (the estimated maximum size of VPD) is consistently 300KB across various systems I've checked.
I've implemented support for this new ABI in the rtas_get_vpd() function in librtas, which the vpdupdate command currently uses to populate its VPD database. I've verified that an unmodified vpdupdate binary generates an identical database when using a librtas.so that prefers the new ABI.
Along with the papr-vpd.h header exposed to user space, this introduces a common papr-miscdev.h uapi header to share a base ioctl ID with similar drivers to come.
--- Documentation/userspace-api/ioctl/ioctl-number.rst | 2 + arch/powerpc/include/uapi/asm/papr-miscdev.h | 9 + arch/powerpc/include/uapi/asm/papr-vpd.h | 22 + arch/powerpc/platforms/pseries/Makefile | 1 + arch/powerpc/platforms/pseries/papr-vpd.c | 541 +++++++++++++++++++++ 5 files changed, 575 insertions(+)
Documentation/userspace-api/ioctl/ioctl-number.rst +2 -0
@@ -349,6 +349,8 @@ Code Seq# Include File Comments <mailto:[email protected]> 0xB1 00-1F PPPoX <mailto:[email protected]> +0xB2 00 arch/powerpc/include/uapi/asm/papr-vpd.h powerpc/pseries VPD API + <mailto:linuxppc-dev> 0xB3 00 linux/mmc/ioctl.h 0xB4 00-0F linux/gpio.h <mailto:[email protected]> 0xB5 00-0F uapi/linux/rpmsg.h <mailto:[email protected]>
arch/powerpc/include/uapi/asm/papr-miscdev.h new file +9 -0
@@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +#ifndef _UAPI_PAPR_MISCDEV_H_ +#define _UAPI_PAPR_MISCDEV_H_ + +enum { +PAPR_MISCDEV_IOC_ID =0xb2 , +}; + +#endif /* _UAPI_PAPR_MISCDEV_H_ */
arch/powerpc/include/uapi/asm/papr-vpd.h new file +22 -0
@@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +#ifndef _UAPI_PAPR_VPD_H_ +#define _UAPI_PAPR_VPD_H_ + +#include <asm/ioctl.h> +#include <asm/papr-miscdev.h> + +struct papr_location_code { +/* +* PAPR+ v2.13 12.3.2.4 Converged Location Code Rules - Length +* Restrictions. 79 characters plus nul. +*/ +char str[80 ]; +}; + +/* +* ioctl for /dev/papr-vpd. Returns a VPD handle fd corresponding to +* the location code. +*/ +#define PAPR_VPD_IOC_CREATE_HANDLE _IOW(PAPR_MISCDEV_IOC_ID, 0, struct papr_location_code) + +#endif /* _UAPI_PAPR_VPD_H_ */
arch/powerpc/platforms/pseries/Makefile +1 -0
@@ -4,6 +4,7 @@ ccflags-$(CONFIG_PPC_PSERIES_DEBUG) += -DDEBUG obj-y := lpar.o hvCall.o nvram.o reconfig.o \ of_helpers.o rtas-work-area.o papr-sysparm.o \ + papr-vpd.o \ setup.o iommu.o event_sources.o ras.o \ firmware.o power.o dlpar.o mobility.o rng.o \ pci.o pci_dlpar.o eeh_pseries.o msi.o \
arch/powerpc/platforms/pseries/papr-vpd.c new file +541 -0
541 lines not on this page · raw message
Signature · 1 line
2.41.0
PATCH v6 10/13 powerpc/pseries/papr-sysparm: Validate buffer object lengths
In reply to Nathan LynchThe ability to get and set system parameters will be exposed to user space, so let's get a little more strict about malformed papr_sysparm_buf objects.
* Create accessors for the length field of struct papr_sysparm_buf. The length is always stored in MSB order and this is better than spreading the necessary conversions all over.
* Reject attempts to submit invalid buffers to RTAS.
* Warn if RTAS returns a buffer with an invalid length, clamping the returned length to a safe value that won't overrun the buffer.
These are meant as precautionary measures to mitigate both firmware and kernel bugs in this area, should they arise, but I am not aware of any.
--- arch/powerpc/platforms/pseries/papr-sysparm.c | 47 +++++++++++++++++++++++++++ 1 file changed, 47 insertions(+)
arch/powerpc/platforms/pseries/papr-sysparm.c +47 -0
@@ -23,6 +23,46 @@ void papr_sysparm_buf_free(struct papr_sysparm_buf *buf)kfree (buf); } +static size_t papr_sysparm_buf_get_length (const struct papr_sysparm_buf *buf) +{ +return be16_to_cpu (buf->len); +} + +static void papr_sysparm_buf_set_length (struct papr_sysparm_buf *buf,size_t length) +{ +WARN_ONCE (length >sizeof (buf->val), +"bogus length %zu, clamping to safe value" , length); + length =min (sizeof (buf->val), length); + buf->len =cpu_to_be16 (length); +} + +/* +* For use on buffers returned from ibm,get-system-parameter before +* returning them to callers. Ensures the encoded length of valid data +* cannot overrun buf->val[]. +*/ +static void papr_sysparm_buf_clamp_length (struct papr_sysparm_buf *buf) +{ +papr_sysparm_buf_set_length (buf,papr_sysparm_buf_get_length (buf)); +} + +/* +* Perform some basic diligence on the system parameter buffer before +* submitting it to RTAS. +*/ +static bool papr_sysparm_buf_can_submit (const struct papr_sysparm_buf *buf) +{ +/* +* Firmware ought to reject buffer lengths that exceed the +* maximum specified in PAPR, but there's no reason for the +* kernel to allow them either. +*/ +if (papr_sysparm_buf_get_length (buf) >sizeof (buf->val)) +return false; + +return true; +} +/** * papr_sysparm_get() - Retrieve the value of a PAPR system parameter. * @param: PAPR system parameter token as described in
@@ -63,6 +103,9 @@ int papr_sysparm_get(papr_sysparm_t param, struct papr_sysparm_buf *buf)if (token ==RTAS_UNKNOWN_SERVICE )return -ENOENT ; +if (!papr_sysparm_buf_can_submit (buf)) +return -EINVAL ; + work_area =rtas_work_area_alloc (sizeof (*buf));memcpy (rtas_work_area_raw_buf (work_area), buf,sizeof (*buf));
@@ -77,6 +120,7 @@ int papr_sysparm_get(papr_sysparm_t param, struct papr_sysparm_buf *buf)case 0 : ret =0 ;memcpy (buf,rtas_work_area_raw_buf (work_area),sizeof (*buf)); +papr_sysparm_buf_clamp_length (buf);break ;case -3 :/* parameter not implemented */ ret = -EOPNOTSUPP ;
@@ -115,6 +159,9 @@ int papr_sysparm_set(papr_sysparm_t param, const struct papr_sysparm_buf *buf)if (token ==RTAS_UNKNOWN_SERVICE )return -ENOENT ; +if (!papr_sysparm_buf_can_submit (buf)) +return -EINVAL ; + work_area =rtas_work_area_alloc (sizeof (*buf));memcpy (rtas_work_area_raw_buf (work_area), buf,sizeof (*buf));
Signature · 1 line
2.41.0
PATCH v6 11/13 powerpc/pseries/papr-sysparm: Expose character device to user space
In reply to Nathan LynchUntil now the papr_sysparm APIs have been kernel-internal. But user space needs access to PAPR system parameters too. The only method available to user space today to get or set system parameters is using sys_rtas() and /dev/mem to pass RTAS-addressable buffers between user space and firmware. This is incompatible with lockdown and should be deprecated.
So provide an alternative ABI to user space in the form of a /dev/papr-sysparm character device with just two ioctl commands (get and set). The data payloads involved are small enough to fit in the ioctl argument buffer, making the code relatively simple.
Exposing the system parameters through sysfs has been considered but it would be too awkward:
* The kernel currently does not have to contain an exhaustive list of defined system parameters. This is a convenient property to maintain because we don't have to update the kernel whenever a new parameter is added to PAPR. Exporting a named attribute in sysfs for each parameter would negate this.
* Some system parameters are text-based and some are not.
* Retrieval of at least one system parameter requires input data, which a simple read-oriented interface can't support.
--- Documentation/userspace-api/ioctl/ioctl-number.rst | 2 + arch/powerpc/include/asm/papr-sysparm.h | 17 ++- arch/powerpc/include/uapi/asm/papr-sysparm.h | 58 ++++++++ arch/powerpc/platforms/pseries/papr-sysparm.c | 158 ++++++++++++++++++++- 4 files changed, 227 insertions(+), 8 deletions(-)
Documentation/userspace-api/ioctl/ioctl-number.rst +2 -0
@@ -351,6 +351,8 @@ Code Seq# Include File Comments <mailto:[email protected]> 0xB2 00 arch/powerpc/include/uapi/asm/papr-vpd.h powerpc/pseries VPD API <mailto:linuxppc-dev> +0xB2 01-02 arch/powerpc/include/uapi/asm/papr-sysparm.h powerpc/pseries system parameter API + <mailto:linuxppc-dev> 0xB3 00 linux/mmc/ioctl.h 0xB4 00-0F linux/gpio.h <mailto:[email protected]> 0xB5 00-0F uapi/linux/rpmsg.h <mailto:[email protected]>
arch/powerpc/include/asm/papr-sysparm.h +11 -6
@@ -2,8 +2,10 @@#ifndef _ASM_POWERPC_PAPR_SYSPARM_H#define _ASM_POWERPC_PAPR_SYSPARM_H +#include <uapi/asm/papr-sysparm.h> +typedef struct { -const u32 token; +u32 token; }papr_sysparm_t ;#define mk_papr_sysparm (x_) ((papr_sysparm_t){ .token = x_, })
@@ -20,11 +22,14 @@ typedef struct {#define PAPR_SYSPARM_TLB_BLOCK_INVALIDATE_ATTRS mk_papr_sysparm(50)#define PAPR_SYSPARM_LPAR_NAME mk_papr_sysparm(55) -enum { -PAPR_SYSPARM_MAX_INPUT =1024 , -PAPR_SYSPARM_MAX_OUTPUT =4000 , -}; - +/** +* struct papr_sysparm_buf - RTAS work area layout for system parameter functions. +* +* This is the memory layout of the buffers passed to/from +* ibm,get-system-parameter and ibm,set-system-parameter. It is +* distinct from the papr_sysparm_io_block structure that is passed +* between user space and the kernel. +*/ struct papr_sysparm_buf {__be16 len;char val[PAPR_SYSPARM_MAX_OUTPUT ];
arch/powerpc/include/uapi/asm/papr-sysparm.h new file +58 -0
58 lines not on this page · raw message
arch/powerpc/platforms/pseries/papr-sysparm.c +156 -2
180 lines not on this page · raw message
Signature · 1 line
2.41.0
PATCH v6 12/13 powerpc/selftests: Add test for papr-vpd
In reply to Nathan LynchAdd selftests for /dev/papr-vpd, exercising the common expected use cases:
* Retrieve all VPD by passing an empty location code.
* Retrieve the "system VPD" by passing a location code derived from DT root node properties, as done by the vpdupdate command.
The tests also verify that certain intended properties of the driver hold:
* Passing an unterminated location code to PAPR_VPD_CREATE_HANDLE gets EINVAL.
* Passing a NULL location code pointer to PAPR_VPD_CREATE_HANDLE gets EFAULT.
* Closing the device node without first issuing a PAPR_VPD_CREATE_HANDLE command to it succeeds.
* Releasing a handle without first consuming any data from it succeeds.
* Re-reading the contents of a handle returns the same data as the first time.
Some minimal validation of the returned data is performed.
The tests are skipped on systems where the papr-vpd driver does not initialize, making this useful only on PowerVM LPARs at this point.
--- tools/testing/selftests/powerpc/Makefile | 1 + .../testing/selftests/powerpc/papr_vpd/.gitignore | 1 + tools/testing/selftests/powerpc/papr_vpd/Makefile | 12 + .../testing/selftests/powerpc/papr_vpd/papr_vpd.c | 352 +++++++++++++++++++++ 4 files changed, 366 insertions(+)
tools/testing/selftests/powerpc/Makefile +1 -0
@@ -32,6 +32,7 @@ SUB_DIRS = alignment \ vphn \ math \ papr_attributes \ + papr_vpd \ ptrace \ security \ mce
tools/testing/selftests/powerpc/papr_vpd/.gitignore new file +1 -0
@@ -0,0 +1 @@ +/papr_vpd
tools/testing/selftests/powerpc/papr_vpd/Makefile new file +12 -0
@@ -0,0 +1,12 @@ +# SPDX-License-Identifier: GPL-2.0 +noarg: + $(MAKE) -C ../ + +TEST_GEN_PROGS := papr_vpd + +top_srcdir = ../../../../.. +include ../../lib.mk + +$(TEST_GEN_PROGS): ../harness.c ../utils.c + +$(OUTPUT)/papr_vpd: CFLAGS += $(KHDR_INCLUDES)
tools/testing/selftests/powerpc/papr_vpd/papr_vpd.c new file +352 -0
352 lines not on this page · raw message
Signature · 1 line
2.41.0
PATCH v6 13/13 powerpc/selftests: Add test for papr-sysparm
In reply to Nathan LynchConsistently testing system parameter access is a bit difficult by nature -- the set of parameters available depends on the model and system configuration, and updating a parameter should be considered a destructive operation reserved for the admin.
So we validate some of the error paths and retrieve the SPLPAR characteristics string, but not much else.
--- tools/testing/selftests/powerpc/Makefile | 1 + .../selftests/powerpc/papr_sysparm/.gitignore | 1 + .../selftests/powerpc/papr_sysparm/Makefile | 12 ++ .../selftests/powerpc/papr_sysparm/papr_sysparm.c | 196 +++++++++++++++++++++ 4 files changed, 210 insertions(+)
tools/testing/selftests/powerpc/Makefile +1 -0
@@ -33,6 +33,7 @@ SUB_DIRS = alignment \ math \ papr_attributes \ papr_vpd \ + papr_sysparm \ ptrace \ security \ mce
tools/testing/selftests/powerpc/papr_sysparm/.gitignore new file +1 -0
@@ -0,0 +1 @@ +/papr_sysparm
tools/testing/selftests/powerpc/papr_sysparm/Makefile new file +12 -0
@@ -0,0 +1,12 @@ +# SPDX-License-Identifier: GPL-2.0 +noarg: + $(MAKE) -C ../ + +TEST_GEN_PROGS := papr_sysparm + +top_srcdir = ../../../../.. +include ../../lib.mk + +$(TEST_GEN_PROGS): ../harness.c ../utils.c + +$(OUTPUT)/papr_sysparm: CFLAGS += $(KHDR_INCLUDES)
tools/testing/selftests/powerpc/papr_sysparm/papr_sysparm.c new file +196 -0
196 lines not on this page · raw message
Signature · 1 line
2.41.0
Michael Ellerman
AppliedQuoted 15 lines · Nathan Lynch
Add character devices that expose PAPR-specific system parameters and VPD to user space.
The problem: important platform features are enabled on Linux VMs through the powerpc-specific
rtas()syscall in combination with writeable mappings of/dev/mem. In typical usage, this is encapsulated behind APIs provided by the librtas library. This paradigm is incompatible with lockdown, which prohibits/dev/memaccess. It also is too low-level in many cases: a single logical operation may require multiplesys_rtas()calls in succession to complete. This carries the risk that a process may exit while leaving an operation unfinished. It also means that callers must coordinate their use of the syscall for functions that cannot tolerate multiple concurrent clients, such as ibm,get-vpd.
Quoted 1 line
[...]
Applied to powerpc/next.
[01/13] powerpc/rtas: Avoid warning on invalid token argument to sys_rtas()
https://git.kernel.org/powerpc/c/01e346ffefda3a7088afebf02b940614179688e7 [02/13] powerpc/rtas: Add for_each_rtas_function() iterator https://git.kernel.org/powerpc/c/c500c6e736df030f8956080738f59701c0b43dd8 [03/13] powerpc/rtas: Fall back to linear search on failed token->function lookup https://git.kernel.org/powerpc/c/669acc7eec223a81ea5e2420de85b61979ab7dad [04/13] powerpc/rtas: Add function return status constants https://git.kernel.org/powerpc/c/9592aa5ad59e736727fe7894e6e820e2d851abcf [05/13] powerpc/rtas: Move token validation from block_rtas_call() to sys_rtas() https://git.kernel.org/powerpc/c/e7582edb78619abb4ebf0a6e1fed125dcd7243b6 [06/13] powerpc/rtas: Facilitate high-level call sequences https://git.kernel.org/powerpc/c/adf7a019e5f82607fc0f0079926d0178afe8f4ef [07/13] powerpc/rtas: Serialize firmware activation sequences https://git.kernel.org/powerpc/c/dc7637c402b90a197d3f21a3d78f2b00b67ea22a [08/13] powerpc/rtas: Warn if per-function lock isn't held https://git.kernel.org/powerpc/c/e3681107bc9f97c5948a1c8a3a97ac64907210ce [09/13] powerpc/pseries: Add papr-vpd character driver for VPD retrieval https://git.kernel.org/powerpc/c/514f6ff4369a30bf0da71a1a09fd47b2fca5d76f [10/13] powerpc/pseries/papr-sysparm: Validate buffer object lengths https://git.kernel.org/powerpc/c/35aae182bd7b422be3cefc08c12207bf2b973364 [11/13] powerpc/pseries/papr-sysparm: Expose character device to user space https://git.kernel.org/powerpc/c/905b9e48786ec55b2c469db77fb46e20bf3e4901 [12/13] powerpc/selftests: Add test for papr-vpd https://git.kernel.org/powerpc/c/9118c5d32bddb5f75bc4f9f31218e70317702502 [13/13] powerpc/selftests: Add test for papr-sysparm https://git.kernel.org/powerpc/c/76b2ec3faeaa0c8d84705acd64ac0e5a307ce9c2
cheers