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

scsi: block: Improve ioprio value validity checks

The introduction of the macro IOPRIO_PRIO_LEVEL() in commit eca2040972b4
("scsi: block: ioprio: Clean up interface definition") results in an
iopriority level to always be masked using the macro IOPRIO_LEVEL_MASK, and
thus to the kernel always seeing an acceptable value for an I/O priority
level when checked in ioprio_check_cap(). Before this patch, this function
would return an error for some (but not all) invalid values for a level
valid range of [0..7].

Restore and improve the detection of invalid priority levels by introducing
the inline function ioprio_value() to check an ioprio class, level and hint
value before combining these fields into a single value to be used with
ioprio_set() or AIOs. If an invalid value for the class, level or hint of
an ioprio is detected, ioprio_value() returns an ioprio using the class
IOPRIO_CLASS_INVALID, indicating an invalid value and causing
ioprio_check_cap() to return -EINVAL.

Fixes: 6c913257226a ("scsi: block: Introduce ioprio hints")
Fixes: eca2040972b4 ("scsi: block: ioprio: Clean up interface definition")
Signed-off-by: Damien Le Moal <dlemoal@kernel.org>
Link: https://lore.kernel.org/r/20230608095556.124001-1-dlemoal@kernel.org
Reviewed-by: Niklas Cassel <niklas.cassel@wdc.com>
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>

authored by

Damien Le Moal and committed by
Martin K. Petersen
01584c1e 022000d3

+34 -17
+1
block/ioprio.c
··· 58 58 if (level) 59 59 return -EINVAL; 60 60 break; 61 + case IOPRIO_CLASS_INVALID: 61 62 default: 62 63 return -EINVAL; 63 64 }
+33 -17
include/uapi/linux/ioprio.h
··· 2 2 #ifndef _UAPI_LINUX_IOPRIO_H 3 3 #define _UAPI_LINUX_IOPRIO_H 4 4 5 + #include <linux/stddef.h> 6 + #include <linux/types.h> 7 + 5 8 /* 6 9 * Gives us 8 prio classes with 13-bits of data for each class 7 10 */ 8 11 #define IOPRIO_CLASS_SHIFT 13 9 - #define IOPRIO_CLASS_MASK 0x07 12 + #define IOPRIO_NR_CLASSES 8 13 + #define IOPRIO_CLASS_MASK (IOPRIO_NR_CLASSES - 1) 10 14 #define IOPRIO_PRIO_MASK ((1UL << IOPRIO_CLASS_SHIFT) - 1) 11 15 12 16 #define IOPRIO_PRIO_CLASS(ioprio) \ 13 17 (((ioprio) >> IOPRIO_CLASS_SHIFT) & IOPRIO_CLASS_MASK) 14 18 #define IOPRIO_PRIO_DATA(ioprio) ((ioprio) & IOPRIO_PRIO_MASK) 15 - #define IOPRIO_PRIO_VALUE(class, data) \ 16 - ((((class) & IOPRIO_CLASS_MASK) << IOPRIO_CLASS_SHIFT) | \ 17 - ((data) & IOPRIO_PRIO_MASK)) 18 19 19 20 /* 20 21 * These are the io priority classes as implemented by the BFQ and mq-deadline ··· 26 25 * served when no one else is using the disk. 27 26 */ 28 27 enum { 29 - IOPRIO_CLASS_NONE, 30 - IOPRIO_CLASS_RT, 31 - IOPRIO_CLASS_BE, 32 - IOPRIO_CLASS_IDLE, 28 + IOPRIO_CLASS_NONE = 0, 29 + IOPRIO_CLASS_RT = 1, 30 + IOPRIO_CLASS_BE = 2, 31 + IOPRIO_CLASS_IDLE = 3, 32 + 33 + /* Special class to indicate an invalid ioprio value */ 34 + IOPRIO_CLASS_INVALID = 7, 33 35 }; 34 36 35 37 /* ··· 78 74 (((ioprio) >> IOPRIO_HINT_SHIFT) & IOPRIO_HINT_MASK) 79 75 80 76 /* 81 - * Alternate macro for IOPRIO_PRIO_VALUE() to define an I/O priority with 82 - * a class, level and hint. 83 - */ 84 - #define IOPRIO_PRIO_VALUE_HINT(class, level, hint) \ 85 - ((((class) & IOPRIO_CLASS_MASK) << IOPRIO_CLASS_SHIFT) | \ 86 - (((hint) & IOPRIO_HINT_MASK) << IOPRIO_HINT_SHIFT) | \ 87 - ((level) & IOPRIO_LEVEL_MASK)) 88 - 89 - /* 90 77 * I/O hints. 91 78 */ 92 79 enum { ··· 101 106 IOPRIO_HINT_DEV_DURATION_LIMIT_6 = 6, 102 107 IOPRIO_HINT_DEV_DURATION_LIMIT_7 = 7, 103 108 }; 109 + 110 + #define IOPRIO_BAD_VALUE(val, max) ((val) < 0 || (val) >= (max)) 111 + 112 + /* 113 + * Return an I/O priority value based on a class, a level and a hint. 114 + */ 115 + static __always_inline __u16 ioprio_value(int class, int level, int hint) 116 + { 117 + if (IOPRIO_BAD_VALUE(class, IOPRIO_NR_CLASSES) || 118 + IOPRIO_BAD_VALUE(level, IOPRIO_NR_LEVELS) || 119 + IOPRIO_BAD_VALUE(hint, IOPRIO_NR_HINTS)) 120 + return IOPRIO_CLASS_INVALID << IOPRIO_CLASS_SHIFT; 121 + 122 + return (class << IOPRIO_CLASS_SHIFT) | 123 + (hint << IOPRIO_HINT_SHIFT) | level; 124 + } 125 + 126 + #define IOPRIO_PRIO_VALUE(class, level) \ 127 + ioprio_value(class, level, IOPRIO_HINT_NONE) 128 + #define IOPRIO_PRIO_VALUE_HINT(class, level, hint) \ 129 + ioprio_value(class, level, hint) 104 130 105 131 #endif /* _UAPI_LINUX_IOPRIO_H */