Skip to contentSkip to conversation

Add character devices for indices and platform-dump RTAS

PATCH Superseded Haren Myneni 2 participants 15 messages Jan 11, 2025 – Feb 6, 2025 PowerPC

v2 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v2 cover Add character devices for indices and platform-dump RTAS

Several APIs such as rtas_get_indices(), rtas_get_dynamic_sensor(), rtas_set_dynamic_indicator() and rtas_platform_dump() provided by librtas library are implemented in user space using rtas syscall in combination with writable mappings of /dev/mem. But this implementation is not compatible with system lockdown which prohibits /dev/mem access. The current kernel already provides char based driver interfaces for several RTAS calls such as VPD and system parameters to support lockdown feature.

This patch series adds new char based drivers, /dev/papr-indices for ibm,get-indices, ibm,get-dynamic-sensor-state and ibm,set-dynamic-indicator RTAS Calls. and /dev/papr-platform-dump for ibm,platform-dump. Providing the similar open/ioctl/read interfaces to the user space as in the case of VPD and system parameters.

I have made changes to librtas library to use the new kernel interfaces if the corresponding device entry is available.

This patch series has the following patches:
powerpc/pseries: Define common functions for RTAS sequence calls
- For some of sequence based RTAS calls, the OS should not start another sequence with different input until the previous sequence is completed. So the sequence should be completed during ioctl() and expose the entire buffer during read(). ibm,get-indices is sequence based RTAS function similar to ibm,get-vpd and we already have the corresponding implementation for VPD driver. So update papr_rtas_sequence struct for RTAS call specific functions and move the top level sequence functions in to a separate file.

powerpc/pseries: Define papr_indices_io_block for papr-indices ioctls
- /dev/papr-indices driver supports ibm,get-indices, ibm,get-dynamic-sensor-state and ibm,set-dynamic-indicator RTAS Calls. papr-indices.h introduces 3 different ioctls for these RTAS calls and the corresponding ioctl input buffer.

powerpc/pseries: Add papr-indices char driver for ibm,get-indices
- Introduce /dev/papr-indices char based driver and add support for get-indices RTAS function

powerpc/pseries: Add ibm,set-dynamic-indicator RTAS call support
- Update /dev/papr-indices for set-dynamic-indicator RTAS function

powerpc/pseries: Add ibm,get-dynamic-sensor-state RTAS call support
- Update /dev/papr-indices for get-dynamic-sensor-state RTAS function

powerpc/pseries: Add papr-platform-dump character driver for dump
retrieval
- Introduce /dev/papr-platform-dump char driver and adds support for ibm,platform-dump. Received suggestions from the previous post as a separate patch - Updated the patch with invalidating the dump using a separate ioctl.

Changelog:

v2:
- Added unlock rtas_ibm_set_dynamic_indicator_lock and rtas_ibm_get_dynamic_sensor_state_lock mutex for failure cases as reported by Dan Carpenter
- Fixed build warnings for the proper function parameter descriptions as reported by kernel test robot <[email protected]>

Haren Myneni (6):
powerpc/pseries: Define common functions for RTAS sequence calls powerpc/pseries: Define papr_indices_io_block for papr-indices ioctls powerpc/pseries: Add papr-indices char driver for ibm,get-indices powerpc/pseries: Add ibm,set-dynamic-indicator RTAS call support powerpc/pseries: Add ibm,get-dynamic-sensor-state RTAS call support powerpc/pseries: Add papr-platform-dump character driver for dump
retrieval

 arch/powerpc/include/asm/rtas.h               |   3 +
 arch/powerpc/include/uapi/asm/papr-indices.h  |  41 ++
 .../include/uapi/asm/papr-platform-dump.h     |  15 +
 arch/powerpc/kernel/rtas.c                    |   6 +-
 arch/powerpc/platforms/pseries/Makefile       |   3 +-
 arch/powerpc/platforms/pseries/papr-indices.c | 558 ++++++++++++++++++
 .../platforms/pseries/papr-platform-dump.c    | 408 +++++++++++++
 .../platforms/pseries/papr-rtas-common.c      | 243 ++++++++
 .../platforms/pseries/papr-rtas-common.h      |  45 ++
 arch/powerpc/platforms/pseries/papr-vpd.c     | 270 ++-------
 10 files changed, 1364 insertions(+), 228 deletions(-)
 create mode 100644 arch/powerpc/include/uapi/asm/papr-indices.h
 create mode 100644 arch/powerpc/include/uapi/asm/papr-platform-dump.h
 create mode 100644 arch/powerpc/platforms/pseries/papr-indices.c
 create mode 100644 arch/powerpc/platforms/pseries/papr-platform-dump.c
 create mode 100644 arch/powerpc/platforms/pseries/papr-rtas-common.c
 create mode 100644 arch/powerpc/platforms/pseries/papr-rtas-common.h
Signature · 1 line
2.43.5
#1 lore raw

PATCH v2 1/6 powerpc/pseries: Define common functions for RTAS sequence calls

The RTAS call can be normal where retrieves the data form the hypervisor once or sequence based RTAS call which has to issue multiple times until the complete data is obtained. For some of these sequence RTAS calls, the OS should not interleave calls with different input until the sequence is completed. The data is collected for each call and copy to the buffer for the entire sequence during ioctl() handle and then expose this buffer to the user space with read() handle.

One such sequence RTAS call is ibm,get-vpd and its support is already included in the current code. To add the similar support for other sequence based calls, move the common functions in to separate file and update papr_rtas_sequence struct with the following callbacks so that RTAS call specific code will be defined and executed to complete the sequence.

struct papr_rtas_sequence {
        int error;
        void params;
        void (*begin) (struct papr_rtas_sequence *, void *);
        void (*end) (struct papr_rtas_sequence *);
        const char * (*work) (struct papr_rtas_sequence *, size_t *);
};

params: Input parameters used to pass for RTAS call.
Begin: RTAS call specific function to initialize data
including work area allocation.
End: RTAS call specific function to free up resources
(free work area) after the sequence is completed.
Work: The actual RTAS call specific function which collects
the data from the hypervisor.

---
 arch/powerpc/platforms/pseries/Makefile       |   2 +-
 .../platforms/pseries/papr-rtas-common.c      | 243 ++++++++++++++++
 .../platforms/pseries/papr-rtas-common.h      |  45 +++
 arch/powerpc/platforms/pseries/papr-vpd.c     | 270 +++---------------
 4 files changed, 335 insertions(+), 225 deletions(-)
 create mode 100644 arch/powerpc/platforms/pseries/papr-rtas-common.c
 create mode 100644 arch/powerpc/platforms/pseries/papr-rtas-common.h
arch/powerpc/platforms/pseries/Makefile +1 -1
index 7bf506f6b8c8..697c216b70dc 100644
@@ -3,7 +3,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 \
+			   papr-rtas-common.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-rtas-common.c new file +243 -0

243 lines not on this page · raw message

arch/powerpc/platforms/pseries/papr-rtas-common.h new file +45 -0

45 lines not on this page · raw message

arch/powerpc/platforms/pseries/papr-vpd.c +46 -224

390 lines not on this page · raw message

Signature · 1 line
2.43.5
#2 lore raw

PATCH v2 2/6 powerpc/pseries: Define papr_indices_io_block for papr-indices ioctls

In reply to Haren Myneni

To issue ibm,get-indices, ibm,set-dynamic-indicator and ibm,get-dynamic-sensor-state in the user space, the RMO buffer is allocated for the work area which is restricted under system lockdown. So instead of user space execution, the kernel will provide /dev/papr-indices interface to execute these RTAS calls.

The user space assigns data in papr_indices_io_block struct depends on the specific HCALL and passes to the following ioctls:

PAPR_INDICES_IOC_GET: Use for ibm,get-indices. Returns a
get-indices handle fd to read data.
PAPR_DYNAMIC_SENSOR_IOC_GET: Use for ibm,get-dynamic-sensor-state.
Updates the sensor state in papr_indices_io_block.dynamic_param.state

PAPR_DYNAMIC_INDICATOR_IOC_SET: Use for ibm,set-dynamic-indicator.
Sets the new state for the input indicator.

---
 arch/powerpc/include/uapi/asm/papr-indices.h | 41 ++++++++++++++++++++
 1 file changed, 41 insertions(+)
 create mode 100644 arch/powerpc/include/uapi/asm/papr-indices.h
arch/powerpc/include/uapi/asm/papr-indices.h new file +41 -0
new file mode 100644
index 000000000000..c580025fe201
@@ -0,0 +1,41 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+#ifndef _UAPI_PAPR_INDICES_H_
+#define _UAPI_PAPR_INDICES_H_
+
+#include <linux/types.h>
+#include <asm/ioctl.h>
+#include <asm/papr-miscdev.h>
+
+#define LOC_CODE_SIZE			80
+#define RTAS_GET_INDICES_BUF_SIZE	SZ_4K
+
+struct papr_indices_io_block {
+	union {
+		struct {
+			__u8 is_sensor; /* 0 for indicator and 1 for sensor */
+			__u32 indice_type;
+		} indices;
+		struct {
+			__u32 token; /* Sensor or indicator token */
+			__u32 state; /* get / set state */
+			/*
+			 * PAPR+ 12.3.2.4 Converged Location Code Rules - Length
+			 * Restrictions. 79 characters plus null.
+			 */
+			char location_code_str[LOC_CODE_SIZE]; /* location code */
+		} dynamic_param;
+	};
+};
+
+/*
+ * ioctls for /dev/papr-indices.
+ * PAPR_INDICES_IOC_GET:	Returns a get-indices handle fd to read data
+ * PAPR_DYNAMIC_SENSOR_IOC_GET:	Gets the state of the input sensor
+ * PAPR_DYNAMIC_INDICATOR_IOC_SET: Sets the new state for the input indicator
+ */
+#define PAPR_INDICES_IOC_GET		_IOW(PAPR_MISCDEV_IOC_ID, 3, struct papr_indices_io_block)
+#define PAPR_DYNAMIC_SENSOR_IOC_GET	_IOWR(PAPR_MISCDEV_IOC_ID, 4, struct papr_indices_io_block)
+#define PAPR_DYNAMIC_INDICATOR_IOC_SET	_IOW(PAPR_MISCDEV_IOC_ID, 5, struct papr_indices_io_block)
+
+
+#endif /* _UAPI_PAPR_INDICES_H_ */
Signature · 1 line
2.43.5
#3 lore raw

PATCH v2 3/6 powerpc/pseries: Add papr-indices char driver for ibm,get-indices

In reply to Haren Myneni

The RTAS call ibm,get-indices is used to obtain indices and location codes for a specified indicator or sensor token. The current implementation uses rtas_get_indices() API provided by librtas library which allocates RMO buffer and issue this RTAS call in the user space. But writable mapping /dev/mem access by the user space is prohibited under system lockdown.

To overcome the restricted access in the user space, the kernel provide interfaces to collect indices data from the hypervisor. This patch adds papr-indices character driver and expose standard interfaces such as open / ioctl/ read to user space in ways that are compatible with lockdown.

PAPR (2.13 7.3.17 ibm,get-indices RTAS Call) describes the following steps to retrieve all indices data:
- User input parameters to the RTAS call: sensor or indicator, and indice type
- ibm,get-indices is sequence RTAS call which means has to issue multiple times to get the entire list of indicators or sensors of a particular type. The hypervisor expects the first RTAS call with the sequence 1 and the subsequent calls with the sequence number returned from the previous calls.
- The OS may not interleave calls to ibm,get-indices for different indicator or sensor types. Means other RTAS calls with different type should not be issued while the previous type sequence is in progress. So collect the entire list of indices and copied to buffer BLOB during ioctl() and expose this buffer to the user space with the file descriptor.
- The hypervisor fills the work area with a specific format but does not return the number of bytes written to the buffer. Instead of parsing the data for each call to determine the data length, copy the work area size (RTAS_GET_INDICES_BUF_SIZE) to the buffer. Return work-area size of data to the user space for each read() call.

Expose these interfaces to user space with a /dev/papr-indices character device using the following programming model:

int devfd = open("/dev/papr-indices", O_RDONLY);
int fd = ioctl(devfd, PAPR_INDICES_IOC_GET,
struct papr_indices_io_block)
- Collect all indices data for the specified token to the buffer
char *buf = malloc(RTAS_GET_INDICES_BUF_SIZE);
length = read(fd, buf, RTAS_GET_INDICES_BUF_SIZE)
- RTAS_GET_INDICES_BUF_SIZE of data is returned to the user space.
- The user space retrieves the indices and their location codes from the buffer
- Should issue multiple read() calls until reaches the end of BLOB buffer.

The read() should use the file descriptor obtained from ioctl to get the data that is exposed to file descriptor. Implemented support in librtas (rtas_get_indices()) for this new ABI for system lockdown.

---
 arch/powerpc/include/asm/rtas.h               |   1 +
 arch/powerpc/kernel/rtas.c                    |   2 +-
 arch/powerpc/platforms/pseries/Makefile       |   2 +-
 arch/powerpc/platforms/pseries/papr-indices.c | 369 ++++++++++++++++++
 4 files changed, 372 insertions(+), 2 deletions(-)
 create mode 100644 arch/powerpc/platforms/pseries/papr-indices.c
arch/powerpc/include/asm/rtas.h +1 -0
index 04406162fc5a..7dc527a5aaac 100644
@@ -515,6 +515,7 @@ extern char rtas_data_buf[RTAS_DATA_BUF_SIZE];
 extern unsigned long rtas_rmo_buf;
 
 extern struct mutex rtas_ibm_get_vpd_lock;
+extern struct mutex rtas_ibm_get_indices_lock;
 
 #define GLOBAL_INTERRUPT_QUEUE 9005
 
arch/powerpc/kernel/rtas.c +1 -1
index d31c9799cab2..76c634b92cb2 100644
@@ -93,11 +93,11 @@ struct rtas_function {
  */
 static DEFINE_MUTEX(rtas_ibm_activate_firmware_lock);
 static DEFINE_MUTEX(rtas_ibm_get_dynamic_sensor_state_lock);
-static DEFINE_MUTEX(rtas_ibm_get_indices_lock);
 static DEFINE_MUTEX(rtas_ibm_lpar_perftools_lock);
 static DEFINE_MUTEX(rtas_ibm_physical_attestation_lock);
 static DEFINE_MUTEX(rtas_ibm_set_dynamic_indicator_lock);
 DEFINE_MUTEX(rtas_ibm_get_vpd_lock);
+DEFINE_MUTEX(rtas_ibm_get_indices_lock);
 
 static struct rtas_function rtas_function_table[] __ro_after_init = {
 	[RTAS_FNIDX__CHECK_EXCEPTION] = {
arch/powerpc/platforms/pseries/Makefile +1 -1
index 697c216b70dc..e1db61877bb9 100644
@@ -3,7 +3,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-rtas-common.o papr-vpd.o \
+			   papr-rtas-common.o papr-vpd.o papr-indices.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-indices.c new file +369 -0

369 lines not on this page · raw message

Signature · 1 line
2.43.5
#4 lore raw

PATCH v2 4/6 powerpc/pseries: Add ibm,set-dynamic-indicator RTAS call support

In reply to Haren Myneni

The RTAS call ibm,set-dynamic-indicator is used to set the new indicator state identified by a location code. The current implementation uses rtas_set_dynamic_indicator() API provided by librtas library which allocates RMO buffer and issue this RTAS call in the user space. But /dev/mem access by the user space is prohibited under system lockdown.

This patch provides an interface with new ioctl PAPR_DYNAMIC_INDICATOR_IOC_SET to the papr-indices character driver and expose this interface to the user space that is compatible with lockdown.

Refer PAPR 7.3.18 ibm,set-dynamic-indicator for more information on this RTAS call.
- User input parameters to the RTAS call: location code string, indicator token and new state

Expose these interfaces to user space with a /dev/papr-indices character device using the following programming model:
int fd = open("/dev/papr-indices", O_RDWR);
int ret = ioctl(fd, PAPR_DYNAMIC_INDICATOR_IOC_SET,
struct papr_indices_io_block)
- The user space passes input parameters in papr_indices_io_block struct

---
 arch/powerpc/include/asm/rtas.h               |   1 +
 arch/powerpc/kernel/rtas.c                    |   2 +-
 arch/powerpc/platforms/pseries/papr-indices.c | 122 ++++++++++++++++++
 3 files changed, 124 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/rtas.h +1 -0
index 7dc527a5aaac..2da52f59e4c6 100644
@@ -516,6 +516,7 @@ extern unsigned long rtas_rmo_buf;
 
 extern struct mutex rtas_ibm_get_vpd_lock;
 extern struct mutex rtas_ibm_get_indices_lock;
+extern struct mutex rtas_ibm_set_dynamic_indicator_lock;
 
 #define GLOBAL_INTERRUPT_QUEUE 9005
 
arch/powerpc/kernel/rtas.c +1 -1
index 76c634b92cb2..88fa416730af 100644
@@ -95,9 +95,9 @@ static DEFINE_MUTEX(rtas_ibm_activate_firmware_lock);
 static DEFINE_MUTEX(rtas_ibm_get_dynamic_sensor_state_lock);
 static DEFINE_MUTEX(rtas_ibm_lpar_perftools_lock);
 static DEFINE_MUTEX(rtas_ibm_physical_attestation_lock);
-static DEFINE_MUTEX(rtas_ibm_set_dynamic_indicator_lock);
 DEFINE_MUTEX(rtas_ibm_get_vpd_lock);
 DEFINE_MUTEX(rtas_ibm_get_indices_lock);
+DEFINE_MUTEX(rtas_ibm_set_dynamic_indicator_lock);
 
 static struct rtas_function rtas_function_table[] __ro_after_init = {
 	[RTAS_FNIDX__CHECK_EXCEPTION] = {
arch/powerpc/platforms/pseries/papr-indices.c +122 -0

146 lines not on this page · raw message

Signature · 1 line
2.43.5
#5 lore raw

PATCH v2 5/6 powerpc/pseries: Add ibm,get-dynamic-sensor-state RTAS call support

In reply to Haren Myneni

The RTAS call ibm,get-dynamic-sensor-state is used to get the sensor state identified by the location code and the sensor token. The librtas library provides an API rtas_get_dynamic_sensor() which uses /dev/mem access for work area allocation but is restricted under system lockdown.

This patch provides an interface with new ioctl
PAPR_DYNAMIC_SENSOR_IOC_GET to the papr-indices character
driver which executes this HCALL and copies the sensor state in the user specified ioctl buffer.

Refer PAPR 7.3.19 ibm,get-dynamic-sensor-state for more information on this RTAS call.
- User input parameters to the RTAS call: location code string and the sensor token

Expose these interfaces to user space with a /dev/papr-indices character device using the following programming model:
int fd = open("/dev/papr-indices", O_RDWR);
int ret = ioctl(fd, PAPR_DYNAMIC_SENSOR_IOC_GET,
struct papr_indices_io_block)
- The user space specifies input parameters in papr_indices_io_block struct
- Returned state for the specified sensor is copied to papr_indices_io_block.dynamic_param.state

---
 arch/powerpc/include/asm/rtas.h               |  1 +
 arch/powerpc/kernel/rtas.c                    |  2 +-
 arch/powerpc/platforms/pseries/papr-indices.c | 67 +++++++++++++++++++
 3 files changed, 69 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/rtas.h +1 -0
index 2da52f59e4c6..fcd822f0e1d7 100644
@@ -517,6 +517,7 @@ extern unsigned long rtas_rmo_buf;
 extern struct mutex rtas_ibm_get_vpd_lock;
 extern struct mutex rtas_ibm_get_indices_lock;
 extern struct mutex rtas_ibm_set_dynamic_indicator_lock;
+extern struct mutex rtas_ibm_get_dynamic_sensor_state_lock;
 
 #define GLOBAL_INTERRUPT_QUEUE 9005
 
arch/powerpc/kernel/rtas.c +1 -1
index 88fa416730af..a4848e7f248e 100644
@@ -92,12 +92,12 @@ struct rtas_function {
  * Per-function locks for sequence-based RTAS functions.
  */
 static DEFINE_MUTEX(rtas_ibm_activate_firmware_lock);
-static DEFINE_MUTEX(rtas_ibm_get_dynamic_sensor_state_lock);
 static DEFINE_MUTEX(rtas_ibm_lpar_perftools_lock);
 static DEFINE_MUTEX(rtas_ibm_physical_attestation_lock);
 DEFINE_MUTEX(rtas_ibm_get_vpd_lock);
 DEFINE_MUTEX(rtas_ibm_get_indices_lock);
 DEFINE_MUTEX(rtas_ibm_set_dynamic_indicator_lock);
+DEFINE_MUTEX(rtas_ibm_get_dynamic_sensor_state_lock);
 
 static struct rtas_function rtas_function_table[] __ro_after_init = {
 	[RTAS_FNIDX__CHECK_EXCEPTION] = {
arch/powerpc/platforms/pseries/papr-indices.c +67 -0

85 lines not on this page · raw message

Signature · 1 line
2.43.5
#6 lore raw

PATCH v2 6/6 powerpc/pseries: Add papr-platform-dump character driver for dump retrieval

In reply to Haren Myneni

ibm,platform-dump RTAS call in combination with writable mapping /dev/mem is issued to collect platform dump from the hypervisor and may need multiple calls to get the complete dump. The current implementation uses rtas_platform_dump() API provided by librtas library to issue these RTAS calls. But /dev/mem access by the user space is prohibited under system lockdown.

The solution should be to restrict access to RTAS function in user space and provide kernel interfaces to collect dump. This patch adds papr-platform-dump character driver and expose standard interfaces such as open / ioctl/ read to user space in ways that are compatible with lockdown.

PAPR (7.3.3.4.1 ibm,platform-dump) provides a method to obtain the complete dump:
- Each dump will be identified by ID called dump tag.
- A sequence of RTAS calls have to be issued until retrieve the complete dump. The hypervisor expects the first RTAS call with the sequence 0 and the subsequent calls with the sequence number returned from the previous calls.
- The hypervisor returns "dump complete" status once the complete dump is retrieved. But expects one more RTAS call from the partition with the NULL buffer to invalidate dump which means the dump will be removed in the hypervisor.
- Sequence of calls are allowed with different dump IDs at the same time but not with the same dump ID.

Expose these interfaces to user space with a /dev/papr-platform-dump character device using the following programming model:

int devfd = open("/dev/papr-platform-dump", O_RDONLY); int fd = ioctl(devfd,PAPR_PLATFORM_DUMP_IOC_CREATE_HANDLE, &dump_id)
- Restrict user space to access with the same dump ID. Typically we do not expect user space requests the dump again for the same dump ID.
char *buf = malloc(size);
length = read(fd, buf, size);
- size should be minimum 1K based on PAPR and <= 4K based on RTAS work area size. It will be restrict to RTAS work area size. Using 4K work area based on the current implementation in librtas library
- Each read call issue RTAS call to get the data based on the size requirement and returns bytes returned from the hypervisor
- If the previous call returns dump complete status, the next read returns 0 like EOF.
ret = ioctl(PAPR_PLATFORM_DUMP_IOC_INVALIDATE, &dump_id)
- RTAS call with NULL buffer to invalidates the dump.

The read API should use the file descriptor obtained from ioctl based on dump ID so that gets dump contents for the corresponding dump ID. Implemented support in librtas (rtas_platform_dump()) for this new ABI to support system lockdown.

---
 .../include/uapi/asm/papr-platform-dump.h     |  15 +
 arch/powerpc/platforms/pseries/Makefile       |   1 +
 .../platforms/pseries/papr-platform-dump.c    | 408 ++++++++++++++++++
 3 files changed, 424 insertions(+)
 create mode 100644 arch/powerpc/include/uapi/asm/papr-platform-dump.h
 create mode 100644 arch/powerpc/platforms/pseries/papr-platform-dump.c
arch/powerpc/include/uapi/asm/papr-platform-dump.h new file +15 -0
new file mode 100644
index 000000000000..3a0f152e3ce8
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+#ifndef _UAPI_PAPR_PLATFORM_DUMP_H_
+#define _UAPI_PAPR_PLATFORM_DUMP_H_
+
+#include <asm/ioctl.h>
+#include <asm/papr-miscdev.h>
+
+/*
+ * ioctl for /dev/papr-platform-dump. Returns a VPD handle fd corresponding to
+ * the location code.
+ */
+#define PAPR_PLATFORM_DUMP_IOC_CREATE_HANDLE _IOW(PAPR_MISCDEV_IOC_ID, 6, __u64)
+#define PAPR_PLATFORM_DUMP_IOC_INVALIDATE    _IOW(PAPR_MISCDEV_IOC_ID, 7, __u64)
+
+#endif /* _UAPI_PAPR_PLATFORM_DUMP_H_ */
arch/powerpc/platforms/pseries/Makefile +1 -0
index e1db61877bb9..c82c94e0a73c 100644
@@ -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-rtas-common.o papr-vpd.o papr-indices.o \
+			   papr-platform-dump.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-platform-dump.c new file +408 -0

408 lines not on this page · raw message

Signature · 1 line
2.43.5
#7 lore raw

Re: [PATCH v2 6/6] powerpc/pseries: Add papr-platform-dump character driver for dump retrieval

Hello,

thanks for working on this!

I see in thes version you ended up reusing the existing RTAS call code which looks better.

From the past discussion it sounds like the get-indices call can list the available dumps, and I do not see this connection documented.

Also the part about it not being used in practice by the service that retrieves the dumps because it gets a message from the hypervisor with the dump id when a dump is available.

Quoted 340 lines · Haren Myneni

ibm,platform-dump RTAS call in combination with writable mapping /dev/mem is issued to collect platform dump from the hypervisor and may need multiple calls to get the complete dump. The current implementation uses rtas_platform_dump() API provided by librtas library to issue these RTAS calls. But /dev/mem access by the user space is prohibited under system lockdown.

The solution should be to restrict access to RTAS function in user space and provide kernel interfaces to collect dump. This patch adds papr-platform-dump character driver and expose standard interfaces such as open / ioctl/ read to user space in ways that are compatible with lockdown.

PAPR (7.3.3.4.1 ibm,platform-dump) provides a method to obtain the complete dump:
- Each dump will be identified by ID called dump tag.
- A sequence of RTAS calls have to be issued until retrieve the complete dump. The hypervisor expects the first RTAS call with the sequence 0 and the subsequent calls with the sequence number returned from the previous calls.
- The hypervisor returns "dump complete" status once the complete dump is retrieved. But expects one more RTAS call from the partition with the NULL buffer to invalidate dump which means the dump will be removed in the hypervisor.
- Sequence of calls are allowed with different dump IDs at the same time but not with the same dump ID.

Expose these interfaces to user space with a /dev/papr-platform-dump character device using the following programming model:

int devfd = open("/dev/papr-platform-dump", O_RDONLY); int fd = ioctl(devfd,PAPR_PLATFORM_DUMP_IOC_CREATE_HANDLE, &dump_id)
- Restrict user space to access with the same dump ID. Typically we do not expect user space requests the dump again for the same dump ID.
char *buf = malloc(size);
length = read(fd, buf, size);
- size should be minimum 1K based on PAPR and <= 4K based on RTAS work area size. It will be restrict to RTAS work area size. Using 4K work area based on the current implementation in librtas library
- Each read call issue RTAS call to get the data based on the size requirement and returns bytes returned from the hypervisor
- If the previous call returns dump complete status, the next read returns 0 like EOF.
ret = ioctl(PAPR_PLATFORM_DUMP_IOC_INVALIDATE, &dump_id)
- RTAS call with NULL buffer to invalidates the dump.

The read API should use the file descriptor obtained from ioctl based on dump ID so that gets dump contents for the corresponding dump ID. Implemented support in librtas (rtas_platform_dump()) for this new ABI to support system lockdown.

---
 .../include/uapi/asm/papr-platform-dump.h     |  15 +
 arch/powerpc/platforms/pseries/Makefile       |   1 +
 .../platforms/pseries/papr-platform-dump.c    | 408 ++++++++++++++++++
 3 files changed, 424 insertions(+)
 create mode 100644 arch/powerpc/include/uapi/asm/papr-platform-dump.h
 create mode 100644 arch/powerpc/platforms/pseries/papr-platform-dump.c
arch/powerpc/include/uapi/asm/papr-platform-dump.h new file +15 -0
new file mode 100644
index 000000000000..3a0f152e3ce8
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+#ifndef _UAPI_PAPR_PLATFORM_DUMP_H_
+#define _UAPI_PAPR_PLATFORM_DUMP_H_
+
+#include <asm/ioctl.h>
+#include <asm/papr-miscdev.h>
+
+/*
+ * ioctl for /dev/papr-platform-dump. Returns a VPD handle fd corresponding to
+ * the location code.
+ */
+#define PAPR_PLATFORM_DUMP_IOC_CREATE_HANDLE _IOW(PAPR_MISCDEV_IOC_ID, 6, __u64)
+#define PAPR_PLATFORM_DUMP_IOC_INVALIDATE    _IOW(PAPR_MISCDEV_IOC_ID, 7, __u64)
+
+#endif /* _UAPI_PAPR_PLATFORM_DUMP_H_ */
arch/powerpc/platforms/pseries/Makefile +1 -0
index e1db61877bb9..c82c94e0a73c 100644
@@ -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-rtas-common.o papr-vpd.o papr-indices.o \
+			   papr-platform-dump.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-platform-dump.c new file +238 -0
new file mode 100644
index 000000000000..13a418d7c37e
@@ -0,0 +1,408 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#define pr_fmt(fmt) "papr-platform-dump: " fmt
+
+#include <linux/anon_inodes.h>
+#include <linux/file.h>
+#include <linux/fs.h>
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/miscdevice.h>
+#include <asm/machdep.h>
+#include <asm/rtas-work-area.h>
+#include <asm/rtas.h>
+#include <uapi/asm/papr-platform-dump.h>
+
+/*
+ * Function-specific return values for ibm,platform-dump, derived from
+ * PAPR+ v2.13 7.3.3.4.1 "ibm,platform-dump RTAS Call".
+ */
+#define	RTAS_IBM_PLATFORM_DUMP_COMPLETE	0	/* Complete dump retrieved. */
+#define	RTAS_IBM_PLATFORM_DUMP_CONTINUE	1	/* Continue dump */
+#define	RTAS_NOT_AUTHORIZED		-9002	/* Not Authorized */
+
+#define	RTAS_IBM_PLATFORM_DUMP_START	2 /* Linux status to start dump */
+
+/**
+ * struct ibm_platform_dump_params - Parameters (in and out) for
+ *                                              ibm,platform-dump
+ * @work_area:		In: work area buffer for results.
+ * @buf_length:		In: work area buffer length in bytes
+ * @dump_tag_hi:	In: Most-significant 32 bits of a Dump_Tag representing
+ *                      an id of the dump being processed.
+ * @dump_tag_lo:	In: Least-significant 32 bits of a Dump_Tag representing
+ *                      an id of the dump being processed.
+ * @sequence_hi:	In: Sequence number in most-significant 32 bits.
+ *                      Out: Next sequence number in most-significant 32 bits.
+ * @sequence_lo:	In: Sequence number in Least-significant 32 bits
+ *                      Out: Next sequence number in Least-significant 32 bits.
+ * @bytes_ret_hi:	Out: Bytes written in most-significant 32 bits.
+ * @bytes_ret_lo:	Out: Bytes written in Least-significant 32 bits.
+ * @status:		Out: RTAS call status.
+ * @list:		Maintain the list of dumps are in progress. Can
+ *                      retrieve multiple dumps with different dump IDs at
+ *                      the same time but not with the same dump ID. This list
+ *                      is used to determine whether the dump for the same ID
+ *                      is in progress.
+ */
+struct ibm_platform_dump_params {
+	struct rtas_work_area	*work_area;
+	u32			buf_length;
+	u32			dump_tag_hi;
+	u32			dump_tag_lo;
+	u32			sequence_hi;
+	u32			sequence_lo;
+	u32			bytes_ret_hi;
+	u32			bytes_ret_lo;
+	s32			status;
+	struct list_head	list;
+};
+
+/*
+ * Multiple dumps with different dump IDs can be retrieved at the same
+ * time, but not with dame dump ID. platform_dump_list_mutex and
+ * platform_dump_list are used to prevent this behavior.
+ */
+static DEFINE_MUTEX(platform_dump_list_mutex);
+static LIST_HEAD(platform_dump_list);
+
+/**
+ * rtas_ibm_platform_dump() - Call ibm,platform-dump to fill a work area
+ * buffer.
+ * @params: See &struct ibm_platform_dump_params.
+ * @buf_addr: Address of dump buffer (work_area)
+ * @buf_length: Length of the buffer in bytes (min. 1024)
+ *
+ * Calls ibm,platform-dump until it errors or successfully deposits data
+ * into the supplied work area. Handles RTAS retry statuses. Maps RTAS
+ * error statuses to reasonable errno values.
+ *
+ * Can request multiple dumps with different dump IDs at the same time,
+ * but not with the same dump ID which is prevented with the check in
+ * the ioctl code (papr_platform_dump_create_handle()).
+ *
+ * The caller should inspect @params.status to determine whether more
+ * calls are needed to complete the sequence.
+ *
+ * Context: May sleep.
+ * Return: -ve on error, 0 for dump complete and 1 for continue dump
+ */
+static int rtas_ibm_platform_dump(struct ibm_platform_dump_params *params,
+				phys_addr_t buf_addr, u32 buf_length)
+{
+	u32 rets[4];
+	s32 fwrc;
+	int ret = 0;
+
+	do {
+		fwrc = rtas_call(rtas_function_token(RTAS_FN_IBM_PLATFORM_DUMP),
+				6, 5,
+				rets,
+				params->dump_tag_hi,
+				params->dump_tag_lo,
+				params->sequence_hi,
+				params->sequence_lo,
+				buf_addr,
+				buf_length);
+	} while (rtas_busy_delay(fwrc));
+
+	switch (fwrc) {
+	case RTAS_HARDWARE_ERROR:
+		ret = -EIO;
+		break;
+	case RTAS_NOT_AUTHORIZED:
+		ret = -EPERM;
+		break;
+	case RTAS_IBM_PLATFORM_DUMP_CONTINUE:
+	case RTAS_IBM_PLATFORM_DUMP_COMPLETE:
+		params->sequence_hi = rets[0];
+		params->sequence_lo = rets[1];
+		params->bytes_ret_hi = rets[2];
+		params->bytes_ret_lo = rets[3];
+		break;
+	default:
+		ret = -EIO;
+		pr_err_ratelimited("unexpected ibm,platform-dump status %d\n",
+				fwrc);
+		break;
+	}
+
+	params->status = fwrc;
+	return ret;
+}
+
+/*
+ * Platform dump is used with multiple RTAS calls to retrieve the
+ * complete dump for the provided dump ID. Once the complete dump is
+ * retrieved, the hypervisor returns dump complete status (0) for the
+ * last RTAS call and expects the caller issues one more call with
+ * NULL buffer to invalidate the dump so that the hypervisor can remove
+ * the dump.
+ *
+ * After the specific dump is invalidated in the hypervisor, expect the
+ * dump complete status for the new sequence - the user space initiates
+ * new request for the same dump ID.
+ */
+static ssize_t papr_platform_dump_handle_read(struct file *file,
+		char __user *buf, size_t size, loff_t *off)
+{
+	struct ibm_platform_dump_params *params = file->private_data;
+	u64 total_bytes;
+	s32 fwrc;
+
+	/*
+	 * Dump already completed with the previous read calls.
+	 * In case if the user space issues further reads, returns
+	 * -EINVAL.
+	 */
+	if (!params->buf_length) {
+		pr_warn_once("Platform dump completed for dump ID %llu\n",
+			(u64) (((u64)params->dump_tag_hi << 32) |
+				params->dump_tag_lo));
+		return -EINVAL;
+	}
+
+	/*
+	 * The hypervisor returns status 0 if no more data available to
+	 * download. The dump will be invalidated with ioctl (see below).
+	 */
+	if (params->status == RTAS_IBM_PLATFORM_DUMP_COMPLETE) {
+		params->buf_length = 0;
+		/*
+		 * Returns 0 to the user space so that user
+		 * space read stops.
+		 */
+		return 0;
+	}
+
+	if (size < SZ_1K) {
+		pr_err_once("Buffer length should be minimum 1024 bytes\n");
+		return -EINVAL;
+	} else if (size > params->buf_length) {
+		/*
+		 * Allocate 4K work area. So if the user requests > 4K,
+		 * resize the buffer length.
+		 */
+		size = params->buf_length;
+	}
+
+	fwrc = rtas_ibm_platform_dump(params,
+			rtas_work_area_phys(params->work_area),
+			size);
+	if (fwrc < 0)
+		return fwrc;
+
+	total_bytes = (u64) (((u64)params->bytes_ret_hi << 32) |
+			params->bytes_ret_lo);
+
+	/*
+	 * Kernel or firmware bug, do not continue.
+	 */
+	if (WARN(total_bytes > size, "possible write beyond end of work area"))
+		return -EFAULT;
+
+	if (copy_to_user(buf, rtas_work_area_raw_buf(params->work_area),
+			total_bytes))
+		return -EFAULT;
+
+	return total_bytes;
+}
+
+static int papr_platform_dump_handle_release(struct inode *inode,
+					struct file *file)
+{
+	struct ibm_platform_dump_params *params = file->private_data;
+
+	if (params->work_area)
+		rtas_work_area_free(params->work_area);
+
+	mutex_lock(&platform_dump_list_mutex);
+	list_del(&params->list);
+	mutex_unlock(&platform_dump_list_mutex);
+
+	kfree(params);
+	file->private_data = NULL;
+	return 0;
+}
+
+/*
+ * This ioctl is used to invalidate the dump assuming the user space
+ * issue this ioctl after obtain the complete dump.
+ * Issue the last RTAS call with NULL buffer to invalidate the dump
+ * which means dump will be freed in the hypervisor.
+ */
+static long papr_platform_dump_invalidate_ioctl(struct file *file,
+				unsigned int ioctl, unsigned long arg)
+{
+	struct ibm_platform_dump_params *params;
+	u64 __user *argp = (void __user *)arg;

arch/powerpc/platforms/pseries/papr-platform-dump.c line 248

Quoted 10 lines
arch/powerpc/platforms/pseries/papr-platform-dump.c new file +10 -0
+	u64 param_dump_tag, dump_tag;
+
+	if (ioctl != PAPR_PLATFORM_DUMP_IOC_INVALIDATE)
+		return -ENOIOCTLCMD;
+
+	if (get_user(dump_tag, argp))
+		return -EFAULT;
+
+	/*
+	 * private_data is freeded during release(), so should not

freed?

Quoted 11 lines
arch/powerpc/platforms/pseries/papr-platform-dump.c new file +11 -0
+	 * happen.
+	 */
+	if (!file->private_data) {
+		pr_err("No valid FD to invalidate dump for the ID(%llu)\n",
+				dump_tag);
+		return -EINVAL;
+	}
+
+	params = file->private_data;
+	param_dump_tag = (u64) (((u64)params->dump_tag_hi << 32) |
+				params->dump_tag_lo);

arch/powerpc/platforms/pseries/papr-platform-dump.c line 269

Quoted 10 lines
arch/powerpc/platforms/pseries/papr-platform-dump.c new file +10 -0
+	if (dump_tag != param_dump_tag) {
+		pr_err("Invalid dump ID(%llu) to invalidate dump\n",
+				dump_tag);
+		return -EINVAL;
+	}
+
+	if (params->status != RTAS_IBM_PLATFORM_DUMP_COMPLETE)
+		pr_warn("Platform dump is not complete, but requested "
+			"to invalidate dump for ID(%llu)\n",
+			dump_tag);

Not sure if something should be done here or if relying on translation of the error from the RTAS call is advisable.

Thanks

Michal

#8 lore raw

Re: [PATCH v2 6/6] powerpc/pseries: Add papr-platform-dump character driver for dump retrieval

Quoted 8 lines · Michal Suchánek

Hello,

thanks for working on this!

I see in thes version you ended up reusing the existing RTAS call code
which looks better.

Quoted 7 lines

From the past discussion it sounds like the get-indices call can list the available dumps, and I do not see this connection documented.

Also the part about it not being used in practice by the service that retrieves the dumps because it gets a message from the hypervisor with
the dump id when a dump is available.

ibm,get-indices is used to obtain data based on indicator and sensor, not related to platform dump. ibm,platform-dump is used only on non-HMC based systems and BMC interface initiates manually to save the dump on tne partition. Sorry in case caused confusion in the previous discussion.

Quoted 425 lines · Haren Myneni
Quoted 393 lines

ibm,platform-dump RTAS call in combination with writable mapping /dev/mem is issued to collect platform dump from the hypervisor and may need multiple calls to get the complete dump. The current implementation uses rtas_platform_dump() API provided by librtas library to issue these RTAS calls. But /dev/mem access by the user space is prohibited under system lockdown.

The solution should be to restrict access to RTAS function in user space and provide kernel interfaces to collect dump. This patch adds papr-platform-dump character driver and expose standard interfaces such as open / ioctl/ read to user space in ways that are compatible with lockdown.

PAPR (7.3.3.4.1 ibm,platform-dump) provides a method to obtain the complete dump:
- Each dump will be identified by ID called dump tag.
- A sequence of RTAS calls have to be issued until retrieve the complete dump. The hypervisor expects the first RTAS call with the sequence 0 and the subsequent calls with the sequence number returned from the previous calls.
- The hypervisor returns "dump complete" status once the complete dump is retrieved. But expects one more RTAS call from the partition with the NULL buffer to invalidate dump which means the dump will be removed in the hypervisor.
- Sequence of calls are allowed with different dump IDs at the same time but not with the same dump ID.

Expose these interfaces to user space with a /dev/papr-platform- dump
character device using the following programming model:

int devfd = open("/dev/papr-platform-dump", O_RDONLY); int fd = ioctl(devfd,PAPR_PLATFORM_DUMP_IOC_CREATE_HANDLE,
&dump_id)
- Restrict user space to access with the same dump ID. Typically we do not expect user space requests the dump again for the same dump ID.
char *buf = malloc(size);
length = read(fd, buf, size);
- size should be minimum 1K based on PAPR and <= 4K based on RTAS work area size. It will be restrict to RTAS work area size. Using 4K work area based on the current implementation in librtas library
- Each read call issue RTAS call to get the data based on the size requirement and returns bytes returned from the hypervisor
- If the previous call returns dump complete status, the next read returns 0 like EOF.
ret = ioctl(PAPR_PLATFORM_DUMP_IOC_INVALIDATE, &dump_id)
- RTAS call with NULL buffer to invalidates the dump.

The read API should use the file descriptor obtained from ioctl based on dump ID so that gets dump contents for the corresponding dump ID. Implemented support in librtas (rtas_platform_dump()) for this new ABI to support system lockdown.

---
 .../include/uapi/asm/papr-platform-dump.h     |  15 +
 arch/powerpc/platforms/pseries/Makefile       |   1 +
 .../platforms/pseries/papr-platform-dump.c    | 408
++++++++++++++++++
 3 files changed, 424 insertions(+)
 create mode 100644 arch/powerpc/include/uapi/asm/papr-platform-
dump.h
 create mode 100644 arch/powerpc/platforms/pseries/papr-platform-

dump.c

arch/powerpc/include/uapi/asm/papr-platform-dump.h new file +9 -0
new file mode 100644
index 000000000000..3a0f152e3ce8
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+#ifndef _UAPI_PAPR_PLATFORM_DUMP_H_
+#define _UAPI_PAPR_PLATFORM_DUMP_H_
+
+#include <asm/ioctl.h>
+#include <asm/papr-miscdev.h>
+
+/*
+ * ioctl for /dev/papr-platform-dump. Returns a VPD handle fd

corresponding to
+ * the location code.
+ */
+#define PAPR_PLATFORM_DUMP_IOC_CREATE_HANDLE
_IOW(PAPR_MISCDEV_IOC_ID, 6, __u64)
+#define
PAPR_PLATFORM_DUMP_IOC_INVALIDATE _IOW(PAPR_MISCDEV_IOC_ID, 7,
__u64)

arch/powerpc/include/uapi/asm/papr-platform-dump.h new file +2 -0
+
+#endif /* _UAPI_PAPR_PLATFORM_DUMP_H_ */
arch/powerpc/platforms/pseries/Makefile +1 -0
index e1db61877bb9..c82c94e0a73c 100644
@@ -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-rtas-common.o papr-vpd.o papr-indices.o 
 \
+			   papr-platform-dump.o \
 			   setup.o iommu.o event_sources.o ras.o \

firmware.o power.o dlpar.o mobility.o rng.o
\

arch/powerpc/platforms/pseries/Makefile +0 -0
 			   pci.o pci_dlpar.o eeh_pseries.o msi.o \
arch/powerpc/platforms/pseries/papr-platform-dump.c new file +17 -0
new file mode 100644
index 000000000000..13a418d7c37e
@@ -0,0 +1,408 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#define pr_fmt(fmt) "papr-platform-dump: " fmt
+
+#include <linux/anon_inodes.h>
+#include <linux/file.h>
+#include <linux/fs.h>
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/miscdevice.h>
+#include <asm/machdep.h>
+#include <asm/rtas-work-area.h>
+#include <asm/rtas.h>
+#include <uapi/asm/papr-platform-dump.h>
+
+/*
+ * Function-specific return values for ibm,platform-dump, derived

from
+ * PAPR+ v2.13 7.3.3.4.1 "ibm,platform-dump RTAS Call".
+ */
+#define RTAS_IBM_PLATFORM_DUMP_COMPLETE 0 /* Complete
dump retrieved. */
+#define RTAS_IBM_PLATFORM_DUMP_CONTINUE 1 /* Continue
dump */
+#define RTAS_NOT_AUTHORIZED -9002 /* Not Authorized
*/

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +2 -0
+
+#define	RTAS_IBM_PLATFORM_DUMP_START	2 /* Linux status

to start dump */

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +7 -0
+
+/**
+ * struct ibm_platform_dump_params - Parameters (in and out) for
+ *                                              ibm,platform-dump
+ * @work_area:		In: work area buffer for results.
+ * @buf_length:		In: work area buffer length in bytes
+ * @dump_tag_hi:	In: Most-significant 32 bits of a Dump_Tag

representing
+ * an id of the dump being processed.
+ * @dump_tag_lo: In: Least-significant 32 bits of a Dump_Tag
representing
+ * an id of the dump being processed.
+ * @sequence_hi: In: Sequence number in most-significant 32
bits.
+ * Out: Next sequence number in most-
significant 32 bits.
+ * @sequence_lo: In: Sequence number in Least-significant 32
bits
+ * Out: Next sequence number in Least-
significant 32 bits.
+ * @bytes_ret_hi: Out: Bytes written in most-significant 32 bits.
+ * @bytes_ret_lo: Out: Bytes written in Least-significant 32
bits.
+ * @status: Out: RTAS call status.
+ * @list: Maintain the list of dumps are in progress. Can
+ * retrieve multiple dumps with different
dump IDs at
+ * the same time but not with the same dump
ID. This list
+ * is used to determine whether the dump for
the same ID
+ * is in progress.
+ */

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +15 -0
+struct ibm_platform_dump_params {
+	struct rtas_work_area	*work_area;
+	u32			buf_length;
+	u32			dump_tag_hi;
+	u32			dump_tag_lo;
+	u32			sequence_hi;
+	u32			sequence_lo;
+	u32			bytes_ret_hi;
+	u32			bytes_ret_lo;
+	s32			status;
+	struct list_head	list;
+};
+
+/*
+ * Multiple dumps with different dump IDs can be retrieved at the

same
+ * time, but not with dame dump ID. platform_dump_list_mutex and
+ * platform_dump_list are used to prevent this behavior.
+ */

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +5 -0
+static DEFINE_MUTEX(platform_dump_list_mutex);
+static LIST_HEAD(platform_dump_list);
+
+/**
+ * rtas_ibm_platform_dump() - Call ibm,platform-dump to fill a

work area
+ * buffer.
+ * @params: See &struct ibm_platform_dump_params.
+ * @buf_addr: Address of dump buffer (work_area)
+ * @buf_length: Length of the buffer in bytes (min. 1024)
+ *
+ * Calls ibm,platform-dump until it errors or successfully deposits data
+ * into the supplied work area. Handles RTAS retry statuses. Maps RTAS
+ * error statuses to reasonable errno values.
+ *
+ * Can request multiple dumps with different dump IDs at the same time,
+ * but not with the same dump ID which is prevented with the check in
+ * the ioctl code (papr_platform_dump_create_handle()).
+ *
+ * The caller should inspect @params.status to determine whether more
+ * calls are needed to complete the sequence.
+ *
+ * Context: May sleep.
+ * Return: -ve on error, 0 for dump complete and 1 for continue dump
+ */
+static int rtas_ibm_platform_dump(struct ibm_platform_dump_params *params,

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +8 -0
+				phys_addr_t buf_addr, u32 buf_length)
+{
+	u32 rets[4];
+	s32 fwrc;
+	int ret = 0;
+
+	do {
+		fwrc =

rtas_call(rtas_function_token(RTAS_FN_IBM_PLATFORM_DUMP),

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +27 -0
+				6, 5,
+				rets,
+				params->dump_tag_hi,
+				params->dump_tag_lo,
+				params->sequence_hi,
+				params->sequence_lo,
+				buf_addr,
+				buf_length);
+	} while (rtas_busy_delay(fwrc));
+
+	switch (fwrc) {
+	case RTAS_HARDWARE_ERROR:
+		ret = -EIO;
+		break;
+	case RTAS_NOT_AUTHORIZED:
+		ret = -EPERM;
+		break;
+	case RTAS_IBM_PLATFORM_DUMP_CONTINUE:
+	case RTAS_IBM_PLATFORM_DUMP_COMPLETE:
+		params->sequence_hi = rets[0];
+		params->sequence_lo = rets[1];
+		params->bytes_ret_hi = rets[2];
+		params->bytes_ret_lo = rets[3];
+		break;
+	default:
+		ret = -EIO;
+		pr_err_ratelimited("unexpected ibm,platform-dump status

%d\n",

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +11 -0
+				fwrc);
+		break;
+	}
+
+	params->status = fwrc;
+	return ret;
+}
+
+/*
+ * Platform dump is used with multiple RTAS calls to retrieve the
+ * complete dump for the provided dump ID. Once the complete dump

is
+ * retrieved, the hypervisor returns dump complete status (0) for the
+ * last RTAS call and expects the caller issues one more call with
+ * NULL buffer to invalidate the dump so that the hypervisor can remove
+ * the dump.
+ *
+ * After the specific dump is invalidated in the hypervisor, expect the
+ * dump complete status for the new sequence - the user space initiates
+ * new request for the same dump ID.
+ */

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +14 -0
+static ssize_t papr_platform_dump_handle_read(struct file *file,
+		char __user *buf, size_t size, loff_t *off)
+{
+	struct ibm_platform_dump_params *params = file->private_data;
+	u64 total_bytes;
+	s32 fwrc;
+
+	/*
+	 * Dump already completed with the previous read calls.
+	 * In case if the user space issues further reads, returns
+	 * -EINVAL.
+	 */
+	if (!params->buf_length) {
+		pr_warn_once("Platform dump completed for dump ID

%llu\n",

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +8 -0
+			(u64) (((u64)params->dump_tag_hi << 32) |
+				params->dump_tag_lo));
+		return -EINVAL;
+	}
+
+	/*
+	 * The hypervisor returns status 0 if no more data available to
+	 * download. The dump will be invalidated with ioctl (see

below).

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +12 -0
+	 */
+	if (params->status == RTAS_IBM_PLATFORM_DUMP_COMPLETE) {
+		params->buf_length = 0;
+		/*
+		 * Returns 0 to the user space so that user
+		 * space read stops.
+		 */
+		return 0;
+	}
+
+	if (size < SZ_1K) {
+		pr_err_once("Buffer length should be minimum 1024

bytes\n");

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +22 -0
+		return -EINVAL;
+	} else if (size > params->buf_length) {
+		/*
+		 * Allocate 4K work area. So if the user requests > 4K,
+		 * resize the buffer length.
+		 */
+		size = params->buf_length;
+	}
+
+	fwrc = rtas_ibm_platform_dump(params,
+			rtas_work_area_phys(params->work_area),
+			size);
+	if (fwrc < 0)
+		return fwrc;
+
+	total_bytes = (u64) (((u64)params->bytes_ret_hi << 32) |
+			params->bytes_ret_lo);
+
+	/*
+	 * Kernel or firmware bug, do not continue.
+	 */
+	if (WARN(total_bytes > size, "possible write beyond end of work

area"))

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +3 -0
+		return -EFAULT;
+
+	if (copy_to_user(buf, rtas_work_area_raw_buf(params-
Quoted 1 line

work_area),

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +25 -0
+			total_bytes))
+		return -EFAULT;
+
+	return total_bytes;
+}
+
+static int papr_platform_dump_handle_release(struct inode *inode,
+					struct file *file)
+{
+	struct ibm_platform_dump_params *params = file->private_data;
+
+	if (params->work_area)
+		rtas_work_area_free(params->work_area);
+
+	mutex_lock(&platform_dump_list_mutex);
+	list_del(&params->list);
+	mutex_unlock(&platform_dump_list_mutex);
+
+	kfree(params);
+	file->private_data = NULL;
+	return 0;
+}
+
+/*
+ * This ioctl is used to invalidate the dump assuming the user

space
+ * issue this ioctl after obtain the complete dump.
+ * Issue the last RTAS call with NULL buffer to invalidate the dump
+ * which means dump will be freed in the hypervisor.
+ */

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +5 -0
+static long papr_platform_dump_invalidate_ioctl(struct file *file,
+				unsigned int ioctl, unsigned long arg)
+{
+	struct ibm_platform_dump_params *params;
+	u64 __user *argp = (void __user *)arg;

arch/powerpc/platforms/pseries/papr-platform-dump.c line 248

Quoted 10 lines
arch/powerpc/platforms/pseries/papr-platform-dump.c new file +10 -0
+	u64 param_dump_tag, dump_tag;
+
+	if (ioctl != PAPR_PLATFORM_DUMP_IOC_INVALIDATE)
+		return -ENOIOCTLCMD;
+
+	if (get_user(dump_tag, argp))
+		return -EFAULT;
+
+	/*
+	 * private_data is freeded during release(), so should not

freed?

Quoted 18 lines
arch/powerpc/platforms/pseries/papr-platform-dump.c new file +4 -0
+	 * happen.
+	 */
+	if (!file->private_data) {
+		pr_err("No valid FD to invalidate dump for the

ID(%llu)\n",

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +13 -0
+				dump_tag);
+		return -EINVAL;
+	}
+
+	params = file->private_data;
+	param_dump_tag = (u64) (((u64)params->dump_tag_hi << 32) |
+				params->dump_tag_lo);
+	if (dump_tag != param_dump_tag) {
+		pr_err("Invalid dump ID(%llu) to invalidate dump\n",
+				dump_tag);
+		return -EINVAL;
+	}
+
Quoted 8 lines

arch/powerpc/platforms/pseries/papr-platform-dump.c line 269

Quoted 4 lines
arch/powerpc/platforms/pseries/papr-platform-dump.c new file +4 -0
+	if (params->status != RTAS_IBM_PLATFORM_DUMP_COMPLETE)
+		pr_warn("Platform dump is not complete, but requested "
+			"to invalidate dump for ID(%llu)\n",
+			dump_tag);

Not sure if something should be done here or if relying on translation
of the error from the RTAS call is advisable.

This check just diplays message in case if the user initiated to invalidate the dump without saving it completely. Then invalidates the dump with RTAS call and retuns the RTAS return value.

As mentioned above, platform-dump is available only on non-HMC based systems. So invoke the collection of dump by BMC based interface, not widely used. I can remove this check if preferred.

Thanks
Haren

Quoted 4 lines

Thanks

Michal

#9 lore raw

Re: [PATCH v2 6/6] powerpc/pseries: Add papr-platform-dump character driver for dump retrieval

Quoted 457 lines · Haren Myneni
Quoted 8 lines · Michal Suchánek

Hello,

thanks for working on this!

I see in thes version you ended up reusing the existing RTAS call code
which looks better.

Quoted 7 lines

From the past discussion it sounds like the get-indices call can list the available dumps, and I do not see this connection documented.

Also the part about it not being used in practice by the service that retrieves the dumps because it gets a message from the hypervisor with
the dump id when a dump is available.

ibm,get-indices is used to obtain data based on indicator and sensor, not related to platform dump. ibm,platform-dump is used only on non-HMC based systems and BMC interface initiates manually to save the dump on tne partition. Sorry in case caused confusion in the previous discussion.

Quoted 425 lines · Haren Myneni
Quoted 393 lines

ibm,platform-dump RTAS call in combination with writable mapping /dev/mem is issued to collect platform dump from the hypervisor and may need multiple calls to get the complete dump. The current implementation uses rtas_platform_dump() API provided by librtas library to issue these RTAS calls. But /dev/mem access by the user space is prohibited under system lockdown.

The solution should be to restrict access to RTAS function in user space and provide kernel interfaces to collect dump. This patch adds papr-platform-dump character driver and expose standard interfaces such as open / ioctl/ read to user space in ways that are compatible with lockdown.

PAPR (7.3.3.4.1 ibm,platform-dump) provides a method to obtain the complete dump:
- Each dump will be identified by ID called dump tag.
- A sequence of RTAS calls have to be issued until retrieve the complete dump. The hypervisor expects the first RTAS call with the sequence 0 and the subsequent calls with the sequence number returned from the previous calls.
- The hypervisor returns "dump complete" status once the complete dump is retrieved. But expects one more RTAS call from the partition with the NULL buffer to invalidate dump which means the dump will be removed in the hypervisor.
- Sequence of calls are allowed with different dump IDs at the same time but not with the same dump ID.

Expose these interfaces to user space with a /dev/papr-platform- dump
character device using the following programming model:

int devfd = open("/dev/papr-platform-dump", O_RDONLY); int fd = ioctl(devfd,PAPR_PLATFORM_DUMP_IOC_CREATE_HANDLE,
&dump_id)
- Restrict user space to access with the same dump ID. Typically we do not expect user space requests the dump again for the same dump ID.
char *buf = malloc(size);
length = read(fd, buf, size);
- size should be minimum 1K based on PAPR and <= 4K based on RTAS work area size. It will be restrict to RTAS work area size. Using 4K work area based on the current implementation in librtas library
- Each read call issue RTAS call to get the data based on the size requirement and returns bytes returned from the hypervisor
- If the previous call returns dump complete status, the next read returns 0 like EOF.
ret = ioctl(PAPR_PLATFORM_DUMP_IOC_INVALIDATE, &dump_id)
- RTAS call with NULL buffer to invalidates the dump.

The read API should use the file descriptor obtained from ioctl based on dump ID so that gets dump contents for the corresponding dump ID. Implemented support in librtas (rtas_platform_dump()) for this new ABI to support system lockdown.

---
 .../include/uapi/asm/papr-platform-dump.h     |  15 +
 arch/powerpc/platforms/pseries/Makefile       |   1 +
 .../platforms/pseries/papr-platform-dump.c    | 408
++++++++++++++++++
 3 files changed, 424 insertions(+)
 create mode 100644 arch/powerpc/include/uapi/asm/papr-platform-
dump.h
 create mode 100644 arch/powerpc/platforms/pseries/papr-platform-

dump.c

arch/powerpc/include/uapi/asm/papr-platform-dump.h new file +9 -0
new file mode 100644
index 000000000000..3a0f152e3ce8
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+#ifndef _UAPI_PAPR_PLATFORM_DUMP_H_
+#define _UAPI_PAPR_PLATFORM_DUMP_H_
+
+#include <asm/ioctl.h>
+#include <asm/papr-miscdev.h>
+
+/*
+ * ioctl for /dev/papr-platform-dump. Returns a VPD handle fd

corresponding to
+ * the location code.
+ */
+#define PAPR_PLATFORM_DUMP_IOC_CREATE_HANDLE
_IOW(PAPR_MISCDEV_IOC_ID, 6, __u64)
+#define
PAPR_PLATFORM_DUMP_IOC_INVALIDATE _IOW(PAPR_MISCDEV_IOC_ID, 7,
__u64)

arch/powerpc/include/uapi/asm/papr-platform-dump.h new file +2 -0
+
+#endif /* _UAPI_PAPR_PLATFORM_DUMP_H_ */
arch/powerpc/platforms/pseries/Makefile +1 -0
index e1db61877bb9..c82c94e0a73c 100644
@@ -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-rtas-common.o papr-vpd.o papr-indices.o 
 \
+			   papr-platform-dump.o \
 			   setup.o iommu.o event_sources.o ras.o \

firmware.o power.o dlpar.o mobility.o rng.o
\

arch/powerpc/platforms/pseries/Makefile +0 -0
 			   pci.o pci_dlpar.o eeh_pseries.o msi.o \
arch/powerpc/platforms/pseries/papr-platform-dump.c new file +17 -0
new file mode 100644
index 000000000000..13a418d7c37e
@@ -0,0 +1,408 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#define pr_fmt(fmt) "papr-platform-dump: " fmt
+
+#include <linux/anon_inodes.h>
+#include <linux/file.h>
+#include <linux/fs.h>
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/miscdevice.h>
+#include <asm/machdep.h>
+#include <asm/rtas-work-area.h>
+#include <asm/rtas.h>
+#include <uapi/asm/papr-platform-dump.h>
+
+/*
+ * Function-specific return values for ibm,platform-dump, derived

from
+ * PAPR+ v2.13 7.3.3.4.1 "ibm,platform-dump RTAS Call".
+ */
+#define RTAS_IBM_PLATFORM_DUMP_COMPLETE 0 /* Complete
dump retrieved. */
+#define RTAS_IBM_PLATFORM_DUMP_CONTINUE 1 /* Continue
dump */
+#define RTAS_NOT_AUTHORIZED -9002 /* Not Authorized
*/

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +2 -0
+
+#define	RTAS_IBM_PLATFORM_DUMP_START	2 /* Linux status

to start dump */

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +7 -0
+
+/**
+ * struct ibm_platform_dump_params - Parameters (in and out) for
+ *                                              ibm,platform-dump
+ * @work_area:		In: work area buffer for results.
+ * @buf_length:		In: work area buffer length in bytes
+ * @dump_tag_hi:	In: Most-significant 32 bits of a Dump_Tag

representing
+ * an id of the dump being processed.
+ * @dump_tag_lo: In: Least-significant 32 bits of a Dump_Tag
representing
+ * an id of the dump being processed.
+ * @sequence_hi: In: Sequence number in most-significant 32
bits.
+ * Out: Next sequence number in most-
significant 32 bits.
+ * @sequence_lo: In: Sequence number in Least-significant 32
bits
+ * Out: Next sequence number in Least-
significant 32 bits.
+ * @bytes_ret_hi: Out: Bytes written in most-significant 32 bits.
+ * @bytes_ret_lo: Out: Bytes written in Least-significant 32
bits.
+ * @status: Out: RTAS call status.
+ * @list: Maintain the list of dumps are in progress. Can
+ * retrieve multiple dumps with different
dump IDs at
+ * the same time but not with the same dump
ID. This list
+ * is used to determine whether the dump for
the same ID
+ * is in progress.
+ */

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +15 -0
+struct ibm_platform_dump_params {
+	struct rtas_work_area	*work_area;
+	u32			buf_length;
+	u32			dump_tag_hi;
+	u32			dump_tag_lo;
+	u32			sequence_hi;
+	u32			sequence_lo;
+	u32			bytes_ret_hi;
+	u32			bytes_ret_lo;
+	s32			status;
+	struct list_head	list;
+};
+
+/*
+ * Multiple dumps with different dump IDs can be retrieved at the

same
+ * time, but not with dame dump ID. platform_dump_list_mutex and
+ * platform_dump_list are used to prevent this behavior.
+ */

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +5 -0
+static DEFINE_MUTEX(platform_dump_list_mutex);
+static LIST_HEAD(platform_dump_list);
+
+/**
+ * rtas_ibm_platform_dump() - Call ibm,platform-dump to fill a

work area
+ * buffer.
+ * @params: See &struct ibm_platform_dump_params.
+ * @buf_addr: Address of dump buffer (work_area)
+ * @buf_length: Length of the buffer in bytes (min. 1024)
+ *
+ * Calls ibm,platform-dump until it errors or successfully deposits data
+ * into the supplied work area. Handles RTAS retry statuses. Maps RTAS
+ * error statuses to reasonable errno values.
+ *
+ * Can request multiple dumps with different dump IDs at the same time,
+ * but not with the same dump ID which is prevented with the check in
+ * the ioctl code (papr_platform_dump_create_handle()).
+ *
+ * The caller should inspect @params.status to determine whether more
+ * calls are needed to complete the sequence.
+ *
+ * Context: May sleep.
+ * Return: -ve on error, 0 for dump complete and 1 for continue dump
+ */
+static int rtas_ibm_platform_dump(struct ibm_platform_dump_params *params,

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +8 -0
+				phys_addr_t buf_addr, u32 buf_length)
+{
+	u32 rets[4];
+	s32 fwrc;
+	int ret = 0;
+
+	do {
+		fwrc =

rtas_call(rtas_function_token(RTAS_FN_IBM_PLATFORM_DUMP),

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +27 -0
+				6, 5,
+				rets,
+				params->dump_tag_hi,
+				params->dump_tag_lo,
+				params->sequence_hi,
+				params->sequence_lo,
+				buf_addr,
+				buf_length);
+	} while (rtas_busy_delay(fwrc));
+
+	switch (fwrc) {
+	case RTAS_HARDWARE_ERROR:
+		ret = -EIO;
+		break;
+	case RTAS_NOT_AUTHORIZED:
+		ret = -EPERM;
+		break;
+	case RTAS_IBM_PLATFORM_DUMP_CONTINUE:
+	case RTAS_IBM_PLATFORM_DUMP_COMPLETE:
+		params->sequence_hi = rets[0];
+		params->sequence_lo = rets[1];
+		params->bytes_ret_hi = rets[2];
+		params->bytes_ret_lo = rets[3];
+		break;
+	default:
+		ret = -EIO;
+		pr_err_ratelimited("unexpected ibm,platform-dump status

%d\n",

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +11 -0
+				fwrc);
+		break;
+	}
+
+	params->status = fwrc;
+	return ret;
+}
+
+/*
+ * Platform dump is used with multiple RTAS calls to retrieve the
+ * complete dump for the provided dump ID. Once the complete dump

is
+ * retrieved, the hypervisor returns dump complete status (0) for the
+ * last RTAS call and expects the caller issues one more call with
+ * NULL buffer to invalidate the dump so that the hypervisor can remove
+ * the dump.
+ *
+ * After the specific dump is invalidated in the hypervisor, expect the
+ * dump complete status for the new sequence - the user space initiates
+ * new request for the same dump ID.
+ */

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +14 -0
+static ssize_t papr_platform_dump_handle_read(struct file *file,
+		char __user *buf, size_t size, loff_t *off)
+{
+	struct ibm_platform_dump_params *params = file->private_data;
+	u64 total_bytes;
+	s32 fwrc;
+
+	/*
+	 * Dump already completed with the previous read calls.
+	 * In case if the user space issues further reads, returns
+	 * -EINVAL.
+	 */
+	if (!params->buf_length) {
+		pr_warn_once("Platform dump completed for dump ID

%llu\n",

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +8 -0
+			(u64) (((u64)params->dump_tag_hi << 32) |
+				params->dump_tag_lo));
+		return -EINVAL;
+	}
+
+	/*
+	 * The hypervisor returns status 0 if no more data available to
+	 * download. The dump will be invalidated with ioctl (see

below).

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +12 -0
+	 */
+	if (params->status == RTAS_IBM_PLATFORM_DUMP_COMPLETE) {
+		params->buf_length = 0;
+		/*
+		 * Returns 0 to the user space so that user
+		 * space read stops.
+		 */
+		return 0;
+	}
+
+	if (size < SZ_1K) {
+		pr_err_once("Buffer length should be minimum 1024

bytes\n");

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +22 -0
+		return -EINVAL;
+	} else if (size > params->buf_length) {
+		/*
+		 * Allocate 4K work area. So if the user requests > 4K,
+		 * resize the buffer length.
+		 */
+		size = params->buf_length;
+	}
+
+	fwrc = rtas_ibm_platform_dump(params,
+			rtas_work_area_phys(params->work_area),
+			size);
+	if (fwrc < 0)
+		return fwrc;
+
+	total_bytes = (u64) (((u64)params->bytes_ret_hi << 32) |
+			params->bytes_ret_lo);
+
+	/*
+	 * Kernel or firmware bug, do not continue.
+	 */
+	if (WARN(total_bytes > size, "possible write beyond end of work

area"))

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +3 -0
+		return -EFAULT;
+
+	if (copy_to_user(buf, rtas_work_area_raw_buf(params-
Quoted 1 line

work_area),

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +25 -0
+			total_bytes))
+		return -EFAULT;
+
+	return total_bytes;
+}
+
+static int papr_platform_dump_handle_release(struct inode *inode,
+					struct file *file)
+{
+	struct ibm_platform_dump_params *params = file->private_data;
+
+	if (params->work_area)
+		rtas_work_area_free(params->work_area);
+
+	mutex_lock(&platform_dump_list_mutex);
+	list_del(&params->list);
+	mutex_unlock(&platform_dump_list_mutex);
+
+	kfree(params);
+	file->private_data = NULL;
+	return 0;
+}
+
+/*
+ * This ioctl is used to invalidate the dump assuming the user

space
+ * issue this ioctl after obtain the complete dump.
+ * Issue the last RTAS call with NULL buffer to invalidate the dump
+ * which means dump will be freed in the hypervisor.
+ */

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +5 -0
+static long papr_platform_dump_invalidate_ioctl(struct file *file,
+				unsigned int ioctl, unsigned long arg)
+{
+	struct ibm_platform_dump_params *params;
+	u64 __user *argp = (void __user *)arg;

arch/powerpc/platforms/pseries/papr-platform-dump.c line 248

Quoted 10 lines
arch/powerpc/platforms/pseries/papr-platform-dump.c new file +10 -0
+	u64 param_dump_tag, dump_tag;
+
+	if (ioctl != PAPR_PLATFORM_DUMP_IOC_INVALIDATE)
+		return -ENOIOCTLCMD;
+
+	if (get_user(dump_tag, argp))
+		return -EFAULT;
+
+	/*
+	 * private_data is freeded during release(), so should not

freed?

Quoted 18 lines
arch/powerpc/platforms/pseries/papr-platform-dump.c new file +4 -0
+	 * happen.
+	 */
+	if (!file->private_data) {
+		pr_err("No valid FD to invalidate dump for the

ID(%llu)\n",

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +13 -0
+				dump_tag);
+		return -EINVAL;
+	}
+
+	params = file->private_data;
+	param_dump_tag = (u64) (((u64)params->dump_tag_hi << 32) |
+				params->dump_tag_lo);
+	if (dump_tag != param_dump_tag) {
+		pr_err("Invalid dump ID(%llu) to invalidate dump\n",
+				dump_tag);
+		return -EINVAL;
+	}
+
Quoted 8 lines

arch/powerpc/platforms/pseries/papr-platform-dump.c line 269

Quoted 4 lines
arch/powerpc/platforms/pseries/papr-platform-dump.c new file +4 -0
+	if (params->status != RTAS_IBM_PLATFORM_DUMP_COMPLETE)
+		pr_warn("Platform dump is not complete, but requested "
+			"to invalidate dump for ID(%llu)\n",
+			dump_tag);

Not sure if something should be done here or if relying on translation
of the error from the RTAS call is advisable.

Quoted 7 lines

This check just diplays message in case if the user initiated to invalidate the dump without saving it completely. Then invalidates the dump with RTAS call and retuns the RTAS return value.

As mentioned above, platform-dump is available only on non-HMC based systems. So invoke the collection of dump by BMC based interface, not widely used. I can remove this check if preferred.

From the previous discussion it sounds like trying to invalidate the dump without first reading it in full is an error.

The state to detect this error is tracked which makes it possible to produce this warning.

Then it's also possible to handle the error without roundtrip to the hypervisor.

Thanks

Michal

Quoted 9 lines

Thanks
Haren

Quoted 4 lines

Thanks

Michal

#10 lore raw

Re: [PATCH v2 6/6] powerpc/pseries: Add papr-platform-dump character driver for dump retrieval

Quoted 545 lines · Michal Suchánek
Quoted 544 lines · Haren Myneni
Quoted 12 lines · Michal Suchánek

Hello,

thanks for working on this!

I see in thes version you ended up reusing the existing RTAS call code
which looks better.

From the past discussion it sounds like the get-indices call can list
the available dumps, and I do not see this connection documented.

Quoted 5 lines

Also the part about it not being used in practice by the service that
retrieves the dumps because it gets a message from the hypervisor with
the dump id when a dump is available.

ibm,get-indices is used to obtain data based on indicator and sensor,
not related to platform dump. ibm,platform-dump is used only on non-HMC
based systems and BMC interface initiates manually to save the dump on
tne partition. Sorry in case caused confusion in the previous discussion.

Quoted 502 lines · Haren Myneni
Quoted 477 lines

ibm,platform-dump RTAS call in combination with writable mapping
/dev/mem is issued to collect platform dump from the hypervisor and may need multiple calls to get the complete dump. The current
implementation uses rtas_platform_dump() API provided by librtas
library to issue these RTAS calls. But /dev/mem access by the user space is prohibited under system lockdown.

The solution should be to restrict access to RTAS function in user
space and provide kernel interfaces to collect dump. This patch adds papr-platform-dump character driver and expose standard interfaces such as open / ioctl/ read to user space in ways that
are compatible with lockdown.

PAPR (7.3.3.4.1 ibm,platform-dump) provides a method to obtain the complete dump:
- Each dump will be identified by ID called dump tag.
- A sequence of RTAS calls have to be issued until retrieve the complete dump. The hypervisor expects the first RTAS call
with
the sequence 0 and the subsequent calls with the sequence number returned from the previous calls.
- The hypervisor returns "dump complete" status once the complete
dump is retrieved. But expects one more RTAS call from the partition with the NULL buffer to invalidate dump which means the dump will be removed in the hypervisor.
- Sequence of calls are allowed with different dump IDs at the same time but not with the same dump ID.

Expose these interfaces to user space with a /dev/papr- platform-
dump
character device using the following programming model:

int devfd = open("/dev/papr-platform-dump", O_RDONLY); int fd = ioctl(devfd,PAPR_PLATFORM_DUMP_IOC_CREATE_HANDLE,
&dump_id)
- Restrict user space to access with the same dump ID. Typically we do not expect user space requests the
dump
again for the same dump ID.
char *buf = malloc(size);
length = read(fd, buf, size);
- size should be minimum 1K based on PAPR and <= 4K
based
on RTAS work area size. It will be restrict to RTAS
work
area size. Using 4K work area based on the current implementation in librtas library
- Each read call issue RTAS call to get the data based
on
the size requirement and returns bytes returned from
the
hypervisor
- If the previous call returns dump complete status,
the
next read returns 0 like EOF.
ret = ioctl(PAPR_PLATFORM_DUMP_IOC_INVALIDATE, &dump_id)
- RTAS call with NULL buffer to invalidates the dump.

The read API should use the file descriptor obtained from ioctl based on dump ID so that gets dump contents for the corresponding
dump ID. Implemented support in librtas (rtas_platform_dump()) for
this new ABI to support system lockdown.

---
 .../include/uapi/asm/papr-platform-dump.h     |  15 +
 arch/powerpc/platforms/pseries/Makefile       |   1 +
 .../platforms/pseries/papr-platform-dump.c    | 408
++++++++++++++++++
 3 files changed, 424 insertions(+)
 create mode 100644 arch/powerpc/include/uapi/asm/papr-
platform-
dump.h
 create mode 100644 arch/powerpc/platforms/pseries/papr-

platform-
dump.c

arch/powerpc/include/uapi/asm/papr-platform-dump.h new file +9 -0
new file mode 100644
index 000000000000..3a0f152e3ce8
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+#ifndef _UAPI_PAPR_PLATFORM_DUMP_H_
+#define _UAPI_PAPR_PLATFORM_DUMP_H_
+
+#include <asm/ioctl.h>
+#include <asm/papr-miscdev.h>
+
+/*
+ * ioctl for /dev/papr-platform-dump. Returns a VPD handle fd

corresponding to
+ * the location code.
+ */
+#define PAPR_PLATFORM_DUMP_IOC_CREATE_HANDLE
_IOW(PAPR_MISCDEV_IOC_ID, 6, __u64)
+#define
PAPR_PLATFORM_DUMP_IOC_INVALIDATE _IOW(PAPR_MISCDEV_IOC_ID,
7,
__u64)

arch/powerpc/include/uapi/asm/papr-platform-dump.h new file +2 -0
+
+#endif /* _UAPI_PAPR_PLATFORM_DUMP_H_ */
arch/powerpc/platforms/pseries/Makefile +0 -1
index e1db61877bb9..c82c94e0a73c 100644
@@ -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-rtas-common.o papr-vpd.o papr-
indices.o
\

arch/powerpc/platforms/pseries/Makefile +1 -0
+			   papr-platform-dump.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 \

(quoted diff) +0 -0

dump.c b/arch/powerpc/platforms/pseries/papr-platform-dump.c new file mode 100644
index 000000000000..13a418d7c37e

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +17 -0
@@ -0,0 +1,408 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#define pr_fmt(fmt) "papr-platform-dump: " fmt
+
+#include <linux/anon_inodes.h>
+#include <linux/file.h>
+#include <linux/fs.h>
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/miscdevice.h>
+#include <asm/machdep.h>
+#include <asm/rtas-work-area.h>
+#include <asm/rtas.h>
+#include <uapi/asm/papr-platform-dump.h>
+
+/*
+ * Function-specific return values for ibm,platform-dump,

derived
from
+ * PAPR+ v2.13 7.3.3.4.1 "ibm,platform-dump RTAS Call".
+ */
+#define RTAS_IBM_PLATFORM_DUMP_COMPLETE 0 /*
Complete
dump retrieved. */
+#define RTAS_IBM_PLATFORM_DUMP_CONTINUE 1 /*
Continue
dump */
+#define RTAS_NOT_AUTHORIZED -9002 /* Not
Authorized
*/

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +2 -0
+
+#define	RTAS_IBM_PLATFORM_DUMP_START	2 /* Linux

status
to start dump */

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +3 -0
+
+/**
+ * struct ibm_platform_dump_params - Parameters (in and out)

for
+ * ibm,platform-
dump
+ * @work_area: In: work area buffer for results.
+ * @buf_length: In: work area buffer length in
bytes
+ * @dump_tag_hi: In: Most-significant 32 bits of a
Dump_Tag
representing
+ * an id of the dump being processed.
+ * @dump_tag_lo: In: Least-significant 32 bits of a
Dump_Tag
representing
+ * an id of the dump being processed.
+ * @sequence_hi: In: Sequence number in most-significant
32
bits.
+ * Out: Next sequence number in most-
significant 32 bits.
+ * @sequence_lo: In: Sequence number in Least-
significant 32
bits
+ * Out: Next sequence number in Least-
significant 32 bits.
+ * @bytes_ret_hi: Out: Bytes written in most-significant
32 bits.
+ * @bytes_ret_lo: Out: Bytes written in Least-significant
32
bits.
+ * @status: Out: RTAS call status.
+ * @list: Maintain the list of dumps are in
progress. Can
+ * retrieve multiple dumps with different
dump IDs at
+ * the same time but not with the same
dump
ID. This list
+ * is used to determine whether the dump
for
the same ID
+ * is in progress.
+ */

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +15 -0
+struct ibm_platform_dump_params {
+	struct rtas_work_area	*work_area;
+	u32			buf_length;
+	u32			dump_tag_hi;
+	u32			dump_tag_lo;
+	u32			sequence_hi;
+	u32			sequence_lo;
+	u32			bytes_ret_hi;
+	u32			bytes_ret_lo;
+	s32			status;
+	struct list_head	list;
+};
+
+/*
+ * Multiple dumps with different dump IDs can be retrieved at

the
same
+ * time, but not with dame dump ID. platform_dump_list_mutex and
+ * platform_dump_list are used to prevent this behavior.
+ */

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +5 -0
+static DEFINE_MUTEX(platform_dump_list_mutex);
+static LIST_HEAD(platform_dump_list);
+
+/**
+ * rtas_ibm_platform_dump() - Call ibm,platform-dump to fill a

work area
+ * buffer.
+ * @params: See &struct ibm_platform_dump_params.
+ * @buf_addr: Address of dump buffer (work_area)
+ * @buf_length: Length of the buffer in bytes (min. 1024)
+ *
+ * Calls ibm,platform-dump until it errors or successfully deposits data
+ * into the supplied work area. Handles RTAS retry statuses. Maps
RTAS
+ * error statuses to reasonable errno values.
+ *
+ * Can request multiple dumps with different dump IDs at the same
time,
+ * but not with the same dump ID which is prevented with the check
in
+ * the ioctl code (papr_platform_dump_create_handle()).
+ *
+ * The caller should inspect @params.status to determine whether
more
+ * calls are needed to complete the sequence.
+ *
+ * Context: May sleep.
+ * Return: -ve on error, 0 for dump complete and 1 for continue
dump
+ */
+static int rtas_ibm_platform_dump(struct ibm_platform_dump_params
*params,

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +1 -0
+				phys_addr_t buf_addr, u32

buf_length)

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +7 -0
+{
+	u32 rets[4];
+	s32 fwrc;
+	int ret = 0;
+
+	do {
+		fwrc =

rtas_call(rtas_function_token(RTAS_FN_IBM_PLATFORM_DUMP),

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +27 -0
+				6, 5,
+				rets,
+				params->dump_tag_hi,
+				params->dump_tag_lo,
+				params->sequence_hi,
+				params->sequence_lo,
+				buf_addr,
+				buf_length);
+	} while (rtas_busy_delay(fwrc));
+
+	switch (fwrc) {
+	case RTAS_HARDWARE_ERROR:
+		ret = -EIO;
+		break;
+	case RTAS_NOT_AUTHORIZED:
+		ret = -EPERM;
+		break;
+	case RTAS_IBM_PLATFORM_DUMP_CONTINUE:
+	case RTAS_IBM_PLATFORM_DUMP_COMPLETE:
+		params->sequence_hi = rets[0];
+		params->sequence_lo = rets[1];
+		params->bytes_ret_hi = rets[2];
+		params->bytes_ret_lo = rets[3];
+		break;
+	default:
+		ret = -EIO;
+		pr_err_ratelimited("unexpected ibm,platform-

dump status
%d\n",

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +10 -0
+				fwrc);
+		break;
+	}
+
+	params->status = fwrc;
+	return ret;
+}
+
+/*
+ * Platform dump is used with multiple RTAS calls to retrieve

the
+ * complete dump for the provided dump ID. Once the complete dump
is
+ * retrieved, the hypervisor returns dump complete status (0) for
the
+ * last RTAS call and expects the caller issues one more call with
+ * NULL buffer to invalidate the dump so that the hypervisor can
remove
+ * the dump.
+ *
+ * After the specific dump is invalidated in the hypervisor, expect the
+ * dump complete status for the new sequence - the user space initiates
+ * new request for the same dump ID.
+ */
+static ssize_t papr_platform_dump_handle_read(struct file *file,

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +3 -0
+		char __user *buf, size_t size, loff_t *off)
+{
+	struct ibm_platform_dump_params *params = file-
Quoted 1 line

private_data;

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +6 -0
+	u64 total_bytes;
+	s32 fwrc;
+
+	/*
+	 * Dump already completed with the previous read calls.
+	 * In case if the user space issues further reads,

returns

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +4 -0
+	 * -EINVAL.
+	 */
+	if (!params->buf_length) {
+		pr_warn_once("Platform dump completed for dump

ID
%llu\n",

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +1 -0
+			(u64) (((u64)params->dump_tag_hi << 32)

|

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +6 -0
+				params->dump_tag_lo));
+		return -EINVAL;
+	}
+
+	/*
+	 * The hypervisor returns status 0 if no more data

available to

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +1 -0
+	 * download. The dump will be invalidated with ioctl

(see
below).

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +2 -0
+	 */
+	if (params->status == RTAS_IBM_PLATFORM_DUMP_COMPLETE)

{

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +10 -0
+		params->buf_length = 0;
+		/*
+		 * Returns 0 to the user space so that user
+		 * space read stops.
+		 */
+		return 0;
+	}
+
+	if (size < SZ_1K) {
+		pr_err_once("Buffer length should be minimum

1024
bytes\n");

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +4 -0
+		return -EINVAL;
+	} else if (size > params->buf_length) {
+		/*
+		 * Allocate 4K work area. So if the user

requests > 4K,

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +12 -0
+		 * resize the buffer length.
+		 */
+		size = params->buf_length;
+	}
+
+	fwrc = rtas_ibm_platform_dump(params,
+			rtas_work_area_phys(params->work_area),
+			size);
+	if (fwrc < 0)
+		return fwrc;
+
+	total_bytes = (u64) (((u64)params->bytes_ret_hi << 32)

|

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +6 -0
+			params->bytes_ret_lo);
+
+	/*
+	 * Kernel or firmware bug, do not continue.
+	 */
+	if (WARN(total_bytes > size, "possible write beyond end

of work
area"))

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +3 -0
+		return -EFAULT;
+
+	if (copy_to_user(buf, rtas_work_area_raw_buf(params-
Quoted 1 line

work_area),

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +7 -0
+			total_bytes))
+		return -EFAULT;
+
+	return total_bytes;
+}
+
+static int papr_platform_dump_handle_release(struct inode

*inode,

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +3 -0
+					struct file *file)
+{
+	struct ibm_platform_dump_params *params = file-
Quoted 1 line

private_data;

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +15 -0
+
+	if (params->work_area)
+		rtas_work_area_free(params->work_area);
+
+	mutex_lock(&platform_dump_list_mutex);
+	list_del(&params->list);
+	mutex_unlock(&platform_dump_list_mutex);
+
+	kfree(params);
+	file->private_data = NULL;
+	return 0;
+}
+
+/*
+ * This ioctl is used to invalidate the dump assuming the user

space
+ * issue this ioctl after obtain the complete dump.
+ * Issue the last RTAS call with NULL buffer to invalidate the dump
+ * which means dump will be freed in the hypervisor.
+ */
+static long papr_platform_dump_invalidate_ioctl(struct file *file,

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +1 -0
+				unsigned int ioctl, unsigned

long arg)

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +13 -0
+{
+	struct ibm_platform_dump_params *params;
+	u64 __user *argp = (void __user *)arg;
+	u64 param_dump_tag, dump_tag;
+
+	if (ioctl != PAPR_PLATFORM_DUMP_IOC_INVALIDATE)
+		return -ENOIOCTLCMD;
+
+	if (get_user(dump_tag, argp))
+		return -EFAULT;
+
+	/*
+	 * private_data is freeded during release(), so should
Quoted 1 line

not

freed?

Quoted 21 lines
arch/powerpc/platforms/pseries/papr-platform-dump.c new file +4 -0
+	 * happen.
+	 */
+	if (!file->private_data) {
+		pr_err("No valid FD to invalidate dump for the

ID(%llu)\n",

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +6 -0
+				dump_tag);
+		return -EINVAL;
+	}
+
+	params = file->private_data;
+	param_dump_tag = (u64) (((u64)params->dump_tag_hi <<

32) |

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +3 -0
+				params->dump_tag_lo);
+	if (dump_tag != param_dump_tag) {
+		pr_err("Invalid dump ID(%llu) to invalidate

dump\n",

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +5 -0
+				dump_tag);
+		return -EINVAL;
+	}
+
+	if (params->status != RTAS_IBM_PLATFORM_DUMP_COMPLETE)
Quoted 8 lines

arch/powerpc/platforms/pseries/papr-platform-dump.c

Quoted 4 lines
arch/powerpc/platforms/pseries/papr-platform-dump.c new file +1 -0
+		pr_warn("Platform dump is not complete, but

requested "

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +2 -0
+			"to invalidate dump for ID(%llu)\n",
+			dump_tag);

Not sure if something should be done here or if relying on translation
of the error from the RTAS call is advisable.

This check just diplays message in case if the user initiated to invalidate the dump without saving it completely. Then invalidates the
dump with RTAS call and retuns the RTAS return value.

Quoted 8 lines
Quoted 5 lines

As mentioned above, platform-dump is available only on non-HMC based
systems. So invoke the collection of dump by BMC based interface, not
widely used. I can remove this check if preferred.

From the previous discussion it sounds like trying to invalidate the dump without first reading it in full is an error.

Thanks for your suggestions.

Yes, it was doing as part of read() calls. But explicit ioctl to invalidate here. I was thinking like user space removing FD without reading or writing operation.

Quoted 6 lines

The state to detect this error is tracked which makes it possible to produce this warning.

Then it's also possible to handle the error without roundtrip to the hypervisor.

Do you prefer return en error without invalidating if the dump is not read completely? Sure we can.

if (params->status != RTAS_IBM_PLATFORM_DUMP_COMPLETE) {
	pr_err("Platform dump is not complete, but requested "
		"to invalidate dump for ID(%llu)\n",
		dump_tag);
	return -EPERM;
}

Thanks
Haren

Quoted 11 lines

Thanks

Michal

Quoted 6 lines

Thanks
Haren

Quoted 3 lines

Thanks

Michal

#11 lore raw

Re: [PATCH v2 6/6] powerpc/pseries: Add papr-platform-dump character driver for dump retrieval

Quoted 552 lines · Haren Myneni
Quoted 551 lines · Michal Suchánek
Quoted 549 lines · Haren Myneni
Quoted 12 lines · Michal Suchánek

Hello,

thanks for working on this!

I see in thes version you ended up reusing the existing RTAS call code
which looks better.

From the past discussion it sounds like the get-indices call can list
the available dumps, and I do not see this connection documented.

Quoted 5 lines

Also the part about it not being used in practice by the service that
retrieves the dumps because it gets a message from the hypervisor with
the dump id when a dump is available.

ibm,get-indices is used to obtain data based on indicator and sensor,
not related to platform dump. ibm,platform-dump is used only on non-HMC
based systems and BMC interface initiates manually to save the dump on
tne partition. Sorry in case caused confusion in the previous discussion.

Quoted 502 lines · Haren Myneni
Quoted 477 lines

ibm,platform-dump RTAS call in combination with writable mapping
/dev/mem is issued to collect platform dump from the hypervisor and may need multiple calls to get the complete dump. The current
implementation uses rtas_platform_dump() API provided by librtas
library to issue these RTAS calls. But /dev/mem access by the user space is prohibited under system lockdown.

The solution should be to restrict access to RTAS function in user
space and provide kernel interfaces to collect dump. This patch adds papr-platform-dump character driver and expose standard interfaces such as open / ioctl/ read to user space in ways that
are compatible with lockdown.

PAPR (7.3.3.4.1 ibm,platform-dump) provides a method to obtain the complete dump:
- Each dump will be identified by ID called dump tag.
- A sequence of RTAS calls have to be issued until retrieve the complete dump. The hypervisor expects the first RTAS call
with
the sequence 0 and the subsequent calls with the sequence number returned from the previous calls.
- The hypervisor returns "dump complete" status once the complete
dump is retrieved. But expects one more RTAS call from the partition with the NULL buffer to invalidate dump which means the dump will be removed in the hypervisor.
- Sequence of calls are allowed with different dump IDs at the same time but not with the same dump ID.

Expose these interfaces to user space with a /dev/papr- platform-
dump
character device using the following programming model:

int devfd = open("/dev/papr-platform-dump", O_RDONLY); int fd = ioctl(devfd,PAPR_PLATFORM_DUMP_IOC_CREATE_HANDLE,
&dump_id)
- Restrict user space to access with the same dump ID. Typically we do not expect user space requests the
dump
again for the same dump ID.
char *buf = malloc(size);
length = read(fd, buf, size);
- size should be minimum 1K based on PAPR and <= 4K
based
on RTAS work area size. It will be restrict to RTAS
work
area size. Using 4K work area based on the current implementation in librtas library
- Each read call issue RTAS call to get the data based
on
the size requirement and returns bytes returned from
the
hypervisor
- If the previous call returns dump complete status,
the
next read returns 0 like EOF.
ret = ioctl(PAPR_PLATFORM_DUMP_IOC_INVALIDATE, &dump_id)
- RTAS call with NULL buffer to invalidates the dump.

The read API should use the file descriptor obtained from ioctl based on dump ID so that gets dump contents for the corresponding
dump ID. Implemented support in librtas (rtas_platform_dump()) for
this new ABI to support system lockdown.

---
 .../include/uapi/asm/papr-platform-dump.h     |  15 +
 arch/powerpc/platforms/pseries/Makefile       |   1 +
 .../platforms/pseries/papr-platform-dump.c    | 408
++++++++++++++++++
 3 files changed, 424 insertions(+)
 create mode 100644 arch/powerpc/include/uapi/asm/papr-
platform-
dump.h
 create mode 100644 arch/powerpc/platforms/pseries/papr-

platform-
dump.c

arch/powerpc/include/uapi/asm/papr-platform-dump.h new file +9 -0
new file mode 100644
index 000000000000..3a0f152e3ce8
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+#ifndef _UAPI_PAPR_PLATFORM_DUMP_H_
+#define _UAPI_PAPR_PLATFORM_DUMP_H_
+
+#include <asm/ioctl.h>
+#include <asm/papr-miscdev.h>
+
+/*
+ * ioctl for /dev/papr-platform-dump. Returns a VPD handle fd

corresponding to
+ * the location code.
+ */
+#define PAPR_PLATFORM_DUMP_IOC_CREATE_HANDLE
_IOW(PAPR_MISCDEV_IOC_ID, 6, __u64)
+#define
PAPR_PLATFORM_DUMP_IOC_INVALIDATE _IOW(PAPR_MISCDEV_IOC_ID,
7,
__u64)

arch/powerpc/include/uapi/asm/papr-platform-dump.h new file +2 -0
+
+#endif /* _UAPI_PAPR_PLATFORM_DUMP_H_ */
arch/powerpc/platforms/pseries/Makefile +0 -1
index e1db61877bb9..c82c94e0a73c 100644
@@ -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-rtas-common.o papr-vpd.o papr-
indices.o
\

arch/powerpc/platforms/pseries/Makefile +1 -0
+			   papr-platform-dump.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 \

(quoted diff) +0 -0

dump.c b/arch/powerpc/platforms/pseries/papr-platform-dump.c new file mode 100644
index 000000000000..13a418d7c37e

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +17 -0
@@ -0,0 +1,408 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#define pr_fmt(fmt) "papr-platform-dump: " fmt
+
+#include <linux/anon_inodes.h>
+#include <linux/file.h>
+#include <linux/fs.h>
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/miscdevice.h>
+#include <asm/machdep.h>
+#include <asm/rtas-work-area.h>
+#include <asm/rtas.h>
+#include <uapi/asm/papr-platform-dump.h>
+
+/*
+ * Function-specific return values for ibm,platform-dump,

derived
from
+ * PAPR+ v2.13 7.3.3.4.1 "ibm,platform-dump RTAS Call".
+ */
+#define RTAS_IBM_PLATFORM_DUMP_COMPLETE 0 /*
Complete
dump retrieved. */
+#define RTAS_IBM_PLATFORM_DUMP_CONTINUE 1 /*
Continue
dump */
+#define RTAS_NOT_AUTHORIZED -9002 /* Not
Authorized
*/

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +2 -0
+
+#define	RTAS_IBM_PLATFORM_DUMP_START	2 /* Linux

status
to start dump */

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +3 -0
+
+/**
+ * struct ibm_platform_dump_params - Parameters (in and out)

for
+ * ibm,platform-
dump
+ * @work_area: In: work area buffer for results.
+ * @buf_length: In: work area buffer length in
bytes
+ * @dump_tag_hi: In: Most-significant 32 bits of a
Dump_Tag
representing
+ * an id of the dump being processed.
+ * @dump_tag_lo: In: Least-significant 32 bits of a
Dump_Tag
representing
+ * an id of the dump being processed.
+ * @sequence_hi: In: Sequence number in most-significant
32
bits.
+ * Out: Next sequence number in most-
significant 32 bits.
+ * @sequence_lo: In: Sequence number in Least-
significant 32
bits
+ * Out: Next sequence number in Least-
significant 32 bits.
+ * @bytes_ret_hi: Out: Bytes written in most-significant
32 bits.
+ * @bytes_ret_lo: Out: Bytes written in Least-significant
32
bits.
+ * @status: Out: RTAS call status.
+ * @list: Maintain the list of dumps are in
progress. Can
+ * retrieve multiple dumps with different
dump IDs at
+ * the same time but not with the same
dump
ID. This list
+ * is used to determine whether the dump
for
the same ID
+ * is in progress.
+ */

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +15 -0
+struct ibm_platform_dump_params {
+	struct rtas_work_area	*work_area;
+	u32			buf_length;
+	u32			dump_tag_hi;
+	u32			dump_tag_lo;
+	u32			sequence_hi;
+	u32			sequence_lo;
+	u32			bytes_ret_hi;
+	u32			bytes_ret_lo;
+	s32			status;
+	struct list_head	list;
+};
+
+/*
+ * Multiple dumps with different dump IDs can be retrieved at

the
same
+ * time, but not with dame dump ID. platform_dump_list_mutex and
+ * platform_dump_list are used to prevent this behavior.
+ */

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +5 -0
+static DEFINE_MUTEX(platform_dump_list_mutex);
+static LIST_HEAD(platform_dump_list);
+
+/**
+ * rtas_ibm_platform_dump() - Call ibm,platform-dump to fill a

work area
+ * buffer.
+ * @params: See &struct ibm_platform_dump_params.
+ * @buf_addr: Address of dump buffer (work_area)
+ * @buf_length: Length of the buffer in bytes (min. 1024)
+ *
+ * Calls ibm,platform-dump until it errors or successfully deposits data
+ * into the supplied work area. Handles RTAS retry statuses. Maps
RTAS
+ * error statuses to reasonable errno values.
+ *
+ * Can request multiple dumps with different dump IDs at the same
time,
+ * but not with the same dump ID which is prevented with the check
in
+ * the ioctl code (papr_platform_dump_create_handle()).
+ *
+ * The caller should inspect @params.status to determine whether
more
+ * calls are needed to complete the sequence.
+ *
+ * Context: May sleep.
+ * Return: -ve on error, 0 for dump complete and 1 for continue
dump
+ */
+static int rtas_ibm_platform_dump(struct ibm_platform_dump_params
*params,

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +1 -0
+				phys_addr_t buf_addr, u32

buf_length)

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +7 -0
+{
+	u32 rets[4];
+	s32 fwrc;
+	int ret = 0;
+
+	do {
+		fwrc =

rtas_call(rtas_function_token(RTAS_FN_IBM_PLATFORM_DUMP),

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +27 -0
+				6, 5,
+				rets,
+				params->dump_tag_hi,
+				params->dump_tag_lo,
+				params->sequence_hi,
+				params->sequence_lo,
+				buf_addr,
+				buf_length);
+	} while (rtas_busy_delay(fwrc));
+
+	switch (fwrc) {
+	case RTAS_HARDWARE_ERROR:
+		ret = -EIO;
+		break;
+	case RTAS_NOT_AUTHORIZED:
+		ret = -EPERM;
+		break;
+	case RTAS_IBM_PLATFORM_DUMP_CONTINUE:
+	case RTAS_IBM_PLATFORM_DUMP_COMPLETE:
+		params->sequence_hi = rets[0];
+		params->sequence_lo = rets[1];
+		params->bytes_ret_hi = rets[2];
+		params->bytes_ret_lo = rets[3];
+		break;
+	default:
+		ret = -EIO;
+		pr_err_ratelimited("unexpected ibm,platform-

dump status
%d\n",

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +10 -0
+				fwrc);
+		break;
+	}
+
+	params->status = fwrc;
+	return ret;
+}
+
+/*
+ * Platform dump is used with multiple RTAS calls to retrieve

the
+ * complete dump for the provided dump ID. Once the complete dump
is
+ * retrieved, the hypervisor returns dump complete status (0) for
the
+ * last RTAS call and expects the caller issues one more call with
+ * NULL buffer to invalidate the dump so that the hypervisor can
remove
+ * the dump.
+ *
+ * After the specific dump is invalidated in the hypervisor, expect the
+ * dump complete status for the new sequence - the user space initiates
+ * new request for the same dump ID.
+ */
+static ssize_t papr_platform_dump_handle_read(struct file *file,

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +3 -0
+		char __user *buf, size_t size, loff_t *off)
+{
+	struct ibm_platform_dump_params *params = file-
Quoted 1 line

private_data;

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +6 -0
+	u64 total_bytes;
+	s32 fwrc;
+
+	/*
+	 * Dump already completed with the previous read calls.
+	 * In case if the user space issues further reads,

returns

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +4 -0
+	 * -EINVAL.
+	 */
+	if (!params->buf_length) {
+		pr_warn_once("Platform dump completed for dump

ID
%llu\n",

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +1 -0
+			(u64) (((u64)params->dump_tag_hi << 32)

|

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +6 -0
+				params->dump_tag_lo));
+		return -EINVAL;
+	}
+
+	/*
+	 * The hypervisor returns status 0 if no more data

available to

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +1 -0
+	 * download. The dump will be invalidated with ioctl

(see
below).

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +2 -0
+	 */
+	if (params->status == RTAS_IBM_PLATFORM_DUMP_COMPLETE)

{

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +10 -0
+		params->buf_length = 0;
+		/*
+		 * Returns 0 to the user space so that user
+		 * space read stops.
+		 */
+		return 0;
+	}
+
+	if (size < SZ_1K) {
+		pr_err_once("Buffer length should be minimum

1024
bytes\n");

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +4 -0
+		return -EINVAL;
+	} else if (size > params->buf_length) {
+		/*
+		 * Allocate 4K work area. So if the user

requests > 4K,

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +12 -0
+		 * resize the buffer length.
+		 */
+		size = params->buf_length;
+	}
+
+	fwrc = rtas_ibm_platform_dump(params,
+			rtas_work_area_phys(params->work_area),
+			size);
+	if (fwrc < 0)
+		return fwrc;
+
+	total_bytes = (u64) (((u64)params->bytes_ret_hi << 32)

|

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +6 -0
+			params->bytes_ret_lo);
+
+	/*
+	 * Kernel or firmware bug, do not continue.
+	 */
+	if (WARN(total_bytes > size, "possible write beyond end

of work
area"))

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +3 -0
+		return -EFAULT;
+
+	if (copy_to_user(buf, rtas_work_area_raw_buf(params-
Quoted 1 line

work_area),

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +7 -0
+			total_bytes))
+		return -EFAULT;
+
+	return total_bytes;
+}
+
+static int papr_platform_dump_handle_release(struct inode

*inode,

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +3 -0
+					struct file *file)
+{
+	struct ibm_platform_dump_params *params = file-
Quoted 1 line

private_data;

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +15 -0
+
+	if (params->work_area)
+		rtas_work_area_free(params->work_area);
+
+	mutex_lock(&platform_dump_list_mutex);
+	list_del(&params->list);
+	mutex_unlock(&platform_dump_list_mutex);
+
+	kfree(params);
+	file->private_data = NULL;
+	return 0;
+}
+
+/*
+ * This ioctl is used to invalidate the dump assuming the user

space
+ * issue this ioctl after obtain the complete dump.
+ * Issue the last RTAS call with NULL buffer to invalidate the dump
+ * which means dump will be freed in the hypervisor.
+ */
+static long papr_platform_dump_invalidate_ioctl(struct file *file,

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +1 -0
+				unsigned int ioctl, unsigned

long arg)

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +13 -0
+{
+	struct ibm_platform_dump_params *params;
+	u64 __user *argp = (void __user *)arg;
+	u64 param_dump_tag, dump_tag;
+
+	if (ioctl != PAPR_PLATFORM_DUMP_IOC_INVALIDATE)
+		return -ENOIOCTLCMD;
+
+	if (get_user(dump_tag, argp))
+		return -EFAULT;
+
+	/*
+	 * private_data is freeded during release(), so should
Quoted 1 line

not

freed?

Quoted 21 lines
arch/powerpc/platforms/pseries/papr-platform-dump.c new file +4 -0
+	 * happen.
+	 */
+	if (!file->private_data) {
+		pr_err("No valid FD to invalidate dump for the

ID(%llu)\n",

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +6 -0
+				dump_tag);
+		return -EINVAL;
+	}
+
+	params = file->private_data;
+	param_dump_tag = (u64) (((u64)params->dump_tag_hi <<

32) |

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +3 -0
+				params->dump_tag_lo);
+	if (dump_tag != param_dump_tag) {
+		pr_err("Invalid dump ID(%llu) to invalidate

dump\n",

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +5 -0
+				dump_tag);
+		return -EINVAL;
+	}
+
+	if (params->status != RTAS_IBM_PLATFORM_DUMP_COMPLETE)
Quoted 8 lines

arch/powerpc/platforms/pseries/papr-platform-dump.c

Quoted 4 lines
arch/powerpc/platforms/pseries/papr-platform-dump.c new file +1 -0
+		pr_warn("Platform dump is not complete, but

requested "

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +2 -0
+			"to invalidate dump for ID(%llu)\n",
+			dump_tag);

Not sure if something should be done here or if relying on translation
of the error from the RTAS call is advisable.

This check just diplays message in case if the user initiated to invalidate the dump without saving it completely. Then invalidates the
dump with RTAS call and retuns the RTAS return value.

As mentioned above, platform-dump is available only on non-HMC based
systems. So invoke the collection of dump by BMC based interface, not
widely used. I can remove this check if preferred.

Quoted 8 lines
Quoted 2 lines

From the previous discussion it sounds like trying to invalidate the dump without first reading it in full is an error.

Thanks for your suggestions.

Yes, it was doing as part of read() calls. But explicit ioctl to invalidate here. I was thinking like user space removing FD without reading or writing operation.

And is it possible to invalidate the dump without reading it fully first?

If not then there is no point trying to do the call that is known to fail anyway.

Thanks

Michal

Quoted 33 lines
Quoted 7 lines

The state to detect this error is tracked which makes it possible to produce this warning.

Then it's also possible to handle the error without roundtrip to the hypervisor.

Do you prefer return en error without invalidating if the dump is not read completely? Sure we can.

if (params->status != RTAS_IBM_PLATFORM_DUMP_COMPLETE) {
	pr_err("Platform dump is not complete, but requested "
		"to invalidate dump for ID(%llu)\n",
		dump_tag);
	return -EPERM;
}

Thanks
Haren

Quoted 11 lines

Thanks

Michal

Quoted 6 lines

Thanks
Haren

Quoted 3 lines

Thanks

Michal

#12 lore raw

Re: [PATCH v2 6/6] powerpc/pseries: Add papr-platform-dump character driver for dump retrieval

Quoted 620 lines · Michal Suchánek
Quoted 613 lines · Haren Myneni
Quoted 608 lines · Michal Suchánek
Quoted 600 lines · Haren Myneni
Quoted 15 lines · Michal Suchánek

Hello,

thanks for working on this!

I see in thes version you ended up reusing the existing RTAS call
code
which looks better.

From the past discussion it sounds like the get-indices call can
list
the available dumps, and I do not see this connection documented.

Quoted 7 lines

Also the part about it not being used in practice by the service
that
retrieves the dumps because it gets a message from the hypervisor
with
the dump id when a dump is available.

ibm,get-indices is used to obtain data based on indicator and sensor,
not related to platform dump. ibm,platform-dump is used only on non-HMC
based systems and BMC interface initiates manually to save the dump
on
tne partition. Sorry in case caused confusion in the previous discussion.

Quoted 550 lines · Haren Myneni
Quoted 526 lines

ibm,platform-dump RTAS call in combination with writable mapping
/dev/mem is issued to collect platform dump from the hypervisor
and may need multiple calls to get the complete dump. The current
implementation uses rtas_platform_dump() API provided by librtas
library to issue these RTAS calls. But /dev/mem access by the
user space is prohibited under system lockdown.

The solution should be to restrict access to RTAS function in
user
space and provide kernel interfaces to collect dump. This patch
adds papr-platform-dump character driver and expose standard
interfaces such as open / ioctl/ read to user space in ways that
are compatible with lockdown.

PAPR (7.3.3.4.1 ibm,platform-dump) provides a method to obtain
the complete dump:
- Each dump will be identified by ID called dump tag.
- A sequence of RTAS calls have to be issued until retrieve the
complete dump. The hypervisor expects the first RTAS call
with
the sequence 0 and the subsequent calls with the sequence number returned from the previous calls.
- The hypervisor returns "dump complete" status once the complete
dump is retrieved. But expects one more RTAS call from
the
partition with the NULL buffer to invalidate dump which
means
the dump will be removed in the hypervisor.
- Sequence of calls are allowed with different dump IDs at the
same time but not with the same dump ID.

Expose these interfaces to user space with a /dev/papr- platform-
dump
character device using the following programming model:

int devfd = open("/dev/papr-platform-dump", O_RDONLY); int fd =
ioctl(devfd,PAPR_PLATFORM_DUMP_IOC_CREATE_HANDLE, &dump_id)
- Restrict user space to access with the same dump ID. Typically we do not expect user space requests
the
dump
again for the same dump ID.
char *buf = malloc(size);
length = read(fd, buf, size);
- size should be minimum 1K based on PAPR and <=
4K
based
on RTAS work area size. It will be restrict to
RTAS
work
area size. Using 4K work area based on the
current
implementation in librtas library
- Each read call issue RTAS call to get the data
based
on
the size requirement and returns bytes returned
from
the
hypervisor
- If the previous call returns dump complete
status,
the
next read returns 0 like EOF.
ret = ioctl(PAPR_PLATFORM_DUMP_IOC_INVALIDATE, &dump_id)
- RTAS call with NULL buffer to invalidates the dump.

The read API should use the file descriptor obtained from ioctl
based on dump ID so that gets dump contents for the corresponding
dump ID. Implemented support in librtas (rtas_platform_dump())
for
this new ABI to support system lockdown.

---
 .../include/uapi/asm/papr-platform-dump.h     |  15 +
 arch/powerpc/platforms/pseries/Makefile       |   1 +
 .../platforms/pseries/papr-platform-dump.c    | 408
++++++++++++++++++
 3 files changed, 424 insertions(+)
 create mode 100644 arch/powerpc/include/uapi/asm/papr-
platform-
dump.h
 create mode 100644 arch/powerpc/platforms/pseries/papr-

platform-
dump.c

(quoted diff) +0 -0

dump.h b/arch/powerpc/include/uapi/asm/papr-platform-dump.h new file mode 100644
index 000000000000..3a0f152e3ce8

arch/powerpc/include/uapi/asm/papr-platform-dump.h new file +1 -0
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-

note */

arch/powerpc/include/uapi/asm/papr-platform-dump.h new file +8 -0
+#ifndef _UAPI_PAPR_PLATFORM_DUMP_H_
+#define _UAPI_PAPR_PLATFORM_DUMP_H_
+
+#include <asm/ioctl.h>
+#include <asm/papr-miscdev.h>
+
+/*
+ * ioctl for /dev/papr-platform-dump. Returns a VPD handle

fd
corresponding to
+ * the location code.
+ */
+#define PAPR_PLATFORM_DUMP_IOC_CREATE_HANDLE
_IOW(PAPR_MISCDEV_IOC_ID, 6, __u64)
+#define
PAPR_PLATFORM_DUMP_IOC_INVALIDATE _IOW(PAPR_MISCDEV_IOC_
ID,
7,
__u64)

arch/powerpc/include/uapi/asm/papr-platform-dump.h new file +2 -0
+
+#endif /* _UAPI_PAPR_PLATFORM_DUMP_H_ */
arch/powerpc/platforms/pseries/Makefile +0 -1
index e1db61877bb9..c82c94e0a73c 100644
@@ -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-rtas-common.o papr-vpd.o papr-
indices.o
\

arch/powerpc/platforms/pseries/Makefile +1 -0
+			   papr-platform-dump.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 \

(quoted diff) +0 -0

dump.c b/arch/powerpc/platforms/pseries/papr-platform-dump.c new file mode 100644
index 000000000000..13a418d7c37e

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +17 -0
@@ -0,0 +1,408 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#define pr_fmt(fmt) "papr-platform-dump: " fmt
+
+#include <linux/anon_inodes.h>
+#include <linux/file.h>
+#include <linux/fs.h>
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/miscdevice.h>
+#include <asm/machdep.h>
+#include <asm/rtas-work-area.h>
+#include <asm/rtas.h>
+#include <uapi/asm/papr-platform-dump.h>
+
+/*
+ * Function-specific return values for ibm,platform-dump,

derived
from
+ * PAPR+ v2.13 7.3.3.4.1 "ibm,platform-dump RTAS Call".
+ */
+#define RTAS_IBM_PLATFORM_DUMP_COMPLETE 0 /*
Complete
dump retrieved. */
+#define RTAS_IBM_PLATFORM_DUMP_CONTINUE 1 /*
Continue
dump */
+#define RTAS_NOT_AUTHORIZED -9002 /* Not
Authorized
*/

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +2 -0
+
+#define	RTAS_IBM_PLATFORM_DUMP_START	2 /* Linux

status
to start dump */

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +3 -0
+
+/**
+ * struct ibm_platform_dump_params - Parameters (in and

out)
for
+
* ibm,platform
-
dump
+ * @work_area: In: work area buffer for
results.
+ * @buf_length: In: work area buffer length in
bytes
+ * @dump_tag_hi: In: Most-significant 32 bits of a
Dump_Tag
representing
+ * an id of the dump being processed.
+ * @dump_tag_lo: In: Least-significant 32 bits of a
Dump_Tag
representing
+ * an id of the dump being processed.
+ * @sequence_hi: In: Sequence number in most-
significant
32
bits.
+ * Out: Next sequence number in most-
significant 32 bits.
+ * @sequence_lo: In: Sequence number in Least-
significant 32
bits
+ * Out: Next sequence number in
Least-
significant 32 bits.
+ * @bytes_ret_hi: Out: Bytes written in most-significant
32 bits.
+ * @bytes_ret_lo: Out: Bytes written in Least-
significant
32
bits.
+ * @status: Out: RTAS call status.
+ * @list: Maintain the list of dumps are in
progress. Can
+ * retrieve multiple dumps with
different
dump IDs at
+ * the same time but not with the
same
dump
ID. This list
+ * is used to determine whether the
dump
for
the same ID
+ * is in progress.
+ */

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +15 -0
+struct ibm_platform_dump_params {
+	struct rtas_work_area	*work_area;
+	u32			buf_length;
+	u32			dump_tag_hi;
+	u32			dump_tag_lo;
+	u32			sequence_hi;
+	u32			sequence_lo;
+	u32			bytes_ret_hi;
+	u32			bytes_ret_lo;
+	s32			status;
+	struct list_head	list;
+};
+
+/*
+ * Multiple dumps with different dump IDs can be retrieved

at
the
same
+ * time, but not with dame dump ID. platform_dump_list_mutex
and
+ * platform_dump_list are used to prevent this behavior.
+ */

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +5 -0
+static DEFINE_MUTEX(platform_dump_list_mutex);
+static LIST_HEAD(platform_dump_list);
+
+/**
+ * rtas_ibm_platform_dump() - Call ibm,platform-dump to

fill a
work area
+ * buffer.
+ * @params: See &struct ibm_platform_dump_params.
+ * @buf_addr: Address of dump buffer (work_area)
+ * @buf_length: Length of the buffer in bytes (min. 1024)
+ *
+ * Calls ibm,platform-dump until it errors or successfully deposits data
+ * into the supplied work area. Handles RTAS retry statuses.
Maps
RTAS
+ * error statuses to reasonable errno values.
+ *
+ * Can request multiple dumps with different dump IDs at the
same
time,
+ * but not with the same dump ID which is prevented with the
check
in
+ * the ioctl code (papr_platform_dump_create_handle()).
+ *
+ * The caller should inspect @params.status to determine whether
more
+ * calls are needed to complete the sequence.
+ *
+ * Context: May sleep.
+ * Return: -ve on error, 0 for dump complete and 1 for continue
dump
+ */
+static int rtas_ibm_platform_dump(struct ibm_platform_dump_params
*params,

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +1 -0
+				phys_addr_t buf_addr, u32

buf_length)

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +7 -0
+{
+	u32 rets[4];
+	s32 fwrc;
+	int ret = 0;
+
+	do {
+		fwrc =

rtas_call(rtas_function_token(RTAS_FN_IBM_PLATFORM_DUMP),

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +27 -0
+				6, 5,
+				rets,
+				params->dump_tag_hi,
+				params->dump_tag_lo,
+				params->sequence_hi,
+				params->sequence_lo,
+				buf_addr,
+				buf_length);
+	} while (rtas_busy_delay(fwrc));
+
+	switch (fwrc) {
+	case RTAS_HARDWARE_ERROR:
+		ret = -EIO;
+		break;
+	case RTAS_NOT_AUTHORIZED:
+		ret = -EPERM;
+		break;
+	case RTAS_IBM_PLATFORM_DUMP_CONTINUE:
+	case RTAS_IBM_PLATFORM_DUMP_COMPLETE:
+		params->sequence_hi = rets[0];
+		params->sequence_lo = rets[1];
+		params->bytes_ret_hi = rets[2];
+		params->bytes_ret_lo = rets[3];
+		break;
+	default:
+		ret = -EIO;
+		pr_err_ratelimited("unexpected ibm,platform-

dump status
%d\n",

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +10 -0
+				fwrc);
+		break;
+	}
+
+	params->status = fwrc;
+	return ret;
+}
+
+/*
+ * Platform dump is used with multiple RTAS calls to

retrieve
the
+ * complete dump for the provided dump ID. Once the complete
dump
is
+ * retrieved, the hypervisor returns dump complete status
(0)
for
the
+ * last RTAS call and expects the caller issues one more call
with
+ * NULL buffer to invalidate the dump so that the hypervisor
can
remove
+ * the dump.
+ *
+ * After the specific dump is invalidated in the hypervisor,
expect the
+ * dump complete status for the new sequence - the user space
initiates
+ * new request for the same dump ID.
+ */
+static ssize_t papr_platform_dump_handle_read(struct file *file,

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +3 -0
+		char __user *buf, size_t size, loff_t *off)
+{
+	struct ibm_platform_dump_params *params = file-
Quoted 1 line

private_data;

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +6 -0
+	u64 total_bytes;
+	s32 fwrc;
+
+	/*
+	 * Dump already completed with the previous read calls.
+	 * In case if the user space issues further reads,

returns

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +4 -0
+	 * -EINVAL.
+	 */
+	if (!params->buf_length) {
+		pr_warn_once("Platform dump completed for dump

ID
%llu\n",

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +7 -0
+			(u64) (((u64)params->dump_tag_hi << 32)
+				params->dump_tag_lo));
+		return -EINVAL;
+	}
+
+	/*
+	 * The hypervisor returns status 0 if no more data

available to

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +1 -0
+	 * download. The dump will be invalidated with ioctl

(see
below).

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +2 -0
+	 */
+	if (params->status == RTAS_IBM_PLATFORM_DUMP_COMPLETE)

{

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +10 -0
+		params->buf_length = 0;
+		/*
+		 * Returns 0 to the user space so that user
+		 * space read stops.
+		 */
+		return 0;
+	}
+
+	if (size < SZ_1K) {
+		pr_err_once("Buffer length should be minimum

1024
bytes\n");

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +4 -0
+		return -EINVAL;
+	} else if (size > params->buf_length) {
+		/*
+		 * Allocate 4K work area. So if the user

requests > 4K,

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +18 -0
+		 * resize the buffer length.
+		 */
+		size = params->buf_length;
+	}
+
+	fwrc = rtas_ibm_platform_dump(params,
+			rtas_work_area_phys(params->work_area),
+			size);
+	if (fwrc < 0)
+		return fwrc;
+
+	total_bytes = (u64) (((u64)params->bytes_ret_hi << 32)
+			params->bytes_ret_lo);
+
+	/*
+	 * Kernel or firmware bug, do not continue.
+	 */
+	if (WARN(total_bytes > size, "possible write beyond end

of work
area"))

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +3 -0
+		return -EFAULT;
+
+	if (copy_to_user(buf, rtas_work_area_raw_buf(params-
Quoted 1 line

work_area),

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +7 -0
+			total_bytes))
+		return -EFAULT;
+
+	return total_bytes;
+}
+
+static int papr_platform_dump_handle_release(struct inode

*inode,

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +3 -0
+					struct file *file)
+{
+	struct ibm_platform_dump_params *params = file-
Quoted 1 line

private_data;

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +15 -0
+
+	if (params->work_area)
+		rtas_work_area_free(params->work_area);
+
+	mutex_lock(&platform_dump_list_mutex);
+	list_del(&params->list);
+	mutex_unlock(&platform_dump_list_mutex);
+
+	kfree(params);
+	file->private_data = NULL;
+	return 0;
+}
+
+/*
+ * This ioctl is used to invalidate the dump assuming the

user
space
+ * issue this ioctl after obtain the complete dump.
+ * Issue the last RTAS call with NULL buffer to invalidate the
dump
+ * which means dump will be freed in the hypervisor.
+ */
+static long papr_platform_dump_invalidate_ioctl(struct file
*file,

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +1 -0
+				unsigned int ioctl, unsigned

long arg)

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +13 -0
+{
+	struct ibm_platform_dump_params *params;
+	u64 __user *argp = (void __user *)arg;
+	u64 param_dump_tag, dump_tag;
+
+	if (ioctl != PAPR_PLATFORM_DUMP_IOC_INVALIDATE)
+		return -ENOIOCTLCMD;
+
+	if (get_user(dump_tag, argp))
+		return -EFAULT;
+
+	/*
+	 * private_data is freeded during release(), so should
Quoted 1 line

not

freed?

Quoted 21 lines
arch/powerpc/platforms/pseries/papr-platform-dump.c new file +4 -0
+	 * happen.
+	 */
+	if (!file->private_data) {
+		pr_err("No valid FD to invalidate dump for the

ID(%llu)\n",

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +6 -0
+				dump_tag);
+		return -EINVAL;
+	}
+
+	params = file->private_data;
+	param_dump_tag = (u64) (((u64)params->dump_tag_hi <<

32) |

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +3 -0
+				params->dump_tag_lo);
+	if (dump_tag != param_dump_tag) {
+		pr_err("Invalid dump ID(%llu) to invalidate

dump\n",

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +5 -0
+				dump_tag);
+		return -EINVAL;
+	}
+
+	if (params->status != RTAS_IBM_PLATFORM_DUMP_COMPLETE)
Quoted 8 lines

arch/powerpc/platforms/pseries/papr-platform-dump.c

Quoted 4 lines
arch/powerpc/platforms/pseries/papr-platform-dump.c new file +1 -0
+		pr_warn("Platform dump is not complete, but

requested "

arch/powerpc/platforms/pseries/papr-platform-dump.c new file +2 -0
+			"to invalidate dump for ID(%llu)\n",
+			dump_tag);

Not sure if something should be done here or if relying on translation
of the error from the RTAS call is advisable.

This check just diplays message in case if the user initiated to
invalidate the dump without saving it completely. Then invalidates
the
dump with RTAS call and retuns the RTAS return value.

Quoted 6 lines

As mentioned above, platform-dump is available only on non-HMC based
systems. So invoke the collection of dump by BMC based interface,
not
widely used. I can remove this check if preferred.

Quoted 3 lines

From the previous discussion it sounds like trying to invalidate the
dump without first reading it in full is an error.

Quoted 5 lines

Thanks for your suggestions.

Yes, it was doing as part of read() calls. But explicit ioctl to invalidate here. I was thinking like user space removing FD without reading or writing operation.

Quoted 5 lines

And is it possible to invalidate the dump without reading it fully first?

If not then there is no point trying to do the call that is known to fail anyway.

Generally not possible if uses librtas API rtas_platform_dump() which reads the dump completely and then the application calls this API explicitly to invalidate the dump (with buffer NULL - as doing in the current implementation). The current use case is extract_platdump command (ppc64-diag package)

extract_platdump() { /* we are not chamging this implementation */

status = rtas_platform_dump() - initial call
while !dump_complete {
status = rtas_platform_dump()
if (status < 0) failure
if (status == 0) dump_complete = 1
}

status = rtas_platform_dump() - to invalidate dump by passing buffer = NULL

We should not expect using any command other than extract_platdump since the use case of collecting platform dump is narrow and is only on non-hmc based systems.

Hence added warning message if the dump is not completely read and invalidate the dump like removing file by mistake.

But I like your suggestion of returning an error (-EPERM) if not saved the dump completely.

Thanks
Haren

Quoted 37 lines

Thanks

Michal

Quoted 32 lines
Quoted 7 lines

The state to detect this error is tracked which makes it possible to
produce this warning.

Then it's also possible to handle the error without roundtrip to the
hypervisor.

Do you prefer return en error without invalidating if the dump is not
read completely? Sure we can.

if (params->status != RTAS_IBM_PLATFORM_DUMP_COMPLETE) {
	pr_err("Platform dump is not complete, but requested "
		"to invalidate dump for ID(%llu)\n",
		dump_tag);
	return -EPERM;
}

Thanks
Haren

Quoted 10 lines

Thanks

Michal

Quoted 6 lines

Thanks
Haren

Quoted 3 lines

Thanks

Michal

#13 lore raw

Re: [PATCH v2 6/6] powerpc/pseries: Add papr-platform-dump character driver for dump retrieval

Quoted 69 lines · Haren Myneni
Quoted 40 lines · Michal Suchánek
Quoted 33 lines · Haren Myneni
Quoted 28 lines · Michal Suchánek
Quoted 20 lines · Haren Myneni
Quoted 10 lines · Michal Suchánek

Review

Quoted 6 lines
(quoted diff) +3 -0
+
+	if (params->status != RTAS_IBM_PLATFORM_DUMP_COMPLETE)
+		pr_warn("Platform dump is not complete, but

requested "

(quoted diff) +2 -0
+			"to invalidate dump for ID(%llu)\n",
+			dump_tag);

Not sure if something should be done here or if relying on translation
of the error from the RTAS call is advisable.

This check just diplays message in case if the user initiated to
invalidate the dump without saving it completely. Then invalidates
the
dump with RTAS call and retuns the RTAS return value.

Quoted 6 lines

As mentioned above, platform-dump is available only on non-HMC based
systems. So invoke the collection of dump by BMC based interface,
not
widely used. I can remove this check if preferred.

Quoted 3 lines

From the previous discussion it sounds like trying to invalidate the
dump without first reading it in full is an error.

Quoted 5 lines

Thanks for your suggestions.

Yes, it was doing as part of read() calls. But explicit ioctl to invalidate here. I was thinking like user space removing FD without reading or writing operation.

Quoted 5 lines

And is it possible to invalidate the dump without reading it fully first?

If not then there is no point trying to do the call that is known to fail anyway.

Generally not possible if uses librtas API rtas_platform_dump() which reads the dump completely and then the application calls this API explicitly to invalidate the dump (with buffer NULL - as doing in the current implementation). The current use case is extract_platdump command (ppc64-diag package)

extract_platdump() { /* we are not chamging this implementation */

status = rtas_platform_dump() - initial call
while !dump_complete {
status = rtas_platform_dump()
if (status < 0) failure
if (status == 0) dump_complete = 1
}

status = rtas_platform_dump() - to invalidate dump by passing buffer = NULL

We should not expect using any command other than extract_platdump since the use case of collecting platform dump is narrow and is only on non-hmc based systems.

Quoted 5 lines

Hence added warning message if the dump is not completely read and invalidate the dump like removing file by mistake.

But I like your suggestion of returning an error (-EPERM) if not saved the dump completely.

I think EPERM is not correct in this case. It's not a problem of permission but of incorrect state.

There are some errors around that like EBUSY or EINPROGRESS.

Thanks

Micahl

#14 lore raw

Re: [PATCH v2 6/6] powerpc/pseries: Add papr-platform-dump character driver for dump retrieval

Quoted 88 lines · Michal Suchánek
Quoted 87 lines · Haren Myneni
Quoted 48 lines · Michal Suchánek
Quoted 39 lines · Haren Myneni
Quoted 33 lines · Michal Suchánek
Quoted 24 lines · Haren Myneni
Quoted 6 lines · Michal Suchánek
Quoted 6 lines
(quoted diff) +2 -0
+
+	if (params->status !=

RTAS_IBM_PLATFORM_DUMP_COMPLETE)

(quoted diff) +1 -0
+		pr_warn("Platform dump is not complete,

but
requested "

Quoted 8 lines

Review

Quoted 3 lines
(quoted diff) +1 -0
+			"to invalidate dump for

ID(%llu)\n",

(quoted diff) +1 -0
+			dump_tag);

Not sure if something should be done here or if relying on
translation
of the error from the RTAS call is advisable.

This check just diplays message in case if the user initiated
to
invalidate the dump without saving it completely. Then invalidates
the
dump with RTAS call and retuns the RTAS return value.

Quoted 7 lines

As mentioned above, platform-dump is available only on non- HMC
based
systems. So invoke the collection of dump by BMC based interface,
not
widely used. I can remove this check if preferred.

Quoted 4 lines

From the previous discussion it sounds like trying to invalidate
the
dump without first reading it in full is an error.

Quoted 7 lines

Thanks for your suggestions.

Yes, it was doing as part of read() calls. But explicit ioctl to
invalidate here. I was thinking like user space removing FD without
reading or writing operation.

Quoted 7 lines

And is it possible to invalidate the dump without reading it fully
first?

If not then there is no point trying to do the call that is known to
fail anyway.

Generally not possible if uses librtas API rtas_platform_dump() which
reads the dump completely and then the application calls this API explicitly to invalidate the dump (with buffer NULL - as doing in the
current implementation). The current use case is extract_platdump command (ppc64-diag package)

extract_platdump() { /* we are not chamging this implementation
*/

status = rtas_platform_dump() - initial call
while !dump_complete {
status = rtas_platform_dump()
if (status < 0) failure
if (status == 0) dump_complete = 1
}

status = rtas_platform_dump() - to invalidate dump by passing buffer =
NULL

We should not expect using any command other than extract_platdump since the use case of collecting platform dump is narrow and is only on
non-hmc based systems.

Hence added warning message if the dump is not completely read and invalidate the dump like removing file by mistake.

Quoted 8 lines
Quoted 3 lines

But I like your suggestion of returning an error (-EPERM) if not saved
the dump completely.

I think EPERM is not correct in this case. It's not a problem of permission but of incorrect state.

There are some errors around that like EBUSY or EINPROGRESS.

Thanks for your suggestions - will use EINPROGRESS.

Quoted 4 lines

Thanks

Micahl