Skip to contentSkip to conversation

bpf: fix potential percpu map overcopy to user.

RFC PATCH In review William Tu 3 participants 10 messages Oct 16, 2016 – Oct 20, 2016

Subsystems (6)
lore
Share & tools
lore

William Tu

author
#0 lore raw

RFC PATCH 1/1 bpf: fix potential percpu map overcopy to user.

When running bpf_map_lookup on percpu elements, the bytes copied to userspace depends on num_possible_cpus() * value_size, which could potentially be larger than memory allocated from user, which depends on sysconf(_SC_NPROCESSORS_CONF) to get the current cpu num. As a result, the inconsistency might corrupt the user's stack.

The fact that sysconf(_SC_NPROCESSORS_CONF) != num_possible_cpu() happens when cpu hotadd is enabled. For example, in Fusion when setting vcpu.hotadd = "TRUE" or in KVM, setting
./qemu-system-x86_64 -smp 2, maxcpus=4 ...
the num_possible_cpu() will be 4 and sysconf() will be 2[1]. Currently the any percpu map lookup suffers this issue, try samples/bpf/test_maps.c or tracex3.c.

Th RFC patch adds additional 'size' param from userspace so that kernel knows the maximum memory it should copy to the user.

[1] https://www.mail-archive.com/[email protected]/msg121183.html

---
 include/uapi/linux/bpf.h       |  5 ++++-
 kernel/bpf/syscall.c           |  5 +++--
 samples/bpf/fds_example.c      |  2 +-
 samples/bpf/libbpf.c           |  3 ++-
 samples/bpf/libbpf.h           |  2 +-
 samples/bpf/test_maps.c        | 30 +++++++++++++++---------------
 tools/include/uapi/linux/bpf.h |  5 ++++-
 7 files changed, 30 insertions(+), 22 deletions(-)
include/uapi/linux/bpf.h +4 -1
index f09c70b..fa0c40b 100644
@@ -123,7 +123,10 @@ union bpf_attr {
 			__aligned_u64 value;
 			__aligned_u64 next_key;
 		};
-		__u64		flags;
+		union {
+			__u64		flags;
+			__u32		size; /* number of bytes allocated in userspace */
+		};
 	};
 
 	struct { /* anonymous struct used by BPF_PROG_LOAD command */
kernel/bpf/syscall.c +3 -2
index 228f962..be211ea 100644
@@ -264,13 +264,14 @@ int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
 }
 
 /* last field in 'union bpf_attr' used by this command */
-#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
+#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD size
 
 static int map_lookup_elem(union bpf_attr *attr)
 {
 	void __user *ukey = u64_to_ptr(attr->key);
 	void __user *uvalue = u64_to_ptr(attr->value);
 	int ufd = attr->map_fd;
+	u32 usize = attr->size;
 	struct bpf_map *map;
 	void *key, *value, *ptr;
 	u32 value_size;
@@ -324,7 +325,7 @@ static int map_lookup_elem(union bpf_attr *attr)
 		goto free_value;
 
 	err = -EFAULT;
-	if (copy_to_user(uvalue, value, value_size) != 0)
+	if (copy_to_user(uvalue, value, min_t(u32, usize, value_size)) != 0)
 		goto free_value;
 
 	err = 0;
samples/bpf/fds_example.c +1 -1
index 625e797..5b833d8 100644
@@ -88,7 +88,7 @@ static int bpf_do_map(const char *file, uint32_t flags, uint32_t key,
 		       ret, strerror(errno));
 		assert(ret == 0);
 	} else if (flags & BPF_F_KEY) {
-		ret = bpf_lookup_elem(fd, &key, &value);
+		ret = bpf_lookup_elem(fd, &key, &value, sizeof(value));
 		printf("bpf: fd:%d l->(%u):%u ret:(%d,%s)\n", fd, key, value,
 		       ret, strerror(errno));
 		assert(ret == 0);
samples/bpf/libbpf.c +2 -1
index 9969e35..9f0a1c3 100644
@@ -44,12 +44,13 @@ int bpf_update_elem(int fd, void *key, void *value, unsigned long long flags)
 	return syscall(__NR_bpf, BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
 }
 
-int bpf_lookup_elem(int fd, void *key, void *value)
+int bpf_lookup_elem(int fd, void *key, void *value, int size)
 {
 	union bpf_attr attr = {
 		.map_fd = fd,
 		.key = ptr_to_u64(key),
 		.value = ptr_to_u64(value),
+		.size = size,
 	};
 
 	return syscall(__NR_bpf, BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
samples/bpf/libbpf.h +1 -1
index ac6edb6..b911185 100644
@@ -7,7 +7,7 @@ struct bpf_insn;
 int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,
 		   int max_entries, int map_flags);
 int bpf_update_elem(int fd, void *key, void *value, unsigned long long flags);
-int bpf_lookup_elem(int fd, void *key, void *value);
+int bpf_lookup_elem(int fd, void *key, void *value, int size);
 int bpf_delete_elem(int fd, void *key);
 int bpf_get_next_key(int fd, void *key, void *next_key);
 
samples/bpf/test_maps.c +15 -15
index cce2b59..a6a8fbe 100644
@@ -47,11 +47,11 @@ static void test_hashmap_sanity(int i, void *data)
 	assert(bpf_update_elem(map_fd, &key, &value, -1) == -1 && errno == EINVAL);
 
 	/* check that key=1 can be found */
-	assert(bpf_lookup_elem(map_fd, &key, &value) == 0 && value == 1234);
+	assert(bpf_lookup_elem(map_fd, &key, &value, sizeof(value)) == 0 && value == 1234);
 
 	key = 2;
 	/* check that key=2 is not found */
-	assert(bpf_lookup_elem(map_fd, &key, &value) == -1 && errno == ENOENT);
+	assert(bpf_lookup_elem(map_fd, &key, &value, sizeof(value)) == -1 && errno == ENOENT);
 
 	/* BPF_EXIST means: update existing element */
 	assert(bpf_update_elem(map_fd, &key, &value, BPF_EXIST) == -1 &&
@@ -139,11 +139,11 @@ static void test_percpu_hashmap_sanity(int task, void *data)
 	 * was run from a different cpu.
 	 */
 	value[0] = 1;
-	assert(bpf_lookup_elem(map_fd, &key, value) == 0 && value[0] == 100);
+	assert(bpf_lookup_elem(map_fd, &key, value, sizeof(value)) == 0 && value[0] == 100);
 
 	key = 2;
 	/* check that key=2 is not found */
-	assert(bpf_lookup_elem(map_fd, &key, value) == -1 && errno == ENOENT);
+	assert(bpf_lookup_elem(map_fd, &key, value, sizeof(value)) == -1 && errno == ENOENT);
 
 	/* BPF_EXIST means: update existing element */
 	assert(bpf_update_elem(map_fd, &key, value, BPF_EXIST) == -1 &&
@@ -170,7 +170,7 @@ static void test_percpu_hashmap_sanity(int task, void *data)
 		assert((expected_key_mask & next_key) == next_key);
 		expected_key_mask &= ~next_key;
 
-		assert(bpf_lookup_elem(map_fd, &next_key, value) == 0);
+		assert(bpf_lookup_elem(map_fd, &next_key, value, sizeof(value)) == 0);
 		for (i = 0; i < nr_cpus; i++)
 			assert(value[i] == i + 100);
 
@@ -218,11 +218,11 @@ static void test_arraymap_sanity(int i, void *data)
 	       errno == EEXIST);
 
 	/* check that key=1 can be found */
-	assert(bpf_lookup_elem(map_fd, &key, &value) == 0 && value == 1234);
+	assert(bpf_lookup_elem(map_fd, &key, &value, sizeof(value)) == 0 && value == 1234);
 
 	key = 0;
 	/* check that key=0 is also found and zero initialized */
-	assert(bpf_lookup_elem(map_fd, &key, &value) == 0 && value == 0);
+	assert(bpf_lookup_elem(map_fd, &key, &value, sizeof(value)) == 0 && value == 0);
 
 
 	/* key=0 and key=1 were inserted, check that key=2 cannot be inserted
@@ -233,7 +233,7 @@ static void test_arraymap_sanity(int i, void *data)
 	       errno == E2BIG);
 
 	/* check that key = 2 doesn't exist */
-	assert(bpf_lookup_elem(map_fd, &key, &value) == -1 && errno == ENOENT);
+	assert(bpf_lookup_elem(map_fd, &key, &value, sizeof(value)) == -1 && errno == ENOENT);
 
 	/* iterate over two elements */
 	assert(bpf_get_next_key(map_fd, &key, &next_key) == 0 &&
@@ -274,7 +274,7 @@ static void test_percpu_arraymap_many_keys(void)
 	for (key = 0; key < nr_keys; key++) {
 		for (i = 0; i < nr_cpus; i++)
 			values[i] = 0;
-		assert(bpf_lookup_elem(map_fd, &key, values) == 0);
+		assert(bpf_lookup_elem(map_fd, &key, values, sizeof(values)) == 0);
 		for (i = 0; i < nr_cpus; i++)
 			assert(values[i] == i + 10);
 	}
@@ -307,11 +307,11 @@ static void test_percpu_arraymap_sanity(int i, void *data)
 	       errno == EEXIST);
 
 	/* check that key=1 can be found */
-	assert(bpf_lookup_elem(map_fd, &key, values) == 0 && values[0] == 100);
+	assert(bpf_lookup_elem(map_fd, &key, values, sizeof(values)) == 0 && values[0] == 100);
 
 	key = 0;
 	/* check that key=0 is also found and zero initialized */
-	assert(bpf_lookup_elem(map_fd, &key, values) == 0 &&
+	assert(bpf_lookup_elem(map_fd, &key, values, sizeof(values)) == 0 &&
 	       values[0] == 0 && values[nr_cpus - 1] == 0);
 
 
@@ -321,7 +321,7 @@ static void test_percpu_arraymap_sanity(int i, void *data)
 	       errno == E2BIG);
 
 	/* check that key = 2 doesn't exist */
-	assert(bpf_lookup_elem(map_fd, &key, values) == -1 && errno == ENOENT);
+	assert(bpf_lookup_elem(map_fd, &key, values, sizeof(values)) == -1 && errno == ENOENT);
 
 	/* iterate over two elements */
 	assert(bpf_get_next_key(map_fd, &key, &next_key) == 0 &&
@@ -371,9 +371,9 @@ static void test_map_large(void)
 	assert(bpf_get_next_key(map_fd, &key, &key) == -1 && errno == ENOENT);
 
 	key.c = 0;
-	assert(bpf_lookup_elem(map_fd, &key, &value) == 0 && value == 0);
+	assert(bpf_lookup_elem(map_fd, &key, &value, sizeof(value)) == 0 && value == 0);
 	key.a = 1;
-	assert(bpf_lookup_elem(map_fd, &key, &value) == -1 && errno == ENOENT);
+	assert(bpf_lookup_elem(map_fd, &key, &value, sizeof(value)) == -1 && errno == ENOENT);
 
 	close(map_fd);
 }
@@ -466,7 +466,7 @@ static void test_map_parallel(void)
 	/* another check for all elements */
 	for (i = 0; i < MAP_SIZE; i++) {
 		key = MAP_SIZE - i - 1;
-		assert(bpf_lookup_elem(map_fd, &key, &value) == 0 &&
+		assert(bpf_lookup_elem(map_fd, &key, &value, sizeof(value)) == 0 &&
 		       value == key);
 	}
 
tools/include/uapi/linux/bpf.h +4 -1
index 9e5fc16..719fa25 100644
@@ -122,7 +122,10 @@ union bpf_attr {
 			__aligned_u64 value;
 			__aligned_u64 next_key;
 		};
-		__u64		flags;
+		union {
+			__u64		flags;
+			__u32		size; /* number of bytes allocated in userspace */
+		};
 	};
 
 	struct { /* anonymous struct used by BPF_PROG_LOAD command */
Signature · 1 line
2.5.0
#1 lore raw
Quoted 43 lines · William Tu

When running bpf_map_lookup on percpu elements, the bytes copied to userspace depends on num_possible_cpus() * value_size, which could potentially be larger than memory allocated from user, which depends on sysconf(_SC_NPROCESSORS_CONF) to get the current cpu num. As a result, the inconsistency might corrupt the user's stack.

The fact that sysconf(_SC_NPROCESSORS_CONF) != num_possible_cpu() happens when cpu hotadd is enabled. For example, in Fusion when setting vcpu.hotadd = "TRUE" or in KVM, setting
./qemu-system-x86_64 -smp 2, maxcpus=4 ...
the num_possible_cpu() will be 4 and sysconf() will be 2[1]. Currently the any percpu map lookup suffers this issue, try samples/bpf/test_maps.c or tracex3.c.

Th RFC patch adds additional 'size' param from userspace so that kernel knows the maximum memory it should copy to the user.

[1] https://www.mail-archive.com/[email protected]/msg121183.html

---
 include/uapi/linux/bpf.h       |  5 ++++-
 kernel/bpf/syscall.c           |  5 +++--
 samples/bpf/fds_example.c      |  2 +-
 samples/bpf/libbpf.c           |  3 ++-
 samples/bpf/libbpf.h           |  2 +-
 samples/bpf/test_maps.c        | 30 +++++++++++++++---------------
 tools/include/uapi/linux/bpf.h |  5 ++++-
 7 files changed, 30 insertions(+), 22 deletions(-)
include/uapi/linux/bpf.h +4 -1
index f09c70b..fa0c40b 100644
@@ -123,7 +123,10 @@ union bpf_attr {
 			__aligned_u64 value;
 			__aligned_u64 next_key;
 		};
-		__u64		flags;
+		union {
+			__u64		flags;
+			__u32		size; /* number of bytes allocated in userspace */
+		};

...

include/uapi/linux/bpf.h

Quoted 3 lines
include/uapi/linux/bpf.h +1 -1
-	if (copy_to_user(uvalue, value, value_size) != 0)
+	if (copy_to_user(uvalue, value, min_t(u32, usize, value_size)) != 0)
 		goto free_value;

I think such approach won't actually fix anything. User space
may lose some of the values and won't have any idea what was lost. I think we need to fix sample code to avoid using sysconf(_SC_NPROCESSORS_CONF) and use /sys/devices/system/cpu/possible instead.
I would argue that glibc should be fixed as well since relying on ls -d /sys/devices/system/cpu/cpu[0-9]*|wc -l turned out to be incorrect.

author
#2 lore raw
Quoted 5 lines

...

kernel/bpf/syscall.c static int map_lookup_elem(union bpf_attr *attr) · line 329

Quoted 3 lines
kernel/bpf/syscall.c +1 -1
-     if (copy_to_user(uvalue, value, value_size) != 0)
+     if (copy_to_user(uvalue, value, min_t(u32, usize, value_size)) != 0)
              goto free_value;
Quoted 7 lines

I think such approach won't actually fix anything. User space
may lose some of the values and won't have any idea what was lost. I think we need to fix sample code to avoid using sysconf(_SC_NPROCESSORS_CONF) and use /sys/devices/system/cpu/possible instead.
I would argue that glibc should be fixed as well since relying on ls -d /sys/devices/system/cpu/cpu[0-9]*|wc -l turned out to be incorrect.

Thanks for the feedback. I think glibc is correct. The _SC_NPROCESSORS_CONF presents the number of processors configured/populated and is indeed "ls /sys/devices/system/cpu/cpu[0-9]*|wc -l". This means the actual number of CPUs installed on your system. On the other hand, the num_possible_cpus() includes both the installed CPUs and the empty CPU socket/slot, in order to support CPU hotplug.

As a example, one of my dual socket motherboard with 1 CPU installed has

# /sys/devices/system/cpu/possible
0-239
# /sys/devices/system/cpu/cpu[0-9]*|wc -l
12
Note that these 12 cpus could be online/offline by
# echo 1/0 > /sys/devices/system/cpu/cpuX/online
Even if it is offline, the entry is still there.

Thinking about another solution, maybe we should use "num_present_cpus()" which means the configured/populated CPUs and the value is the same as sysconf(_SC_NPROCESSORS_CONF). Consider:
1) cpuX is online/offline: the num_present_cpus() remains the same.
2) new cpu is hotplug into the empty socket: the num_present_cpus() gets updates, and also the sysconf(_SC_NPROCESSORS_CONF).

+++ b/kernel/bpf/syscall.c
@@ -297,7 +297,7 @@ static int map_lookup_elem(union bpf_attr *attr)

        if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
            map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
-               value_size = round_up(map->value_size, 8) * num_possible_cpus();
+               value_size = round_up(map->value_size, 8) * num_present_cpus();
        else
                value_size = map->value_size;

Thanks. Regards,
William

maintainer
#3 lore raw
Quoted 12 lines · William Tu
Quoted 11 lines

...

kernel/bpf/syscall.c static int map_lookup_elem(union bpf_attr *attr) · line 329

Quoted 3 lines
kernel/bpf/syscall.c +1 -1
-     if (copy_to_user(uvalue, value, value_size) != 0)
+     if (copy_to_user(uvalue, value, min_t(u32, usize, value_size)) != 0)
               goto free_value;

I think such approach won't actually fix anything. User space
may lose some of the values and won't have any idea what was lost. I think we need to fix sample code to avoid using sysconf(_SC_NPROCESSORS_CONF) and use /sys/devices/system/cpu/possible instead.
I would argue that glibc should be fixed as well since relying on ls -d /sys/devices/system/cpu/cpu[0-9]*|wc -l turned out to be incorrect.

Quoted 5 lines

Thanks for the feedback. I think glibc is correct. The _SC_NPROCESSORS_CONF presents the number of processors configured/populated and is indeed "ls /sys/devices/system/cpu/cpu[0-9]*|wc -l". This means the actual number of CPUs installed on your system. On the other hand, the

In glibc __get_nprocs_conf() seems to try a number of things, first it tries equivalent of /sys/devices/system/cpu/cpu[0-9]*|wc -l, if that fails, depending on the config, it either tries to count cpus in /proc/cpuinfo, or returns the _SC_NPROCESSORS_ONLN value instead. If /proc/cpuinfo has some issue, it returns just 1 worst case. _SC_NPROCESSORS_ONLN will parse /sys/devices/system/cpu/online, if that fails it looks into /proc/stat for cpuX entries, and if also that fails for some reason, /proc/cpuinfo is consulted (and returning 1 if unlikely all breaks down).

Quoted 2 lines

num_possible_cpus() includes both the installed CPUs and the empty CPU socket/slot, in order to support CPU hotplug.

Correct.

Quoted 18 lines

As a example, one of my dual socket motherboard with 1 CPU installed has

# /sys/devices/system/cpu/possible
0-239
# /sys/devices/system/cpu/cpu[0-9]*|wc -l
12
Note that these 12 cpus could be online/offline by
# echo 1/0 > /sys/devices/system/cpu/cpuX/online
Even if it is offline, the entry is still there.

Thinking about another solution, maybe we should use "num_present_cpus()" which means the configured/populated CPUs and the value is the same as sysconf(_SC_NPROCESSORS_CONF). Consider:
1) cpuX is online/offline: the num_present_cpus() remains the same.
2) new cpu is hotplug into the empty socket: the num_present_cpus() gets updates, and also the sysconf(_SC_NPROCESSORS_CONF).

+++ b/kernel/bpf/syscall.c

Review static int map_lookup_elem(union bpf_attr *attr) · line 302

Quoted 7 lines
(quoted diff) +1 -1
@@ -297,7 +297,7 @@ static int map_lookup_elem(union bpf_attr *attr)
 
         if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
             map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
-               value_size = round_up(map->value_size, 8) * num_possible_cpus();
+               value_size = round_up(map->value_size, 8) * num_present_cpus();
         else
                 value_size = map->value_size;

But as you say in 2) that also has a chance of being racy on CPU hotplug compared to num_possible_cpus() which is fixed at boot time.

Documentation/cputopology.txt +106 says /sys/devices/system/cpu/possible outputs cpu_possible_mask. That is the same as in num_possible_cpus(), so first step would be to fix the buggy example code, imho.

What perhaps could be done in a second step to reduce overhead is an option for bpf(2) to pass in a cpu mask similarly as for sched_{get,set}affinity() syscalls, where user space can construct a mask via CPU_SET(3). For the syscall time, kernel would lock hot plugging via get_online_cpus() and put_online_cpus(), it would check whether passed CPUs are online to query and if so then it would copy the values into the user provided buffer. I'd think this might be useful in a number of ways anyway.

Thanks,
Daniel

maintainer
#8 lore raw
In reply to William Tu
Quoted 12 lines · William Tu
Quoted 11 lines

...

kernel/bpf/syscall.c static int map_lookup_elem(union bpf_attr *attr) · line 329

Quoted 3 lines
kernel/bpf/syscall.c +1 -1
-     if (copy_to_user(uvalue, value, value_size) != 0)
+     if (copy_to_user(uvalue, value, min_t(u32, usize, value_size)) != 0)
               goto free_value;

I think such approach won't actually fix anything. User space
may lose some of the values and won't have any idea what was lost. I think we need to fix sample code to avoid using sysconf(_SC_NPROCESSORS_CONF) and use /sys/devices/system/cpu/possible instead.
I would argue that glibc should be fixed as well since relying on ls -d /sys/devices/system/cpu/cpu[0-9]*|wc -l turned out to be incorrect.

Quoted 5 lines

Thanks for the feedback. I think glibc is correct. The _SC_NPROCESSORS_CONF presents the number of processors configured/populated and is indeed "ls /sys/devices/system/cpu/cpu[0-9]*|wc -l". This means the actual number of CPUs installed on your system. On the other hand, the

In glibc __get_nprocs_conf() seems to try a number of things, first it tries equivalent of /sys/devices/system/cpu/cpu[0-9]*|wc -l, if that fails, depending on the config, it either tries to count cpus in /proc/cpuinfo, or returns the _SC_NPROCESSORS_ONLN value instead. If /proc/cpuinfo has some issue, it returns just 1 worst case. _SC_NPROCESSORS_ONLN will parse /sys/devices/system/cpu/online, if that fails it looks into /proc/stat for cpuX entries, and if also that fails for some reason, /proc/cpuinfo is consulted (and returning 1 if unlikely all breaks down).

Quoted 2 lines

num_possible_cpus() includes both the installed CPUs and the empty CPU socket/slot, in order to support CPU hotplug.

Correct.

Quoted 18 lines

As a example, one of my dual socket motherboard with 1 CPU installed has

# /sys/devices/system/cpu/possible
0-239
# /sys/devices/system/cpu/cpu[0-9]*|wc -l
12
Note that these 12 cpus could be online/offline by
# echo 1/0 > /sys/devices/system/cpu/cpuX/online
Even if it is offline, the entry is still there.

Thinking about another solution, maybe we should use "num_present_cpus()" which means the configured/populated CPUs and the value is the same as sysconf(_SC_NPROCESSORS_CONF). Consider:
1) cpuX is online/offline: the num_present_cpus() remains the same.
2) new cpu is hotplug into the empty socket: the num_present_cpus() gets updates, and also the sysconf(_SC_NPROCESSORS_CONF).

+++ b/kernel/bpf/syscall.c

Review static int map_lookup_elem(union bpf_attr *attr) · line 302

Quoted 7 lines
(quoted diff) +1 -1
@@ -297,7 +297,7 @@ static int map_lookup_elem(union bpf_attr *attr)
 
         if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
             map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
-               value_size = round_up(map->value_size, 8) * num_possible_cpus();
+               value_size = round_up(map->value_size, 8) * num_present_cpus();
         else
                 value_size = map->value_size;

But as you say in 2) that also has a chance of being racy on CPU hotplug compared to num_possible_cpus() which is fixed at boot time.

Documentation/cputopology.txt +106 says /sys/devices/system/cpu/possible outputs cpu_possible_mask. That is the same as in num_possible_cpus(), so first step would be to fix the buggy example code, imho.

What perhaps could be done in a second step to reduce overhead is an option for bpf(2) to pass in a cpu mask similarly as for sched_{get,set}affinity() syscalls, where user space can construct a mask via CPU_SET(3). For the syscall time, kernel would lock hot plugging via get_online_cpus() and put_online_cpus(), it would check whether passed CPUs are online to query and if so then it would copy the values into the user provided buffer. I'd think this might be useful in a number of ways anyway.

Thanks,
Daniel

maintainer
#4 lore raw
In reply to Daniel Borkmann
Quoted 66 lines · Daniel Borkmann
Quoted 12 lines · William Tu
Quoted 11 lines

...

Review

Quoted 3 lines
(quoted diff) +1 -1
-     if (copy_to_user(uvalue, value, value_size) != 0)
+     if (copy_to_user(uvalue, value, min_t(u32, usize, value_size)) != 0)
               goto free_value;

I think such approach won't actually fix anything. User space
may lose some of the values and won't have any idea what was lost. I think we need to fix sample code to avoid using sysconf(_SC_NPROCESSORS_CONF) and use /sys/devices/system/cpu/possible instead.
I would argue that glibc should be fixed as well since relying on ls -d /sys/devices/system/cpu/cpu[0-9]*|wc -l turned out to be incorrect.

Quoted 5 lines

Thanks for the feedback. I think glibc is correct. The _SC_NPROCESSORS_CONF presents the number of processors configured/populated and is indeed "ls /sys/devices/system/cpu/cpu[0-9]*|wc -l". This means the actual number of CPUs installed on your system. On the other hand, the

In glibc __get_nprocs_conf() seems to try a number of things, first it tries equivalent of /sys/devices/system/cpu/cpu[0-9]*|wc -l, if that fails, depending on the config, it either tries to count cpus in /proc/cpuinfo, or returns the _SC_NPROCESSORS_ONLN value instead. If /proc/cpuinfo has some issue, it returns just 1 worst case. _SC_NPROCESSORS_ONLN will parse /sys/devices/system/cpu/online, if that fails it looks into /proc/stat for cpuX entries, and if also that fails for some reason, /proc/cpuinfo is consulted (and returning 1 if unlikely all breaks down).

Quoted 2 lines

num_possible_cpus() includes both the installed CPUs and the empty CPU socket/slot, in order to support CPU hotplug.

Correct.

Quoted 18 lines

As a example, one of my dual socket motherboard with 1 CPU installed has

# /sys/devices/system/cpu/possible
0-239
# /sys/devices/system/cpu/cpu[0-9]*|wc -l
12
Note that these 12 cpus could be online/offline by
# echo 1/0 > /sys/devices/system/cpu/cpuX/online
Even if it is offline, the entry is still there.

Thinking about another solution, maybe we should use "num_present_cpus()" which means the configured/populated CPUs and the value is the same as sysconf(_SC_NPROCESSORS_CONF). Consider:
1) cpuX is online/offline: the num_present_cpus() remains the same.
2) new cpu is hotplug into the empty socket: the num_present_cpus() gets updates, and also the sysconf(_SC_NPROCESSORS_CONF).

+++ b/kernel/bpf/syscall.c

Review static int map_lookup_elem(union bpf_attr *attr) · line 302

Quoted 7 lines
(quoted diff) +1 -1
@@ -297,7 +297,7 @@ static int map_lookup_elem(union bpf_attr *attr)
 
         if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
             map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
-               value_size = round_up(map->value_size, 8) * num_possible_cpus();
+               value_size = round_up(map->value_size, 8) * num_present_cpus();
         else
                 value_size = map->value_size;

But as you say in 2) that also has a chance of being racy on CPU hotplug compared to num_possible_cpus() which is fixed at boot time.

Documentation/cputopology.txt +106 says /sys/devices/system/cpu/possible outputs cpu_possible_mask. That is the same as in num_possible_cpus(), so first step would be to fix the buggy example code, imho.

Quoted 7 lines

What perhaps could be done in a second step to reduce overhead is an option for bpf(2) to pass in a cpu mask similarly as for sched_{get,set}affinity() syscalls, where user space can construct a mask via CPU_SET(3). For the syscall time, kernel would lock hot plugging via get_online_cpus() and put_online_cpus(), it would check whether passed CPUs are online to query and if so then it would copy the values into the user provided buffer. I'd think this might be useful in a number of ways anyway.

Maybe something like this as mentioned first step to fix the examples. Does this work for you, William?

 tools/testing/selftests/bpf/test_maps.c | 33 ++++++++++++++++++++++++++++++---
 1 file changed, 30 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/test_maps.c +30 -3
index ee384f0..d4832e8 100644
@@ -25,6 +25,33 @@
 
 static int map_flags;
 
+static unsigned int num_possible_cpus(void)
+{
+	static const char *fcpu = "/sys/devices/system/cpu/possible";
+	unsigned int val, possible_cpus = 0;
+	char buff[128];
+	FILE *fp;
+
+	fp = fopen(fcpu, "r");
+	if (!fp) {
+		printf("Failed to open %s: '%s'!\n", fcpu, strerror(errno));
+		exit(1);
+	}
+
+	while (fgets(buff, sizeof(buff), fp)) {
+		if (sscanf(buff, "%*u-%u", &val) == 1)
+			possible_cpus = val;
+	}
+
+	fclose(fp);
+	if (!possible_cpus) {
+		printf("Failed to retrieve # possible CPUs!\n");
+		exit(1);
+	}
+
+	return possible_cpus;
+}
+
 static void test_hashmap(int task, void *data)
 {
 	long long key, next_key, value;
@@ -110,7 +137,7 @@ static void test_hashmap(int task, void *data)
 
 static void test_hashmap_percpu(int task, void *data)
 {
-	unsigned int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
+	unsigned int nr_cpus = num_possible_cpus();
 	long long value[nr_cpus];
 	long long key, next_key;
 	int expected_key_mask = 0;
@@ -258,7 +285,7 @@ static void test_arraymap(int task, void *data)
 
 static void test_arraymap_percpu(int task, void *data)
 {
-	unsigned int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
+	unsigned int nr_cpus = num_possible_cpus();
 	int key, next_key, fd, i;
 	long values[nr_cpus];
 
@@ -313,7 +340,7 @@ static void test_arraymap_percpu(int task, void *data)
 
 static void test_arraymap_percpu_many_keys(void)
 {
-	unsigned int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
+	unsigned int nr_cpus = num_possible_cpus();
 	unsigned int nr_keys = 20000;
 	long values[nr_cpus];
 	int key, fd, i;
Signature · 1 line
1.9.3
#5 lore raw
Quoted 16 lines · Daniel Borkmann
tools/testing/selftests/bpf/test_maps.c +7 -0
index ee384f0..d4832e8 100644
@@ -25,6 +25,33 @@
 
 static int map_flags;
 
+static unsigned int num_possible_cpus(void)
+{
+	static const char *fcpu = "/sys/devices/system/cpu/possible";
+	unsigned int val, possible_cpus = 0;
+	char buff[128];
+	FILE *fp;
+

tools/testing/selftests/bpf/test_maps.c line 44

Quoted 10 lines
tools/testing/selftests/bpf/test_maps.c +10 -0
+	fp = fopen(fcpu, "r");
+	if (!fp) {
+		printf("Failed to open %s: '%s'!\n", fcpu, strerror(errno));
+		exit(1);
+	}
+
+	while (fgets(buff, sizeof(buff), fp)) {
+		if (sscanf(buff, "%*u-%u", &val) == 1)
+			possible_cpus = val;
+	}

looks great to me.
Could you move it into bpf_sys.h or somehow make it common in libbpf and reuse it in samples/bpf/ ?
Since quite a few samples need this fix as well.
Thanks!

author
#6 lore raw
Quoted 25 lines · Alexei Starovoitov
Quoted 24 lines · Daniel Borkmann
tools/testing/selftests/bpf/test_maps.c +15 -0
index ee384f0..d4832e8 100644
@@ -25,6 +25,33 @@
 
 static int map_flags;
 
+static unsigned int num_possible_cpus(void)
+{
+     static const char *fcpu = "/sys/devices/system/cpu/possible";
+     unsigned int val, possible_cpus = 0;
+     char buff[128];
+     FILE *fp;
+
+     fp = fopen(fcpu, "r");
+     if (!fp) {
+             printf("Failed to open %s: '%s'!\n", fcpu, strerror(errno));
+             exit(1);
+     }
+
+     while (fgets(buff, sizeof(buff), fp)) {
+             if (sscanf(buff, "%*u-%u", &val) == 1)
Quoted 9 lines

tools/testing/selftests/bpf/test_maps.c line 44

Quoted 2 lines
tools/testing/selftests/bpf/test_maps.c +2 -0
+                     possible_cpus = val;
+     }

looks great to me.
Could you move it into bpf_sys.h or somehow make it common in libbpf and reuse it in samples/bpf/ ?
Since quite a few samples need this fix as well.
Thanks!

Looks good to me. I tested it and it works fine.
Thanks!
William

author
#9 lore raw
In reply to Daniel Borkmann
Quoted 4 lines

Documentation/cputopology.txt +106 says /sys/devices/system/cpu/possible outputs cpu_possible_mask. That is the same as in num_possible_cpus(), so first step would be to fix the buggy example code, imho.

Quoted 8 lines

What perhaps could be done in a second step to reduce overhead is an option for bpf(2) to pass in a cpu mask similarly as for sched_{get,set}affinity() syscalls, where user space can construct a mask via CPU_SET(3). For the syscall time, kernel would lock hot plugging via get_online_cpus() and put_online_cpus(), it would check whether passed CPUs are online to query and if so then it would copy the values into the user provided buffer. I'd think this might be useful in a number of ways anyway.

I like this idea. So in this case, the only the data at the cpu specified by user in the CPU_SET is copied to userspace, potentially have better performance than always copying the data * num_possible_cpus() bytes.

Regards,
William

maintainer
#7 lore raw
In reply to William Tu
Quoted 26 lines · William Tu
Quoted 24 lines · Alexei Starovoitov
Quoted 23 lines · Daniel Borkmann
tools/testing/selftests/bpf/test_maps.c +14 -0
index ee384f0..d4832e8 100644
@@ -25,6 +25,33 @@
 
  static int map_flags;
 
+static unsigned int num_possible_cpus(void)
+{
+     static const char *fcpu = "/sys/devices/system/cpu/possible";
+     unsigned int val, possible_cpus = 0;
+     char buff[128];
+     FILE *fp;
+
+     fp = fopen(fcpu, "r");
+     if (!fp) {
+             printf("Failed to open %s: '%s'!\n", fcpu, strerror(errno));
+             exit(1);
+     }
+
+     while (fgets(buff, sizeof(buff), fp)) {
Quoted 8 lines
Quoted 8 lines

tools/testing/selftests/bpf/test_maps.c line 44

Quoted 3 lines
tools/testing/selftests/bpf/test_maps.c +3 -0
+             if (sscanf(buff, "%*u-%u", &val) == 1)
+                     possible_cpus = val;
+     }

looks great to me.
Could you move it into bpf_sys.h or somehow make it common in libbpf and reuse it in samples/bpf/ ?
Since quite a few samples need this fix as well.

Ahh, true.

Quoted 3 lines
Quoted 1 line

Thanks!

Looks good to me. I tested it and it works fine.

Okay, thanks. I'll fix that up, mid-term we should try and move most of that over to kernel selftests/bpf, and reuse tools/lib/bpf/.

Quoted 2 lines

Thanks!
William