From: Ming Lei <tom.leiming@gmail.com> Date: 2016-01-11 15:57:12
Hi,
In case of ARRAY map, the index of the array is used as key
of the map, then inevitably the mapped element/value can be
accessed from more than one CPU concurrently, so expensive
atomic operations are often required in eBPF prog. And we can
see these usages in tracex3, sockex1 and sockex3 in sample/bpf/
of kernel tree.
This patchset trys to introduce percpu ARRAY map to address
the issue.
The 1st two patches prepares for supporting percpu map, and
introduces one file to hold the map common functions.
The following 3 patches introdues percpu version of update/
lookup element in bpf_map_ops, bpf helpers and syscall, so
that percpu value can be retrieved/updated from eBPF prog
and syscall.
The 6th patch implements percpu array map.
The last 3 patches are changes in samples/bpf, and implements
test for perpcu array and converts to percpu array in sockex1
exmaple.
include/linux/bpf.h | 10 ++++
include/uapi/linux/bpf.h | 10 ++++
kernel/bpf/Makefile | 2 +-
kernel/bpf/arraymap.c | 153 +++++++++++++++++++++++++++++++++++++++++++++++++++---------
kernel/bpf/bpf_map.h | 15 ++++++
kernel/bpf/core.c | 2 +
kernel/bpf/hashtab.c | 4 ++
kernel/bpf/helpers.c | 53 +++++++++++++++++++++
kernel/bpf/map.c | 43 +++++++++++++++++
kernel/bpf/syscall.c | 48 +++++++++++++++----
net/core/filter.c | 4 ++
samples/bpf/bpf_helpers.h | 5 ++
samples/bpf/libbpf.c | 42 +++++++++++++++++
samples/bpf/libbpf.h | 5 ++
samples/bpf/sockex1_kern.c | 7 +--
samples/bpf/sockex1_user.c | 20 ++++++--
samples/bpf/test_maps.c | 110 +++++++++++++++++++++++++++++++++++++++++++
17 files changed, 493 insertions(+), 40 deletions(-)
thanks,
Ming
From: Ming Lei <tom.leiming@gmail.com> Date: 2016-01-11 15:57:22
So that we can remove the per-map nop map fucntions.
Signed-off-by: Ming Lei <tom.leiming@gmail.com>
---
kernel/bpf/arraymap.c | 19 +++++--------------
1 file changed, 5 insertions(+), 14 deletions(-)
@@ -17,6 +17,8 @@#include<linux/filter.h>#include<linux/perf_event.h>+#include"bpf_map.h"+/* Called from syscall */staticstructbpf_map*array_map_alloc(unionbpf_attr*attr){
@@ -115,12 +117,6 @@ static int array_map_update_elem(struct bpf_map *map, void *key, void *value,return0;}-/* Called from syscall or from eBPF program */-staticintarray_map_delete_elem(structbpf_map*map,void*key)-{-return-EINVAL;-}-/* Called when map->refcnt goes to zero, either from workqueue or from syscall */staticvoidarray_map_free(structbpf_map*map){
@@ -178,11 +174,6 @@ static void fd_array_map_free(struct bpf_map *map)kvfree(array);}-staticvoid*fd_array_map_lookup_elem(structbpf_map*map,void*key)-{-returnNULL;-}-/* only called from syscall */staticintfd_array_map_update_elem(structbpf_map*map,void*key,void*value,u64map_flags)
From: Ming Lei <tom.leiming@gmail.com> Date: 2016-01-11 15:57:30
This patch is preparing for supporting percpu map, which
will be done in the following patches.
Signed-off-by: Ming Lei <tom.leiming@gmail.com>
---
include/linux/bpf.h | 5 +++++
kernel/bpf/arraymap.c | 6 ++++++
kernel/bpf/bpf_map.h | 4 ++++
kernel/bpf/hashtab.c | 4 ++++
kernel/bpf/map.c | 11 +++++++++++
5 files changed, 30 insertions(+)
@@ -25,6 +25,11 @@ struct bpf_map_ops {int(*map_update_elem)(structbpf_map*map,void*key,void*value,u64flags);int(*map_delete_elem)(structbpf_map*map,void*key);+/* funcs callable from userspace and from eBPF programs */+void*(*map_lookup_elem_percpu)(structbpf_map*map,void*key,u32cpu);+int(*map_update_elem_percpu)(structbpf_map*map,void*key,+void*value,u64flags,u32cpu);+/* funcs called by prog_array and perf_event_array map */void*(*map_fd_get_ptr)(structbpf_map*map,intfd);void(*map_fd_put_ptr)(void*ptr);
From: Ming Lei <tom.leiming@gmail.com> Date: 2016-01-11 15:57:34
Prepare for supporting percpu map in the following patch.
Now userspace can lookup/update mapped value in one specific
CPU in case of percpu map.
Signed-off-by: Ming Lei <tom.leiming@gmail.com>
---
include/uapi/linux/bpf.h | 3 +++
kernel/bpf/syscall.c | 48 ++++++++++++++++++++++++++++++++++++++----------
2 files changed, 41 insertions(+), 10 deletions(-)
@@ -231,8 +231,9 @@ static void __user *u64_to_ptr(__u64 val)/* last field in 'union bpf_attr' used by this command */#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value+#define BPF_MAP_LOOKUP_ELEM_PERCPU_LAST_FIELD cpu-staticintmap_lookup_elem(unionbpf_attr*attr)+staticintmap_lookup_elem(unionbpf_attr*attr,boolpercpu){void__user*ukey=u64_to_ptr(attr->key);void__user*uvalue=u64_to_ptr(attr->value);
@@ -242,8 +243,14 @@ static int map_lookup_elem(union bpf_attr *attr)structfdf;interr;-if(CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))-return-EINVAL;+if(!percpu){+if(CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))+return-EINVAL;+}else{+if(CHECK_ATTR(BPF_MAP_LOOKUP_ELEM_PERCPU)||+attr->cpu>=num_possible_cpus())+return-EINVAL;+}f=fdget(ufd);map=__bpf_map_get(f);
@@ -265,7 +272,10 @@ static int map_lookup_elem(union bpf_attr *attr)gotofree_key;rcu_read_lock();-ptr=map->ops->map_lookup_elem(map,key);+if(!percpu)+ptr=map->ops->map_lookup_elem(map,key);+else+ptr=map->ops->map_lookup_elem_percpu(map,key,attr->cpu);if(ptr)memcpy(value,ptr,map->value_size);rcu_read_unlock();
From: Ming Lei <tom.leiming@gmail.com> Date: 2016-01-11 15:57:38
This patch introduces percpu array map so that expensive
atomic operations can be avoided in eBPF prog in case of
ARRAY map.
PERCPU MAP uses the percpu version of update/lookup element
helpers and callbacks to access element in the map, and
the previous update/lookup element helpers and callbacks
don't work at the same time.
Signed-off-by: Ming Lei <tom.leiming@gmail.com>
---
include/linux/bpf.h | 2 +
include/uapi/linux/bpf.h | 1 +
kernel/bpf/arraymap.c | 136 ++++++++++++++++++++++++++++++++++++++++++-----
kernel/bpf/bpf_map.h | 2 +
kernel/bpf/map.c | 6 +++
5 files changed, 135 insertions(+), 12 deletions(-)
@@ -19,11 +19,36 @@#include"bpf_map.h"-/* Called from syscall */-staticstructbpf_map*array_map_alloc(unionbpf_attr*attr)+staticvoidfree_percpu_array(structbpf_array*array)+{+inti;++for(i=0;i<array->map.max_entries;i++)+free_percpu(array->pptrs[i]);+}++staticintalloc_percpu_array(structbpf_array*array,intcnt,intelem_size)+{+inti;++for(i=0;i<cnt;i++){+void__percpu*ptr=__alloc_percpu(elem_size,8);++if(!ptr){+free_percpu_array(array);+return-ENOMEM;+}+array->pptrs[i]=ptr;+}++array->percpu=true;+return0;+}++staticstructbpf_map*__array_map_alloc(unionbpf_attr*attr,boolpercpu){structbpf_array*array;-u32elem_size,array_size;+u32elem_size,array_size,elem_alloc_size;/* check sanity of attributes */if(attr->max_entries==0||attr->key_size!=4||
@@ -38,12 +63,22 @@ static struct bpf_map *array_map_alloc(union bpf_attr *attr)elem_size=round_up(attr->value_size,8);+/*+*Incaseofpercpu-array,eachelementintheallocatedarray+*pointstoonepercpuelement.+*/+if(percpu)+elem_alloc_size=sizeof(void*);+else+elem_alloc_size=elem_size;+/* check round_up into zero and u32 overflow */-if(elem_size==0||-attr->max_entries>(U32_MAX-PAGE_SIZE-sizeof(*array))/elem_size)+if(elem_alloc_size==0||+attr->max_entries>(U32_MAX-PAGE_SIZE-sizeof(*array))/+elem_alloc_size)returnERR_PTR(-ENOMEM);-array_size=sizeof(*array)+attr->max_entries*elem_size;+array_size=sizeof(*array)+attr->max_entries*elem_alloc_size;/* allocate all map elements and zero-initialize them */array=kzalloc(array_size,GFP_USER|__GFP_NOWARN);
@@ -53,16 +88,39 @@ static struct bpf_map *array_map_alloc(union bpf_attr *attr)returnERR_PTR(-ENOMEM);}+if(percpu){+if(alloc_percpu_array(array,attr->max_entries,+attr->value_size)){+kvfree(array);+returnERR_PTR(-ENOMEM);+}+array->map.pages=round_up(attr->max_entries*+attr->value_size*num_possible_cpus(),+PAGE_SIZE)>>PAGE_SHIFT;+}+/* copy mandatory map attributes */array->map.key_size=attr->key_size;array->map.value_size=attr->value_size;array->map.max_entries=attr->max_entries;-array->map.pages=round_up(array_size,PAGE_SIZE)>>PAGE_SHIFT;+array->map.pages+=round_up(array_size,PAGE_SIZE)>>PAGE_SHIFT;array->elem_size=elem_size;return&array->map;}+/* Called from syscall */+staticstructbpf_map*array_map_alloc(unionbpf_attr*attr)+{+return__array_map_alloc(attr,false);+}++/* Called from syscall */+staticstructbpf_map*percpu_array_map_alloc(unionbpf_attr*attr)+{+return__array_map_alloc(attr,true);+}+/* Called from syscall or from eBPF program */staticvoid*array_map_lookup_elem(structbpf_map*map,void*key){
@@ -75,6 +133,19 @@ static void *array_map_lookup_elem(struct bpf_map *map, void *key)returnarray->value+array->elem_size*index;}+/* Called from syscall or from eBPF program */+staticvoid*array_map_lookup_elem_percpu(structbpf_map*map,+void*key,u32cpu)+{+structbpf_array*array=container_of(map,structbpf_array,map);+u32index=*(u32*)key;++if(index>=array->map.max_entries)+returnNULL;++returnper_cpu_ptr(array->pptrs[index],cpu);+}+/* Called from syscall */staticintarray_map_get_next_key(structbpf_map*map,void*key,void*next_key){
@@ -95,11 +166,10 @@ static int array_map_get_next_key(struct bpf_map *map, void *key, void *next_key}/* Called from syscall or from eBPF program */-staticintarray_map_update_elem(structbpf_map*map,void*key,void*value,-u64map_flags)+staticinlineint__array_map_update_elem(structbpf_array*array,+u32index,void*value,+u64map_flags,void*ptr){-structbpf_array*array=container_of(map,structbpf_array,map);-u32index=*(u32*)key;if(map_flags>BPF_EXIST)/* unknown flags */
@@ -113,10 +183,32 @@ static int array_map_update_elem(struct bpf_map *map, void *key, void *value,/* all elements already exist */return-EEXIST;-memcpy(array->value+array->elem_size*index,value,map->value_size);+memcpy(ptr,value,array->map.value_size);return0;}+/* Called from syscall or from eBPF program */+staticintarray_map_update_elem(structbpf_map*map,void*key,void*value,+u64map_flags)+{+structbpf_array*array=container_of(map,structbpf_array,map);+u32index=*(u32*)key;+void*ptr=array->value+array->elem_size*index;++return__array_map_update_elem(array,index,value,map_flags,ptr);+}++/* Called from syscall or from eBPF program */+staticintarray_map_update_elem_percpu(structbpf_map*map,void*key,+void*value,u64map_flags,u32cpu)+{+structbpf_array*array=container_of(map,structbpf_array,map);+u32index=*(u32*)key;+void*ptr=per_cpu_ptr(array->pptrs[index],cpu);++return__array_map_update_elem(array,index,value,map_flags,ptr);+}+/* Called when map->refcnt goes to zero, either from workqueue or from syscall */staticvoidarray_map_free(structbpf_map*map){
@@ -39,6 +39,11 @@ static int (*bpf_redirect)(int ifindex, int flags) =(void*)BPF_FUNC_redirect;staticint(*bpf_perf_event_output)(void*ctx,void*map,intindex,void*data,intsize)=(void*)BPF_FUNC_perf_event_output;+staticvoid*(*bpf_map_lookup_elem_percpu)(void*map,void*key,unsignedcpu)=+(void*)BPF_FUNC_map_lookup_elem_percpu;+staticint(*bpf_map_update_elem_percpu)(void*map,void*key,void*value,+unsignedlonglongflags,unsignedcpu)=+(void*)BPF_FUNC_map_update_elem_percpu;/* llvm builtin functions that eBPF C program may use to*emitBPF_LD_ABSandBPF_LD_INDinstructions
@@ -43,6 +43,20 @@ int bpf_update_elem(int fd, void *key, void *value, unsigned long long flags)returnsyscall(__NR_bpf,BPF_MAP_UPDATE_ELEM,&attr,sizeof(attr));}+intbpf_update_elem_percpu(intfd,void*key,void*value,+unsignedlonglongflags,unsignedcpu)+{+unionbpf_attrattr={+.map_fd=fd,+.key=ptr_to_u64(key),+.value=ptr_to_u64(value),+.flags=flags,+.cpu=cpu+};++returnsyscall(__NR_bpf,BPF_MAP_UPDATE_ELEM_PERCPU,&attr,sizeof(attr));+}+intbpf_lookup_elem(intfd,void*key,void*value){unionbpf_attrattr={
@@ -8,6 +8,11 @@ int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,intmax_entries);intbpf_update_elem(intfd,void*key,void*value,unsignedlonglongflags);intbpf_lookup_elem(intfd,void*key,void*value);+intbpf_update_elem_percpu(intfd,void*key,void*value,+unsignedlonglongflags,unsignedcpu);+intbpf_lookup_elem_percpu(intfd,void*key,void*value,unsignedcpu);+intbpf_lookup_elem_allcpu(intfd,void*key,void*value_percpu,void*value,+int(*handle_one_cpu)(unsigned,void*,void*));intbpf_delete_elem(intfd,void*key);intbpf_get_next_key(intfd,void*key,void*next_key);
@@ -142,6 +142,106 @@ static void test_arraymap_sanity(int i, void *data)close(map_fd);}+staticinthandle_one_cpu(unsignedcpu,void*val_cpu,void*val)+{+unsignedlong*cnt=val;++*cnt+=*(long*)val_cpu;+return0;+}+++staticvoidtest_percpu_arraymap_all_cpu_sanity(inti)+{+intkey,map_fd;+unsignedlongvalue=0;+unsignedlongval_cpu;+unsignedcpu;+unsignednr_keys=2;+unsignednr_cpus=sysconf(_SC_NPROCESSORS_CONF);++map_fd=bpf_create_map(BPF_MAP_TYPE_ARRAY_PERCPU,sizeof(key),+sizeof(value),nr_keys);+if(map_fd<0){+printf("failed to create arraymap '%s'\n",strerror(errno));+exit(1);+}++for(key=0;key<nr_keys;key++){+for(cpu=0;cpu<nr_cpus;cpu++){+val_cpu=key;+assert(bpf_update_elem_percpu(map_fd,&key,&val_cpu,+BPF_ANY,cpu)==0);+}+}++for(key=0;key<nr_keys;key++){+assert(bpf_lookup_elem_allcpu(map_fd,&key,&val_cpu,+&value,handle_one_cpu)==0);+assert(value==nr_cpus*key);+}++close(map_fd);+}++staticvoidtest_percpu_arraymap_single_cpu_sanity(inti,void*data)+{+intkey,next_key,map_fd;+longlongvalue;+unsignedcpu=*(unsigned*)data;++map_fd=bpf_create_map(BPF_MAP_TYPE_ARRAY_PERCPU,sizeof(key),+sizeof(value),2);+if(map_fd<0){+printf("failed to create arraymap '%s'\n",strerror(errno));+exit(1);+}++key=1;+value=1234;+/* insert key=1 element */+assert(bpf_update_elem_percpu(map_fd,&key,&value,BPF_ANY,cpu)==0);++value=0;+assert(bpf_update_elem_percpu(map_fd,&key,&value,BPF_NOEXIST,+cpu)==-1&&errno==EEXIST);++/* check that key=1 can be found */+assert(bpf_lookup_elem_percpu(map_fd,&key,&value,cpu)==0&&+value==1234);++key=0;+/* check that key=0 is also found and zero initialized */+assert(bpf_lookup_elem_percpu(map_fd,&key,&value,cpu)==0&&+value==0);+++/* key=0 and key=1 were inserted, check that key=2 cannot be inserted+*duetomax_entrieslimit+*/+key=2;+assert(bpf_update_elem_percpu(map_fd,&key,&value,BPF_EXIST,+cpu)==-1&&errno==E2BIG);++/* check that key = 2 doesn't exist */+assert(bpf_lookup_elem_percpu(map_fd,&key,&value,cpu)==-1&&+errno==ENOENT);++/* iterate over two elements */+assert(bpf_get_next_key(map_fd,&key,&next_key)==0&&+next_key==0);+assert(bpf_get_next_key(map_fd,&next_key,&next_key)==0&&+next_key==1);+assert(bpf_get_next_key(map_fd,&next_key,&next_key)==-1&&+errno==ENOENT);++/* delete shouldn't succeed */+key=1;+assert(bpf_delete_elem(map_fd,&key)==-1&&errno==EINVAL);++close(map_fd);+}+#define MAP_SIZE (32 * 1024)staticvoidtest_map_large(void){
From: Ming Lei <tom.leiming@gmail.com> Date: 2016-01-11 15:58:34
It is demonstrated the expensive atomic operations can
be removed in eBPF prog.
Signed-off-by: Ming Lei <tom.leiming@gmail.com>
---
samples/bpf/sockex1_kern.c | 7 ++++---
samples/bpf/sockex1_user.c | 20 ++++++++++++++++----
2 files changed, 20 insertions(+), 7 deletions(-)
From: Ming Lei <tom.leiming@gmail.com> Date: 2016-01-11 15:59:37
Prepare for supporting percpu map.
These introduced two callback & helpers can be used to
retrieve/update the value from one specific CPU for percpu map.
Signed-off-by: Ming Lei <tom.leiming@gmail.com>
---
include/linux/bpf.h | 3 +++
include/uapi/linux/bpf.h | 6 ++++++
kernel/bpf/core.c | 2 ++
kernel/bpf/helpers.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++
net/core/filter.c | 4 ++++
5 files changed, 68 insertions(+)
@@ -54,6 +54,36 @@ const struct bpf_func_proto bpf_map_lookup_elem_proto = {.arg2_type=ARG_PTR_TO_MAP_KEY,};+staticu64bpf_map_lookup_elem_percpu(u64r1,u64r2,u64r3,u64r4,u64r5)+{+/* verifier checked that R1 contains a valid pointer to bpf_map+*andR2pointstoaprogramstackandmap->key_sizebyteswere+*initialized+*/+structbpf_map*map=(structbpf_map*)(unsignedlong)r1;+void*key=(void*)(unsignedlong)r2;+u32cpu=(u32)(unsignedlong)r3;+void*value;++WARN_ON_ONCE(!rcu_read_lock_held());++value=map->ops->map_lookup_elem_percpu(map,key,cpu);++/* lookup() returns either pointer to element value or NULL+*whichisthemeaningofPTR_TO_MAP_VALUE_OR_NULLtype+*/+return(unsignedlong)value;+}++conststructbpf_func_protobpf_map_lookup_elem_percpu_proto={+.func=bpf_map_lookup_elem_percpu,+.gpl_only=false,+.ret_type=RET_PTR_TO_MAP_VALUE_OR_NULL,+.arg1_type=ARG_CONST_MAP_PTR,+.arg2_type=ARG_PTR_TO_MAP_KEY,+.arg3_type=ARG_ANYTHING,+};+staticu64bpf_map_update_elem(u64r1,u64r2,u64r3,u64r4,u64r5){structbpf_map*map=(structbpf_map*)(unsignedlong)r1;
On Mon, Jan 11, 2016 at 11:56:57PM +0800, Ming Lei wrote:
Prepare for supporting percpu map in the following patch.
Now userspace can lookup/update mapped value in one specific
CPU in case of percpu map.
Signed-off-by: Ming Lei <tom.leiming@gmail.com>
I think this approach is less potent than Martin's for several reasons:
- bpf program shouldn't be supplying bpf_smp_processor_id(), since
it's error prone and a bit slower than doing it explicitly as in:
http://patchwork.ozlabs.org/patch/564482/
although Martin's patch also needs to use this_cpu_ptr() instead
of per_cpu_ptr(.., smp_processor_id());
- two new bpf helpers are not necessary in Martin's approach.
regular map_lookup_elem() will work for both per-cpu maps.
- such map_lookup_elem_percpu() from syscall is not accurate.
Martin's approach via smp_call_function_single() returns precise value,
whereas here memcpy() will race with other cpus.
Overall I think both pre-cpu hash and per-cpu array maps are quite useful.
For this particular set I would suggest to rebase on top of Martin's
to reuse BPF_MAP_LOOKUP_PERCPU_ELEM command that should be applicable
to both per-cpu array and per-cpu hash maps.
and add BPF_MAP_UPDATE_PERCPU_ELEM via smp_call as another patch
that should work for both as well.
On Mon, Jan 11, 2016 at 11:56:54PM +0800, Ming Lei wrote:
quoted hunk
So that we can remove the per-map nop map fucntions.
Signed-off-by: Ming Lei <tom.leiming@gmail.com>
---
kernel/bpf/arraymap.c | 19 +++++--------------
1 file changed, 5 insertions(+), 14 deletions(-)
@@ -17,6 +17,8 @@#include<linux/filter.h>#include<linux/perf_event.h>+#include"bpf_map.h"+/* Called from syscall */staticstructbpf_map*array_map_alloc(unionbpf_attr*attr){
@@ -115,12 +117,6 @@ static int array_map_update_elem(struct bpf_map *map, void *key, void *value,return0;}-/* Called from syscall or from eBPF program */-staticintarray_map_delete_elem(structbpf_map*map,void*key)-{-return-EINVAL;-}-/* Called when map->refcnt goes to zero, either from workqueue or from syscall */staticvoidarray_map_free(structbpf_map*map){
On Mon, Jan 11, 2016 at 11:56:58PM +0800, Ming Lei wrote:
This patch introduces percpu array map so that expensive
atomic operations can be avoided in eBPF prog in case of
ARRAY map.
PERCPU MAP uses the percpu version of update/lookup element
helpers and callbacks to access element in the map, and
the previous update/lookup element helpers and callbacks
don't work at the same time.
Signed-off-by: Ming Lei <tom.leiming@gmail.com>
From: Ming Lei <tom.leiming@gmail.com> Date: 2016-01-12 05:00:04
Hi Alexei,
Thanks for your review.
On Tue, Jan 12, 2016 at 3:02 AM, Alexei Starovoitov
[off-list ref] wrote:
On Mon, Jan 11, 2016 at 11:56:57PM +0800, Ming Lei wrote:
quoted
Prepare for supporting percpu map in the following patch.
Now userspace can lookup/update mapped value in one specific
CPU in case of percpu map.
Signed-off-by: Ming Lei <tom.leiming@gmail.com>
I think this approach is less potent than Martin's for several reasons:
- bpf program shouldn't be supplying bpf_smp_processor_id(), since
it's error prone and a bit slower than doing it explicitly as in:
http://patchwork.ozlabs.org/patch/564482/
although Martin's patch also needs to use this_cpu_ptr() instead
of per_cpu_ptr(.., smp_processor_id());
For PERCPU map, smp_processor_id() is definitely required, and
Martin's patch need that too, please see htab_percpu_map_lookup_elem()
in his patch.
- two new bpf helpers are not necessary in Martin's approach.
regular map_lookup_elem() will work for both per-cpu maps.
For percpu ARRAY, they are not necessary, but it is flexiable to
provide them since we should allow prog to retrieve the perpcu
value, also it is easier to implement the system call with the two
helpers.
For percpu HASH, they are required since eBPF prog need to support
deleting element, so we have provide these helpers for prog to retrieve
percpu value before deleting the elem.
- such map_lookup_elem_percpu() from syscall is not accurate.
Martin's approach via smp_call_function_single() returns precise value,
I don't understand why Martin's approach is precise and my patch isn't,
could you explain it a bit?
whereas here memcpy() will race with other cpus.
Overall I think both pre-cpu hash and per-cpu array maps are quite useful.
percpu hash isn't a must since we can get similar effect by making real_key
and cpu_id as key with less memory consumption, but we can introduce that.
For this particular set I would suggest to rebase on top of Martin's
to reuse BPF_MAP_LOOKUP_PERCPU_ELEM command that should be
applicable
to both per-cpu array and per-cpu hash maps.
Martin's patch doesn't introduce the two helpers, which is required for percpu
hash, and it also makes the syscall easier to implement.
and add BPF_MAP_UPDATE_PERCPU_ELEM via smp_call as another patch
that should work for both as well.
On Tue, Jan 12, 2016 at 01:00:00PM +0800, Ming Lei wrote:
Hi Alexei,
Thanks for your review.
On Tue, Jan 12, 2016 at 3:02 AM, Alexei Starovoitov
[off-list ref] wrote:
quoted
On Mon, Jan 11, 2016 at 11:56:57PM +0800, Ming Lei wrote:
quoted
Prepare for supporting percpu map in the following patch.
Now userspace can lookup/update mapped value in one specific
CPU in case of percpu map.
Signed-off-by: Ming Lei <tom.leiming@gmail.com>
I think this approach is less potent than Martin's for several reasons:
- bpf program shouldn't be supplying bpf_smp_processor_id(), since
it's error prone and a bit slower than doing it explicitly as in:
http://patchwork.ozlabs.org/patch/564482/
although Martin's patch also needs to use this_cpu_ptr() instead
of per_cpu_ptr(.., smp_processor_id());
For PERCPU map, smp_processor_id() is definitely required, and
Martin's patch need that too, please see htab_percpu_map_lookup_elem()
in his patch.
hmm. it's definitely _not_ required. right?
bpf programs shouldn't be accessing other per-cpu regions
only their own. That's what this_cpu_ptr is for.
I don't see a case where accessing other cpu per-cpu element
wouldn't be a bug in the program.
quoted
- two new bpf helpers are not necessary in Martin's approach.
regular map_lookup_elem() will work for both per-cpu maps.
For percpu ARRAY, they are not necessary, but it is flexiable to
provide them since we should allow prog to retrieve the perpcu
value, also it is easier to implement the system call with the two
helpers.
For percpu HASH, they are required since eBPF prog need to support
deleting element, so we have provide these helpers for prog to retrieve
percpu value before deleting the elem.
bpf programs cannot have loops, so there is no valid case to access
other cpu element, since program cannot aggregate all-cpu values.
Therefore the programs can only update/lookup this_cpu element and
delete such element across all cpus.
quoted
- such map_lookup_elem_percpu() from syscall is not accurate.
Martin's approach via smp_call_function_single() returns precise value,
I don't understand why Martin's approach is precise and my patch isn't,
could you explain it a bit?
because simple mempcy() called from syscall will race with lookup/increment
done to this_cpu element on another cpu. To avoid this race the smp_call
is needed, so that memcpy() happens on the cpu that updated the element,
so smp_call's memcpy and bpf program won't be touch that cpu value
at the same time and user space will read the correct element values.
If program updates them a lot, the value that user space reads will become
stale very quickly, but it will be valid. That's especially important
when program have multiple counters inside single element value.
quoted
whereas here memcpy() will race with other cpus.
Overall I think both pre-cpu hash and per-cpu array maps are quite useful.
percpu hash isn't a must since we can get similar effect by making real_key
and cpu_id as key with less memory consumption, but we can introduce that.
I don't think so. bpf programs shouldn't be dealing with smp_processor_id()
It was poor man's per-cpu hack and it had too many disadvantages.
Like get_next_key() doesn't work properly when key is {key+processor_id},
so walking over hash map to aggregate fake per-cpu elements requires
user space to create another map just for walking.
map->max_entries limit becomes bogus.
this_cpu_ptr(..) is typically faster than per_cpu_ptr(.., smp_proc_id())
From: Ming Lei <tom.leiming@gmail.com> Date: 2016-01-12 11:05:52
On Tue, Jan 12, 2016 at 1:49 PM, Alexei Starovoitov
[off-list ref] wrote:
On Tue, Jan 12, 2016 at 01:00:00PM +0800, Ming Lei wrote:
quoted
Hi Alexei,
Thanks for your review.
On Tue, Jan 12, 2016 at 3:02 AM, Alexei Starovoitov
[off-list ref] wrote:
quoted
On Mon, Jan 11, 2016 at 11:56:57PM +0800, Ming Lei wrote:
quoted
Prepare for supporting percpu map in the following patch.
Now userspace can lookup/update mapped value in one specific
CPU in case of percpu map.
Signed-off-by: Ming Lei <tom.leiming@gmail.com>
I think this approach is less potent than Martin's for several reasons:
- bpf program shouldn't be supplying bpf_smp_processor_id(), since
it's error prone and a bit slower than doing it explicitly as in:
http://patchwork.ozlabs.org/patch/564482/
although Martin's patch also needs to use this_cpu_ptr() instead
of per_cpu_ptr(.., smp_processor_id());
For PERCPU map, smp_processor_id() is definitely required, and
Martin's patch need that too, please see htab_percpu_map_lookup_elem()
in his patch.
hmm. it's definitely _not_ required. right?
bpf programs shouldn't be accessing other per-cpu regions
only their own. That's what this_cpu_ptr is for.
I don't see a case where accessing other cpu per-cpu element
wouldn't be a bug in the program.
quoted
quoted
- two new bpf helpers are not necessary in Martin's approach.
regular map_lookup_elem() will work for both per-cpu maps.
For percpu ARRAY, they are not necessary, but it is flexiable to
provide them since we should allow prog to retrieve the perpcu
value, also it is easier to implement the system call with the two
helpers.
For percpu HASH, they are required since eBPF prog need to support
deleting element, so we have provide these helpers for prog to retrieve
percpu value before deleting the elem.
bpf programs cannot have loops, so there is no valid case to access
other cpu element, since program cannot aggregate all-cpu values.
Therefore the programs can only update/lookup this_cpu element and
delete such element across all cpus.
Looks I missed the point of looping constraint, then basically delete element
helper doesn't make sense in percpu hash.
quoted
quoted
- such map_lookup_elem_percpu() from syscall is not accurate.
Martin's approach via smp_call_function_single() returns precise value,
I don't understand why Martin's approach is precise and my patch isn't,
could you explain it a bit?
because simple mempcy() called from syscall will race with lookup/increment
done to this_cpu element on another cpu. To avoid this race the smp_call
is needed, so that memcpy() happens on the cpu that updated the element,
so smp_call's memcpy and bpf program won't be touch that cpu value
at the same time and user space will read the correct element values.
If program updates them a lot, the value that user space reads will become
stale very quickly, but it will be valid. That's especially important
when program have multiple counters inside single element value.
But smp_call is often very slow because of IPI, so the value acculated
finally becomes stale easily even though the value from the requested cpu
is 'precise' at the exact time, especially when there are lots of CPUs, so I
think using smp_call is really a bad idea. And smp_call is worse than
iterating from CPUs simply.
quoted
quoted
whereas here memcpy() will race with other cpus.
Overall I think both pre-cpu hash and per-cpu array maps are quite useful.
percpu hash isn't a must since we can get similar effect by making real_key
and cpu_id as key with less memory consumption, but we can introduce that.
I don't think so. bpf programs shouldn't be dealing with smp_processor_id()
It was poor man's per-cpu hack and it had too many disadvantages.
Like get_next_key() doesn't work properly when key is {key+processor_id},
so walking over hash map to aggregate fake per-cpu elements requires
user space to create another map just for walking.
map->max_entries limit becomes bogus.
this_cpu_ptr(..) is typically faster than per_cpu_ptr(.., smp_proc_id())
OK, then this_cpu_ptr() is better since we don't need to access the value
of other CPUs.
--
Ming Lei
@@ -142,6 +142,106 @@ static void test_arraymap_sanity(int i, void *data)close(map_fd);}+staticinthandle_one_cpu(unsignedcpu,void*val_cpu,void*val)+{+unsignedlong*cnt=val;++*cnt+=*(long*)val_cpu;
Integer pointer casts ring big alarm bells - they are accidents
waiting to happen.
If the pointers are 'pointer to long' then define them as such.
David
From: Martin KaFai Lau <hidden> Date: 2016-01-12 19:14:12
On Tue, Jan 12, 2016 at 07:05:47PM +0800, Ming Lei wrote:
On Tue, Jan 12, 2016 at 1:49 PM, Alexei Starovoitov
[off-list ref] wrote:
quoted
On Tue, Jan 12, 2016 at 01:00:00PM +0800, Ming Lei wrote:
quoted
Hi Alexei,
Thanks for your review.
On Tue, Jan 12, 2016 at 3:02 AM, Alexei Starovoitov
[off-list ref] wrote:
quoted
On Mon, Jan 11, 2016 at 11:56:57PM +0800, Ming Lei wrote:
quoted
Prepare for supporting percpu map in the following patch.
Now userspace can lookup/update mapped value in one specific
CPU in case of percpu map.
Signed-off-by: Ming Lei <tom.leiming@gmail.com>
For PERCPU map, smp_processor_id() is definitely required, and
Martin's patch need that too, please see htab_percpu_map_lookup_elem()
in his patch.
hmm. it's definitely _not_ required. right?
bpf programs shouldn't be accessing other per-cpu regions
only their own. That's what this_cpu_ptr is for.
I don't see a case where accessing other cpu per-cpu element
wouldn't be a bug in the program.
quoted
quoted
- two new bpf helpers are not necessary in Martin's approach.
regular map_lookup_elem() will work for both per-cpu maps.
For percpu ARRAY, they are not necessary, but it is flexiable to
provide them since we should allow prog to retrieve the perpcu
value, also it is easier to implement the system call with the two
helpers.
For percpu HASH, they are required since eBPF prog need to support
deleting element, so we have provide these helpers for prog to retrieve
percpu value before deleting the elem.
bpf programs cannot have loops, so there is no valid case to access
other cpu element, since program cannot aggregate all-cpu values.
Therefore the programs can only update/lookup this_cpu element and
delete such element across all cpus.
Looks I missed the point of looping constraint, then basically delete element
helper doesn't make sense in percpu hash.
quoted
quoted
quoted
- such map_lookup_elem_percpu() from syscall is not accurate.
Martin's approach via smp_call_function_single() returns precise value,
I don't understand why Martin's approach is precise and my patch isn't,
could you explain it a bit?
because simple mempcy() called from syscall will race with lookup/increment
done to this_cpu element on another cpu. To avoid this race the smp_call
is needed, so that memcpy() happens on the cpu that updated the element,
so smp_call's memcpy and bpf program won't be touch that cpu value
at the same time and user space will read the correct element values.
If program updates them a lot, the value that user space reads will become
stale very quickly, but it will be valid. That's especially important
when program have multiple counters inside single element value.
But smp_call is often very slow because of IPI, so the value acculated
finally becomes stale easily even though the value from the requested cpu
is 'precise' at the exact time, especially when there are lots of CPUs, so I
think using smp_call is really a bad idea. And smp_call is worse than
iterating from CPUs simply.
The userspace usually only aggregates value across all cpu every X seconds.
I hardly consider some number of micro-seconds old data is stale.
From: Ming Lei <tom.leiming@gmail.com> Date: 2016-01-13 00:38:20
On Wed, Jan 13, 2016 at 3:10 AM, Martin KaFai Lau [off-list ref] wrote:
On Tue, Jan 12, 2016 at 07:05:47PM +0800, Ming Lei wrote:
quoted
On Tue, Jan 12, 2016 at 1:49 PM, Alexei Starovoitov
[off-list ref] wrote:
quoted
On Tue, Jan 12, 2016 at 01:00:00PM +0800, Ming Lei wrote:
quoted
Hi Alexei,
Thanks for your review.
On Tue, Jan 12, 2016 at 3:02 AM, Alexei Starovoitov
[off-list ref] wrote:
quoted
On Mon, Jan 11, 2016 at 11:56:57PM +0800, Ming Lei wrote:
quoted
Prepare for supporting percpu map in the following patch.
Now userspace can lookup/update mapped value in one specific
CPU in case of percpu map.
Signed-off-by: Ming Lei <tom.leiming@gmail.com>
For PERCPU map, smp_processor_id() is definitely required, and
Martin's patch need that too, please see htab_percpu_map_lookup_elem()
in his patch.
hmm. it's definitely _not_ required. right?
bpf programs shouldn't be accessing other per-cpu regions
only their own. That's what this_cpu_ptr is for.
I don't see a case where accessing other cpu per-cpu element
wouldn't be a bug in the program.
quoted
quoted
- two new bpf helpers are not necessary in Martin's approach.
regular map_lookup_elem() will work for both per-cpu maps.
For percpu ARRAY, they are not necessary, but it is flexiable to
provide them since we should allow prog to retrieve the perpcu
value, also it is easier to implement the system call with the two
helpers.
For percpu HASH, they are required since eBPF prog need to support
deleting element, so we have provide these helpers for prog to retrieve
percpu value before deleting the elem.
bpf programs cannot have loops, so there is no valid case to access
other cpu element, since program cannot aggregate all-cpu values.
Therefore the programs can only update/lookup this_cpu element and
delete such element across all cpus.
Looks I missed the point of looping constraint, then basically delete element
helper doesn't make sense in percpu hash.
quoted
quoted
quoted
- such map_lookup_elem_percpu() from syscall is not accurate.
Martin's approach via smp_call_function_single() returns precise value,
I don't understand why Martin's approach is precise and my patch isn't,
could you explain it a bit?
because simple mempcy() called from syscall will race with lookup/increment
done to this_cpu element on another cpu. To avoid this race the smp_call
is needed, so that memcpy() happens on the cpu that updated the element,
so smp_call's memcpy and bpf program won't be touch that cpu value
at the same time and user space will read the correct element values.
If program updates them a lot, the value that user space reads will become
stale very quickly, but it will be valid. That's especially important
when program have multiple counters inside single element value.
But smp_call is often very slow because of IPI, so the value acculated
finally becomes stale easily even though the value from the requested cpu
is 'precise' at the exact time, especially when there are lots of CPUs, so I
think using smp_call is really a bad idea. And smp_call is worse than
iterating from CPUs simply.
The userspace usually only aggregates value across all cpu every X seconds.
That is just in your case, and Alexei worried the issue of data stale.
I hardly consider some number of micro-seconds old data is stale.
Firstly CPU can do hugh things in micro-seconds, such as the if's irq
may just come duirng the period.
Secondly, the time can become longer(maybe dozens of us, or in milli-seconds)
if CPU number is very bigger.
So why not do it in the quick way?
--
Ming Lei
From: Martin KaFai Lau <hidden> Date: 2016-01-13 02:22:31
On Wed, Jan 13, 2016 at 08:38:18AM +0800, Ming Lei wrote:
On Wed, Jan 13, 2016 at 3:10 AM, Martin KaFai Lau [off-list ref] wrote:
quoted
On Tue, Jan 12, 2016 at 07:05:47PM +0800, Ming Lei wrote:
quoted
On Tue, Jan 12, 2016 at 1:49 PM, Alexei Starovoitov
[off-list ref] wrote:
quoted
On Tue, Jan 12, 2016 at 01:00:00PM +0800, Ming Lei wrote:
quoted
Hi Alexei,
Thanks for your review.
On Tue, Jan 12, 2016 at 3:02 AM, Alexei Starovoitov
[off-list ref] wrote:
quoted
On Mon, Jan 11, 2016 at 11:56:57PM +0800, Ming Lei wrote:
quoted
Prepare for supporting percpu map in the following patch.
Now userspace can lookup/update mapped value in one specific
CPU in case of percpu map.
Signed-off-by: Ming Lei <tom.leiming@gmail.com>
For PERCPU map, smp_processor_id() is definitely required, and
Martin's patch need that too, please see htab_percpu_map_lookup_elem()
in his patch.
hmm. it's definitely _not_ required. right?
bpf programs shouldn't be accessing other per-cpu regions
only their own. That's what this_cpu_ptr is for.
I don't see a case where accessing other cpu per-cpu element
wouldn't be a bug in the program.
quoted
quoted
- two new bpf helpers are not necessary in Martin's approach.
regular map_lookup_elem() will work for both per-cpu maps.
For percpu ARRAY, they are not necessary, but it is flexiable to
provide them since we should allow prog to retrieve the perpcu
value, also it is easier to implement the system call with the two
helpers.
For percpu HASH, they are required since eBPF prog need to support
deleting element, so we have provide these helpers for prog to retrieve
percpu value before deleting the elem.
bpf programs cannot have loops, so there is no valid case to access
other cpu element, since program cannot aggregate all-cpu values.
Therefore the programs can only update/lookup this_cpu element and
delete such element across all cpus.
Looks I missed the point of looping constraint, then basically delete element
helper doesn't make sense in percpu hash.
quoted
quoted
quoted
- such map_lookup_elem_percpu() from syscall is not accurate.
Martin's approach via smp_call_function_single() returns precise value,
I don't understand why Martin's approach is precise and my patch isn't,
could you explain it a bit?
because simple mempcy() called from syscall will race with lookup/increment
done to this_cpu element on another cpu. To avoid this race the smp_call
is needed, so that memcpy() happens on the cpu that updated the element,
so smp_call's memcpy and bpf program won't be touch that cpu value
at the same time and user space will read the correct element values.
If program updates them a lot, the value that user space reads will become
stale very quickly, but it will be valid. That's especially important
when program have multiple counters inside single element value.
But smp_call is often very slow because of IPI, so the value acculated
finally becomes stale easily even though the value from the requested cpu
is 'precise' at the exact time, especially when there are lots of CPUs, so I
think using smp_call is really a bad idea. And smp_call is worse than
iterating from CPUs simply.
The userspace usually only aggregates value across all cpu every X seconds.
That is just in your case, and Alexei worried the issue of data stale.
I believe we are talking about validity of a value. How to
make use of a less-stale but invalid data?
From: Ming Lei <tom.leiming@gmail.com> Date: 2016-01-13 03:17:26
On Wed, Jan 13, 2016 at 10:22 AM, Martin KaFai Lau [off-list ref] wrote:
On Wed, Jan 13, 2016 at 08:38:18AM +0800, Ming Lei wrote:
quoted
quoted
The userspace usually only aggregates value across all cpu every X seconds.
That is just in your case, and Alexei worried the issue of data stale.
I believe we are talking about validity of a value. How to
make use of a less-stale but invalid data?
About the 'invalidity' thing, it should be same between using
smp_call(run in IPI irq handler) and simple memcpy().
When smp_call_function_single() is used to request to lookup element in
the specific CPU, the value of the element may be in updating in that CPU
and not completed yet in eBPF prog, then IPI comes and half updated
data is still returned to syscall.
Thanks,
Ming Lei
On Wed, Jan 13, 2016 at 11:17:23AM +0800, Ming Lei wrote:
On Wed, Jan 13, 2016 at 10:22 AM, Martin KaFai Lau [off-list ref] wrote:
quoted
On Wed, Jan 13, 2016 at 08:38:18AM +0800, Ming Lei wrote:
quoted
quoted
The userspace usually only aggregates value across all cpu every X seconds.
That is just in your case, and Alexei worried the issue of data stale.
I believe we are talking about validity of a value. How to
make use of a less-stale but invalid data?
About the 'invalidity' thing, it should be same between using
smp_call(run in IPI irq handler) and simple memcpy().
When smp_call_function_single() is used to request to lookup element in
the specific CPU, the value of the element may be in updating in that CPU
and not completed yet in eBPF prog, then IPI comes and half updated
data is still returned to syscall.
hmm. I'm not following. bpf programs are executing with preempt disabled,
so smp_call_function_single suppose to execute when bpf is not running.
From: Ming Lei <tom.leiming@gmail.com> Date: 2016-01-13 14:56:41
On Wed, Jan 13, 2016 at 1:30 PM, Alexei Starovoitov
[off-list ref] wrote:
On Wed, Jan 13, 2016 at 11:17:23AM +0800, Ming Lei wrote:
quoted
On Wed, Jan 13, 2016 at 10:22 AM, Martin KaFai Lau [off-list ref] wrote:
quoted
On Wed, Jan 13, 2016 at 08:38:18AM +0800, Ming Lei wrote:
quoted
quoted
The userspace usually only aggregates value across all cpu every X seconds.
That is just in your case, and Alexei worried the issue of data stale.
I believe we are talking about validity of a value. How to
make use of a less-stale but invalid data?
About the 'invalidity' thing, it should be same between using
smp_call(run in IPI irq handler) and simple memcpy().
When smp_call_function_single() is used to request to lookup element in
the specific CPU, the value of the element may be in updating in that CPU
and not completed yet in eBPF prog, then IPI comes and half updated
data is still returned to syscall.
hmm. I'm not following. bpf programs are executing with preempt disabled,
so smp_call_function_single suppose to execute when bpf is not running.
Preempt disabled doesn't mean irq disabled, does it? So when bpf prog is
running, the IPI irq for smp_call still may come on that CPU.
Also in current non-percpu hash, the situation exists too between
lookup elem syscall and updating value of element from bpf prog in
SMP.
--
Ming Lei
On Wed, Jan 13, 2016 at 10:56:38PM +0800, Ming Lei wrote:
On Wed, Jan 13, 2016 at 1:30 PM, Alexei Starovoitov
[off-list ref] wrote:
quoted
On Wed, Jan 13, 2016 at 11:17:23AM +0800, Ming Lei wrote:
quoted
On Wed, Jan 13, 2016 at 10:22 AM, Martin KaFai Lau [off-list ref] wrote:
quoted
On Wed, Jan 13, 2016 at 08:38:18AM +0800, Ming Lei wrote:
quoted
quoted
The userspace usually only aggregates value across all cpu every X seconds.
That is just in your case, and Alexei worried the issue of data stale.
I believe we are talking about validity of a value. How to
make use of a less-stale but invalid data?
About the 'invalidity' thing, it should be same between using
smp_call(run in IPI irq handler) and simple memcpy().
When smp_call_function_single() is used to request to lookup element in
the specific CPU, the value of the element may be in updating in that CPU
and not completed yet in eBPF prog, then IPI comes and half updated
data is still returned to syscall.
hmm. I'm not following. bpf programs are executing with preempt disabled,
so smp_call_function_single suppose to execute when bpf is not running.
Preempt disabled doesn't mean irq disabled, does it? So when bpf prog is
running, the IPI irq for smp_call still may come on that CPU.
In case of kprobes irqs are disabled, but yeah for sockets smp_call won't help.
Can probably use schedule_work_on(), but that's too heavy.
I guess we need bpf_map_lookup_and_delete_elem() syscall command, so we can
delete single pointer out of per-cpu hash map and in call_rcu() copy precise
counters.
Also in current non-percpu hash, the situation exists too between
lookup elem syscall and updating value of element from bpf prog in
SMP.
looks like regular bpf_map_lookup_elem() syscall will return inaccurate data
even for per-cpu hash. hmm. we need to brain storm more on it.
From: Ming Lei <tom.leiming@gmail.com> Date: 2016-01-14 02:42:46
On Thu, Jan 14, 2016 at 9:19 AM, Alexei Starovoitov
[off-list ref] wrote:
On Wed, Jan 13, 2016 at 10:56:38PM +0800, Ming Lei wrote:
quoted
On Wed, Jan 13, 2016 at 1:30 PM, Alexei Starovoitov
[off-list ref] wrote:
quoted
On Wed, Jan 13, 2016 at 11:17:23AM +0800, Ming Lei wrote:
quoted
On Wed, Jan 13, 2016 at 10:22 AM, Martin KaFai Lau [off-list ref] wrote:
quoted
On Wed, Jan 13, 2016 at 08:38:18AM +0800, Ming Lei wrote:
quoted
quoted
The userspace usually only aggregates value across all cpu every X seconds.
That is just in your case, and Alexei worried the issue of data stale.
I believe we are talking about validity of a value. How to
make use of a less-stale but invalid data?
About the 'invalidity' thing, it should be same between using
smp_call(run in IPI irq handler) and simple memcpy().
When smp_call_function_single() is used to request to lookup element in
the specific CPU, the value of the element may be in updating in that CPU
and not completed yet in eBPF prog, then IPI comes and half updated
data is still returned to syscall.
hmm. I'm not following. bpf programs are executing with preempt disabled,
so smp_call_function_single suppose to execute when bpf is not running.
Preempt disabled doesn't mean irq disabled, does it? So when bpf prog is
running, the IPI irq for smp_call still may come on that CPU.
In case of kprobes irqs are disabled, but yeah for sockets smp_call won't help.
From 'Documentation/kprobes.txt', looks irqs aren't disabled always, see blow:
Probe handlers are run with preemption disabled. Depending on the
architecture and optimization state, handlers may also run with
interrupts disabled (e.g., kretprobe handlers and optimized kprobe
handlers run without interrupt disabled on x86/x86-64).
Can probably use schedule_work_on(), but that's too heavy.
I guess we need bpf_map_lookup_and_delete_elem() syscall command, so we can
delete single pointer out of per-cpu hash map and in call_rcu() copy precise
counters.
The partial update is one generic issue, not only on percpu map.
quoted
Also in current non-percpu hash, the situation exists too between
lookup elem syscall and updating value of element from bpf prog in
SMP.
looks like regular bpf_map_lookup_elem() syscall will return inaccurate data
even for per-cpu hash. hmm. we need to brain storm more on it.
That is the reason I don't like smp_call now, since the issue is generic
and not only on percpu map.
But any generic protection might introduce some cost in updating path
from eBPF prog, which we don't like too.
The partial update only exists when one element holds more than one
counter, or one element holds one 64bit counter on 32bit machine(which
can be thought as double counter too).
1) single counter case
- if the counter in the element may be updated concurrently, the counter
has to be updated with atomic operation in prog, and that is perpcu map's
value to avoid the atomic operation
- now no protection is needed since the updating on the element is atomic
2) multiple counter case
- lots of protection can be used, such per-element rw-spin, percpu lock,
srcu, ..., but each each one may introduce cost in update path of prog.
- prog code can choose if they want precise counting with the extra cost.
- the lock mechanism can be provided by bpf helpers
Thanks,
Ming Lei
On Thu, Jan 14, 2016 at 10:42:44AM +0800, Ming Lei wrote:
quoted
In case of kprobes irqs are disabled, but yeah for sockets smp_call won't help.
From 'Documentation/kprobes.txt', looks irqs aren't disabled always, see blow:
Probe handlers are run with preemption disabled. Depending on the
architecture and optimization state, handlers may also run with
interrupts disabled (e.g., kretprobe handlers and optimized kprobe
handlers run without interrupt disabled on x86/x86-64).
bpf tracing progs go through ftrace that disables irqs even for
optimized kprobes on x64.
but yeah, there could be an arch that doesn't do it
and long term we probably want to do something about it on x64 as well.
tracepoints+bpf will be with irqs on as well.
2) multiple counter case
- lots of protection can be used, such per-element rw-spin, percpu lock,
srcu, ..., but each each one may introduce cost in update path of prog.
- the lock mechanism can be provided by bpf helpers
The above techniques cannot be easily used with bpf progs, since it would
require very significant additions to verifier.
Say we introduce a helper that takes some hidden lock and increments
the counter which is part of map element value. What will you pass into it?
An address of the counter? How verifier can statically check it?
Theoretically it's doable, it's quite complex and run-time performance
would be bad if we have to do lock,++,unlock for every counter.
Existing bpf_xadd insn is likely going to be faster despite cache line
bouncing comparing to per-cpu lock,++,unlock
from your other email:
3) if we use syscall to implement Ri(i=1...3), the period between T(i)
and T(i+1)
can become quite big, for example dozens of seconds, so the accumulated value
in A4 can't represent the actual/correct value(counter) at any time between T0
and T4, and the value is wrong actually, and all events in above diagram
(E0(0)~E0(2M), E1(0)~E1(1M), E2(0) .... E2(10K), ...) aren't counted at all,
and the missed number can be quite huge.
So does the value got by A4 make sense for user?
yes it does. In your example it's number of packets received.
Regardless how slow or fast the per-cpu loop is the aggreate value is still valid.
The kernel is full of loops like:
for_each_possible_cpu(cpu) {
struct stats *pcpu = per_cpu_ptr(stats, cpu);
sum1 += pcpu->cnt1;
sum2 += pcpu->cnt2;
}
and they compute valid values.
It doesn't matter how slow or fast that loop is.
Obviously the faster it is the more accurate the aggragtes will be,
but one can add mdelay() after each iteration and it's still valid.
Anyway, me and Martin had a discussion offline about this. To summarize:
. smp_call() is not a good approach, since it works only kprobe+bpf
. disable irqs for socket-style bpf programs is not an options either, since
pushf/popf adds unnecessary overhead and having irqs off for the life of
the program is bad
. optional irq off for bpf progs that use per-cpu maps is just as bad
. we can do bpf_map_lookup_and_delete() technique for per-cpu hash maps:
delete elem and do for_each_possible_cpu() { copy values into buffer }
from call_rcu() callback, but it needs extra sync wait logic in syscall,
so complexity is probably not worth the gain, though nice that
it's generic and works on all archs
. we can do for_each_possible_cpu() {atomic_long_memcpy of values} in
bpf_map_lookup() syscall. since we know that hash map values are always
8 byte aligned, atomic_long_memcpy() will be a loop of explicit
4-byte or 8-byte copies on 32-bit and 64-bit archs respectively.
User space would need to provide value_size*max_cpus buffer, which will
be partially filled by kernel due to holes in possible_cpus mask.
For #1 'counter' use case the userspace can bzero() the buffer
and aggregate all slots ignoring possible holes, since they're zero.
Doing syscall for each cpu is slower, since for 40+ cpus the cost adds up.
. bpf_map_update() becomes similar with atomic_long_memcpy
The most appealing to me is that no new helpers needed and no new
syscall commands. For per-cpu maps bpf_map_lookup/update() from kernel
operates only on this_cpu() and bpf_map_lookup/update() from syscall
use value_size*num_cpus buffer.
From: Ming Lei <tom.leiming@gmail.com> Date: 2016-01-14 07:16:16
On Thu, Jan 14, 2016 at 1:08 PM, Alexei Starovoitov
[off-list ref] wrote:
On Thu, Jan 14, 2016 at 10:42:44AM +0800, Ming Lei wrote:
quoted
quoted
In case of kprobes irqs are disabled, but yeah for sockets smp_call won't help.
From 'Documentation/kprobes.txt', looks irqs aren't disabled always, see blow:
Probe handlers are run with preemption disabled. Depending on the
architecture and optimization state, handlers may also run with
interrupts disabled (e.g., kretprobe handlers and optimized kprobe
handlers run without interrupt disabled on x86/x86-64).
bpf tracing progs go through ftrace that disables irqs even for
optimized kprobes on x64.
but yeah, there could be an arch that doesn't do it
and long term we probably want to do something about it on x64 as well.
tracepoints+bpf will be with irqs on as well.
quoted
2) multiple counter case
- lots of protection can be used, such per-element rw-spin, percpu lock,
srcu, ..., but each each one may introduce cost in update path of prog.
- the lock mechanism can be provided by bpf helpers
The above techniques cannot be easily used with bpf progs, since it would
require very significant additions to verifier.
Say we introduce a helper that takes some hidden lock and increments
the counter which is part of map element value. What will you pass into it?
An address of the counter? How verifier can statically check it?
Theoretically it's doable, it's quite complex and run-time performance
would be bad if we have to do lock,++,unlock for every counter.
Existing bpf_xadd insn is likely going to be faster despite cache line
bouncing comparing to per-cpu lock,++,unlock
There are two simple approaches I thought of:
1) introduce two helpers of lookup_and_lock_element(map, key) &&
unlock_element(map, key)
- the disadvantage is that unlock_element() need one extra lookup
- verifier needn't any change
2) embedded one lock at the head of returned value
- looks a bit ugly
- it is tricky to obtain the lock in kernel(syscall path)
- still needn't verifier's change
Or other ideas?
from your other email:
quoted
3) if we use syscall to implement Ri(i=1...3), the period between T(i)
and T(i+1)
can become quite big, for example dozens of seconds, so the accumulated value
in A4 can't represent the actual/correct value(counter) at any time between T0
and T4, and the value is wrong actually, and all events in above diagram
(E0(0)~E0(2M), E1(0)~E1(1M), E2(0) .... E2(10K), ...) aren't counted at all,
and the missed number can be quite huge.
So does the value got by A4 make sense for user?
yes it does. In your example it's number of packets received.
Regardless how slow or fast the per-cpu loop is the aggreate value is still valid.
The kernel is full of loops like:
for_each_possible_cpu(cpu) {
struct stats *pcpu = per_cpu_ptr(stats, cpu);
sum1 += pcpu->cnt1;
sum2 += pcpu->cnt2;
}
Yes, that is the way I suggest to use instead of using nr_cpu syscall to
aggreate perpcu value, which kind of usage I never see before.
and they compute valid values.
It doesn't matter how slow or fast that loop is.
I don't think so, quantity breeds quality. In syscall path, there are
lots of possible and long delay(schedule out, memory allocation,
page in, ...) especially when system is in high loading. In the above
loop kernel is using, no all these possible delay.
Obviously the faster it is the more accurate the aggragtes will be,
but one can add mdelay() after each iteration and it's still valid.
It might be valid, but the aggragtes can deviate too much from the
correct value, and it becomes useless, then it doesn't matter about
the validity, does it?
If you don't object, I will try to figure out one patch to support
implementing the aggragate function from bpf prog and we can use
the kernel way to aggragate percpu value, then one single syscall
is enough. Looks one new prog type is needed, but it is very similar
with the sock filter usage.
Anyway, me and Martin had a discussion offline about this. To summarize:
. smp_call() is not a good approach, since it works only kprobe+bpf
Yes.
. disable irqs for socket-style bpf programs is not an options either, since
pushf/popf adds unnecessary overhead and having irqs off for the life of
the program is bad
Agree.
. optional irq off for bpf progs that use per-cpu maps is just as bad
Yes.
. we can do bpf_map_lookup_and_delete() technique for per-cpu hash maps:
delete elem and do for_each_possible_cpu() { copy values into buffer }
from call_rcu() callback, but it needs extra sync wait logic in syscall,
call_rcu() callback often takes long time.
so complexity is probably not worth the gain, though nice that
it's generic and works on all archs
I suggest to not consider for percpu only, since it is a generic issue,
and the approach should cover current array/hash map.
. we can do for_each_possible_cpu() {atomic_long_memcpy of values} in
bpf_map_lookup() syscall. since we know that hash map values are always
8 byte aligned, atomic_long_memcpy() will be a loop of explicit
4-byte or 8-byte copies on 32-bit and 64-bit archs respectively.
User space would need to provide value_size*max_cpus buffer, which will
be partially filled by kernel due to holes in possible_cpus mask.
For #1 'counter' use case the userspace can bzero() the buffer
and aggregate all slots ignoring possible holes, since they're zero.
Doing syscall for each cpu is slower, since for 40+ cpus the cost adds up.
Exactly, that is why I think it is good to define the aggregate function into
bpf prog code, then we can get the total value in single syscall.
. bpf_map_update() becomes similar with atomic_long_memcpy
The most appealing to me is that no new helpers needed and no new
I agree with you about no new helpers.
syscall commands. For per-cpu maps bpf_map_lookup/update() from kernel
No new syscall means we have to aggregate the value from kernel via
bpf prog.
operates only on this_cpu() and bpf_map_lookup/update() from syscall
use value_size*num_cpus buffer.