Re: [PATCH 3/3] sched: Use clamp() and clamp_val() to make it more readable.

From: Dongsheng Yang
Date: Tue Mar 18 2014 - 22:21:40 EST


On 03/19/2014 10:16 AM, Steven Rostedt wrote:
On Wed, 12 Mar 2014 18:26:44 +0800
Dongsheng Yang <yangds.fnst@xxxxxxxxxxxxxx> wrote:

As Kees suggested, I use clamp() function to replace the if and
else branch, making it more readable and modular.

Suggested-by: Kees Cook <keescook@xxxxxxxxxxxx>
Signed-off-by: Dongsheng Yang <yangds.fnst@xxxxxxxxxxxxxx>
---
kernel/sched/core.c | 11 ++---------
1 file changed, 2 insertions(+), 9 deletions(-)

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 259ab85..85f4231 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -3068,17 +3068,10 @@ SYSCALL_DEFINE1(nice, int, increment)
* We don't have to worry. Conceptually one call occurs first
* and we have a single winner.
*/
- if (increment < -NICE_WIDTH)
- increment = -NICE_WIDTH;
- if (increment > NICE_WIDTH)
- increment = NICE_WIDTH;
-
Patch 2 and 3 need to be merged into a single patch.

Okey, I will squash them into one.

Thanx

-- Steve

+ increment = clamp(increment, -NICE_WIDTH, NICE_WIDTH);
nice = task_nice(current) + increment;
- if (nice < MIN_NICE)
- nice = MIN_NICE;
- if (nice > MAX_NICE)
- nice = MAX_NICE;
+ nice = clamp_val(nice, MIN_NICE, MAX_NICE);
if (increment < 0 && !can_nice(current, nice))
return -EPERM;


--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/