On Sat, 7 Oct 2023 21:51:01 +0800 Hou Tao <houtao@xxxxxxxxxxxxxxx> wrote: > From: Hou Tao <houtao1@xxxxxxxxxx> > > Introduce alloc_size_percpu() 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 multiple per-cpu area caches for multiple > area sizes and it needs the size of dynamic per-cpu area to select the > corresponding cache when bpf program frees the dynamic per-cpu area. > > --- a/mm/percpu.c > +++ b/mm/percpu.c > @@ -2244,6 +2244,35 @@ static void pcpu_balance_workfn(struct work_struct *work) > mutex_unlock(&pcpu_alloc_mutex); > } > > +/** > + * alloc_size_percpu - 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 alloc_size_percpu(void __percpu *ptr) > +{ > + struct pcpu_chunk *chunk; > + int bit_off, end; It's minor, but I'd suggest unsigned long for both. > + 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; void* - void* is a ptrdiff_t, which is long or int. > + end = find_next_bit(chunk->bound_map, pcpu_chunk_map_bits(chunk), bit_off + 1); find_next_bit takes an unsigned long > + return (end - bit_off) * PCPU_MIN_ALLOC_SIZE; And then we don't need to worry about signedness issues. > +} > +