Skip to contentSkip to conversation

bpf: Use struct_size() helper

PATCH Applied Xiu Jianfeng 3 participants 3 messages Dec 20, 2021 – Dec 21, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: Use struct_size() helper

In an effort to avoid open-coded arithmetic in the kernel, use the struct_size() helper instead of open-coded calculation.

---
 kernel/bpf/local_storage.c   | 3 +--
 kernel/bpf/reuseport_array.c | 6 +-----
 2 files changed, 2 insertions(+), 7 deletions(-)
kernel/bpf/local_storage.c +1 -2
index 035e9e3a7132..23f7f9d08a62 100644
@@ -163,8 +163,7 @@ static int cgroup_storage_update_elem(struct bpf_map *map, void *key,
 		return 0;
 	}
 
-	new = bpf_map_kmalloc_node(map, sizeof(struct bpf_storage_buffer) +
-				   map->value_size,
+	new = bpf_map_kmalloc_node(map, struct_size(new, data, map->value_size),
 				   __GFP_ZERO | GFP_ATOMIC | __GFP_NOWARN,
 				   map->numa_node);
 	if (!new)
kernel/bpf/reuseport_array.c +1 -5
index 93a55391791a..556a769b5b80 100644
@@ -152,16 +152,12 @@ static struct bpf_map *reuseport_array_alloc(union bpf_attr *attr)
 {
 	int numa_node = bpf_map_attr_numa_node(attr);
 	struct reuseport_array *array;
-	u64 array_size;
 
 	if (!bpf_capable())
 		return ERR_PTR(-EPERM);
 
-	array_size = sizeof(*array);
-	array_size += (u64)attr->max_entries * sizeof(struct sock *);
-
 	/* allocate all map elements and zero-initialize them */
-	array = bpf_map_area_alloc(array_size, numa_node);
+	array = bpf_map_area_alloc(struct_size(array, ptrs, attr->max_entries), numa_node);
 	if (!array)
 		return ERR_PTR(-ENOMEM);
 
Signature · 1 line
2.17.1
Acked-by on v1 1/1
#1 lore raw
Quoted 5 lines · Xiu Jianfeng

In an effort to avoid open-coded arithmetic in the kernel, use the struct_size() helper instead of open-coded calculation.

Ack with a minor comments below.

Quoted 24 lines
---
  kernel/bpf/local_storage.c   | 3 +--
  kernel/bpf/reuseport_array.c | 6 +-----
  2 files changed, 2 insertions(+), 7 deletions(-)
kernel/bpf/local_storage.c +1 -2
index 035e9e3a7132..23f7f9d08a62 100644
@@ -163,8 +163,7 @@ static int cgroup_storage_update_elem(struct bpf_map *map, void *key,
  		return 0;
  	}
 
-	new = bpf_map_kmalloc_node(map, sizeof(struct bpf_storage_buffer) +
-				   map->value_size,
+	new = bpf_map_kmalloc_node(map, struct_size(new, data, map->value_size),
  				   __GFP_ZERO | GFP_ATOMIC | __GFP_NOWARN,
  				   map->numa_node);
  	if (!new)
kernel/bpf/reuseport_array.c +0 -0
index 93a55391791a..556a769b5b80 100644

kernel/bpf/reuseport_array.c static struct bpf_map *reuseport_array_alloc(union bpf_attr *attr) · line 161

Quoted 10 lines
kernel/bpf/reuseport_array.c +0 -3
@@ -152,16 +152,12 @@ static struct bpf_map *reuseport_array_alloc(union bpf_attr *attr)
  {
  	int numa_node = bpf_map_attr_numa_node(attr);
  	struct reuseport_array *array;
-	u64 array_size;
 
  	if (!bpf_capable())
  		return ERR_PTR(-EPERM);
 
-	array_size = sizeof(*array);
-	array_size += (u64)attr->max_entries * sizeof(struct sock *);

We have u64 type conversion here while struct_size used type 'size_t'. So we won't have problems for 64bit system. But for 32bit system, we may have a slight different behavior here as the current code can return a
u64 value and the struct_size(...) returns a size_t type value with max value SIZE_MAX.

In __bpf_map_area_alloc() we have a check:
if (size >= SIZE_MAX)
return NULL;
so we should be fine for 32bit system.

Quoted 7 lines
kernel/bpf/reuseport_array.c +1 -2
-
  	/* allocate all map elements and zero-initialize them */
-	array = bpf_map_area_alloc(array_size, numa_node);
+	array = bpf_map_area_alloc(struct_size(array, ptrs, attr->max_entries), numa_node);
  	if (!array)
  		return ERR_PTR(-ENOMEM);
 
#2 lore raw
In reply to Xiu Jianfeng
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Quoted 9 lines · On Mon, 20 Dec 2021 19:30:48 +0800 you wrote:

In an effort to avoid open-coded arithmetic in the kernel, use the struct_size() helper instead of open-coded calculation.

---
 kernel/bpf/local_storage.c   | 3 +--
 kernel/bpf/reuseport_array.c | 6 +-----
 2 files changed, 2 insertions(+), 7 deletions(-)

Here is the summary with links:
- [bpf-next] bpf: Use struct_size() helper

    https://git.kernel.org/bpf/bpf-next/c/0dd668d2080c

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html