diff mbox series

[V3] block: uapi: Use unsigned int type for IOPRIO_PRIO_MASK

Message ID 1722409287-12183-1-git-send-email-zhiguo.niu@unisoc.com (mailing list archive)
State New, archived
Headers show
Series [V3] block: uapi: Use unsigned int type for IOPRIO_PRIO_MASK | expand

Commit Message

Zhiguo Niu July 31, 2024, 7:01 a.m. UTC
Generally, the input of IOPRIO_PRIO_DATA has 16 bits, but the output of
IOPRIO_PRIO_DATA will be expanded to "UL" from IOPRIO_PRIO_MASK.
 #define IOPRIO_PRIO_MASK	((1UL << IOPRIO_CLASS_SHIFT) - 1)
This is not reasonable and meaningless, unsigned int is more suitable for it.

So if use format "%d" to print IOPRIO_PRIO_DATA directly, there will be a
build warning or error showned as the following, which is from the
local test when I modify f2fs codes.

fs/f2fs/sysfs.c:348:31: warning: format ‘%d’ expects argument of type ‘int’,
but argument 4 has type ‘long unsigned int’ [-Wformat=]
   return sysfs_emit(buf, "%s,%d\n",
                              ~^
                              %ld

When modules use IOPRIO_PRIO_CLASS & IOPRIO_PRIO_LEVEL get ioprio's class and
level, their outputs are both unsigned int.
 IOPRIO_CLASS_MASK is:
 #define IOPRIO_CLASS_SHIFT	13
 #define IOPRIO_NR_CLASSES	8
 #define IOPRIO_CLASS_MASK	(IOPRIO_NR_CLASSES - 1)
 IOPRIO_LEVEL_MASK is:
 #define IOPRIO_LEVEL_NR_BITS	3
 #define IOPRIO_NR_LEVELS	(1 << IOPRIO_LEVEL_NR_BITS)
 #define IOPRIO_LEVEL_MASK	(IOPRIO_NR_LEVELS - 1)

Ioprio is passed along as an int internally, so we should not be using an
unsigned long for IOPRIO_PRIO_MASK to not end up with IOPRIO_PRIO_DATA
returning an unsigned long as well.

Fixes: 06447ae5e33b ("ioprio: move user space relevant ioprio bits to UAPI includes")
Cc: stable@vger.kernel.org
Cc: Oliver Hartkopp <socketcan@hartkopp.net>
Signed-off-by: Zhiguo Niu <zhiguo.niu@unisoc.com>
Reviewed-by: Bart Van Assche <bvanassche@acm.org>
Link: https://lore.kernel.org/all/1717155071-20409-1-git-send-email-zhiguo.niu@unisoc.com
---
v3: modify commit message according to Damien Le Moal'ssuggestion
v2: add Fixes tag and Cc tag
---
---
 include/uapi/linux/ioprio.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Damien Le Moal July 31, 2024, 9:28 a.m. UTC | #1
On 7/31/24 16:01, Zhiguo Niu wrote:
> Generally, the input of IOPRIO_PRIO_DATA has 16 bits, but the output of
> IOPRIO_PRIO_DATA will be expanded to "UL" from IOPRIO_PRIO_MASK.
>  #define IOPRIO_PRIO_MASK	((1UL << IOPRIO_CLASS_SHIFT) - 1)
> This is not reasonable and meaningless, unsigned int is more suitable for it.
> 
> So if use format "%d" to print IOPRIO_PRIO_DATA directly, there will be a
> build warning or error showned as the following, which is from the
> local test when I modify f2fs codes.
> 
> fs/f2fs/sysfs.c:348:31: warning: format ‘%d’ expects argument of type ‘int’,
> but argument 4 has type ‘long unsigned int’ [-Wformat=]
>    return sysfs_emit(buf, "%s,%d\n",
>                               ~^
>                               %ld
> 
> When modules use IOPRIO_PRIO_CLASS & IOPRIO_PRIO_LEVEL get ioprio's class and
> level, their outputs are both unsigned int.
>  IOPRIO_CLASS_MASK is:
>  #define IOPRIO_CLASS_SHIFT	13
>  #define IOPRIO_NR_CLASSES	8
>  #define IOPRIO_CLASS_MASK	(IOPRIO_NR_CLASSES - 1)
>  IOPRIO_LEVEL_MASK is:
>  #define IOPRIO_LEVEL_NR_BITS	3
>  #define IOPRIO_NR_LEVELS	(1 << IOPRIO_LEVEL_NR_BITS)
>  #define IOPRIO_LEVEL_MASK	(IOPRIO_NR_LEVELS - 1)
> 
> Ioprio is passed along as an int internally, so we should not be using an
> unsigned long for IOPRIO_PRIO_MASK to not end up with IOPRIO_PRIO_DATA
> returning an unsigned long as well.

I would write this commit message like this:


An ioprio is passed internally as an int value. When IOPRIO_PRIO_CLASS() and
IOPRIO_PRIO_LEVEL() are used to extract from it the priority class and level,
the values obtained are thus also int.
However, the IOPRIO_PRIO_MASK() macro used to define the IOPRIO_PRIO_DATA()
macro is defined as:

#define IOPRIO_PRIO_MASK	((1UL << IOPRIO_CLASS_SHIFT) - 1)

that is, the macro gives an unsigned long value, which leads to
IOPRIO_PRIO_DATA() also returning an unsigned long.

Make things consistent between class, level and data and use int everywhere by
removing forced unsigned long from IOPRIO_PRIO_MASK.
Zhiguo Niu July 31, 2024, 11:07 a.m. UTC | #2
Hi Damien Le Moal

Damien Le Moal <dlemoal@kernel.org> 于2024年7月31日周三 17:28写道:
>
> On 7/31/24 16:01, Zhiguo Niu wrote:
> > Generally, the input of IOPRIO_PRIO_DATA has 16 bits, but the output of
> > IOPRIO_PRIO_DATA will be expanded to "UL" from IOPRIO_PRIO_MASK.
> >  #define IOPRIO_PRIO_MASK     ((1UL << IOPRIO_CLASS_SHIFT) - 1)
> > This is not reasonable and meaningless, unsigned int is more suitable for it.
> >
> > So if use format "%d" to print IOPRIO_PRIO_DATA directly, theire will be a
> > build warning or error showned as the following, which is from the
> > local test when I modify f2fs codes.
> >
> > fs/f2fs/sysfs.c:348:31: warning: format ‘%d’ expects argument of type ‘int’,
> > but argument 4 has type ‘long unsigned int’ [-Wformat=]
> >    return sysfs_emit(buf, "%s,%d\n",
> >                               ~^
> >                               %ld
> >
> > When modules use IOPRIO_PRIO_CLASS & IOPRIO_PRIO_LEVEL get ioprio's class and
> > level, their outputs are both unsigned int.
> >  IOPRIO_CLASS_MASK is:
> >  #define IOPRIO_CLASS_SHIFT   13
> >  #define IOPRIO_NR_CLASSES    8
> >  #define IOPRIO_CLASS_MASK    (IOPRIO_NR_CLASSES - 1)
> >  IOPRIO_LEVEL_MASK is:
> >  #define IOPRIO_LEVEL_NR_BITS 3
> >  #define IOPRIO_NR_LEVELS     (1 << IOPRIO_LEVEL_NR_BITS)
> >  #define IOPRIO_LEVEL_MASK    (IOPRIO_NR_LEVELS - 1)
> >
> > Ioprio is passed along as an int internally, so we should not be using an
> > unsigned long for IOPRIO_PRIO_MASK to not end up with IOPRIO_PRIO_DATA
> > returning an unsigned long as well.
>
> I would write this commit message like this:
>
>
> An ioprio is passed internally as an int value. When IOPRIO_PRIO_CLASS() and
> IOPRIO_PRIO_LEVEL() are used to extract from it the priority class and level,
> the values obtained are thus also int.
> However, the IOPRIO_PRIO_MASK() macro used to define the IOPRIO_PRIO_DATA()
> macro is defined as:
>
> #define IOPRIO_PRIO_MASK        ((1UL << IOPRIO_CLASS_SHIFT) - 1)
>
> that is, the macro gives an unsigned long value, which leads to
> IOPRIO_PRIO_DATA() also returning an unsigned long.
>
> Make things consistent between class, level and data and use int everywhere by
> removing forced unsigned long from IOPRIO_PRIO_MASK.

Thank you for your professional advice and sharing. I will update this.

>
>
>
> --
> Damien Le Moal
> Western Digital Research
>
diff mbox series

Patch

diff --git a/include/uapi/linux/ioprio.h b/include/uapi/linux/ioprio.h
index bee2bdb0..9ead07f 100644
--- a/include/uapi/linux/ioprio.h
+++ b/include/uapi/linux/ioprio.h
@@ -11,7 +11,7 @@ 
 #define IOPRIO_CLASS_SHIFT	13
 #define IOPRIO_NR_CLASSES	8
 #define IOPRIO_CLASS_MASK	(IOPRIO_NR_CLASSES - 1)
-#define IOPRIO_PRIO_MASK	((1UL << IOPRIO_CLASS_SHIFT) - 1)
+#define IOPRIO_PRIO_MASK	((1U << IOPRIO_CLASS_SHIFT) - 1)
 
 #define IOPRIO_PRIO_CLASS(ioprio)	\
 	(((ioprio) >> IOPRIO_CLASS_SHIFT) & IOPRIO_CLASS_MASK)