Message ID | 20231018113343.2446300-3-houtao@huaweicloud.com (mailing list archive) |
---|---|
State | Superseded |
Delegated to: | BPF |
Headers | show |
Series | bpf: Fixes for per-cpu kptr | expand |
On Wed, Oct 18, 2023 at 4:32 AM Hou Tao <houtao@huaweicloud.com> wrote: > > From: Hou Tao <houtao1@huawei.com> > > Introduce pcpu_alloc_size() to get the size of the dynamic per-cpu > area. It will be used by bpf memory allocator in the following patches. > BPF memory allocator maintains per-cpu area caches for multiple area > sizes and its free API only has the to-be-freed per-cpu pointer, so it > needs the size of dynamic per-cpu area to select the corresponding cache > when bpf program frees the dynamic per-cpu pointer. > > Signed-off-by: Hou Tao <houtao1@huawei.com> > --- > include/linux/percpu.h | 1 + > mm/percpu.c | 30 ++++++++++++++++++++++++++++++ > 2 files changed, 31 insertions(+) > > diff --git a/include/linux/percpu.h b/include/linux/percpu.h > index 68fac2e7cbe6..8c677f185901 100644 > --- a/include/linux/percpu.h > +++ b/include/linux/percpu.h > @@ -132,6 +132,7 @@ extern void __init setup_per_cpu_areas(void); > extern void __percpu *__alloc_percpu_gfp(size_t size, size_t align, gfp_t gfp) __alloc_size(1); > extern void __percpu *__alloc_percpu(size_t size, size_t align) __alloc_size(1); > extern void free_percpu(void __percpu *__pdata); > +extern size_t pcpu_alloc_size(void __percpu *__pdata); > > DEFINE_FREE(free_percpu, void __percpu *, free_percpu(_T)) > > diff --git a/mm/percpu.c b/mm/percpu.c > index 76b9c5e63c56..b0cea2dc16a9 100644 > --- a/mm/percpu.c > +++ b/mm/percpu.c > @@ -2244,6 +2244,36 @@ static void pcpu_balance_workfn(struct work_struct *work) > mutex_unlock(&pcpu_alloc_mutex); > } > > +/** > + * pcpu_alloc_size - the size of the dynamic percpu area > + * @ptr: pointer to the dynamic percpu area > + * > + * Return the size of the dynamic percpu area @ptr. > + * > + * RETURNS: > + * The size of the dynamic percpu area. > + * > + * CONTEXT: > + * Can be called from atomic context. > + */ > +size_t pcpu_alloc_size(void __percpu *ptr) > +{ > + struct pcpu_chunk *chunk; > + unsigned long bit_off, end; > + void *addr; > + > + if (!ptr) > + return 0; > + > + addr = __pcpu_ptr_to_addr(ptr); > + /* No pcpu_lock here: ptr has not been freed, so chunk is still alive */ > + chunk = pcpu_chunk_addr_search(addr); > + bit_off = (addr - chunk->base_addr) / PCPU_MIN_ALLOC_SIZE; > + end = find_next_bit(chunk->bound_map, pcpu_chunk_map_bits(chunk), > + bit_off + 1); > + return (end - bit_off) * PCPU_MIN_ALLOC_SIZE; > +} Dennis, Tejun, or Christoph, Could you please Ack patch 1 and 2, so we can apply this fix to bpf tree before the merge window. The series fixes a serious bug.
On Wed, Oct 18, 2023 at 07:33:38PM +0800, Hou Tao wrote: > From: Hou Tao <houtao1@huawei.com> > > Introduce pcpu_alloc_size() to get the size of the dynamic per-cpu > area. It will be used by bpf memory allocator in the following patches. > BPF memory allocator maintains per-cpu area caches for multiple area > sizes and its free API only has the to-be-freed per-cpu pointer, so it > needs the size of dynamic per-cpu area to select the corresponding cache > when bpf program frees the dynamic per-cpu pointer. > > Signed-off-by: Hou Tao <houtao1@huawei.com> > --- > include/linux/percpu.h | 1 + > mm/percpu.c | 30 ++++++++++++++++++++++++++++++ > 2 files changed, 31 insertions(+) > > diff --git a/include/linux/percpu.h b/include/linux/percpu.h > index 68fac2e7cbe6..8c677f185901 100644 > --- a/include/linux/percpu.h > +++ b/include/linux/percpu.h > @@ -132,6 +132,7 @@ extern void __init setup_per_cpu_areas(void); > extern void __percpu *__alloc_percpu_gfp(size_t size, size_t align, gfp_t gfp) __alloc_size(1); > extern void __percpu *__alloc_percpu(size_t size, size_t align) __alloc_size(1); > extern void free_percpu(void __percpu *__pdata); > +extern size_t pcpu_alloc_size(void __percpu *__pdata); > > DEFINE_FREE(free_percpu, void __percpu *, free_percpu(_T)) > > diff --git a/mm/percpu.c b/mm/percpu.c > index 76b9c5e63c56..b0cea2dc16a9 100644 > --- a/mm/percpu.c > +++ b/mm/percpu.c > @@ -2244,6 +2244,36 @@ static void pcpu_balance_workfn(struct work_struct *work) > mutex_unlock(&pcpu_alloc_mutex); > } > > +/** > + * pcpu_alloc_size - the size of the dynamic percpu area > + * @ptr: pointer to the dynamic percpu area > + * > + * Return the size of the dynamic percpu area @ptr. > + * Alexei, can you modify the above comment to: Returns the size of the @ptr allocation. This is undefined for statically defined percpu variables as there is no corresponding chunk->bound_map. > + * RETURNS: > + * The size of the dynamic percpu area. > + * > + * CONTEXT: > + * Can be called from atomic context. > + */ > +size_t pcpu_alloc_size(void __percpu *ptr) > +{ > + struct pcpu_chunk *chunk; > + unsigned long bit_off, end; > + void *addr; > + > + if (!ptr) > + return 0; > + > + addr = __pcpu_ptr_to_addr(ptr); > + /* No pcpu_lock here: ptr has not been freed, so chunk is still alive */ > + chunk = pcpu_chunk_addr_search(addr); > + bit_off = (addr - chunk->base_addr) / PCPU_MIN_ALLOC_SIZE; > + end = find_next_bit(chunk->bound_map, pcpu_chunk_map_bits(chunk), > + bit_off + 1); > + return (end - bit_off) * PCPU_MIN_ALLOC_SIZE; > +} > + > /** > * free_percpu - free percpu area > * @ptr: pointer to area to free > -- > 2.29.2 > > Acked-by: Dennis Zhou <dennis@kernel.org> Thanks, Dennis
On Thu, Oct 19, 2023 at 9:09 PM Dennis Zhou <dennis@kernel.org> wrote: > > On Wed, Oct 18, 2023 at 07:33:38PM +0800, Hou Tao wrote: > > From: Hou Tao <houtao1@huawei.com> > > > > Introduce pcpu_alloc_size() to get the size of the dynamic per-cpu > > area. It will be used by bpf memory allocator in the following patches. > > BPF memory allocator maintains per-cpu area caches for multiple area > > sizes and its free API only has the to-be-freed per-cpu pointer, so it > > needs the size of dynamic per-cpu area to select the corresponding cache > > when bpf program frees the dynamic per-cpu pointer. > > > > Signed-off-by: Hou Tao <houtao1@huawei.com> > > --- > > include/linux/percpu.h | 1 + > > mm/percpu.c | 30 ++++++++++++++++++++++++++++++ > > 2 files changed, 31 insertions(+) > > > > diff --git a/include/linux/percpu.h b/include/linux/percpu.h > > index 68fac2e7cbe6..8c677f185901 100644 > > --- a/include/linux/percpu.h > > +++ b/include/linux/percpu.h > > @@ -132,6 +132,7 @@ extern void __init setup_per_cpu_areas(void); > > extern void __percpu *__alloc_percpu_gfp(size_t size, size_t align, gfp_t gfp) __alloc_size(1); > > extern void __percpu *__alloc_percpu(size_t size, size_t align) __alloc_size(1); > > extern void free_percpu(void __percpu *__pdata); > > +extern size_t pcpu_alloc_size(void __percpu *__pdata); > > > > DEFINE_FREE(free_percpu, void __percpu *, free_percpu(_T)) > > > > diff --git a/mm/percpu.c b/mm/percpu.c > > index 76b9c5e63c56..b0cea2dc16a9 100644 > > --- a/mm/percpu.c > > +++ b/mm/percpu.c > > @@ -2244,6 +2244,36 @@ static void pcpu_balance_workfn(struct work_struct *work) > > mutex_unlock(&pcpu_alloc_mutex); > > } > > > > +/** > > + * pcpu_alloc_size - the size of the dynamic percpu area > > + * @ptr: pointer to the dynamic percpu area > > + * > > + * Return the size of the dynamic percpu area @ptr. > > + * > > Alexei, can you modify the above comment to: > > Returns the size of the @ptr allocation. This is undefined for statically > defined percpu variables as there is no corresponding chunk->bound_map. Good point! Will do. Thanks for the quick review!
Hi, On 10/20/2023 12:16 PM, Alexei Starovoitov wrote: > On Thu, Oct 19, 2023 at 9:09 PM Dennis Zhou <dennis@kernel.org> wrote: >> On Wed, Oct 18, 2023 at 07:33:38PM +0800, Hou Tao wrote: >>> From: Hou Tao <houtao1@huawei.com> >>> >>> Introduce pcpu_alloc_size() to get the size of the dynamic per-cpu >>> area. It will be used by bpf memory allocator in the following patches. >>> BPF memory allocator maintains per-cpu area caches for multiple area >>> sizes and its free API only has the to-be-freed per-cpu pointer, so it >>> needs the size of dynamic per-cpu area to select the corresponding cache >>> when bpf program frees the dynamic per-cpu pointer. >>> >>> Signed-off-by: Hou Tao <houtao1@huawei.com> >>> --- >>> include/linux/percpu.h | 1 + >>> mm/percpu.c | 30 ++++++++++++++++++++++++++++++ >>> 2 files changed, 31 insertions(+) >>> >>> diff --git a/include/linux/percpu.h b/include/linux/percpu.h >>> index 68fac2e7cbe6..8c677f185901 100644 >>> --- a/include/linux/percpu.h >>> +++ b/include/linux/percpu.h >>> @@ -132,6 +132,7 @@ extern void __init setup_per_cpu_areas(void); >>> extern void __percpu *__alloc_percpu_gfp(size_t size, size_t align, gfp_t gfp) __alloc_size(1); >>> extern void __percpu *__alloc_percpu(size_t size, size_t align) __alloc_size(1); >>> extern void free_percpu(void __percpu *__pdata); >>> +extern size_t pcpu_alloc_size(void __percpu *__pdata); >>> >>> DEFINE_FREE(free_percpu, void __percpu *, free_percpu(_T)) >>> >>> diff --git a/mm/percpu.c b/mm/percpu.c >>> index 76b9c5e63c56..b0cea2dc16a9 100644 >>> --- a/mm/percpu.c >>> +++ b/mm/percpu.c >>> @@ -2244,6 +2244,36 @@ static void pcpu_balance_workfn(struct work_struct *work) >>> mutex_unlock(&pcpu_alloc_mutex); >>> } >>> >>> +/** >>> + * pcpu_alloc_size - the size of the dynamic percpu area >>> + * @ptr: pointer to the dynamic percpu area >>> + * >>> + * Return the size of the dynamic percpu area @ptr. >>> + * >> Alexei, can you modify the above comment to: >> >> Returns the size of the @ptr allocation. This is undefined for statically >> defined percpu variables as there is no corresponding chunk->bound_map. > Good point! Will do. I will post v3 to update the API document. > > Thanks for the quick review! > > .
diff --git a/include/linux/percpu.h b/include/linux/percpu.h index 68fac2e7cbe6..8c677f185901 100644 --- a/include/linux/percpu.h +++ b/include/linux/percpu.h @@ -132,6 +132,7 @@ extern void __init setup_per_cpu_areas(void); extern void __percpu *__alloc_percpu_gfp(size_t size, size_t align, gfp_t gfp) __alloc_size(1); extern void __percpu *__alloc_percpu(size_t size, size_t align) __alloc_size(1); extern void free_percpu(void __percpu *__pdata); +extern size_t pcpu_alloc_size(void __percpu *__pdata); DEFINE_FREE(free_percpu, void __percpu *, free_percpu(_T)) diff --git a/mm/percpu.c b/mm/percpu.c index 76b9c5e63c56..b0cea2dc16a9 100644 --- a/mm/percpu.c +++ b/mm/percpu.c @@ -2244,6 +2244,36 @@ static void pcpu_balance_workfn(struct work_struct *work) mutex_unlock(&pcpu_alloc_mutex); } +/** + * pcpu_alloc_size - the size of the dynamic percpu area + * @ptr: pointer to the dynamic percpu area + * + * Return the size of the dynamic percpu area @ptr. + * + * RETURNS: + * The size of the dynamic percpu area. + * + * CONTEXT: + * Can be called from atomic context. + */ +size_t pcpu_alloc_size(void __percpu *ptr) +{ + struct pcpu_chunk *chunk; + unsigned long bit_off, end; + void *addr; + + if (!ptr) + return 0; + + addr = __pcpu_ptr_to_addr(ptr); + /* No pcpu_lock here: ptr has not been freed, so chunk is still alive */ + chunk = pcpu_chunk_addr_search(addr); + bit_off = (addr - chunk->base_addr) / PCPU_MIN_ALLOC_SIZE; + end = find_next_bit(chunk->bound_map, pcpu_chunk_map_bits(chunk), + bit_off + 1); + return (end - bit_off) * PCPU_MIN_ALLOC_SIZE; +} + /** * free_percpu - free percpu area * @ptr: pointer to area to free