Skip to content

Commit acc3a0d

Browse files
mykyta5Alexei Starovoitov
authored andcommitted
bpf: htab: extract helper for freeing special structs
Extract the cleanup of known embedded structs into the dedicated helper. Remove duplication and introduce a single source of truth for freeing special embedded structs in hashtab. Signed-off-by: Mykyta Yatsenko <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Acked-by: Eduard Zingerman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 5eab266 commit acc3a0d

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

kernel/bpf/hashtab.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,16 @@ 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)
219+
{
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+
}
227+
218228
static void htab_free_prealloced_timers_and_wq(struct bpf_htab *htab)
219229
{
220230
u32 num_entries = htab->map.max_entries;
@@ -227,12 +237,7 @@ static void htab_free_prealloced_timers_and_wq(struct bpf_htab *htab)
227237
struct htab_elem *elem;
228238

229239
elem = get_htab_elem(htab, i);
230-
if (btf_record_has_field(htab->map.record, BPF_TIMER))
231-
bpf_obj_free_timer(htab->map.record,
232-
htab_elem_value(elem, htab->map.key_size));
233-
if (btf_record_has_field(htab->map.record, BPF_WORKQUEUE))
234-
bpf_obj_free_workqueue(htab->map.record,
235-
htab_elem_value(elem, htab->map.key_size));
240+
htab_free_internal_structs(htab, elem);
236241
cond_resched();
237242
}
238243
}
@@ -1502,12 +1507,7 @@ static void htab_free_malloced_timers_and_wq(struct bpf_htab *htab)
15021507

15031508
hlist_nulls_for_each_entry(l, n, head, hash_node) {
15041509
/* We only free timer on uref dropping to zero */
1505-
if (btf_record_has_field(htab->map.record, BPF_TIMER))
1506-
bpf_obj_free_timer(htab->map.record,
1507-
htab_elem_value(l, htab->map.key_size));
1508-
if (btf_record_has_field(htab->map.record, BPF_WORKQUEUE))
1509-
bpf_obj_free_workqueue(htab->map.record,
1510-
htab_elem_value(l, htab->map.key_size));
1510+
htab_free_internal_structs(htab, l);
15111511
}
15121512
cond_resched_rcu();
15131513
}

0 commit comments

Comments
 (0)