mirror of
https://github.com/torvalds/linux.git
synced 2026-11-20 13:03:48 +00:00
MKDEV(ma, mi) evaluates ((ma) << MINORBITS) in whatever type the caller passes, usually int. For majors >= 2048 (legal: majors go up to 4095) the result exceeds INT_MAX, which C11 leaves undefined for a signed left shift; shifting a negative ma is undefined regardless of the major's value. In practice, on the compilers and two's complement targets the kernel supports, both cases wrap to the intended bit pattern; this patch makes the macro well defined regardless by casting the major to dev_t before shifting. For major and minor numbers in range, the device number it produces is unchanged. isofs feeds the Rock Ridge 'PN' entry's dev_high and dev_low into MKDEV() while holding them in int variables (fs/isofs/rock.c), so a crafted image can drive this exact shift. Fixing the macro covers every caller instead of just that one. Link: https://lore.kernel.org/[email protected] Signed-off-by: Matthias Goergens <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Reviewed-by: Jan Kara <[email protected]> Cc: Greg Kroah-Hartman <[email protected]>
84 lines
1.8 KiB
C
84 lines
1.8 KiB
C
/* SPDX-License-Identifier: GPL-2.0 */
|
|
#ifndef _LINUX_KDEV_T_H
|
|
#define _LINUX_KDEV_T_H
|
|
|
|
#include <uapi/linux/kdev_t.h>
|
|
|
|
#define MINORBITS 20
|
|
#define MINORMASK ((1U << MINORBITS) - 1)
|
|
|
|
#define MAJOR(dev) ((unsigned int) ((dev) >> MINORBITS))
|
|
#define MINOR(dev) ((unsigned int) ((dev) & MINORMASK))
|
|
#define MKDEV(ma, mi) (((dev_t)(ma) << MINORBITS) | (mi))
|
|
|
|
#define print_dev_t(buffer, dev) \
|
|
sprintf((buffer), "%u:%u\n", MAJOR(dev), MINOR(dev))
|
|
|
|
#define format_dev_t(buffer, dev) \
|
|
({ \
|
|
sprintf(buffer, "%u:%u", MAJOR(dev), MINOR(dev)); \
|
|
buffer; \
|
|
})
|
|
|
|
/* acceptable for old filesystems */
|
|
static __always_inline bool old_valid_dev(dev_t dev)
|
|
{
|
|
return MAJOR(dev) < 256 && MINOR(dev) < 256;
|
|
}
|
|
|
|
static __always_inline u16 old_encode_dev(dev_t dev)
|
|
{
|
|
return (MAJOR(dev) << 8) | MINOR(dev);
|
|
}
|
|
|
|
static __always_inline dev_t old_decode_dev(u16 val)
|
|
{
|
|
return MKDEV((val >> 8) & 255, val & 255);
|
|
}
|
|
|
|
static __always_inline u32 new_encode_dev(dev_t dev)
|
|
{
|
|
unsigned major = MAJOR(dev);
|
|
unsigned minor = MINOR(dev);
|
|
return (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
|
|
}
|
|
|
|
static __always_inline dev_t new_decode_dev(u32 dev)
|
|
{
|
|
unsigned major = (dev & 0xfff00) >> 8;
|
|
unsigned minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
|
|
return MKDEV(major, minor);
|
|
}
|
|
|
|
static __always_inline u64 huge_encode_dev(dev_t dev)
|
|
{
|
|
return new_encode_dev(dev);
|
|
}
|
|
|
|
static __always_inline dev_t huge_decode_dev(u64 dev)
|
|
{
|
|
return new_decode_dev(dev);
|
|
}
|
|
|
|
static __always_inline int sysv_valid_dev(dev_t dev)
|
|
{
|
|
return MAJOR(dev) < (1<<14) && MINOR(dev) < (1<<18);
|
|
}
|
|
|
|
static __always_inline u32 sysv_encode_dev(dev_t dev)
|
|
{
|
|
return MINOR(dev) | (MAJOR(dev) << 18);
|
|
}
|
|
|
|
static __always_inline unsigned sysv_major(u32 dev)
|
|
{
|
|
return (dev >> 18) & 0x3fff;
|
|
}
|
|
|
|
static __always_inline unsigned sysv_minor(u32 dev)
|
|
{
|
|
return dev & 0x3ffff;
|
|
}
|
|
|
|
#endif
|