[1/2] wordpart.h: Helpers for making u16/u32/u64 values

Message ID 20240214214654.1700-1-michal.wajdeczko@intel.com
State New
Headers
Series [1/2] wordpart.h: Helpers for making u16/u32/u64 values |

Commit Message

Michal Wajdeczko Feb. 14, 2024, 9:46 p.m. UTC
  It is quite common practice to make u16, u32 or u64 values from
smaller words.  Add simple helpers for that.

Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
---
v2: new macro names due to conflict with crypto/aria.h
    explicit cast and truncation everywhere (Alexey)
    moved to wordpart.h (Andy)
---
Cc: Kees Cook <keescook@chromium.org>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Jani Nikula <jani.nikula@intel.com>
---
 include/linux/wordpart.h | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
  

Comments

Kees Cook Feb. 14, 2024, 10:09 p.m. UTC | #1
On Wed, Feb 14, 2024 at 10:46:53PM +0100, Michal Wajdeczko wrote:
> It is quite common practice to make u16, u32 or u64 values from
> smaller words.  Add simple helpers for that.
> 
> Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
> ---
> v2: new macro names due to conflict with crypto/aria.h
>     explicit cast and truncation everywhere (Alexey)
>     moved to wordpart.h (Andy)
> ---
> Cc: Kees Cook <keescook@chromium.org>
> Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> Cc: Alexey Dobriyan <adobriyan@gmail.com>
> Cc: Jani Nikula <jani.nikula@intel.com>
> ---
>  include/linux/wordpart.h | 32 ++++++++++++++++++++++++++++++++
>  1 file changed, 32 insertions(+)
> 
> diff --git a/include/linux/wordpart.h b/include/linux/wordpart.h
> index f6f8f83b15b0..8c75a5355112 100644
> --- a/include/linux/wordpart.h
> +++ b/include/linux/wordpart.h
> @@ -31,6 +31,38 @@
>   */
>  #define lower_16_bits(n) ((u16)((n) & 0xffff))
>  
> +/**
> + * make_u16_from_u8 - make u16 value from two u8 values
> + * @hi: value representing upper 8 bits
> + * @lo: value representing lower 8 bits
> + */
> +#define make_u16_from_u8(hi, lo) ((u16)((u16)(u8)(hi) << 8 | (u8)(lo)))

Do we want to actually do type validation here? Right now it's just
cast/truncating, which based on the version log is by design. Is silent
truncation the right thing to do?
  
Michal Wajdeczko Feb. 15, 2024, 8:40 p.m. UTC | #2
On 14.02.2024 23:09, Kees Cook wrote:
> On Wed, Feb 14, 2024 at 10:46:53PM +0100, Michal Wajdeczko wrote:
>> It is quite common practice to make u16, u32 or u64 values from
>> smaller words.  Add simple helpers for that.
>>
>> Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
>> ---
>> v2: new macro names due to conflict with crypto/aria.h
>>     explicit cast and truncation everywhere (Alexey)
>>     moved to wordpart.h (Andy)
>> ---
>> Cc: Kees Cook <keescook@chromium.org>
>> Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
>> Cc: Alexey Dobriyan <adobriyan@gmail.com>
>> Cc: Jani Nikula <jani.nikula@intel.com>
>> ---
>>  include/linux/wordpart.h | 32 ++++++++++++++++++++++++++++++++
>>  1 file changed, 32 insertions(+)
>>
>> diff --git a/include/linux/wordpart.h b/include/linux/wordpart.h
>> index f6f8f83b15b0..8c75a5355112 100644
>> --- a/include/linux/wordpart.h
>> +++ b/include/linux/wordpart.h
>> @@ -31,6 +31,38 @@
>>   */
>>  #define lower_16_bits(n) ((u16)((n) & 0xffff))
>>  
>> +/**
>> + * make_u16_from_u8 - make u16 value from two u8 values
>> + * @hi: value representing upper 8 bits
>> + * @lo: value representing lower 8 bits
>> + */
>> +#define make_u16_from_u8(hi, lo) ((u16)((u16)(u8)(hi) << 8 | (u8)(lo)))
> 
> Do we want to actually do type validation here? Right now it's just
> cast/truncating, which based on the version log is by design. Is silent
> truncation the right thing to do?

note that even FIELD_PREP() is doing silent truncation and these macros
here could be treated as specialized/simplified variants of FIELD_PREP()
as alternate implementation can look like:

#define make_u16_from_u8(hi, lo) \
	((u16)(FIELD_PREP_CONST(GENMASK(15, 8), (hi)) | \
	       FIELD_PREP_CONST(GENMASK(7, 0), (lo))))

#define make_u32_from_u16(hi, lo) \
	((u32)(FIELD_PREP_CONST(GENMASK(31, 16), (hi)) | \
	       FIELD_PREP_CONST(GENMASK(15, 0), (lo))))

#define make_u64_from_u32(hi, lo) \
	((u64)(FIELD_PREP_CONST(GENMASK_ULL(63, 32), (hi)) | \
	       FIELD_PREP_CONST(GENMASK_ULL(31, 0), (lo))))

but then it will not match simplicity of the lower|upper_XX_bits macros
  
Kees Cook Feb. 15, 2024, 10:47 p.m. UTC | #3
On Thu, Feb 15, 2024 at 09:40:40PM +0100, Michal Wajdeczko wrote:
> On 14.02.2024 23:09, Kees Cook wrote:
> > On Wed, Feb 14, 2024 at 10:46:53PM +0100, Michal Wajdeczko wrote:
> >> It is quite common practice to make u16, u32 or u64 values from
> >> smaller words.  Add simple helpers for that.
> >>
> >> Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
> >> ---
> >> v2: new macro names due to conflict with crypto/aria.h
> >>     explicit cast and truncation everywhere (Alexey)
> >>     moved to wordpart.h (Andy)
> >> ---
> >> Cc: Kees Cook <keescook@chromium.org>
> >> Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> >> Cc: Alexey Dobriyan <adobriyan@gmail.com>
> >> Cc: Jani Nikula <jani.nikula@intel.com>
> >> ---
> >>  include/linux/wordpart.h | 32 ++++++++++++++++++++++++++++++++
> >>  1 file changed, 32 insertions(+)
> >>
> >> diff --git a/include/linux/wordpart.h b/include/linux/wordpart.h
> >> index f6f8f83b15b0..8c75a5355112 100644
> >> --- a/include/linux/wordpart.h
> >> +++ b/include/linux/wordpart.h
> >> @@ -31,6 +31,38 @@
> >>   */
> >>  #define lower_16_bits(n) ((u16)((n) & 0xffff))
> >>  
> >> +/**
> >> + * make_u16_from_u8 - make u16 value from two u8 values
> >> + * @hi: value representing upper 8 bits
> >> + * @lo: value representing lower 8 bits
> >> + */
> >> +#define make_u16_from_u8(hi, lo) ((u16)((u16)(u8)(hi) << 8 | (u8)(lo)))
> > 
> > Do we want to actually do type validation here? Right now it's just
> > cast/truncating, which based on the version log is by design. Is silent
> > truncation the right thing to do?
> 
> note that even FIELD_PREP() is doing silent truncation and these macros
> here could be treated as specialized/simplified variants of FIELD_PREP()
> as alternate implementation can look like:

Also, isn't all of this endian-specific?
  

Patch

diff --git a/include/linux/wordpart.h b/include/linux/wordpart.h
index f6f8f83b15b0..8c75a5355112 100644
--- a/include/linux/wordpart.h
+++ b/include/linux/wordpart.h
@@ -31,6 +31,38 @@ 
  */
 #define lower_16_bits(n) ((u16)((n) & 0xffff))
 
+/**
+ * make_u16_from_u8 - make u16 value from two u8 values
+ * @hi: value representing upper 8 bits
+ * @lo: value representing lower 8 bits
+ */
+#define make_u16_from_u8(hi, lo) ((u16)((u16)(u8)(hi) << 8 | (u8)(lo)))
+
+/**
+ * make_u32_from_u16 - make u32 value from two u16 values
+ * @hi: value representing upper 16 bits
+ * @lo: value representing lower 16 bits
+ */
+#define make_u32_from_u16(hi, lo) ((u32)((u32)(u16)(hi) << 16 | (u16)(lo)))
+
+/**
+ * make_u32_from_u8 - make u32 value from u8 values
+ * @a: value representing bits 31-24
+ * @b: value representing bits 23-16
+ * @c: value representing bits 15-8
+ * @d: value representing bits 7-0
+ */
+#define make_u32_from_u8(a, b, c, d) \
+	make_u32_from_u16(make_u16_from_u8((a), (b)), \
+			  make_u16_from_u8((c), (d)))
+
+/**
+ * make_u64_from_u32 - make u64 value from two u32 values
+ * @hi: value representing upper 32 bits
+ * @lo: value representing lower 32 bits
+ */
+#define make_u64_from_u32(hi, lo) ((u64)((u64)(u32)(hi) << 32 | (u32)(lo)))
+
 /**
  * REPEAT_BYTE - repeat the value @x multiple times as an unsigned long value
  * @x: value to repeat