Skip to content

Commit 9e54dd8

Browse files
Kent Overstreetakpm00
authored andcommitted
rhashtable: plumb through alloc tag
This gives better memory allocation profiling results; rhashtable allocations will be accounted to the code that initialized the rhashtable. [[email protected]: undo _noprof additions in the documentation] Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Kent Overstreet <[email protected]> Signed-off-by: Suren Baghdasaryan <[email protected]> Tested-by: Kees Cook <[email protected]> Cc: Alexander Viro <[email protected]> Cc: Alex Gaynor <[email protected]> Cc: Alice Ryhl <[email protected]> Cc: Andreas Hindborg <[email protected]> Cc: Benno Lossin <[email protected]> Cc: "Björn Roy Baron" <[email protected]> Cc: Boqun Feng <[email protected]> Cc: Christoph Lameter <[email protected]> Cc: Dennis Zhou <[email protected]> Cc: Gary Guo <[email protected]> Cc: Miguel Ojeda <[email protected]> Cc: Pasha Tatashin <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Tejun Heo <[email protected]> Cc: Vlastimil Babka <[email protected]> Cc: Wedson Almeida Filho <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 88ae5fb commit 9e54dd8

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

include/linux/alloc_tag.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,16 @@ static inline void alloc_tag_sub(union codetag_ref *ref, size_t bytes)
152152
ref->ct = NULL;
153153
}
154154

155+
#define alloc_tag_record(p) ((p) = current->alloc_tag)
156+
155157
#else /* CONFIG_MEM_ALLOC_PROFILING */
156158

157159
#define DEFINE_ALLOC_TAG(_alloc_tag)
158160
static inline bool mem_alloc_profiling_enabled(void) { return false; }
159161
static inline void alloc_tag_add(union codetag_ref *ref, struct alloc_tag *tag,
160162
size_t bytes) {}
161163
static inline void alloc_tag_sub(union codetag_ref *ref, size_t bytes) {}
164+
#define alloc_tag_record(p) do {} while (0)
162165

163166
#endif /* CONFIG_MEM_ALLOC_PROFILING */
164167

include/linux/rhashtable-types.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef _LINUX_RHASHTABLE_TYPES_H
1010
#define _LINUX_RHASHTABLE_TYPES_H
1111

12+
#include <linux/alloc_tag.h>
1213
#include <linux/atomic.h>
1314
#include <linux/compiler.h>
1415
#include <linux/mutex.h>
@@ -88,6 +89,9 @@ struct rhashtable {
8889
struct mutex mutex;
8990
spinlock_t lock;
9091
atomic_t nelems;
92+
#ifdef CONFIG_MEM_ALLOC_PROFILING
93+
struct alloc_tag *alloc_tag;
94+
#endif
9195
};
9296

9397
/**
@@ -127,9 +131,12 @@ struct rhashtable_iter {
127131
bool end_of_table;
128132
};
129133

130-
int rhashtable_init(struct rhashtable *ht,
134+
int rhashtable_init_noprof(struct rhashtable *ht,
131135
const struct rhashtable_params *params);
132-
int rhltable_init(struct rhltable *hlt,
136+
#define rhashtable_init(...) alloc_hooks(rhashtable_init_noprof(__VA_ARGS__))
137+
138+
int rhltable_init_noprof(struct rhltable *hlt,
133139
const struct rhashtable_params *params);
140+
#define rhltable_init(...) alloc_hooks(rhltable_init_noprof(__VA_ARGS__))
134141

135142
#endif /* _LINUX_RHASHTABLE_TYPES_H */

lib/rhashtable.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ static union nested_table *nested_table_alloc(struct rhashtable *ht,
130130
if (ntbl)
131131
return ntbl;
132132

133-
ntbl = kzalloc(PAGE_SIZE, GFP_ATOMIC);
133+
ntbl = alloc_hooks_tag(ht->alloc_tag,
134+
kmalloc_noprof(PAGE_SIZE, GFP_ATOMIC|__GFP_ZERO));
134135

135136
if (ntbl && leaf) {
136137
for (i = 0; i < PAGE_SIZE / sizeof(ntbl[0]); i++)
@@ -157,7 +158,8 @@ static struct bucket_table *nested_bucket_table_alloc(struct rhashtable *ht,
157158

158159
size = sizeof(*tbl) + sizeof(tbl->buckets[0]);
159160

160-
tbl = kzalloc(size, gfp);
161+
tbl = alloc_hooks_tag(ht->alloc_tag,
162+
kmalloc_noprof(size, gfp|__GFP_ZERO));
161163
if (!tbl)
162164
return NULL;
163165

@@ -181,7 +183,9 @@ static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
181183
int i;
182184
static struct lock_class_key __key;
183185

184-
tbl = kvzalloc(struct_size(tbl, buckets, nbuckets), gfp);
186+
tbl = alloc_hooks_tag(ht->alloc_tag,
187+
kvmalloc_node_noprof(struct_size(tbl, buckets, nbuckets),
188+
gfp|__GFP_ZERO, NUMA_NO_NODE));
185189

186190
size = nbuckets;
187191

@@ -1016,7 +1020,7 @@ static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed)
10161020
* .obj_hashfn = my_hash_fn,
10171021
* };
10181022
*/
1019-
int rhashtable_init(struct rhashtable *ht,
1023+
int rhashtable_init_noprof(struct rhashtable *ht,
10201024
const struct rhashtable_params *params)
10211025
{
10221026
struct bucket_table *tbl;
@@ -1031,6 +1035,8 @@ int rhashtable_init(struct rhashtable *ht,
10311035
spin_lock_init(&ht->lock);
10321036
memcpy(&ht->p, params, sizeof(*params));
10331037

1038+
alloc_tag_record(ht->alloc_tag);
1039+
10341040
if (params->min_size)
10351041
ht->p.min_size = roundup_pow_of_two(params->min_size);
10361042

@@ -1076,7 +1082,7 @@ int rhashtable_init(struct rhashtable *ht,
10761082

10771083
return 0;
10781084
}
1079-
EXPORT_SYMBOL_GPL(rhashtable_init);
1085+
EXPORT_SYMBOL_GPL(rhashtable_init_noprof);
10801086

10811087
/**
10821088
* rhltable_init - initialize a new hash list table
@@ -1087,15 +1093,15 @@ EXPORT_SYMBOL_GPL(rhashtable_init);
10871093
*
10881094
* See documentation for rhashtable_init.
10891095
*/
1090-
int rhltable_init(struct rhltable *hlt, const struct rhashtable_params *params)
1096+
int rhltable_init_noprof(struct rhltable *hlt, const struct rhashtable_params *params)
10911097
{
10921098
int err;
10931099

1094-
err = rhashtable_init(&hlt->ht, params);
1100+
err = rhashtable_init_noprof(&hlt->ht, params);
10951101
hlt->ht.rhlist = true;
10961102
return err;
10971103
}
1098-
EXPORT_SYMBOL_GPL(rhltable_init);
1104+
EXPORT_SYMBOL_GPL(rhltable_init_noprof);
10991105

11001106
static void rhashtable_free_one(struct rhashtable *ht, struct rhash_head *obj,
11011107
void (*free_fn)(void *ptr, void *arg),

0 commit comments

Comments
 (0)