s390: Diag204 Rework
v3 cover s390: Diag204 Rework
v3:
- Fix memory leak of extended buffer in diag204_probe() in fallback case (Sashiko)
- Add an ioctl definition for userspace to extract diag204 subcode (Sashiko)
- Fix unlocked cache access and mutex unlock in diag204_store() (Sashiko)
- Replace diag204 ioctl struct data types (Sashiko)
- Change diag204's allow_busy and allow_busy_wait flags for hypfs_diag_create_files() to match usecase before the rework (Sashiko)
- Add a check for the case where user specifies allow_busy but busy indication facility is not present.
v2:
- prevent redundant work rearm after cache free in diag204_work_fn(). (Sashiko)
- always fallback to subcode 4 in diag204_probe(). (Sashiko)
- fix errno check during busy wait loop and prevent uncessary sleeps. (Sashiko)
- simplify diag204 cache update function, allow for diag to return busy even when diag cache is empty. (Sashiko)
- add a check for diag204 subcode in sthyi as it depends on subcode 7 being available. (Sashiko)
- remove CAP_SYS_ADMIN check in IOCTL handler (Hendrik Brueckner)
- add missing READ_ONCE() to g_cache.timestamp
- schedule delayed work only after a successful cache update
- rebase patches
Changes from the earlier series;
* Address mutex lockdep issues (Sumanth Korikkar)
* Add permission check for IOCTL (Sumanth Korikkar)
* Pass buffers set to 0 when calling diag (Sumanth Korikkar)
Background
========================================================================
The goal is to create a single source where all relevant diag204 functionality is exposed and managed.
The main problems being addressed are;
* Diag204 is expensive and calling it too frequently can cause performance issues and also data integrity problems. Rate limiting is required.
* Diag204 requires a prep phase to utilize properly which means code duplication for each user.
* Diag204's current interface in debugfs may not be easily reachable as debugfs is seen as a security vulnarability by some distributions. The new IOCTL diag device should offer alternartive for diag204.
This is also a prerequisite for utilizing diag204 data in hiperdispatch. There are plans to incorporate utilization data for making smarter decisions.
Patches
========================================================================
Patch 1-2:
This patch is split into two, first half is introducing the new diag204 implementation and the second half is replacing the exisiting users. This is purely for easier review. Before accepted I expect first two patches to be squashed.
Patch 1:
The first patch introduces a caching mechanism inspired by Sumanth Korikkar's diag324 implementation. Data buffer gets cached and cache access is serialized via mutexes. If unused for a defined period diag204 data cache gets freed by a worker after a delay, that way it is guaranteed that if an LPAR has no diag204 users, no memory is consumed.
Patch 2:
The second patch adapts the existing diag204 users to the new interface.
Patch 3:
The third patch brings diag204 functionality to the new diag misc device. Misc device gets two new magic values for diag204, one to fetch the data size and the other is to actually get the diag data.
The uapi now has a new struct diag204_request for requesting the data. Struct holds a pointer address to the userspace buffer and two behavior altering flags; allow_busy and allow_busy_loop. Impact of said flags are explained in Considerations section below.
New Diag204 Usage
========================================================================
Users from kernel are now exposed to a couple of common functions which should allow for a simpler way of utilizing diag204. The new streamlined flow now looks like;
...
{
void *buf = diag204_allocate_buffer();
int rc = diag204_store(buf, allow_busy, allow_busy_wait);
/* Do whatever with the diag data*/
diag204_free_buffer(buf);...
Most of the details of diag204 is now hidden away. Initial probing and availability check is done via a device_initcall().
Implementation
========================================================================
Initial checks for diag204 support, determining which subcode to use and querying page count required to hold diag data are all done in a device_initcall() once. Page count returned is the maximum amount required for that CEC, subcodes should also stay the same during runtime.
Diag204 should be rate limited to a diag call per second at most. In order to achive the rate limiting while also allowing users to acquire data a caching mechanism similar to Sumanth's diag324 implementation is proposed.
The caching mechanism is slightly more complicated to allow support for interruptible busy waiting. When diag204's busy indication facility is installed users can specify a flag to put diag204_store() into a sleepable and interruptible loop that lasts until diag204 is no longer busy. This way the amount of diag calls and system calls from userspace are reduced.
If busy indication facility is not present default fallback is the blocking diag204 call as before. Users don't get an indication about this.(IMO, it wouldn't be useful)
Diag204 calls are serialized with a mutex and cache updates are protected with help of a flag. Since busy wait loops can sleep they have to release the mutex before sleep. The updating cache flag comes in here, it is only modified when the mutex is being held and if a cache update is taking place.
Utilizing diag204 arms a work function which periodically checks if the current cache is expired. If it finds that the cache is expired, it frees the cache buffer and stops.
IOCTL Interface
========================================================================
A similar IOCTL interface to existing diag calls is implemented. Two new ioctl definitions along with a new data struct is added to the uapi. DIAG204_GET_LEN allows userspace to acquire required buffer length and DIAG204_GET_BUF fetches the diag204 data. DIAG_204_GET_SUBCODE enables users from userspace to figure out which diag204 subcode is being used to fetch data. Using the subcode users can determine which data types need to be mapped to the diag204 buffer. When issuing an IOCTL call with DIAG204_GET_BUF users have to utilize the new diag204_request struct to specify the allocated buffer address along with the allow_busy an allow_busy_wait flags for diag204 store operation.
Considerations
========================================================================
* At the moment diag204 size request returns a page count requirement of
423. That is the maximum amount of space needed currently for a CEC to service diag204.
With this series at times there will be ~6Mb (1.6Mb for each; cache, temp, user buffer...) memory allocated for diag204 data per lpar.
* With the new IOCTL interface, userspace is defaulted to the largest diag204 data request subcode available(from subcodes 4, 6 and 7 always the largest available is used as it returns more data). While this aligns with the existing behavior, question is, would there be any value in allowing users to specify which subcode to use? Although this also means more complicated implementation.
* A new addition with the IOCTL interface are the allow_busy and allow_busy_wait toggles. For sure there is benefit in allowing diag204 to return BUSY instead of waiting for longer durations when it is not ready at the moment. allow_busy_wait adds an interruptable polling loop around that, with that mechanism diag204 users from userspace can wait for diag204 to return from its BUSY state within the same system call. Obviously using only a single system call is better but diag204 users are not performance critical atm and the same polling can be implemented from userspace side. This can help reduce some complexity on the kernel code.
---
---
base-commit: a804c5742bff224d4cb4d25dd383554d598be32b
Mete Durlu (3):
s390/diag204: Introduce diag204 rework s390/diag204: Replace diag204 users with new implementation s390/diag: Add diag204 IOCTL to diag misc device
arch/s390/hypfs/hypfs_diag.c | 140 ++------------- arch/s390/hypfs/hypfs_diag.h | 4 +- arch/s390/hypfs/hypfs_diag_fs.c | 9 +- arch/s390/include/asm/diag.h | 8 +- arch/s390/include/uapi/asm/diag.h | 9 + arch/s390/kernel/diag/Makefile | 2 +- arch/s390/kernel/diag/diag204.c | 343 +++++++++++++++++++++++++++++++++++++ arch/s390/kernel/diag/diag_ioctl.h | 4 + arch/s390/kernel/diag/diag_misc.c | 9 + arch/s390/kernel/sthyi.c | 30 ++-- 10 files changed, 401 insertions(+), 157 deletions(-)
PATCH v3 1/3 s390/diag204: Introduce diag204 rework
diag204 is an expensive call, limit its invocation to once per second at most for all users. Use a caching mechanism to provide diag204 data at demand. Cache buffer gets freed by a delayed work function if diag204 data is not fetched after a certain amount of time.
--- arch/s390/include/asm/diag.h | 7 + arch/s390/kernel/diag/diag204.c | 293 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 300 insertions(+)
arch/s390/include/asm/diag.h +7 -0
@@ -336,6 +336,13 @@ static inline bool diag204_has_bif(void)return sclp.has_diag204_bif; } +int diag204_store (void *buf,int allow_busy,int allow_busy_wait); +enum diag204_format diag204_get_info_type (void ); +enum diag204_sc diag204_get_subcode (void ); +void *diag204_allocate_buffer (void ); +void diag204_free_buffer (void *buf); +bool diag204_is_available (void ); +int diag204_get_buf_size (void );int diag204 (unsigned long subcode,unsigned long size,void *addr);int diag224 (void *ptr);int diag26c (void *req,void *resp,enum diag26c_sc subcode);
arch/s390/kernel/diag/diag204.c new file +293 -0
293 lines not on this page · raw message
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH v3 1/3] s390/diag204: Introduce diag204 rework
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] s390/diag204: diag204_store() rejects allow_busy when BIF is unsupported - [Medium] s390/diag204…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] s390/diag204: diag204_store() rejects allow_busy when BIF is unsupported
- [Medium] s390/diag204: diag204_store() returns -ERESTARTSYS when allow_busy_wait is 0 --
commit 4b4fb3ec71dc3de5bb7d6e208cc41238367d2b59
Author: Mete Durlu <[email protected]>
s390/diag204: Introduce diag204 rework
This commit introduces a caching layer for diagnose 204 calls in arch/s390/kernel/diag/diag204.c to limit hardware invocations to at most once per second, freeing the cache buffer via delayed work when idle.
Quoted 6 lines
arch/s390/kernel/diag/diag204.c new file +0
-0@@ -0,0 +1,293 @@
[ ... ]
Quoted 13 lines
arch/s390/kernel/diag/diag204.c new file +13
-0+static int diag204_update_cache (int allow_busy,int allow_busy_wait) +{ +unsigned long subcode; +void *buf; +int rc; + +lockdep_assert_held (&diag204_mutex); + buf =diag204_allocate_buffer (); +if (!buf) +return -ENOMEM ; + subcode = g_info.type | g_info.subcode; +if (g_info.has_bif && allow_busy) + subcode |=DIAG204_BIF_BIT ;
[ ... ]
arch/s390/kernel/diag/diag204.c line 201
Quoted 10 lines
arch/s390/kernel/diag/diag204.c new file +10
-0+int diag204_store (void *buf,int allow_busy,int allow_busy_wait) +{ +int rc; + +if (!diag204_is_available ()) +return -EOPNOTSUPP ; +if (!allow_busy && allow_busy_wait) +return -EOPNOTSUPP ; +if (allow_busy && !g_info.has_bif) +return -EINVAL ;
[Severity: High]
Should diag204_store() in arch/s390/kernel/diag/diag204.c return -EINVAL when allow_busy is set and g_info.has_bif is false, or should it fall back to a regular blocking diagnose 204 call?
In diag204_update_cache(), the code checks both g_info.has_bif and allow_busy before setting DIAG204_BIF_BIT:
subcode = g_info.type | g_info.subcode; if (g_info.has_bif && allow_busy) subcode |= DIAG204_BIF_BIT;
Because diag204_update_cache() is only called from diag204_store(), the early return of -EINVAL in diag204_store() when allow_busy && !g_info.has_bif makes the g_info.has_bif check in diag204_update_cache() unreachable when !g_info.has_bif.
When the follow-up change ("s390/diag204: Replace diag204 users with new implementation") converts existing callers:
- dbfs_d204_create() in arch/s390/hypfs/hypfs_diag.c and hypfs_diag_create_files() in arch/s390/hypfs/hypfs_diag_fs.c both call diag204_store(..., 1, 1) unconditionally.
- diag204_get_data() in arch/s390/kernel/sthyi.c calls diag204_store(diag204_buf, diag204_allow_busy, 0), where diag204_allow_busy is true on every cache refresh after the initial fill.
On systems where !sclp.has_diag204_bif, will this cause hypfs initialization, debugfs diag_204 reads, and sthyi cache updates to fail with -EINVAL instead of falling back to a non-busy-indication diag204 call?
Quoted 5 lines
arch/s390/kernel/diag/diag204.c new file +5
-0+if (!buf) +return -ENOMEM ; + rc =0 ; + do { + mutex_lock(&diag204_mutex);
arch/s390/kernel/diag/diag204.c line 216
Quoted 10 lines
arch/s390/kernel/diag/diag204.c new file +10
-0+/* Exit loop with lock taken if there is no concurrent update */ +if (!READ_ONCE (g_cache.updating)) +break ; +mutex_unlock (&diag204_mutex); +if (allow_busy && !allow_busy_wait) +return -EBUSY ; +if (signal_pending (current)) +return -ERESTARTSYS ; +schedule_timeout_interruptible (DIAG204_BUSY_WAIT ); + }while (1 );
[Severity: Medium]
Can this wait loop in diag204_store() in arch/s390/kernel/diag/diag204.c unexpectedly sleep interruptibly and return -ERESTARTSYS when the caller passes allow_busy = 0 and allow_busy_wait = 0?
If one thread is inside diag204_update_cache() with g_cache.updating set to true while dropping diag204_mutex, a second thread calling diag204_store(buf, 0, 0) will see READ_ONCE(g_cache.updating) as true:
if (allow_busy && !allow_busy_wait) return -EBUSY; if (signal_pending(current)) return -ERESTARTSYS; schedule_timeout_interruptible(DIAG204_BUSY_WAIT);
Because allow_busy is 0, the first condition is false and the loop checks signal_pending(current) and returns -ERESTARTSYS even though allow_busy_wait was not requested.
When the follow-up commits ("s390/diag204: Replace diag204 users with new implementation" and "s390/diag: Add diag204 IOCTL to diag misc device") are applied, callers such as diag204_buf() or the guest sthyi path:
handle_sthyi()
-> sthyi_fill()
-> sthyi_update_cache()
-> fill_dst()
-> diag204_get_data(false)
-> diag204_store(diag204_buf, 0, 0)can hit this path when a signal or TIF_NOTIFY_SIGNAL is pending during a concurrent cache update. In handle_sthyi(), an unexpected -ERESTARTSYS return from sthyi_fill() when the sthyi cache is not yet populated causes handle_sthyi() to return before filling the guest buffer or setting the guest condition code after the guest instruction pointer has already been advanced.
Should the concurrent update wait in diag204_store() avoid interruptible sleep and -ERESTARTSYS when !allow_busy_wait?
Quoted 7 lines
arch/s390/kernel/diag/diag204.c new file +7
-0+if (!diag204_is_cache_valid (DIAG204_CACHE_EXPIRY )) + rc =diag204_update_cache (allow_busy, allow_busy_wait); +if (rc >=0 ) +memcpy (buf,READ_ONCE (g_cache.buf),diag204_get_buf_size ()); +mutex_unlock (&diag204_mutex); +return rc; +}
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v3 2/3 s390/diag204: Replace diag204 users with new implementation
In reply to Mete DurluReplace the existing diag204 users in kernelspace with the new diag204 interface.
--- arch/s390/hypfs/hypfs_diag.c | 140 ++++------------------------------------ arch/s390/hypfs/hypfs_diag.h | 4 +- arch/s390/hypfs/hypfs_diag_fs.c | 9 ++- arch/s390/include/asm/diag.h | 1 - arch/s390/kernel/diag/Makefile | 2 +- arch/s390/kernel/sthyi.c | 30 +++------ 6 files changed, 29 insertions(+), 157 deletions(-)
arch/s390/hypfs/hypfs_diag.c +13 -127
@@ -22,139 +22,24 @@#define DBFS_D204_HDR_VERSION 0 -static enum diag204_sc diag204_store_sc;/* used subcode for store */ -static enum diag204_format diag204_info_type;/* used diag 204 data format */ -static void *diag204_buf;/* 4K aligned buffer for diag204 data */ -static int diag204_buf_pages;/* number of pages for diag204 data */ - -enum diag204_format diag204_get_info_type (void ) -{ -return diag204_info_type; -} - -static void diag204_set_info_type (enum diag204_format type) -{ - diag204_info_type = type; -} - -/* Diagnose 204 functions */ -/* -* For the old diag subcode 4 with simple data format we have to use real -* memory. If we use subcode 6 or 7 with extended data format, we can (and -* should) use vmalloc, since we need a lot of memory in that case. Currently -* up to 93 pages! -*/ -static void diag204_free_buffer (void ) +static void hypfs_diag204_free_buffer (void ) { -vfree (diag204_buf); +diag204_free_buffer (diag204_buf); diag204_buf =NULL ; } -void *diag204_get_buffer (enum diag204_format fmt,int *pages) +void *hypfs_diag204_get_buffer (void ) { -if (diag204_buf) { - *pages = diag204_buf_pages; +if (diag204_buf)return diag204_buf; - } -if (fmt ==DIAG204_INFO_SIMPLE ) { - *pages =1 ; - }else {/* DIAG204_INFO_EXT */ - *pages =diag204 ((unsigned long )DIAG204_SUBC_RSI | - (unsigned long )DIAG204_INFO_EXT ,0 ,NULL ); -if (*pages <=0 ) -return ERR_PTR (-EOPNOTSUPP ); - } - diag204_buf =__vmalloc_node (array_size (*pages,PAGE_SIZE ), -PAGE_SIZE ,GFP_KERNEL ,NUMA_NO_NODE , -__builtin_return_address (0 )); + diag204_buf =diag204_allocate_buffer ();if (!diag204_buf)return ERR_PTR (-ENOMEM ); - diag204_buf_pages = *pages;return diag204_buf; } -/* -* diag204_probe() has to find out, which type of diagnose 204 implementation -* we have on our machine. Currently there are three possible scanarios: -* - subcode 4 + simple data format (only one page) -* - subcode 4-6 + extended data format -* - subcode 4-7 + extended data format -* -* Subcode 5 is used to retrieve the size of the data, provided by subcodes -* 6 and 7. Subcode 7 basically has the same function as subcode 6. In addition -* to subcode 6 it provides also information about secondary cpus. -* In order to get as much information as possible, we first try -* subcode 7, then 6 and if both fail, we use subcode 4. -*/ - -static int diag204_probe (void ) -{ -void *buf; -int pages, rc; - - buf =diag204_get_buffer (DIAG204_INFO_EXT , &pages); -if (!IS_ERR (buf)) { -if (diag204 ((unsigned long )DIAG204_SUBC_STIB7 | - (unsigned long )DIAG204_INFO_EXT , pages, buf) >=0 ) { - diag204_store_sc =DIAG204_SUBC_STIB7 ; -diag204_set_info_type (DIAG204_INFO_EXT ); - goto out; - } -if (diag204 ((unsigned long )DIAG204_SUBC_STIB6 | - (unsigned long )DIAG204_INFO_EXT , pages, buf) >=0 ) { - diag204_store_sc =DIAG204_SUBC_STIB6 ; -diag204_set_info_type (DIAG204_INFO_EXT ); - goto out; - } -diag204_free_buffer (); - } - -/* subcodes 6 and 7 failed, now try subcode 4 */ - - buf =diag204_get_buffer (DIAG204_INFO_SIMPLE , &pages); -if (IS_ERR (buf)) { - rc =PTR_ERR (buf); - goto fail_alloc; - } -if (diag204 ((unsigned long )DIAG204_SUBC_STIB4 | - (unsigned long )DIAG204_INFO_SIMPLE , pages, buf) >=0 ) { - diag204_store_sc =DIAG204_SUBC_STIB4 ; -diag204_set_info_type (DIAG204_INFO_SIMPLE ); - goto out; - }else { - rc = -EOPNOTSUPP ; - goto fail_store; - } -out: - rc =0 ; -fail_store: -diag204_free_buffer (); -fail_alloc: -return rc; -} - -int diag204_store (void *buf,int pages) -{ -unsigned long subcode; -int rc; - - subcode =diag204_get_info_type (); - subcode |= diag204_store_sc; -if (diag204_has_bif ()) - subcode |=DIAG204_BIF_BIT ; -while (1 ) { - rc =diag204 (subcode, pages, buf); -if (rc != -EBUSY ) -break ; -if (signal_pending (current)) -return -ERESTARTSYS ; -schedule_timeout_interruptible (DIAG204_BUSY_WAIT ); - } -return rc <0 ? rc :0 ; -} -struct dbfs_d204_hdr {u64 len;/* Length of d204 buffer without header */ u16 version;/* Version of header */
@@ -173,19 +58,20 @@ static int dbfs_d204_create(void **data, void **data_free_ptr, size_t *size)int rc, buf_size;void *base; - buf_size =PAGE_SIZE * (diag204_buf_pages +1 ) +sizeof (d204->hdr); + buf_size =diag204_get_buf_size (); + buf_size +=PAGE_SIZE +sizeof (d204->hdr); base =vzalloc (buf_size);if (!base)return -ENOMEM ; d204 =PTR_ALIGN (base +sizeof (d204->hdr),PAGE_SIZE ) -sizeof (d204->hdr); - rc =diag204_store (d204->buf, diag204_buf_pages); -if (rc) { + rc =diag204_store (d204->buf,1 ,1 ); +if (rc <0 ) {vfree (base);return rc; } d204->hdr.version =DBFS_D204_HDR_VERSION ; - d204->hdr.len =PAGE_SIZE * diag204_buf_pages; - d204->hdr.sc = diag204_store_sc; + d204->hdr.len =diag204_get_buf_size (); + d204->hdr.sc =diag204_get_subcode (); *data = d204; *data_free_ptr = base; *size = d204->hdr.len +sizeof (struct dbfs_d204_hdr );
@@ -202,7 +88,7 @@ __init int hypfs_diag_init(void) {int rc; -if (diag204_probe ()) { +if (!diag204_is_available ()) {pr_info ("The hardware system does not support hypfs\n" );return -ENODATA ; }
@@ -219,6 +105,6 @@ __init int hypfs_diag_init(void)void hypfs_diag_exit (void ) {hypfs_diag_fs_exit (); -diag204_free_buffer (); +hypfs_diag204_free_buffer ();hypfs_dbfs_remove_file (&dbfs_file_d204); }
arch/s390/hypfs/hypfs_diag.h +1 -3
@@ -12,9 +12,7 @@#include <asm/diag.h> -enum diag204_format diag204_get_info_type (void ); -void *diag204_get_buffer (enum diag204_format fmt,int *pages); -int diag204_store (void *buf,int pages); +void *hypfs_diag204_get_buffer (void );int __hypfs_diag_fs_init (void );void __hypfs_diag_fs_exit (void );
arch/s390/hypfs/hypfs_diag_fs.c +4 -5
@@ -303,15 +303,14 @@ int hypfs_diag_create_files(struct dentry *root)struct dentry *systems_dir, *hyp_dir;void *time_hdr, *part_hdr;void *buffer, *ptr; -int i, rc, pages; +int i, rc; - buffer =diag204_get_buffer (diag204_get_info_type (), &pages); + buffer =hypfs_diag204_get_buffer ();if (IS_ERR (buffer))return PTR_ERR (buffer); - rc =diag204_store (buffer, pages); -if (rc) + rc =diag204_store (buffer,1 ,1 ); +if (rc <0 )return rc; - systems_dir =hypfs_mkdir (root,"systems" );if (IS_ERR (systems_dir))return PTR_ERR (systems_dir);
arch/s390/include/asm/diag.h +0 -1
@@ -121,7 +121,6 @@ enum diag204_sc {#define DIAG204_SUBCODE_MASK 0xffff#define DIAG204_BIF_BIT 0x80000000 -#define DIAG204_BUSY_WAIT (HZ / 10)/* The two available diag 204 data formats */ enum diag204_format {
arch/s390/kernel/diag/Makefile +1 -1
@@ -1 +1 @@ -obj-y := diag_misc.o diag324.o diag.o diag310.o +obj-y := diag_misc.o diag324.o diag.o diag310.o diag204.o
arch/s390/kernel/sthyi.c +10 -20
@@ -305,29 +305,19 @@ static struct diag204_x_part_block *lpar_cpu_inf(struct lpar_cpu_inf *part_inf,static void *diag204_get_data (bool diag204_allow_busy) { -unsigned long subcode;void *diag204_buf; -int pages, rc; - - subcode =DIAG204_SUBC_RSI ; - subcode |=DIAG204_INFO_EXT ; - pages =diag204 (subcode,0 ,NULL ); -if (pages <0 ) -return ERR_PTR (pages); -if (pages ==0 ) -return ERR_PTR (-ENODATA ); - diag204_buf =__vmalloc_node (array_size (pages,PAGE_SIZE ), -PAGE_SIZE ,GFP_KERNEL ,NUMA_NO_NODE , -__builtin_return_address (0 )); +int rc; + +if (!diag204_is_available ()) +return ERR_PTR (-EOPNOTSUPP ); +if (diag204_get_subcode () !=DIAG204_SUBC_STIB7 ) +return ERR_PTR (-EOPNOTSUPP ); + diag204_buf =diag204_allocate_buffer ();if (!diag204_buf)return ERR_PTR (-ENOMEM ); - subcode =DIAG204_SUBC_STIB7 ; - subcode |=DIAG204_INFO_EXT ; -if (diag204_has_bif () && diag204_allow_busy) - subcode |=DIAG204_BIF_BIT ; - rc =diag204 (subcode, pages, diag204_buf); + rc =diag204_store (diag204_buf, diag204_allow_busy,0 );if (rc <0 ) { -vfree (diag204_buf); +diag204_free_buffer (diag204_buf);return ERR_PTR (rc); }return diag204_buf;
@@ -462,7 +452,7 @@ static int fill_dst(void *dst, u64 *rc)fill_hdr (sctns);fill_stsi (sctns);fill_diag (sctns, diag204_buf); -vfree (diag204_buf); +diag204_free_buffer (diag204_buf);return 0 ; }
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH v3 2/3] s390/diag204: Replace diag204 users with new implementation
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] s390/diag204: diag204_store() calls fail with -EINVAL without BIF support --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] s390/diag204: diag204_store() calls fail with -EINVAL without BIF support --
commit e6a66aa1f5ad19fef893eed0590d4b6f880aaa42
Author: Mete Durlu <[email protected]>
s390/diag204: Replace diag204 users with new implementation
This commit converts existing kernelspace diagnose 204 users in hypfs and sthyi to use the new centralized diag204 interface.
Quoted 5 lines
arch/s390/hypfs/hypfs_diag.c +0
-0@@ -22,139 +22,24 @@
[ ... ]
Quoted 41 lines
arch/s390/hypfs/hypfs_diag.c +4
-23-int diag204_store (void *buf,int pages) -{ -unsigned long subcode; -int rc; - - subcode =diag204_get_info_type (); - subcode |= diag204_store_sc; -if (diag204_has_bif ()) - subcode |=DIAG204_BIF_BIT ; -while (1 ) { - rc =diag204 (subcode, pages, buf); -if (rc != -EBUSY ) -break ; -if (signal_pending (current)) -return -ERESTARTSYS ; -schedule_timeout_interruptible (DIAG204_BUSY_WAIT ); - } -return rc <0 ? rc :0 ; -} -struct dbfs_d204_hdr {u64 len;/* Length of d204 buffer without header */ u16 version;/* Version of header */ @@ -173,19 +58,20 @@ static int dbfs_d204_create(void **data, void **data_free_ptr, size_t *size)int rc, buf_size;void *base; - buf_size =PAGE_SIZE * (diag204_buf_pages +1 ) +sizeof (d204->hdr); + buf_size =diag204_get_buf_size (); + buf_size +=PAGE_SIZE +sizeof (d204->hdr); base =vzalloc (buf_size);if (!base)return -ENOMEM ; d204 =PTR_ALIGN (base +sizeof (d204->hdr),PAGE_SIZE ) -sizeof (d204->hdr); - rc =diag204_store (d204->buf, diag204_buf_pages); -if (rc) { + rc =diag204_store (d204->buf,1 ,1 ); +if (rc <0 ) {vfree (base);return rc; }
[ ... ]
Quoted 24 lines
arch/s390/hypfs/hypfs_diag_fs.c +4
-5@@ -303,15 +303,14 @@ int hypfs_diag_create_files(struct dentry *root)struct dentry *systems_dir, *hyp_dir;void *time_hdr, *part_hdr;void *buffer, *ptr; -int i, rc, pages; +int i, rc; - buffer =diag204_get_buffer (diag204_get_info_type (), &pages); + buffer =hypfs_diag204_get_buffer ();if (IS_ERR (buffer))return PTR_ERR (buffer); - rc =diag204_store (buffer, pages); -if (rc) + rc =diag204_store (buffer,1 ,1 ); +if (rc <0 )return rc; - systems_dir =hypfs_mkdir (root,"systems" );if (IS_ERR (systems_dir))return PTR_ERR (systems_dir);
[ ... ]
Quoted 33 lines
arch/s390/kernel/sthyi.c +7
-15@@ -305,29 +305,19 @@ static struct diag204_x_part_block *lpar_cpu_inf(struct lpar_cpu_inf *part_inf,static void *diag204_get_data (bool diag204_allow_busy) { -unsigned long subcode;void *diag204_buf; -int pages, rc; - - subcode =DIAG204_SUBC_RSI ; - subcode |=DIAG204_INFO_EXT ; - pages =diag204 (subcode,0 ,NULL ); -if (pages <0 ) -return ERR_PTR (pages); -if (pages ==0 ) -return ERR_PTR (-ENODATA ); - diag204_buf =__vmalloc_node (array_size (pages,PAGE_SIZE ), -PAGE_SIZE ,GFP_KERNEL ,NUMA_NO_NODE , -__builtin_return_address (0 )); +int rc; + +if (!diag204_is_available ()) +return ERR_PTR (-EOPNOTSUPP ); +if (diag204_get_subcode () !=DIAG204_SUBC_STIB7 ) +return ERR_PTR (-EOPNOTSUPP ); + diag204_buf =diag204_allocate_buffer ();if (!diag204_buf)return ERR_PTR (-ENOMEM ); - subcode =DIAG204_SUBC_STIB7 ; - subcode |=DIAG204_INFO_EXT ;
arch/s390/kernel/sthyi.c static struct diag204_x_part_block *lpar_cpu_inf(struct lpar_cpu_inf *part_inf, · line 323
Quoted 10 lines
arch/s390/kernel/sthyi.c +2
-4-if (diag204_has_bif () && diag204_allow_busy) - subcode |=DIAG204_BIF_BIT ; - rc =diag204 (subcode, pages, diag204_buf); + rc =diag204_store (diag204_buf, diag204_allow_busy,0 );if (rc <0 ) { -vfree (diag204_buf); +diag204_free_buffer (diag204_buf);return ERR_PTR (rc); }return diag204_buf;
[Severity: High]
Does this cause diag204_store() calls in hypfs and sthyi to fail with -EINVAL on systems without the Diagnose 204 Busy Indication Facility (BIF)?
In arch/s390/kernel/diag/diag204.c, diag204_store() rejects any call where allow_busy is set if g_info.has_bif is false:
if (!allow_busy && allow_busy_wait)
return -EOPNOTSUPP;
if (allow_busy && !g_info.has_bif)
return -EINVAL;Previously, diag204_store() in arch/s390/hypfs/hypfs_diag.c checked diag204_has_bif() before setting DIAG204_BIF_BIT, and diag204_get_data() in arch/s390/kernel/sthyi.c checked both diag204_has_bif() and diag204_allow_busy. When BIF was not supported, both callers issued diagnose 204 without DIAG204_BIF_BIT.
With this change, dbfs_d204_create() in arch/s390/hypfs/hypfs_diag.c and hypfs_diag_create_files() in arch/s390/hypfs/hypfs_diag_fs.c both pass allow_busy = 1 and allow_busy_wait = 1 unconditionally:
rc = diag204_store(d204->buf, 1, 1);
and:
rc = diag204_store(buffer, 1, 1);
When g_info.has_bif is false, reading the debugfs diag_204 file or mounting and updating s390_hypfs will immediately return -EINVAL.
Similarly, in arch/s390/kernel/sthyi.c, diag204_get_data() now passes diag204_allow_busy directly to diag204_store():
sthyi_fill()
-> sthyi_update_cache()
-> fill_dst()
-> diag204_get_data(is_diag204_cached(sctns))
-> diag204_store(diag204_buf, diag204_allow_busy, 0)On systems without hardware STHYI (!test_facility(74)) and without BIF, the first sthyi_fill() call succeeds because sthyi_cache.info is zeroed and is_diag204_cached(sctns) is false. Once fill_diag() populates the validity bits in sthyi_cache.info and the cache expires after CACHE_VALID_JIFFIES, subsequent calls to sthyi_fill() pass diag204_allow_busy = true and fail with -EINVAL because sthyi_update_cache() only handles -EBUSY.
Should these callers gate allow_busy and allow_busy_wait on BIF support, or should diag204_store() fall back to a non-BIF store when !g_info.has_bif?
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v3 3/3 s390/diag: Add diag204 IOCTL to diag misc device
In reply to Mete DurluAdd IOCTL implementation necessary to fetch diag204 data from the new diag misc device under /dev.
--- arch/s390/include/uapi/asm/diag.h | 9 +++++++ arch/s390/kernel/diag/diag204.c | 50 ++++++++++++++++++++++++++++++++++++++ arch/s390/kernel/diag/diag_ioctl.h | 4 +++ arch/s390/kernel/diag/diag_misc.c | 9 +++++++ 4 files changed, 72 insertions(+)
arch/s390/include/uapi/asm/diag.h +9 -0
@@ -22,11 +22,20 @@ struct diag310_memtop { __u64 nesting_lvl; }; +struct diag204_request { +__u64 address; +__u32 allow_busy; +__u32 allow_busy_wait; +}; +/* Diag ioctl definitions */ #define DIAG324_GET_PIBBUF _IOWR(DIAG_MAGIC_STR, 0x77, struct diag324_pib)#define DIAG324_GET_PIBLEN _IOR(DIAG_MAGIC_STR, 0x78, size_t)#define DIAG310_GET_STRIDE _IOR(DIAG_MAGIC_STR, 0x79, size_t)#define DIAG310_GET_MEMTOPLEN _IOWR(DIAG_MAGIC_STR, 0x7a, size_t)#define DIAG310_GET_MEMTOPBUF _IOWR(DIAG_MAGIC_STR, 0x7b, struct diag310_memtop) +#define DIAG204_GET_SUBCODE _IOR(DIAG_MAGIC_STR, 0x7c, size_t) +#define DIAG204_GET_LEN _IOR(DIAG_MAGIC_STR, 0x7d, size_t) +#define DIAG204_GET_BUF _IOR(DIAG_MAGIC_STR, 0x7e, struct diag204_request)#endif /* __S390_UAPI_ASM_DIAG_H */
arch/s390/kernel/diag/diag204.c +50 -0
62 lines not on this page · raw message
arch/s390/kernel/diag/diag_ioctl.h +4 -0
@@ -11,4 +11,8 @@ long diag310_memtop_stride(unsigned long arg);long diag310_memtop_len (unsigned long arg);long diag310_memtop_buf (unsigned long arg); +long diag204_subcode (unsigned long arg); +long diag204_len (unsigned long arg); +long diag204_buf (unsigned long arg); +#endif /* _DIAG_IOCTL_H */
arch/s390/kernel/diag/diag_misc.c +9 -0
@@ -35,6 +35,15 @@ static long diag_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)case DIAG310_GET_MEMTOPBUF : rc =diag310_memtop_buf (arg);break ; +case DIAG204_GET_SUBCODE : + rc =diag204_subcode (arg); +break ; +case DIAG204_GET_LEN : + rc =diag204_len (arg); +break ; +case DIAG204_GET_BUF : + rc =diag204_buf (arg); +break ;default : rc = -ENOIOCTLCMD ;break ;
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH v3 3/3] s390/diag: Add diag204 IOCTL to diag misc device
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] s390/diag: diag204_store() fails with -EINVAL when BIF is unavailable --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] s390/diag: diag204_store() fails with -EINVAL when BIF is unavailable --
commit 24db640b46c74f692d9871eb2b56c29382e3bc2f
Author: Mete Durlu <[email protected]>
s390/diag: Add diag204 IOCTL to diag misc device
This commit adds the DIAG204_GET_SUBCODE, DIAG204_GET_LEN, and DIAG204_GET_BUF ioctls to the /dev/diag misc device to allow userspace to retrieve diagnose 204 data.
Quoted 4 lines
arch/s390/kernel/diag/diag204.c +0
-0
[ ... ]
Quoted 4 lines
arch/s390/kernel/diag/diag204.c +0
-0@@ -222,6 +224,54 @@ int diag204_store(void *buf, int allow_busy, int allow_busy_wait)return rc; }
[ ... ]
Quoted 10 lines
arch/s390/kernel/diag/diag204.c +10
-0+long diag204_buf (unsigned long arg) +{ +struct diag204_request __user *udata; +int rc, allow_busy, allow_busy_wait; +u64 address; +void *buf; + +if (!diag204_is_available ()) +return -EOPNOTSUPP ; + udata = (struct diag204_request __user *)arg;
arch/s390/kernel/diag/diag204.c int diag204_store(void *buf, int allow_busy, int allow_busy_wait) · line 264
Quoted 10 lines
arch/s390/kernel/diag/diag204.c +10
-0+if (get_user (address, &udata->address)) +return -EFAULT ; +if (get_user (allow_busy, &udata->allow_busy)) +return -EFAULT ; +if (get_user (allow_busy_wait, &udata->allow_busy_wait)) +return -EFAULT ; + buf =diag204_allocate_buffer (); +if (!buf) +return -ENOMEM ; + rc =diag204_store (buf, allow_busy, allow_busy_wait);
[Severity: Medium]
What happens on systems where diagnose 204 is available, but the busy indication facility (g_info.has_bif) is not supported, when a caller passes a non-zero allow_busy value?
In arch/s390/kernel/diag/diag204.c, diag204_update_cache() is written to fall back to a regular diagnose 204 call when g_info.has_bif is false:
subcode = g_info.type | g_info.subcode;
if (g_info.has_bif && allow_busy)
subcode |= DIAG204_BIF_BIT;However, earlier in the series, diag204_store() in arch/s390/kernel/diag/diag204.c rejects any call with allow_busy set when g_info.has_bif is false:
if (allow_busy && !g_info.has_bif)
return -EINVAL;Because arch/s390/include/uapi/asm/diag.h does not provide an ioctl for userspace to query whether BIF is supported, a userspace caller invoking ioctl(fd, DIAG204_GET_BUF, &req) -> diag_ioctl() -> diag204_buf() -> diag204_store() with allow_busy set has no way to know upfront that it must pass allow_busy = 0 on machines without BIF.
In addition, the in-kernel callers converted earlier in the series pass allow_busy = 1 unconditionally when BIF is not supported:
- arch/s390/hypfs/hypfs_diag.c: dbfs_d204_create() calls diag204_store(d204->buf, 1, 1)
- arch/s390/hypfs/hypfs_diag_fs.c: hypfs_diag_create_files() calls diag204_store(buffer, 1, 1)
- arch/s390/kernel/sthyi.c: diag204_get_data() calls diag204_store(diag204_buf, diag204_allow_busy, 0) where diag204_allow_busy = is_diag204_cached(sctns) is true after the first cache fill
Should diag204_store() allow allow_busy when !g_info.has_bif and let diag204_update_cache() ignore it (or clamp allow_busy and allow_busy_wait to 0 when !g_info.has_bif) instead of returning -EINVAL?
Quoted 9 lines
arch/s390/kernel/diag/diag204.c +9
-0+if (rc <0 ) { +diag204_free_buffer (buf); +return rc; + } + rc =copy_to_user ((void __user *)address, buf, +diag204_get_buf_size ()); +diag204_free_buffer (buf); +return rc ? -EFAULT :0 ; +}
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3