Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

s390/cio: add CRW inject functionality

This patch introduces the mechanism to inject artificial events to the
CIO layer.

One of the main-event type which triggers the CommonIO operations are
Channel Report events. When a malfunction or other condition affecting
channel-subsystem operation is recognized, a Channel Report Word
(consisting of one or more CRWs) describing the condition is made
pending for retrieval and analysis by the program. The CRW contains
information concerning the identity and state of a facility following
the detection of the malfunction or other condition.

The patch introduces two debugfs interfaces which can be used to inject
'artificial' events from the userspace. It is intended to provide an easy
means to increase the test coverage for CIO code. And this functionality
can be enabled via a new configuration option CONFIG_CIO_INJECT.

The newly introduces debugfs interfaces can be used as mentioned below
to generate different fake-events. To use the crw_inject, first we should
enable it by using enable_inject interface.
i.e

echo 1 > /sys/kernel/debug/s390/cio/enable_inject

After the first step, user can simulate CRW as follows:

echo <solicited> <overflow> <chaining> <rsc> <ancillary> <erc> <rsid> \
> /sys/kernel/debug/s390/cio/crw_inject

Example:
A permanent error ERC on CHPID 0x60 would look like this:

echo 0 0 0 4 0 6 0x60 > /sys/kernel/debug/s390/cio/crw_inject

and an initialized ERC on the same CHPID:

echo 0 0 0 4 0 2 0x60 > /sys/kernel/debug/s390/cio/crw_inject

Signed-off-by: Vineeth Vijayan <vneethv@linux.ibm.com>
Reviewed-by: Peter Oberparleiter <oberpar@linux.ibm.com>
Signed-off-by: Heiko Carstens <hca@linux.ibm.com>

authored by

Vineeth Vijayan and committed by
Heiko Carstens
a4f17cc7 64a715ab

+222 -1
+8
arch/s390/Kconfig.debug
··· 15 15 exits or otherwise impact performance. 16 16 17 17 If unsure, say N. 18 + 19 + config CIO_INJECT 20 + bool "CIO Inject interfaces" 21 + depends on DEBUG_KERNEL && DEBUG_FS 22 + help 23 + This option provides a debugging facility to inject certain artificial events 24 + and instruction responses to the CIO layer of Linux kernel. The newly created 25 + debugfs user-interfaces will be at /sys/kernel/debug/s390/cio/*
+1
arch/s390/configs/debug_defconfig
··· 829 829 CONFIG_FTRACE_STARTUP_TEST=y 830 830 # CONFIG_EVENT_TRACE_STARTUP_TEST is not set 831 831 CONFIG_DEBUG_ENTRY=y 832 + CONFIG_CIO_INJECT=y 832 833 CONFIG_NOTIFIER_ERROR_INJECTION=m 833 834 CONFIG_NETDEV_NOTIFIER_ERROR_INJECT=m 834 835 CONFIG_FAULT_INJECTION=y
+2
drivers/s390/cio/Makefile
··· 23 23 vfio_ccw-objs += vfio_ccw_drv.o vfio_ccw_cp.o vfio_ccw_ops.o vfio_ccw_fsm.o \ 24 24 vfio_ccw_async.o vfio_ccw_trace.o vfio_ccw_chp.o 25 25 obj-$(CONFIG_VFIO_CCW) += vfio_ccw.o 26 + 27 + obj-$(CONFIG_CIO_INJECT) += cio_inject.o
+171
drivers/s390/cio/cio_inject.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * CIO inject interface 4 + * 5 + * Copyright IBM Corp. 2021 6 + * Author(s): Vineeth Vijayan <vneethv@linux.ibm.com> 7 + */ 8 + 9 + #define KMSG_COMPONENT "cio" 10 + #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 11 + 12 + #include <linux/slab.h> 13 + #include <linux/spinlock.h> 14 + #include <linux/mm.h> 15 + #include <linux/debugfs.h> 16 + #include <asm/chpid.h> 17 + 18 + #include "cio_inject.h" 19 + #include "cio_debug.h" 20 + 21 + static DEFINE_SPINLOCK(crw_inject_lock); 22 + DEFINE_STATIC_KEY_FALSE(cio_inject_enabled); 23 + static struct crw *crw_inject_data; 24 + 25 + /** 26 + * crw_inject : Initiate the artificial CRW inject 27 + * @crw: The data which needs to be injected as new CRW. 28 + * 29 + * The CRW handler is called, which will use the provided artificial 30 + * data instead of the CRW from the underlying hardware. 31 + * 32 + * Return: 0 on success 33 + */ 34 + static int crw_inject(struct crw *crw) 35 + { 36 + int rc = 0; 37 + struct crw *copy; 38 + unsigned long flags; 39 + 40 + copy = kmemdup(crw, sizeof(*crw), GFP_KERNEL); 41 + if (!copy) 42 + return -ENOMEM; 43 + 44 + spin_lock_irqsave(&crw_inject_lock, flags); 45 + if (crw_inject_data) { 46 + kfree(copy); 47 + rc = -EBUSY; 48 + } else { 49 + crw_inject_data = copy; 50 + } 51 + spin_unlock_irqrestore(&crw_inject_lock, flags); 52 + 53 + if (!rc) 54 + crw_handle_channel_report(); 55 + 56 + return rc; 57 + } 58 + 59 + /** 60 + * stcrw_get_injected: Copy the artificial CRW data to CRW struct. 61 + * @crw: The target CRW pointer. 62 + * 63 + * Retrieve an injected CRW data. Return 0 on success, 1 if no 64 + * injected-CRW is available. The function reproduces the return 65 + * code of the actual STCRW function. 66 + */ 67 + int stcrw_get_injected(struct crw *crw) 68 + { 69 + int rc = 1; 70 + unsigned long flags; 71 + 72 + spin_lock_irqsave(&crw_inject_lock, flags); 73 + if (crw_inject_data) { 74 + memcpy(crw, crw_inject_data, sizeof(*crw)); 75 + kfree(crw_inject_data); 76 + crw_inject_data = NULL; 77 + rc = 0; 78 + } 79 + spin_unlock_irqrestore(&crw_inject_lock, flags); 80 + 81 + return rc; 82 + } 83 + 84 + /* The debugfs write handler for crw_inject nodes operation */ 85 + static ssize_t crw_inject_write(struct file *file, const char __user *buf, 86 + size_t lbuf, loff_t *ppos) 87 + { 88 + u32 slct, oflw, chn, rsc, anc, erc, rsid; 89 + struct crw crw; 90 + char *buffer; 91 + int rc; 92 + 93 + if (!static_branch_likely(&cio_inject_enabled)) { 94 + pr_warn("CIO inject is not enabled - ignoring CRW inject\n"); 95 + return -EINVAL; 96 + } 97 + 98 + buffer = vmemdup_user(buf, lbuf); 99 + if (IS_ERR(buffer)) 100 + return -ENOMEM; 101 + 102 + rc = sscanf(buffer, "%x %x %x %x %x %x %x", &slct, &oflw, &chn, &rsc, &anc, 103 + &erc, &rsid); 104 + 105 + kvfree(buffer); 106 + if (rc != 7) { 107 + pr_warn("crw_inject: Invalid format (need <solicited> <overflow> <chaining> <rsc> <ancillary> <erc> <rsid>)\n"); 108 + return -EINVAL; 109 + } 110 + 111 + memset(&crw, 0, sizeof(crw)); 112 + crw.slct = slct; 113 + crw.oflw = oflw; 114 + crw.chn = chn; 115 + crw.rsc = rsc; 116 + crw.anc = anc; 117 + crw.erc = erc; 118 + crw.rsid = rsid; 119 + 120 + rc = crw_inject(&crw); 121 + if (rc) 122 + return rc; 123 + 124 + return lbuf; 125 + } 126 + 127 + /* Debugfs write handler for inject_enable node*/ 128 + static ssize_t enable_inject_write(struct file *file, const char __user *buf, 129 + size_t lbuf, loff_t *ppos) 130 + { 131 + unsigned long en = 0; 132 + int rc; 133 + 134 + rc = kstrtoul_from_user(buf, lbuf, 10, &en); 135 + if (rc) 136 + return rc; 137 + 138 + switch (en) { 139 + case 0: 140 + static_branch_disable(&cio_inject_enabled); 141 + break; 142 + case 1: 143 + static_branch_enable(&cio_inject_enabled); 144 + break; 145 + } 146 + 147 + return lbuf; 148 + } 149 + 150 + static const struct file_operations crw_fops = { 151 + .owner = THIS_MODULE, 152 + .write = crw_inject_write, 153 + }; 154 + 155 + static const struct file_operations cio_en_fops = { 156 + .owner = THIS_MODULE, 157 + .write = enable_inject_write, 158 + }; 159 + 160 + static int __init cio_inject_init(void) 161 + { 162 + /* enable_inject node enables the static branching */ 163 + debugfs_create_file("enable_inject", 0200, cio_debugfs_dir, 164 + NULL, &cio_en_fops); 165 + 166 + debugfs_create_file("crw_inject", 0200, cio_debugfs_dir, 167 + NULL, &crw_fops); 168 + return 0; 169 + } 170 + 171 + device_initcall(cio_inject_init);
+18
drivers/s390/cio/cio_inject.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + /* 3 + * Copyright IBM Corp. 2021 4 + * Author(s): Vineeth Vijayan <vneethv@linux.ibm.com> 5 + */ 6 + 7 + #ifndef CIO_CRW_INJECT_H 8 + #define CIO_CRW_INJECT_H 9 + 10 + #ifdef CONFIG_CIO_INJECT 11 + 12 + #include <asm/crw.h> 13 + 14 + DECLARE_STATIC_KEY_FALSE(cio_inject_enabled); 15 + int stcrw_get_injected(struct crw *crw); 16 + 17 + #endif 18 + #endif
+22 -1
drivers/s390/cio/ioasm.c
··· 12 12 #include "ioasm.h" 13 13 #include "orb.h" 14 14 #include "cio.h" 15 + #include "cio_inject.h" 15 16 16 17 static inline int __stsch(struct subchannel_id schid, struct schib *addr) 17 18 { ··· 261 260 return ccode; 262 261 } 263 262 264 - int stcrw(struct crw *crw) 263 + static inline int __stcrw(struct crw *crw) 265 264 { 266 265 int ccode; 267 266 ··· 272 271 : "=d" (ccode), "=m" (*crw) 273 272 : "a" (crw) 274 273 : "cc"); 274 + return ccode; 275 + } 276 + 277 + static inline int _stcrw(struct crw *crw) 278 + { 279 + #ifdef CONFIG_CIO_INJECT 280 + if (static_branch_unlikely(&cio_inject_enabled)) { 281 + if (stcrw_get_injected(crw) == 0) 282 + return 0; 283 + } 284 + #endif 285 + 286 + return __stcrw(crw); 287 + } 288 + 289 + int stcrw(struct crw *crw) 290 + { 291 + int ccode; 292 + 293 + ccode = _stcrw(crw); 275 294 trace_s390_cio_stcrw(crw, ccode); 276 295 277 296 return ccode;