From nobody Wed Dec 17 15:31:53 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9853D1A2FD9; Tue, 16 Jul 2024 18:33:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721154820; cv=none; b=fTiWKZHQcPW//1b6rLTWJHNp5eArKl+92DiYVA/Ws0PVk4a1w8dawUxgUYO8TyA56YeLiSUsXOSlyvJ/Kj+2aQHf3/WUWTqADzSRZP+Xj8kh22tBfcIkAYd2PO8bzs3FIKeEbrSc/juRC6VygtcbVX05LP+o9x1/xIqLSy2PR7Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721154820; c=relaxed/simple; bh=eSQnHIurCv0WdHboEQsJS3wu5QW1VDQGyaA45BgH5o4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=mWd2jJCXk3udrfen+WET+lkGwlCJ1tGDs/xbRNB+k05djx/NR0vTDjXfE6zlLYyRx78yObI4Ux1cwCUleXMU63RadqwpHxNoThqK5lvA0iW8Ybu5Ub4J93fxZGv7kqxdxzonK2jkmv/TIA6aRVJJ1h7WAMSzroVqEbjjUppvphY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rwDOK7Iy; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="rwDOK7Iy" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 48C28C4AF0F; Tue, 16 Jul 2024 18:33:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721154820; bh=eSQnHIurCv0WdHboEQsJS3wu5QW1VDQGyaA45BgH5o4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rwDOK7IyT/ZM6ao4qA5AG2Tzcp31+ojHGao7k37lf8pMqKmRozSAIKd6x/tc0nA1u t3sooCktKLoSa36FQTQRpbyBYJrGeoHp+ovrdg4d2Tb9hIwIf4AX7mYsvIIzmCzY6K O0z7Mj9FGdOQhB1vYIsuzeXKCoJZckKpXEBFuExzscISoUr4m03JQmZWhBHvvBnacw tUAc4zgVEe6aiLne9vW9GsMSqbsXOzB8eWnBiCw6ABuEnpvi/4SEOZLyLeZJT1e5Y4 XzBJ3LX7JcCpq0HIEAswkqELRsUle9OfN1Lqe30mjO8XcXN0rd2kW4H2ZTrUWmQW8D 8apbV1iL8Dwog== From: SeongJae Park To: stable@vger.kernel.org, gregkh@linuxfoundation.org Cc: Bart Van Assche , Luc Van Oostenryck , Steven Rostedt , Ingo Molnar , linux-sparse@vger.kernel.org, linux-kernel@vger.kernel.org, Rasmus Villemoes , Kees Cook , Linus Torvalds , SeongJae Park Subject: [PATCH 5.15.y 1/8] tracing: Define the is_signed_type() macro once Date: Tue, 16 Jul 2024 11:33:26 -0700 Message-Id: <20240716183333.138498-2-sj@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240716183333.138498-1-sj@kernel.org> References: <20240716183333.138498-1-sj@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Bart Van Assche commit a49a64b5bf195381c09202c524f0f84b5f3e816f upstream. There are two definitions of the is_signed_type() macro: one in and a second definition in . As suggested by Linus, move the definition of the is_signed_type() macro into the header file. Change the definition of the is_signed_type() macro to make sure that it does not trigger any sparse warnings with future versions of sparse for bitwise types. Link: https://lore.kernel.org/all/CAHk-=3DwhjH6p+qzwUdx5SOVVHjS3WvzJQr6mDUw= hEyTf6pJWzaQ@mail.gmail.com/ Link: https://lore.kernel.org/all/CAHk-=3DwjQGnVfb4jehFR0XyZikdQvCZouE96xR_= nnf5kqaM5qqQ@mail.gmail.com/ Cc: Rasmus Villemoes Cc: Steven Rostedt Acked-by: Kees Cook Signed-off-by: Bart Van Assche Signed-off-by: Linus Torvalds (cherry picked from commit a49a64b5bf195381c09202c524f0f84b5f3e816f) Signed-off-by: SeongJae Park --- include/linux/compiler.h | 6 ++++++ include/linux/overflow.h | 1 - include/linux/trace_events.h | 2 -- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/include/linux/compiler.h b/include/linux/compiler.h index 0f7fd205ab7e..65111de4ad6b 100644 --- a/include/linux/compiler.h +++ b/include/linux/compiler.h @@ -246,6 +246,12 @@ static inline void *offset_to_ptr(const int *off) /* &a[0] degrades to a pointer: a different type from an array */ #define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0])) =20 +/* + * Whether 'type' is a signed type or an unsigned type. Supports scalar ty= pes, + * bool and also pointer types. + */ +#define is_signed_type(type) (((type)(-1)) < (__force type)1) + /* * This is needed in functions which generate the stack canary, see * arch/x86/kernel/smpboot.c::start_secondary() for an example. diff --git a/include/linux/overflow.h b/include/linux/overflow.h index 73bc67ec2136..e6bf14f462e9 100644 --- a/include/linux/overflow.h +++ b/include/linux/overflow.h @@ -29,7 +29,6 @@ * https://mail-index.netbsd.org/tech-misc/2007/02/05/0000.html - * credit to Christian Biere. */ -#define is_signed_type(type) (((type)(-1)) < (type)1) #define __type_half_max(type) ((type)1 << (8*sizeof(type) - 1 - is_signed_= type(type))) #define type_max(T) ((T)((__type_half_max(T) - 1) + __type_half_max(T))) #define type_min(T) ((T)((T)-type_max(T)-(T)1)) diff --git a/include/linux/trace_events.h b/include/linux/trace_events.h index 17575aa2a53c..511c43ce9421 100644 --- a/include/linux/trace_events.h +++ b/include/linux/trace_events.h @@ -801,8 +801,6 @@ extern int trace_add_event_call(struct trace_event_call= *call); extern int trace_remove_event_call(struct trace_event_call *call); extern int trace_event_get_offsets(struct trace_event_call *call); =20 -#define is_signed_type(type) (((type)(-1)) < (type)1) - int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set); int trace_set_clr_event(const char *system, const char *event, int set); int trace_array_set_clr_event(struct trace_array *tr, const char *system, --=20 2.39.2 From nobody Wed Dec 17 15:31:54 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5B79F1A38D0; Tue, 16 Jul 2024 18:33:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721154821; cv=none; b=eUNu5jcjA8v1Bja67tibwtwvhODpHIjGkHDNHMH7l3UkCfSuUHSntiPYcI6iwh2npcs1zvFg+5rDIWl12biWQvxSIKdsCnqQNssvEGAdn+cymN+RcxdkTwHeMXco3Hc2sURUUcV2gyyYn+m8v4lKHygssq48dfK3Frh7unQuKgw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721154821; c=relaxed/simple; bh=p3+cbQrtzD34rlMr4M+1CIjAqE6/GqsWTPXFKfSsxdw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=fcUsKxVjBe3dfpfa4UN9nQGeHesZy0JQBy9scVVpPilIGfFBM1rb1Y6GRuuSAWImRkFux2aK7alX1u0K08/R6OBbU99xdwVr54lWyhIpBDNEceO87HXBAzul7M6jCeMqOK+WqXN0NTNsGFr2K8kEPNRlIFuytWl8XGl+RgvVR+0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=NH9hh+Dh; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="NH9hh+Dh" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 60F1AC4AF16; Tue, 16 Jul 2024 18:33:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721154820; bh=p3+cbQrtzD34rlMr4M+1CIjAqE6/GqsWTPXFKfSsxdw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NH9hh+DhItnmgk775fsd6NbmWuiEbaBKpvEwb4dXxlZVeSj0GBRgJZlbYH9P7eb8A xO40NafFyNbr4aEBk6dxOSs3RR1cQl+IQ7xBq18iOceYhJTwog2wXUyD9fTB/VWU3Z N+Ul0eoljI4FJ9Kk9azWQhtuQNfHF+38VoIRKe6uQQT2eFcMD2IvNwPoFCkKv5u2Ph m7R+w1jXABvFjL2H+Y8jkeY4cJR4ag5NUYg+gqviveoCS76Ks0T/UQGjmeBx9nuy5D vW6HJujf3ru8ChCorbxi+D0KvcoeNH7VVORTaKzvd5e7MWTt0L+MP1C7QJkVYcUp2j j1gNQ1aX+Ktmg== From: SeongJae Park To: stable@vger.kernel.org, gregkh@linuxfoundation.org Cc: "Jason A. Donenfeld" , linux-kernel@vger.kernel.org, Andy Shevchenko , Kees Cook , Andrew Morton , SeongJae Park Subject: [PATCH 5.15.y 2/8] minmax: sanity check constant bounds when clamping Date: Tue, 16 Jul 2024 11:33:27 -0700 Message-Id: <20240716183333.138498-3-sj@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240716183333.138498-1-sj@kernel.org> References: <20240716183333.138498-1-sj@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Jason A. Donenfeld" commit 5efcecd9a3b18078d3398b359a84c83f549e22cf upstream. The clamp family of functions only makes sense if hi>=3Dlo. If hi and lo are compile-time constants, then raise a build error. Doing so has already caught buggy code. This also introduces the infrastructure to improve the clamping function in subsequent commits. [akpm@linux-foundation.org: coding-style cleanups] [akpm@linux-foundation.org: s@&&\@&& \@] Link: https://lkml.kernel.org/r/20220926133435.1333846-1-Jason@zx2c4.com Signed-off-by: Jason A. Donenfeld Reviewed-by: Andy Shevchenko Cc: Kees Cook Signed-off-by: Andrew Morton (cherry picked from commit 5efcecd9a3b18078d3398b359a84c83f549e22cf) Signed-off-by: SeongJae Park --- include/linux/minmax.h | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index 1aea34b8f19b..8b092c66c5aa 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -37,6 +37,28 @@ __cmp(x, y, op), \ __cmp_once(x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y), op)) =20 +#define __clamp(val, lo, hi) \ + __cmp(__cmp(val, lo, >), hi, <) + +#define __clamp_once(val, lo, hi, unique_val, unique_lo, unique_hi) ({ \ + typeof(val) unique_val =3D (val); \ + typeof(lo) unique_lo =3D (lo); \ + typeof(hi) unique_hi =3D (hi); \ + __clamp(unique_val, unique_lo, unique_hi); }) + +#define __clamp_input_check(lo, hi) \ + (BUILD_BUG_ON_ZERO(__builtin_choose_expr( \ + __is_constexpr((lo) > (hi)), (lo) > (hi), false))) + +#define __careful_clamp(val, lo, hi) ({ \ + __clamp_input_check(lo, hi) + \ + __builtin_choose_expr(__typecheck(val, lo) && __typecheck(val, hi) && \ + __typecheck(hi, lo) && __is_constexpr(val) && \ + __is_constexpr(lo) && __is_constexpr(hi), \ + __clamp(val, lo, hi), \ + __clamp_once(val, lo, hi, __UNIQUE_ID(__val), \ + __UNIQUE_ID(__lo), __UNIQUE_ID(__hi))); }) + /** * min - return minimum of two values of the same or compatible types * @x: first value @@ -103,7 +125,7 @@ * This macro does strict typechecking of @lo/@hi to make sure they are of= the * same type as @val. See the unnecessary pointer comparisons. */ -#define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi) +#define clamp(val, lo, hi) __careful_clamp(val, lo, hi) =20 /* * ..and if you can't take the strict @@ -138,7 +160,7 @@ * This macro does no typechecking and uses temporary variables of type * @type to make all the comparisons. */ -#define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi) +#define clamp_t(type, val, lo, hi) __careful_clamp((type)(val), (type)(lo)= , (type)(hi)) =20 /** * clamp_val - return a value clamped to a given range using val's type --=20 2.39.2 From nobody Wed Dec 17 15:31:54 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DDBFA1A38E8; Tue, 16 Jul 2024 18:33:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721154822; cv=none; b=X9vCTQfEHVHJo3n0B59DfCZMqkDpcBhEa2SQus4QWpghFsIL4twX0Wi81tF8lAt4sG7/EvFnoEgf6emtwteCEaWPluMneX2zMB1H2QxFzACwZX35hEXcCGyFC24gatXDwCSc9taaYU+oojXthg6niapYWDGxkduRrZnUbkqWryo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721154822; c=relaxed/simple; bh=aThZ+zEPJT8k/go9mqoqA2XrygvL1lG02piMFP8v6xI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=tfOLk2HuKfbbI1moG8MdvrwZr7dU0NIaxzzgbBGf6E7NyXKeIOJ7BheSCFc2HPSbKZS4AHyPFTAzbciQixHjBaLYJKWl7HO20NOz7qUUTkAsrcnIVPz7G4nYQSsaQVNeiMSiPKkavg+JC2JMP8aBgi5GGcUzpvsbd9bowm1XkmQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=VO927fIY; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="VO927fIY" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2AB2EC4AF1A; Tue, 16 Jul 2024 18:33:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721154821; bh=aThZ+zEPJT8k/go9mqoqA2XrygvL1lG02piMFP8v6xI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VO927fIYnhOOJJ3IegUZRd98oSYGyrihL5S/9MWQ7ZUo3NUn/3geP+Z3ylmvauqUh ktKhIn8OgXE5AJ5XG7XgQ889czz51zDJG9gBC12mkUHECgnQIGeyeJxhd9+f/al8OO R+60gZ3sn3DpVbW45AfLkYFsurqDRDpl4fAK47oK8hctrHo1WHKcYXDtlnZOahpWrl AC33pXam+Y/dxam2SvTBePg4fflQ4I3I6nAdSDN6hxcZK2qKZq8WM2bpmSfoPsCR19 +wqV7tn4OqvROQ9EdX+JiU8uzMlLUvCmQmLKhc7WwoOUgB1M+pXc/+Mrv72R7os5fT RwNdTxJ6WSL9A== From: SeongJae Park To: stable@vger.kernel.org, gregkh@linuxfoundation.org Cc: "Jason A. Donenfeld" , linux-kernel@vger.kernel.org, Andy Shevchenko , Kees Cook , Andrew Morton , SeongJae Park Subject: [PATCH 5.15.y 3/8] minmax: clamp more efficiently by avoiding extra comparison Date: Tue, 16 Jul 2024 11:33:28 -0700 Message-Id: <20240716183333.138498-4-sj@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240716183333.138498-1-sj@kernel.org> References: <20240716183333.138498-1-sj@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Jason A. Donenfeld" commit 2122e2a4efc2cd139474079e11939b6e07adfacd upstream. Currently the clamp algorithm does: if (val > hi) val =3D hi; if (val < lo) val =3D lo; But since hi > lo by definition, this can be made more efficient with: if (val > hi) val =3D hi; else if (val < lo) val =3D lo; So fix up the clamp and clamp_t functions to do this, adding the same argument checking as for min and min_t. For simple cases, code generation on x86_64 and aarch64 stay about the same: before: cmp edi, edx mov eax, esi cmova edi, edx cmp edi, esi cmovnb eax, edi ret after: cmp edi, esi mov eax, edx cmovnb esi, edi cmp edi, edx cmovb eax, esi ret before: cmp w0, w2 csel w8, w0, w2, lo cmp w8, w1 csel w0, w8, w1, hi ret after: cmp w0, w1 csel w8, w0, w1, hi cmp w0, w2 csel w0, w8, w2, lo ret On MIPS64, however, code generation improves, by removing arithmetic in the second branch: before: sltu $3,$6,$4 bne $3,$0,.L2 move $2,$6 move $2,$4 .L2: sltu $3,$2,$5 bnel $3,$0,.L7 move $2,$5 .L7: jr $31 nop after: sltu $3,$4,$6 beq $3,$0,.L13 move $2,$6 sltu $3,$4,$5 bne $3,$0,.L12 move $2,$4 .L13: jr $31 nop .L12: jr $31 move $2,$5 For more complex cases with surrounding code, the effects are a bit more complicated. For example, consider this simplified version of timestamp_truncate() from fs/inode.c on x86_64: struct timespec64 timestamp_truncate(struct timespec64 t, struct inode = *inode) { struct super_block *sb =3D inode->i_sb; unsigned int gran =3D sb->s_time_gran; t.tv_sec =3D clamp(t.tv_sec, sb->s_time_min, sb->s_time_max); if (t.tv_sec =3D=3D sb->s_time_max || t.tv_sec =3D=3D sb->s_time_mi= n) t.tv_nsec =3D 0; return t; } before: mov r8, rdx mov rdx, rsi mov rcx, QWORD PTR [r8] mov rax, QWORD PTR [rcx+8] mov rcx, QWORD PTR [rcx+16] cmp rax, rdi mov r8, rcx cmovge rdi, rax cmp rdi, rcx cmovle r8, rdi cmp rax, r8 je .L4 cmp rdi, rcx jge .L4 mov rax, r8 ret .L4: xor edx, edx mov rax, r8 ret after: mov rax, QWORD PTR [rdx] mov rdx, QWORD PTR [rax+8] mov rax, QWORD PTR [rax+16] cmp rax, rdi jg .L6 mov r8, rax xor edx, edx .L2: mov rax, r8 ret .L6: cmp rdx, rdi mov r8, rdi cmovge r8, rdx cmp rax, r8 je .L4 xor eax, eax cmp rdx, rdi cmovl rax, rsi mov rdx, rax mov rax, r8 ret .L4: xor edx, edx jmp .L2 In this case, we actually gain a branch, unfortunately, because the compiler's replacement axioms no longer as cleanly apply. So all and all, this change is a bit of a mixed bag. Link: https://lkml.kernel.org/r/20220926133435.1333846-2-Jason@zx2c4.com Signed-off-by: Jason A. Donenfeld Cc: Andy Shevchenko Cc: Kees Cook Signed-off-by: Andrew Morton (cherry picked from commit 2122e2a4efc2cd139474079e11939b6e07adfacd) Signed-off-by: SeongJae Park --- include/linux/minmax.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index 8b092c66c5aa..abdeae409dad 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -38,7 +38,7 @@ __cmp_once(x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y), op)) =20 #define __clamp(val, lo, hi) \ - __cmp(__cmp(val, lo, >), hi, <) + ((val) >=3D (hi) ? (hi) : ((val) <=3D (lo) ? (lo) : (val))) =20 #define __clamp_once(val, lo, hi, unique_val, unique_lo, unique_hi) ({ \ typeof(val) unique_val =3D (val); \ --=20 2.39.2 From nobody Wed Dec 17 15:31:54 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0EDC11A3BBB; Tue, 16 Jul 2024 18:33:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721154823; cv=none; b=iNNbuwtgoGe60QuNp25ZCC2SM8rEpoYa+D3sqIX+hOchkwPB27kTeFcin1qCjKfh7sKz5s1PGU4iID4uI69SduKASlI+9pxc601DOnV/tBTHHh7KJwS2D/1Ybyhs8CStA94eQY6i+xnP5sw8Vo3SHdOFwIv+feYGFr8GQ2aqyhk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721154823; c=relaxed/simple; bh=DfS+ZGQlpkyRoXAydI0Q0HXfxyiuVuX7x3ZtcbMPeEY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=uECurZngU71+ja9HDFUvLcVzMfMtzjZlC9N8oFkH4ESNN2YhiuFoCA/j5FU6KRsZYNsSR7k/F1DypFEm8bfhUggUjqqYb2QAFsZzaFmYNwg1e2m1G25n0vRbgjmR4C5K5/5+tVqayXpxZuqdsJOmFwbaJe8JQXlSEZ8rnfX3fLo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DzATlsJx; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="DzATlsJx" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 02EA0C4AF12; Tue, 16 Jul 2024 18:33:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721154822; bh=DfS+ZGQlpkyRoXAydI0Q0HXfxyiuVuX7x3ZtcbMPeEY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DzATlsJxPkHLUoSCYVjxNIIPUBGaM+IQS/4StZsm7UtFY9wwg/WnUI35Rd6j2RCiZ OZLbjNwGOM5g7s5Ryq2qo1QAM7+/0Spg+i+oe1atJ62tpqOd8rIb+0nKDQHGd4/gMm j2Bl/bLKRvYP8edQjxitnIlo0x1orVcKofRJkZJ77JYn65oe5RdCogpX4LxvP13HRA mpo0xG8uQ/UCNKpQVf08+ewDWxEOEnyLOwsJNHZt35YlyGAa1mP7D0adfu4G23Pe/A q65G0SuEJNIWEbe2sqwlP/xRXvf/hJNM3y7diZhUgHtqadUl3KPvtlKr91ekF2TW4A 6VwGZ/MpEUYzQ== From: SeongJae Park To: stable@vger.kernel.org, gregkh@linuxfoundation.org Cc: Andy Shevchenko , linux-kernel@vger.kernel.org, Herve Codina , Rasmus Villemoes , Andrew Morton , SeongJae Park Subject: [PATCH 5.15.y 4/8] minmax: fix header inclusions Date: Tue, 16 Jul 2024 11:33:29 -0700 Message-Id: <20240716183333.138498-5-sj@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240716183333.138498-1-sj@kernel.org> References: <20240716183333.138498-1-sj@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Andy Shevchenko commit f6e9d38f8eb00ac8b52e6d15f6aa9bcecacb081b upstream. BUILD_BUG_ON*() macros are defined in build_bug.h. Include it. Replace compiler_types.h by compiler.h, which provides the former, to have a definition of the __UNIQUE_ID(). Link: https://lkml.kernel.org/r/20230912092355.79280-1-andriy.shevchenko@li= nux.intel.com Signed-off-by: Andy Shevchenko Reviewed-by: Herve Codina Cc: Rasmus Villemoes Signed-off-by: Andrew Morton (cherry picked from commit f6e9d38f8eb00ac8b52e6d15f6aa9bcecacb081b) Signed-off-by: SeongJae Park [Fix a conflict due to absence of compiler_types.h include] --- include/linux/minmax.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index abdeae409dad..e8e9642809e0 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -2,6 +2,8 @@ #ifndef _LINUX_MINMAX_H #define _LINUX_MINMAX_H =20 +#include +#include #include =20 /* --=20 2.39.2 From nobody Wed Dec 17 15:31:54 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B58941A3BD4; Tue, 16 Jul 2024 18:33:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721154823; cv=none; b=MRxcQe4kEkr26QURQcJYYBY7ydwkPFMsTiZhZmW1sPDcP4iuAVYEvcwQLIp4nspSq6QShkU/QSa0Oo5LRDHomXUh132np8aoeATxhpynXaOKWp/UvPb70G8H5S9yIDVZTn6QlD5S6VbmlFQSxI4utb0yWQBfs8tStIjxeaN4DZw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721154823; c=relaxed/simple; bh=G7wLAJvUm7VMiE14Qqoyq66ELgz7AVWyGzvBzNL41RQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=SnpFN1U+H3mSsw3bLwuVXLq5J/DEOQao81ZWNfnz21vaGUxkW0bNYgxVIkrprNA+zOGTduCxYqRK/9pxKGYYZ7YdUqiA5+shVDqPqYkBI8QB7DRi9ScS+l/s3qVSDAdpH9RzcYLPo25F0CjDeCB/K79/gerQH7mtAKNXm4oKf/c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Rw+g4AQq; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Rw+g4AQq" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C0735C4AF14; Tue, 16 Jul 2024 18:33:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721154823; bh=G7wLAJvUm7VMiE14Qqoyq66ELgz7AVWyGzvBzNL41RQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Rw+g4AQqZR97onVr61BNpEWmzvKwg4NyQ6awE4l7utNaMMXCHaHRiWiBQRfDLGgKZ 3EtGT6kOY+ewSemDKTwqoaBrK95Vn7JOSSBHxKfiB4+cFvVeoPrr+tlGdDeGw63Owc JorEFVbmxQC2AcKLbbCXdBhyZNC+ZVsBJTB17UPgNyEL3zucJugQojhZ8LytiWDYIb cNAmp29vuMGk3xXVbF351kMJ/1BNn6F8NFlq8sJdy8Qr75yTWyH6xLwrwK9U4dYKEe WNHYJFNCUixgxhpok/UjdnWs8WmWFU7QytnnXLXicAe1d0dGSzJJDubb6n8cP7SJlE 5z5fW1BzOegzA== From: SeongJae Park To: stable@vger.kernel.org, gregkh@linuxfoundation.org Cc: David Laight , linux-kernel@vger.kernel.org, David Laight , Andy Shevchenko , Christoph Hellwig , "Jason A . Donenfeld" , Linus Torvalds , Matthew Wilcox , Andrew Morton , SeongJae Park Subject: [PATCH 5.15.y 5/8] minmax: allow min()/max()/clamp() if the arguments have the same signedness. Date: Tue, 16 Jul 2024 11:33:30 -0700 Message-Id: <20240716183333.138498-6-sj@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240716183333.138498-1-sj@kernel.org> References: <20240716183333.138498-1-sj@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Laight commit d03eba99f5bf7cbc6e2fdde3b6fa36954ad58e09 upstream. The type-check in min()/max() is there to stop unexpected results if a negative value gets converted to a large unsigned value. However it also rejects 'unsigned int' v 'unsigned long' compares which are common and never problematc. Replace the 'same type' check with a 'same signedness' check. The new test isn't itself a compile time error, so use static_assert() to report the error and give a meaningful error message. Due to the way builtin_choose_expr() works detecting the error in the 'non-constant' side (where static_assert() can be used) also detects errors when the arguments are constant. Link: https://lkml.kernel.org/r/fe7e6c542e094bfca655abcd323c1c98@AcuMS.acul= ab.com Signed-off-by: David Laight Cc: Andy Shevchenko Cc: Christoph Hellwig Cc: Jason A. Donenfeld Cc: Linus Torvalds Cc: Matthew Wilcox (Oracle) Signed-off-by: Andrew Morton (cherry picked from commit d03eba99f5bf7cbc6e2fdde3b6fa36954ad58e09) Signed-off-by: SeongJae Park --- include/linux/minmax.h | 60 ++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index e8e9642809e0..501fab582d68 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -11,9 +11,8 @@ * * - avoid multiple evaluations of the arguments (so side-effects like * "x++" happen only once) when non-constant. - * - perform strict type-checking (to generate warnings instead of - * nasty runtime surprises). See the "unnecessary" pointer comparison - * in __typecheck(). + * - perform signed v unsigned type-checking (to generate compile + * errors instead of nasty runtime surprises). * - retain result as a constant expressions when called with only * constant expressions (to avoid tripping VLA warnings in stack * allocation usage). @@ -21,23 +20,30 @@ #define __typecheck(x, y) \ (!!(sizeof((typeof(x) *)1 =3D=3D (typeof(y) *)1))) =20 -#define __no_side_effects(x, y) \ - (__is_constexpr(x) && __is_constexpr(y)) +/* is_signed_type() isn't a constexpr for pointer types */ +#define __is_signed(x) \ + __builtin_choose_expr(__is_constexpr(is_signed_type(typeof(x))), \ + is_signed_type(typeof(x)), 0) =20 -#define __safe_cmp(x, y) \ - (__typecheck(x, y) && __no_side_effects(x, y)) +#define __types_ok(x, y) \ + (__is_signed(x) =3D=3D __is_signed(y)) =20 -#define __cmp(x, y, op) ((x) op (y) ? (x) : (y)) +#define __cmp_op_min < +#define __cmp_op_max > =20 -#define __cmp_once(x, y, unique_x, unique_y, op) ({ \ +#define __cmp(op, x, y) ((x) __cmp_op_##op (y) ? (x) : (y)) + +#define __cmp_once(op, x, y, unique_x, unique_y) ({ \ typeof(x) unique_x =3D (x); \ typeof(y) unique_y =3D (y); \ - __cmp(unique_x, unique_y, op); }) + static_assert(__types_ok(x, y), \ + #op "(" #x ", " #y ") signedness error, fix types or consider u" #op "(= ) before " #op "_t()"); \ + __cmp(op, unique_x, unique_y); }) =20 -#define __careful_cmp(x, y, op) \ - __builtin_choose_expr(__safe_cmp(x, y), \ - __cmp(x, y, op), \ - __cmp_once(x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y), op)) +#define __careful_cmp(op, x, y) \ + __builtin_choose_expr(__is_constexpr((x) - (y)), \ + __cmp(op, x, y), \ + __cmp_once(op, x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y))) =20 #define __clamp(val, lo, hi) \ ((val) >=3D (hi) ? (hi) : ((val) <=3D (lo) ? (lo) : (val))) @@ -46,17 +52,15 @@ typeof(val) unique_val =3D (val); \ typeof(lo) unique_lo =3D (lo); \ typeof(hi) unique_hi =3D (hi); \ + static_assert(__builtin_choose_expr(__is_constexpr((lo) > (hi)), \ + (lo) <=3D (hi), true), \ + "clamp() low limit " #lo " greater than high limit " #hi); \ + static_assert(__types_ok(val, lo), "clamp() 'lo' signedness error"); \ + static_assert(__types_ok(val, hi), "clamp() 'hi' signedness error"); \ __clamp(unique_val, unique_lo, unique_hi); }) =20 -#define __clamp_input_check(lo, hi) \ - (BUILD_BUG_ON_ZERO(__builtin_choose_expr( \ - __is_constexpr((lo) > (hi)), (lo) > (hi), false))) - #define __careful_clamp(val, lo, hi) ({ \ - __clamp_input_check(lo, hi) + \ - __builtin_choose_expr(__typecheck(val, lo) && __typecheck(val, hi) && \ - __typecheck(hi, lo) && __is_constexpr(val) && \ - __is_constexpr(lo) && __is_constexpr(hi), \ + __builtin_choose_expr(__is_constexpr((val) - (lo) + (hi)), \ __clamp(val, lo, hi), \ __clamp_once(val, lo, hi, __UNIQUE_ID(__val), \ __UNIQUE_ID(__lo), __UNIQUE_ID(__hi))); }) @@ -66,14 +70,14 @@ * @x: first value * @y: second value */ -#define min(x, y) __careful_cmp(x, y, <) +#define min(x, y) __careful_cmp(min, x, y) =20 /** * max - return maximum of two values of the same or compatible types * @x: first value * @y: second value */ -#define max(x, y) __careful_cmp(x, y, >) +#define max(x, y) __careful_cmp(max, x, y) =20 /** * umin - return minimum of two non-negative values @@ -82,7 +86,7 @@ * @y: second value */ #define umin(x, y) \ - __careful_cmp((x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull, <) + __careful_cmp(min, (x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull) =20 /** * umax - return maximum of two non-negative values @@ -90,7 +94,7 @@ * @y: second value */ #define umax(x, y) \ - __careful_cmp((x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull, >) + __careful_cmp(max, (x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull) =20 /** * min3 - return minimum of three values @@ -142,7 +146,7 @@ * @x: first value * @y: second value */ -#define min_t(type, x, y) __careful_cmp((type)(x), (type)(y), <) +#define min_t(type, x, y) __careful_cmp(min, (type)(x), (type)(y)) =20 /** * max_t - return maximum of two values, using the specified type @@ -150,7 +154,7 @@ * @x: first value * @y: second value */ -#define max_t(type, x, y) __careful_cmp((type)(x), (type)(y), >) +#define max_t(type, x, y) __careful_cmp(max, (type)(x), (type)(y)) =20 /** * clamp_t - return a value clamped to a given range using a given type --=20 2.39.2 From nobody Wed Dec 17 15:31:54 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C398A1A4F1E; Tue, 16 Jul 2024 18:33:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721154824; cv=none; b=quc9qoACCnsAu26U1Z5u70sXHlrkVoyiSI0aWb/PFXDRU1makNrCSJPoFW7p7AMJi44q3Xyhw1Vod++zmi7pl9hu6NtYn1ZMKI62tsvzGaeDv286DNJfRAONWv9P4PxIGiCqFDnksRWsYAC8S9uMHylBzTRz2fRLsSh6vTDf+CY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721154824; c=relaxed/simple; bh=nUuPHoyJXLPZI9hsORkcW/G6LJ7x+O14qehpn3HYZSg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=kIn04ZsLpRp2Rjm5aiYAhUXCQUzM+RYR/hzkhLmvF8+hjIqxARuUPM9BYVZeN52O/n5KOGRW/symvzaZQSz+o6MDdWoIAMvC+dYqIqLz+T1s9exQSwLEUoLM/vscaK06SKiEsJbYOaCW8xsoSMcXLb/0HDdon6nz9GIzoMXVM48= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=IJok/xsA; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="IJok/xsA" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CE849C4AF0B; Tue, 16 Jul 2024 18:33:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721154824; bh=nUuPHoyJXLPZI9hsORkcW/G6LJ7x+O14qehpn3HYZSg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IJok/xsAxb6Hi7w7ANep6/L6G90vx4WvBodR4zWHhWBDYDCcNxPqpCn8zBsMkYy1q h5JIGCTN5/7zFkWBA3WVwUfAIjWSliZ1qT7EVyZtMPVynqedQ0et+5Tw2D880hj8Oa EaNIsrGPt5/fbyM1KICckGFftbWykQUL1vs55DBKNWrPa7QK50XRuAhUgerCwjC2GS vbojg5qm74S9qtR0LW1tjRzh67vx4g3YK77C/gS7PfCMAMqIgLucOKZtDX51F913iC +zgunR2H/Qy/dB5FY1OSGJqkKIPOjWvkOjxM4nQph6fVTL24/ZogxdVKPGrw52/49G 1dpOq8Uy5fnHg== From: SeongJae Park To: stable@vger.kernel.org, gregkh@linuxfoundation.org Cc: David Laight , linux-kernel@vger.kernel.org, David Laight , Andy Shevchenko , Christoph Hellwig , "Jason A . Donenfeld" , Linus Torvalds , Matthew Wilcox , Andrew Morton , SeongJae Park Subject: [PATCH 5.15.y 6/8] minmax: allow comparisons of 'int' against 'unsigned char/short' Date: Tue, 16 Jul 2024 11:33:31 -0700 Message-Id: <20240716183333.138498-7-sj@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240716183333.138498-1-sj@kernel.org> References: <20240716183333.138498-1-sj@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Laight commit 4ead534fba42fc4fd41163297528d2aa731cd121 upstream. Since 'unsigned char/short' get promoted to 'signed int' it is safe to compare them against an 'int' value. Link: https://lkml.kernel.org/r/8732ef5f809c47c28a7be47c938b28d4@AcuMS.acul= ab.com Signed-off-by: David Laight Cc: Andy Shevchenko Cc: Christoph Hellwig Cc: Jason A. Donenfeld Cc: Linus Torvalds Cc: Matthew Wilcox (Oracle) Signed-off-by: Andrew Morton (cherry picked from commit 4ead534fba42fc4fd41163297528d2aa731cd121) Signed-off-by: SeongJae Park --- include/linux/minmax.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index 501fab582d68..f76b7145fc11 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -25,8 +25,9 @@ __builtin_choose_expr(__is_constexpr(is_signed_type(typeof(x))), \ is_signed_type(typeof(x)), 0) =20 -#define __types_ok(x, y) \ - (__is_signed(x) =3D=3D __is_signed(y)) +#define __types_ok(x, y) \ + (__is_signed(x) =3D=3D __is_signed(y) || \ + __is_signed((x) + 0) =3D=3D __is_signed((y) + 0)) =20 #define __cmp_op_min < #define __cmp_op_max > --=20 2.39.2 From nobody Wed Dec 17 15:31:54 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 29BDB1A2553; Tue, 16 Jul 2024 18:33:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721154826; cv=none; b=Q8cLhgn+cbBYivTRSfqj8A0seC5UZKWMfNlyuDhhhdzuV4knZR4U1cL0CRs6fP+fWECai11Jv9V1UCi5/VLHURSfVaGkfIO3nV02IhU1kqupV7i4zXwikBkpSa0RvyHRh780+i3T+zRfjsUrCDKqYb9b8zaEUsXSdk+hT62txFk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721154826; c=relaxed/simple; bh=ndvNxnnobdXY1qRGFjwKkxZc5JAWm9Mq+lzm6iZ2oDw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=G6uaVHVqpsRfVZWxzPcJ9cWiv7YuDBXdclsm/QPZixvM/bXuXdwYPjFD4xkNrZQP1mYP3AXcq7fR8/p60tYM6dKt0jXziuOvuNj4wV1OLEw+vaCtYEM+MhtS4E47/y7I4P4FmH3g7yrH5SF2nXFJ7XXuHEcgwJ2uLaYGXs9aRkE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=nsrniOlO; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="nsrniOlO" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D9198C4AF0D; Tue, 16 Jul 2024 18:33:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721154825; bh=ndvNxnnobdXY1qRGFjwKkxZc5JAWm9Mq+lzm6iZ2oDw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nsrniOlOCA9NbqSZROhsTDlJdmUSh/BIZPZSXpXBQ5Kz0mQHgIaawfAfa05qS7c7Z Nt517df9+ryBl4iNVvRHO0QYiHqNe+f2+PE1Y+cT06qDHrkbbJ6KHOMaZ8ZlcO7uBL Khgw3NMxO2iJoplVwD2WmD+mtCc/ZQp5lIQa17/0f8D4VcOdhAxbfiojHSPVSzgD79 o8wwBgfor7wEvlp+fS96m/lE/l3Eo+M/W1el70kLQ/yex2Nae/moHQ2cKyM+Ef27km 32YbxdsRMNi1/FQ8WHzMyUJxWaXDPW/6BmpEzEBygVxVyjWM9cy0WJFPxNgQt1hs8r d5LKPgNkl/6ww== From: SeongJae Park To: stable@vger.kernel.org, gregkh@linuxfoundation.org Cc: David Laight , linux-kernel@vger.kernel.org, David Laight , Andy Shevchenko , Christoph Hellwig , "Jason A . Donenfeld" , Linus Torvalds , Matthew Wilcox , Andrew Morton , SeongJae Park Subject: [PATCH 5.15.y 7/8] minmax: relax check to allow comparison between unsigned arguments and signed constants Date: Tue, 16 Jul 2024 11:33:32 -0700 Message-Id: <20240716183333.138498-8-sj@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240716183333.138498-1-sj@kernel.org> References: <20240716183333.138498-1-sj@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Laight commit 867046cc7027703f60a46339ffde91a1970f2901 upstream. Allow (for example) min(unsigned_var, 20). The opposite min(signed_var, 20u) is still errored. Since a comparison between signed and unsigned never makes the unsigned value negative it is only necessary to adjust the __types_ok() test. Link: https://lkml.kernel.org/r/633b64e2f39e46bb8234809c5595b8c7@AcuMS.acul= ab.com Signed-off-by: David Laight Cc: Andy Shevchenko Cc: Christoph Hellwig Cc: Jason A. Donenfeld Cc: Linus Torvalds Cc: Matthew Wilcox (Oracle) Signed-off-by: Andrew Morton (cherry picked from commit 867046cc7027703f60a46339ffde91a1970f2901) Signed-off-by: SeongJae Park --- include/linux/minmax.h | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index f76b7145fc11..dd52969698f7 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -9,13 +9,18 @@ /* * min()/max()/clamp() macros must accomplish three things: * - * - avoid multiple evaluations of the arguments (so side-effects like + * - Avoid multiple evaluations of the arguments (so side-effects like * "x++" happen only once) when non-constant. - * - perform signed v unsigned type-checking (to generate compile - * errors instead of nasty runtime surprises). - * - retain result as a constant expressions when called with only + * - Retain result as a constant expressions when called with only * constant expressions (to avoid tripping VLA warnings in stack * allocation usage). + * - Perform signed v unsigned type-checking (to generate compile + * errors instead of nasty runtime surprises). + * - Unsigned char/short are always promoted to signed int and can be + * compared against signed or unsigned arguments. + * - Unsigned arguments can be compared against non-negative signed consta= nts. + * - Comparison of a signed argument against an unsigned constant fails + * even if the constant is below __INT_MAX__ and could be cast to int. */ #define __typecheck(x, y) \ (!!(sizeof((typeof(x) *)1 =3D=3D (typeof(y) *)1))) @@ -25,9 +30,14 @@ __builtin_choose_expr(__is_constexpr(is_signed_type(typeof(x))), \ is_signed_type(typeof(x)), 0) =20 -#define __types_ok(x, y) \ - (__is_signed(x) =3D=3D __is_signed(y) || \ - __is_signed((x) + 0) =3D=3D __is_signed((y) + 0)) +/* True for a non-negative signed int constant */ +#define __is_noneg_int(x) \ + (__builtin_choose_expr(__is_constexpr(x) && __is_signed(x), x, -1) >=3D 0) + +#define __types_ok(x, y) \ + (__is_signed(x) =3D=3D __is_signed(y) || \ + __is_signed((x) + 0) =3D=3D __is_signed((y) + 0) || \ + __is_noneg_int(x) || __is_noneg_int(y)) =20 #define __cmp_op_min < #define __cmp_op_max > --=20 2.39.2 From nobody Wed Dec 17 15:31:54 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 844211A6522; Tue, 16 Jul 2024 18:33:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721154826; cv=none; b=Rn9+N+oCUnH2prOAKm6LNSzs7pUUPsCM4tXXhwRtup3wRTujs1NT1Ro8Y7wkFHcF6tKqJSyi6Ad87o2RP2SQiAiYiPomh1MyLJzMq6yzrFCx/zFPgdgzo7SZERVpaPvpOmKwz0VnigF527Y5xBx2x4Oa+Ebdv5WZhyG/Ww5ktKc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721154826; c=relaxed/simple; bh=B154PBKiYl5EWcFNNPsQO6OKsGY+pG6XCthbORAZL/o=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=cwqEjc/L6sm47S1WE14iEv439diWqiLqsYkNwpME+uLpyvJEjbEn0IyS8yZyhGNuaCyKJ+d6lZz7vK3E5OeSf2fexwoK35r4ZvIfr/51dJhsaBM5OIMCFeAJSvf3gF/KX3cgy2B3dziyQ8ph5r3xcslHKzeuSmEtqnF+M2lR+pY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=gjw3rELS; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="gjw3rELS" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E32FFC4AF0F; Tue, 16 Jul 2024 18:33:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721154826; bh=B154PBKiYl5EWcFNNPsQO6OKsGY+pG6XCthbORAZL/o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gjw3rELSz2/fCMGSuNWZ10azZwg3mYDgyAqtfvlP832tU+6N3FLJn+IuvProFtFYT IweLKEc4JMCkzpyvgFIM4lRGbSxCr/8M9xpnnTKG6bqihzxFrWThfu1sWZ8rA7RMie p+0bphGvjbJaKeKZ1K4C8H50AAmXXG1yhe6jn7E3M3HTbhvPJedzf+LUxDykAf+wB8 M6zJhR7S58Eu7CLgS3HlmcQXYRjw8FVOP5HttxscCQTAOfJiekydgXZlquULnWgylv 1hnSyuhuXjz1TKXE6gdW4tu/Jzm9mzWPmhYoTja8YCC0rXb+MZfOG8aLlr3av03LuF R5tdHbMUyfYvg== From: SeongJae Park To: stable@vger.kernel.org, gregkh@linuxfoundation.org Cc: SeongJae Park , Andrew Morton , damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 5.15.y 8/8] mm/damon/core: merge regions aggressively when max_nr_regions is unmet Date: Tue, 16 Jul 2024 11:33:33 -0700 Message-Id: <20240716183333.138498-9-sj@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240716183333.138498-1-sj@kernel.org> References: <20240716183333.138498-1-sj@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" commit 310d6c15e9104c99d5d9d0ff8e5383a79da7d5e6 upstream. DAMON keeps the number of regions under max_nr_regions by skipping regions split operations when doing so can make the number higher than the limit. It works well for preventing violation of the limit. But, if somehow the violation happens, it cannot recovery well depending on the situation. In detail, if the real number of regions having different access pattern is higher than the limit, the mechanism cannot reduce the number below the limit. In such a case, the system could suffer from high monitoring overhead of DAMON. The violation can actually happen. For an example, the user could reduce max_nr_regions while DAMON is running, to be lower than the current number of regions. Fix the problem by repeating the merge operations with increasing aggressiveness in kdamond_merge_regions() for the case, until the limit is met. [sj@kernel.org: increase regions merge aggressiveness while respecting min_= nr_regions] Link: https://lkml.kernel.org/r/20240626164753.46270-1-sj@kernel.org [sj@kernel.org: ensure max threshold attempt for max_nr_regions violation] Link: https://lkml.kernel.org/r/20240627163153.75969-1-sj@kernel.org Link: https://lkml.kernel.org/r/20240624175814.89611-1-sj@kernel.org Fixes: b9a6ac4e4ede ("mm/damon: adaptively adjust regions") Signed-off-by: SeongJae Park Cc: [5.15+] Signed-off-by: Andrew Morton (cherry picked from commit 310d6c15e9104c99d5d9d0ff8e5383a79da7d5e6) Signed-off-by: SeongJae Park [Remove use of unexisting damon_ctx->attrs field] --- mm/damon/core.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/mm/damon/core.c b/mm/damon/core.c index 7a4912d6e65f..4f031412f65c 100644 --- a/mm/damon/core.c +++ b/mm/damon/core.c @@ -507,14 +507,31 @@ static void damon_merge_regions_of(struct damon_targe= t *t, unsigned int thres, * access frequencies are similar. This is for minimizing the monitoring * overhead under the dynamically changeable access pattern. If a merge w= as * unnecessarily made, later 'kdamond_split_regions()' will revert it. + * + * The total number of regions could be higher than the user-defined limit, + * max_nr_regions for some cases. For example, the user can update + * max_nr_regions to a number that lower than the current number of regions + * while DAMON is running. For such a case, repeat merging until the limi= t is + * met while increasing @threshold up to possible maximum level. */ static void kdamond_merge_regions(struct damon_ctx *c, unsigned int thresh= old, unsigned long sz_limit) { struct damon_target *t; - - damon_for_each_target(t, c) - damon_merge_regions_of(t, threshold, sz_limit); + unsigned int nr_regions; + unsigned int max_thres; + + max_thres =3D c->aggr_interval / + (c->sample_interval ? c->sample_interval : 1); + do { + nr_regions =3D 0; + damon_for_each_target(t, c) { + damon_merge_regions_of(t, threshold, sz_limit); + nr_regions +=3D damon_nr_regions(t); + } + threshold =3D max(1, threshold * 2); + } while (nr_regions > c->max_nr_regions && + threshold / 2 < max_thres); } =20 /* --=20 2.39.2