History

block: fix IOPRIO_PRIO_CLASS() and IOPRIO_PRIO_VALUE() macros

The ki_ioprio field of struct kiocb is 16-bits (u16) but often handled as an int in the block layer. E.g. ioprio_check_cap() takes an int as argument. With such implicit int casting function calls, the upper 16-bits of the int argument may be left uninitialized by the compiler, resulting in invalid values for the IOPRIO_PRIO_CLASS() macro (garbage upper bits) and in an error return for functions such as ioprio_check_cap(). Fix this by masking the result of the shift by IOPRIO_CLASS_SHIFT bits in the IOPRIO_PRIO_CLASS() macro. The new macro IOPRIO_CLASS_MASK defines the 3-bits mask for the priority class. Similarly, apply the IOPRIO_PRIO_MASK mask to the data argument of the IOPRIO_PRIO_VALUE() macro to ignore the upper bits of the data value. The IOPRIO_CLASS_MASK mask is also applied to the class argument of this macro before shifting the result by IOPRIO_CLASS_SHIFT bits. While at it, also change the argument name of the IOPRIO_PRIO_CLASS() and IOPRIO_PRIO_DATA() macros from "mask" to "ioprio" to reflect the fact that a priority value should be passed rather than a mask.

Damien Le Moal committed 5 years ago

Commit ba052001 parent a553a831 file · +8 −4Browse files

Showing the changes to include/uapi (1 of 1 file). Show all

Unified · Split

include/uapi/linux/ioprio.h+8−4
@@ -5,12 +5,16 @@ · 4 unmodified lines
55 /*
66 * Gives us 8 prio classes with 13-bits of data for each class
77 */
8−#define IOPRIO_CLASS_SHIFT (13)
8+#define IOPRIO_CLASS_SHIFT 13
9+#define IOPRIO_CLASS_MASK 0x07
910 #define IOPRIO_PRIO_MASK ((1UL << IOPRIO_CLASS_SHIFT) - 1)
1011
11−#define IOPRIO_PRIO_CLASS(mask) ((mask) >> IOPRIO_CLASS_SHIFT)
12−#define IOPRIO_PRIO_DATA(mask) ((mask) & IOPRIO_PRIO_MASK)
13−#define IOPRIO_PRIO_VALUE(class, data) (((class) << IOPRIO_CLASS_SHIFT) | data)
12+#define IOPRIO_PRIO_CLASS(ioprio) \
13+ (((ioprio) >> IOPRIO_CLASS_SHIFT) & IOPRIO_CLASS_MASK)
14+#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))
1418
1519 /*
1620 * These are the io priority groups as implemented by the BFQ and mq-deadline