[-next] sched/fair: Use struct_size()

Message ID 20230725195020.3469132-1-ruanjinjie@huawei.com
State New
Headers
Series [-next] sched/fair: Use struct_size() |

Commit Message

Jinjie Ruan July 25, 2023, 7:50 p.m. UTC
  Use struct_size() instead of hand-writing it, when allocating a structure
with a flex array.

This is less verbose.

Signed-off-by: Ruan Jinjie <ruanjinjie@huawei.com>
---
 kernel/sched/fair.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
  

Comments

Peter Zijlstra July 25, 2023, 1:25 p.m. UTC | #1
On Tue, Jul 25, 2023 at 07:50:20PM +0000, Ruan Jinjie wrote:
> Use struct_size() instead of hand-writing it, when allocating a structure
> with a flex array.
> 
> This is less verbose.

As per all the other times, no. It is also less clear. I never can
remember what that thing does and the code it replaces is utterly
trivial.

> Signed-off-by: Ruan Jinjie <ruanjinjie@huawei.com>
> ---
>  kernel/sched/fair.c | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)
> 
> diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
> index f8b71ea5948b..cc041a48100f 100644
> --- a/kernel/sched/fair.c
> +++ b/kernel/sched/fair.c
> @@ -2889,9 +2889,8 @@ static void task_numa_group(struct task_struct *p, int cpupid, int flags,
>  	int i;
>  
>  	if (unlikely(!deref_curr_numa_group(p))) {
> -		unsigned int size = sizeof(struct numa_group) +
> -				    NR_NUMA_HINT_FAULT_STATS *
> -				    nr_node_ids * sizeof(unsigned long);
> +		unsigned int size = struct_size(grp, faults,
> +		                                NR_NUMA_HINT_FAULT_STATS * nr_node_ids);
>  
>  		grp = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
>  		if (!grp)
> -- 
> 2.34.1
>
  

Patch

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index f8b71ea5948b..cc041a48100f 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -2889,9 +2889,8 @@  static void task_numa_group(struct task_struct *p, int cpupid, int flags,
 	int i;
 
 	if (unlikely(!deref_curr_numa_group(p))) {
-		unsigned int size = sizeof(struct numa_group) +
-				    NR_NUMA_HINT_FAULT_STATS *
-				    nr_node_ids * sizeof(unsigned long);
+		unsigned int size = struct_size(grp, faults,
+		                                NR_NUMA_HINT_FAULT_STATS * nr_node_ids);
 
 		grp = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
 		if (!grp)