Skip to content

Commit b8f9a04

Browse files
committed
bpf: extract internal structs helpers
arraymap and hashtab duplicate the logic that checks for and frees internal structs (timer, workqueue, task_work) based on BTF record flags. Centralize this by introducing two helpers: * bpf_map_has_internal_structs(map) Returns true if the map value contains any of internal structs: BPF_TIMER | BPF_WORKQUEUE | BPF_TASK_WORK. * bpf_map_free_internal_structs(map, obj) Frees the internal structs for a single value object. Convert arraymap and both the prealloc/malloc hashtab paths to use the new generic functions. This keeps the functionality for when/how to free these special fields in one place and makes it easier to add support for new internal structs in the future without touching every map implementation. Signed-off-by: Mykyta Yatsenko <[email protected]>
1 parent eb6f2b6 commit b8f9a04

File tree

3 files changed

+35
-31
lines changed

3 files changed

+35
-31
lines changed

include/linux/bpf.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,10 @@ int map_check_no_btf(const struct bpf_map *map,
663663
bool bpf_map_meta_equal(const struct bpf_map *meta0,
664664
const struct bpf_map *meta1);
665665

666+
bool bpf_map_has_internal_structs(struct bpf_map *map);
667+
668+
void bpf_map_free_internal_structs(struct bpf_map *map, void *obj);
669+
666670
extern const struct bpf_map_ops bpf_map_offload_ops;
667671

668672
/* bpf_type_flag contains a set of flags that are applicable to the values of

kernel/bpf/arraymap.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -448,19 +448,14 @@ static void array_map_free_internal_structs(struct bpf_map *map)
448448
struct bpf_array *array = container_of(map, struct bpf_array, map);
449449
int i;
450450

451-
/* We don't reset or free fields other than timer and workqueue
451+
/* We don't reset or free fields other than timer, workqueue and task_work
452452
* on uref dropping to zero.
453453
*/
454-
if (btf_record_has_field(map->record, BPF_TIMER | BPF_WORKQUEUE | BPF_TASK_WORK)) {
455-
for (i = 0; i < array->map.max_entries; i++) {
456-
if (btf_record_has_field(map->record, BPF_TIMER))
457-
bpf_obj_free_timer(map->record, array_map_elem_ptr(array, i));
458-
if (btf_record_has_field(map->record, BPF_WORKQUEUE))
459-
bpf_obj_free_workqueue(map->record, array_map_elem_ptr(array, i));
460-
if (btf_record_has_field(map->record, BPF_TASK_WORK))
461-
bpf_obj_free_task_work(map->record, array_map_elem_ptr(array, i));
462-
}
463-
}
454+
if (!bpf_map_has_internal_structs(map))
455+
return;
456+
457+
for (i = 0; i < array->map.max_entries; i++)
458+
bpf_map_free_internal_structs(map, array_map_elem_ptr(array, i));
464459
}
465460

466461
/* Called when map->refcnt goes to zero, either from workqueue or from syscall */

kernel/bpf/hashtab.c

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -215,17 +215,19 @@ static bool htab_has_extra_elems(struct bpf_htab *htab)
215215
return !htab_is_percpu(htab) && !htab_is_lru(htab) && !is_fd_htab(htab);
216216
}
217217

218-
static void htab_free_internal_structs(struct bpf_htab *htab, struct htab_elem *elem)
218+
bool bpf_map_has_internal_structs(struct bpf_map *map)
219219
{
220-
if (btf_record_has_field(htab->map.record, BPF_TIMER))
221-
bpf_obj_free_timer(htab->map.record,
222-
htab_elem_value(elem, htab->map.key_size));
223-
if (btf_record_has_field(htab->map.record, BPF_WORKQUEUE))
224-
bpf_obj_free_workqueue(htab->map.record,
225-
htab_elem_value(elem, htab->map.key_size));
226-
if (btf_record_has_field(htab->map.record, BPF_TASK_WORK))
227-
bpf_obj_free_task_work(htab->map.record,
228-
htab_elem_value(elem, htab->map.key_size));
220+
return btf_record_has_field(map->record, BPF_TIMER | BPF_WORKQUEUE | BPF_TASK_WORK);
221+
}
222+
223+
void bpf_map_free_internal_structs(struct bpf_map *map, void *obj)
224+
{
225+
if (btf_record_has_field(map->record, BPF_TIMER))
226+
bpf_obj_free_timer(map->record, obj);
227+
else if (btf_record_has_field(map->record, BPF_WORKQUEUE))
228+
bpf_obj_free_workqueue(map->record, obj);
229+
else if (btf_record_has_field(map->record, BPF_TASK_WORK))
230+
bpf_obj_free_task_work(map->record, obj);
229231
}
230232

231233
static void htab_free_prealloced_internal_structs(struct bpf_htab *htab)
@@ -240,7 +242,8 @@ static void htab_free_prealloced_internal_structs(struct bpf_htab *htab)
240242
struct htab_elem *elem;
241243

242244
elem = get_htab_elem(htab, i);
243-
htab_free_internal_structs(htab, elem);
245+
bpf_map_free_internal_structs(&htab->map,
246+
htab_elem_value(elem, htab->map.key_size));
244247
cond_resched();
245248
}
246249
}
@@ -1509,8 +1512,9 @@ static void htab_free_malloced_internal_structs(struct bpf_htab *htab)
15091512
struct htab_elem *l;
15101513

15111514
hlist_nulls_for_each_entry(l, n, head, hash_node) {
1512-
/* We only free timer on uref dropping to zero */
1513-
htab_free_internal_structs(htab, l);
1515+
/* We only free timer, wq, task_work on uref dropping to zero */
1516+
bpf_map_free_internal_structs(&htab->map,
1517+
htab_elem_value(l, htab->map.key_size));
15141518
}
15151519
cond_resched_rcu();
15161520
}
@@ -1521,13 +1525,14 @@ static void htab_map_free_internal_structs(struct bpf_map *map)
15211525
{
15221526
struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
15231527

1524-
/* We only free timer and workqueue on uref dropping to zero */
1525-
if (btf_record_has_field(htab->map.record, BPF_TIMER | BPF_WORKQUEUE | BPF_TASK_WORK)) {
1526-
if (!htab_is_prealloc(htab))
1527-
htab_free_malloced_internal_structs(htab);
1528-
else
1529-
htab_free_prealloced_internal_structs(htab);
1530-
}
1528+
/* We only free timer, workqueue, task_work on uref dropping to zero */
1529+
if (!bpf_map_has_internal_structs(map))
1530+
return;
1531+
1532+
if (htab_is_prealloc(htab))
1533+
htab_free_prealloced_internal_structs(htab);
1534+
else
1535+
htab_free_malloced_internal_structs(htab);
15311536
}
15321537

15331538
/* Called when map->refcnt goes to zero, either from workqueue or from syscall */

0 commit comments

Comments
 (0)