Skip to contentSkip to conversation

overflow: Make sure size helpers are always inlined

PATCH In review Kees Cook 2 participants 2 messages Feb 24 Kernel hardening (not covered by other areas)

lore
Share & tools
lore

Kees Cook

author maintainer
#0 lore raw

PATCH 1/1 overflow: Make sure size helpers are always inlined

With kmalloc_obj() performing implicit size calculations, the embedded size_mul() calls, while marked inline, were not always being inlined. I noticed a couple places where allocations were making a call out for things that would otherwise be compile-time calculated. Force the compilers to always inline these calculations.

---
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: <[email protected]>

---
 include/linux/overflow.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
include/linux/overflow.h +4 -4
index eddd987a8513..a8cb6319b4fb 100644
@@ -42,7 +42,7 @@
  * both the type-agnostic benefits of the macros while also being able to
  * enforce that the return value is, in fact, checked.
  */
-static inline bool __must_check __must_check_overflow(bool overflow)
+static __always_inline bool __must_check __must_check_overflow(bool overflow)
 {
 	return unlikely(overflow);
 }
@@ -327,7 +327,7 @@ static inline bool __must_check __must_check_overflow(bool overflow)
  * with any overflow causing the return value to be SIZE_MAX. The
  * lvalue must be size_t to avoid implicit type conversion.
  */
-static inline size_t __must_check size_mul(size_t factor1, size_t factor2)
+static __always_inline size_t __must_check size_mul(size_t factor1, size_t factor2)
 {
 	size_t bytes;
 
@@ -346,7 +346,7 @@ static inline size_t __must_check size_mul(size_t factor1, size_t factor2)
  * with any overflow causing the return value to be SIZE_MAX. The
  * lvalue must be size_t to avoid implicit type conversion.
  */
-static inline size_t __must_check size_add(size_t addend1, size_t addend2)
+static __always_inline size_t __must_check size_add(size_t addend1, size_t addend2)
 {
 	size_t bytes;
 
@@ -367,7 +367,7 @@ static inline size_t __must_check size_add(size_t addend1, size_t addend2)
  * argument may be SIZE_MAX (or the result with be forced to SIZE_MAX).
  * The lvalue must be size_t to avoid implicit type conversion.
  */
-static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
+static __always_inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
 {
 	size_t bytes;
 
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Kees Cook

With kmalloc_obj() performing implicit size calculations, the embedded size_mul() calls, while marked inline, were not always being inlined. I noticed a couple places where allocations were making a call out for things that would otherwise be compile-time calculated. Force the compilers to always inline these calculations.

Thanks!
-Gustavo

Quoted 47 lines

---
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: <[email protected]>

---
  include/linux/overflow.h | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
include/linux/overflow.h +4 -4
index eddd987a8513..a8cb6319b4fb 100644
@@ -42,7 +42,7 @@
   * both the type-agnostic benefits of the macros while also being able to
   * enforce that the return value is, in fact, checked.
   */
-static inline bool __must_check __must_check_overflow(bool overflow)
+static __always_inline bool __must_check __must_check_overflow(bool overflow)
  {
  	return unlikely(overflow);
  }
@@ -327,7 +327,7 @@ static inline bool __must_check __must_check_overflow(bool overflow)
   * with any overflow causing the return value to be SIZE_MAX. The
   * lvalue must be size_t to avoid implicit type conversion.
   */
-static inline size_t __must_check size_mul(size_t factor1, size_t factor2)
+static __always_inline size_t __must_check size_mul(size_t factor1, size_t factor2)
  {
  	size_t bytes;
 
@@ -346,7 +346,7 @@ static inline size_t __must_check size_mul(size_t factor1, size_t factor2)
   * with any overflow causing the return value to be SIZE_MAX. The
   * lvalue must be size_t to avoid implicit type conversion.
   */
-static inline size_t __must_check size_add(size_t addend1, size_t addend2)
+static __always_inline size_t __must_check size_add(size_t addend1, size_t addend2)
  {
  	size_t bytes;
 
@@ -367,7 +367,7 @@ static inline size_t __must_check size_add(size_t addend1, size_t addend2)
   * argument may be SIZE_MAX (or the result with be forced to SIZE_MAX).
   * The lvalue must be size_t to avoid implicit type conversion.
   */
-static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
+static __always_inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
  {
  	size_t bytes;