Skip to content

Commit d07afd9

Browse files
committed
tracing collisions
Signed-off-by: Tingmao Wang <m@maowtm.org>
1 parent 985151f commit d07afd9

File tree

5 files changed

+149
-64
lines changed

5 files changed

+149
-64
lines changed

include/trace/events/landlock.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* Copyright © 2025 Microsoft Corporation
4+
*/
5+
6+
#undef TRACE_SYSTEM
7+
#define TRACE_SYSTEM landlock
8+
9+
#if !defined(_TRACE_LANDLOCK_H) || defined(TRACE_HEADER_MULTI_READ)
10+
#define _TRACE_LANDLOCK_H
11+
12+
#include <linux/tracepoint.h>
13+
14+
struct landlock_domain_index;
15+
16+
TRACE_EVENT(
17+
landlock_domain_hash_find,
18+
TP_PROTO(
19+
const struct landlock_domain_index* indices_arr,
20+
u32 num_indices,
21+
int hash_bits,
22+
const struct landlock_domain_index* elem_to_find,
23+
u32 collisions_followed
24+
),
25+
26+
TP_ARGS(indices_arr, num_indices, hash_bits, elem_to_find, collisions_followed),
27+
TP_STRUCT__entry(
28+
__field(const struct landlock_domain_index *, indices_arr)
29+
__field(u32, num_indices)
30+
__field(u32, hash_bits)
31+
__field(uintptr_t, key)
32+
__field(u32, collisions_followed)
33+
),
34+
35+
TP_fast_assign(
36+
__entry->indices_arr = indices_arr;
37+
__entry->num_indices = num_indices;
38+
__entry->hash_bits = hash_bits;
39+
__entry->key = *(uintptr_t *)elem_to_find;
40+
__entry->collisions_followed = collisions_followed;
41+
),
42+
43+
TP_printk(
44+
"indices_arr=%p num_indices=%u hash_bits=%u, key=%lx collisions_followed=%u",
45+
__entry->indices_arr,
46+
__entry->num_indices,
47+
__entry->hash_bits,
48+
__entry->key,
49+
__entry->collisions_followed
50+
)
51+
);
52+
53+
#endif /* _TRACE_LANDLOCK_H */
54+
55+
/* This part must be outside protection */
56+
#include <trace/define_trace.h>

security/landlock/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
obj-$(CONFIG_SECURITY_LANDLOCK) := landlock.o
22

33
landlock-y := setup.o syscalls.o object.o ruleset.o \
4-
cred.o task.o fs.o domain.o
4+
cred.o task.o fs.o domain.o trace.o
55

66
landlock-$(CONFIG_INET) += net.o
77

security/landlock/coalesced_hash.h

Lines changed: 79 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,18 @@ typedef void (*set_next_collision_t)(void *elem, h_index_t next_collision);
2929
typedef bool (*compare_element_t)(const void *key_elem, const void *found_elem);
3030
typedef bool (*element_is_empty_t)(const void *elem);
3131

32-
static inline void *h_find(const void *table, h_index_t table_size,
33-
int hash_bits, size_t elem_size,
34-
const void *elem_to_find, hash_element_t hash_elem,
35-
get_next_collision_t get_next_collision,
36-
compare_element_t compare_elem,
37-
element_is_empty_t element_is_empty)
32+
static inline void *
33+
h_find(const void *table, h_index_t table_size, int hash_bits, size_t elem_size,
34+
const void *elem_to_find, h_index_t *nb_collisions_followed,
35+
hash_element_t hash_elem, get_next_collision_t get_next_collision,
36+
compare_element_t compare_elem, element_is_empty_t element_is_empty)
3837
{
3938
h_index_t curr_index, next_collision;
4039
const void *curr_elem;
4140

41+
if (nb_collisions_followed)
42+
*nb_collisions_followed = 0;
43+
4244
if (unlikely(table_size == 0))
4345
return NULL;
4446

@@ -54,6 +56,8 @@ static inline void *h_find(const void *table, h_index_t table_size,
5456
next_collision = get_next_collision(curr_elem);
5557
while (next_collision != curr_index) {
5658
curr_index = next_collision;
59+
if (nb_collisions_followed)
60+
(*nb_collisions_followed)++;
5761
curr_elem = table + curr_index * elem_size;
5862
if (compare_elem(elem_to_find, curr_elem))
5963
return (void *)curr_elem;
@@ -332,61 +336,74 @@ static inline void h_insert(struct h_insert_scratch *scratch, const void *elem,
332336
* empty (i.e. not used). Empty elements are not returned by find. If
333337
* the zero value of @elem_type is not "empty", the caller must set all
334338
* the slots to empty before using the table.
339+
* @trace_find: A function that traces the number of collisions
340+
* followed during a find operation, given paramemters:
341+
* const @elem_type *table,
342+
* h_index_t table_size,
343+
* int hash_bits,
344+
* const @elem_type *elem_to_find,
345+
* h_index_t *collisions_followed.
335346
*/
336-
#define DEFINE_COALESCED_HASH_TABLE(elem_type, table_func_prefix, key_member, \
337-
next_collision_member, hash_expr, \
338-
is_empty_expr) \
339-
static inline h_index_t table_func_prefix##_hash_elem( \
340-
const void *_elem, h_index_t table_size, int hash_bits) \
341-
{ \
342-
const elem_type *elem = _elem; \
343-
return hash_expr; \
344-
} \
345-
static inline h_index_t table_func_prefix##_get_next_collision( \
346-
const void *elem) \
347-
{ \
348-
return ((const elem_type *)elem)->next_collision_member; \
349-
} \
350-
static inline void table_func_prefix##_set_next_collision( \
351-
void *elem, h_index_t next_collision) \
352-
{ \
353-
((elem_type *)elem)->next_collision_member = next_collision; \
354-
} \
355-
static inline bool table_func_prefix##_compare_elem( \
356-
const void *key_elem, const void *found_elem) \
357-
{ \
358-
const elem_type *key = key_elem; \
359-
const elem_type *found = found_elem; \
360-
return key->key_member.data == found->key_member.data; \
361-
} \
362-
static inline bool table_func_prefix##_element_is_empty( \
363-
const void *_elem) \
364-
{ \
365-
const elem_type *elem = _elem; \
366-
return is_empty_expr; \
367-
} \
368-
static inline const elem_type *table_func_prefix##_find( \
369-
const elem_type *table, h_index_t table_size, int hash_bits, \
370-
const elem_type *elem_to_find) \
371-
{ \
372-
return h_find(table, table_size, hash_bits, sizeof(elem_type), \
373-
elem_to_find, table_func_prefix##_hash_elem, \
374-
table_func_prefix##_get_next_collision, \
375-
table_func_prefix##_compare_elem, \
376-
table_func_prefix##_element_is_empty); \
377-
} \
378-
static inline void table_func_prefix##_initialize( \
379-
elem_type *table, h_index_t table_size) \
380-
{ \
381-
h_initialize(table, table_size, sizeof(elem_type), \
382-
table_func_prefix##_set_next_collision, \
383-
table_func_prefix##_element_is_empty); \
384-
} \
385-
static inline void table_func_prefix##_insert( \
386-
struct h_insert_scratch *scratch, const elem_type *elem) \
387-
{ \
388-
h_insert(scratch, elem, table_func_prefix##_hash_elem, \
389-
table_func_prefix##_get_next_collision, \
390-
table_func_prefix##_set_next_collision, \
391-
table_func_prefix##_element_is_empty); \
347+
#define DEFINE_COALESCED_HASH_TABLE(elem_type, table_func_prefix, key_member, \
348+
next_collision_member, hash_expr, \
349+
is_empty_expr, trace_find) \
350+
static inline h_index_t table_func_prefix##_hash_elem( \
351+
const void *_elem, h_index_t table_size, int hash_bits) \
352+
{ \
353+
const elem_type *elem = _elem; \
354+
return hash_expr; \
355+
} \
356+
static inline h_index_t table_func_prefix##_get_next_collision( \
357+
const void *elem) \
358+
{ \
359+
return ((const elem_type *)elem)->next_collision_member; \
360+
} \
361+
static inline void table_func_prefix##_set_next_collision( \
362+
void *elem, h_index_t next_collision) \
363+
{ \
364+
((elem_type *)elem)->next_collision_member = next_collision; \
365+
} \
366+
static inline bool table_func_prefix##_compare_elem( \
367+
const void *key_elem, const void *found_elem) \
368+
{ \
369+
const elem_type *key = key_elem; \
370+
const elem_type *found = found_elem; \
371+
return key->key_member.data == found->key_member.data; \
372+
} \
373+
static inline bool table_func_prefix##_element_is_empty( \
374+
const void *_elem) \
375+
{ \
376+
const elem_type *elem = _elem; \
377+
return is_empty_expr; \
378+
} \
379+
static inline const elem_type *table_func_prefix##_find( \
380+
const elem_type *table, h_index_t table_size, int hash_bits, \
381+
const elem_type *elem_to_find) \
382+
{ \
383+
h_index_t collisions_followed; \
384+
const elem_type *result = h_find( \
385+
table, table_size, hash_bits, sizeof(elem_type), \
386+
elem_to_find, &collisions_followed, \
387+
table_func_prefix##_hash_elem, \
388+
table_func_prefix##_get_next_collision, \
389+
table_func_prefix##_compare_elem, \
390+
table_func_prefix##_element_is_empty); \
391+
trace_find(table, table_size, hash_bits, elem_to_find, \
392+
collisions_followed); \
393+
return result; \
394+
} \
395+
static inline void table_func_prefix##_initialize( \
396+
elem_type *table, h_index_t table_size) \
397+
{ \
398+
h_initialize(table, table_size, sizeof(elem_type), \
399+
table_func_prefix##_set_next_collision, \
400+
table_func_prefix##_element_is_empty); \
401+
} \
402+
static inline void table_func_prefix##_insert( \
403+
struct h_insert_scratch *scratch, const elem_type *elem) \
404+
{ \
405+
h_insert(scratch, elem, table_func_prefix##_hash_elem, \
406+
table_func_prefix##_get_next_collision, \
407+
table_func_prefix##_set_next_collision, \
408+
table_func_prefix##_element_is_empty); \
392409
}

security/landlock/domain.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/refcount.h>
1919
#include <linux/sched.h>
2020
#include <linux/slab.h>
21+
#include <trace/events/landlock.h>
2122

2223
#include "access.h"
2324
#include "ruleset.h"
@@ -198,7 +199,8 @@ static inline int get_hash_bits(const u32 table_size)
198199
DEFINE_COALESCED_HASH_TABLE(struct landlock_domain_index, dom_hash, key,
199200
next_collision,
200201
dom_index_hash_func(elem, table_size, hash_bits),
201-
dom_index_is_empty(elem))
202+
dom_index_is_empty(elem),
203+
trace_landlock_domain_hash_find)
202204

203205
struct landlock_domain *
204206
landlock_alloc_domain(const struct landlock_domain *sizes);

security/landlock/trace.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Landlock - Tracepoints
4+
*
5+
* Copyright © 2025 Microsoft Corporation
6+
* Copyright © 2025 Tingmao Wang <m@maowtm.org>
7+
*/
8+
9+
#define CREATE_TRACE_POINTS
10+
#include <trace/events/landlock.h>

0 commit comments

Comments
 (0)