2022-04-14 14:01:00

by Hyeonggon Yoo

[permalink] [raw]
Subject: [PATCH v2 14/23] mm/slab_common: print cache name in tracepoints

Print cache name in tracepoints. If there is no corresponding cache
(kmalloc in SLOB or kmalloc_large_node), use KMALLOC_{,LARGE_}NAME
macro.

Signed-off-by: Hyeonggon Yoo <[email protected]>
---
include/trace/events/kmem.h | 34 +++++++++++++++++++---------------
mm/slab.c | 9 +++++----
mm/slab.h | 4 ++++
mm/slab_common.c | 6 ++----
mm/slob.c | 10 +++++-----
mm/slub.c | 10 +++++-----
6 files changed, 40 insertions(+), 33 deletions(-)

diff --git a/include/trace/events/kmem.h b/include/trace/events/kmem.h
index ddc8c944f417..35e6887c6101 100644
--- a/include/trace/events/kmem.h
+++ b/include/trace/events/kmem.h
@@ -61,16 +61,18 @@ DEFINE_EVENT(kmem_alloc, kmem_cache_alloc,

DECLARE_EVENT_CLASS(kmem_alloc_node,

- TP_PROTO(unsigned long call_site,
+ TP_PROTO(const char *name,
+ unsigned long call_site,
const void *ptr,
size_t bytes_req,
size_t bytes_alloc,
gfp_t gfp_flags,
int node),

- TP_ARGS(call_site, ptr, bytes_req, bytes_alloc, gfp_flags, node),
+ TP_ARGS(name, call_site, ptr, bytes_req, bytes_alloc, gfp_flags, node),

TP_STRUCT__entry(
+ __string( name, name )
__field( unsigned long, call_site )
__field( const void *, ptr )
__field( size_t, bytes_req )
@@ -80,6 +82,7 @@ DECLARE_EVENT_CLASS(kmem_alloc_node,
),

TP_fast_assign(
+ __assign_str(name, name);
__entry->call_site = call_site;
__entry->ptr = ptr;
__entry->bytes_req = bytes_req;
@@ -88,7 +91,8 @@ DECLARE_EVENT_CLASS(kmem_alloc_node,
__entry->node = node;
),

- TP_printk("call_site=%pS ptr=%p bytes_req=%zu bytes_alloc=%zu gfp_flags=%s node=%d",
+ TP_printk("name=%s call_site=%pS ptr=%p bytes_req=%zu bytes_alloc=%zu gfp_flags=%s node=%d",
+ __get_str(name),
(void *)__entry->call_site,
__entry->ptr,
__entry->bytes_req,
@@ -99,20 +103,20 @@ DECLARE_EVENT_CLASS(kmem_alloc_node,

DEFINE_EVENT(kmem_alloc_node, kmalloc_node,

- TP_PROTO(unsigned long call_site, const void *ptr,
- size_t bytes_req, size_t bytes_alloc,
+ TP_PROTO(const char *name, unsigned long call_site,
+ const void *ptr, size_t bytes_req, size_t bytes_alloc,
gfp_t gfp_flags, int node),

- TP_ARGS(call_site, ptr, bytes_req, bytes_alloc, gfp_flags, node)
+ TP_ARGS(name, call_site, ptr, bytes_req, bytes_alloc, gfp_flags, node)
);

DEFINE_EVENT(kmem_alloc_node, kmem_cache_alloc_node,

- TP_PROTO(unsigned long call_site, const void *ptr,
- size_t bytes_req, size_t bytes_alloc,
+ TP_PROTO(const char *name, unsigned long call_site,
+ const void *ptr, size_t bytes_req, size_t bytes_alloc,
gfp_t gfp_flags, int node),

- TP_ARGS(call_site, ptr, bytes_req, bytes_alloc, gfp_flags, node)
+ TP_ARGS(name, call_site, ptr, bytes_req, bytes_alloc, gfp_flags, node)
);

TRACE_EVENT(kfree,
@@ -137,24 +141,24 @@ TRACE_EVENT(kfree,

TRACE_EVENT(kmem_cache_free,

- TP_PROTO(unsigned long call_site, const void *ptr, const char *name),
+ TP_PROTO(const char *name, unsigned long call_site, const void *ptr),

- TP_ARGS(call_site, ptr, name),
+ TP_ARGS(name, call_site, ptr),

TP_STRUCT__entry(
+ __string( name, name )
__field( unsigned long, call_site )
__field( const void *, ptr )
- __string( name, name )
),

TP_fast_assign(
+ __assign_str(name, name);
__entry->call_site = call_site;
__entry->ptr = ptr;
- __assign_str(name, name);
),

- TP_printk("call_site=%pS ptr=%p name=%s",
- (void *)__entry->call_site, __entry->ptr, __get_str(name))
+ TP_printk("name=%s call_site=%pS ptr=%p",
+ __get_str(name), (void *)__entry->call_site, __entry->ptr)
);

TRACE_EVENT(mm_page_free,
diff --git a/mm/slab.c b/mm/slab.c
index 1dfe0f9d5882..3c47d0979706 100644
--- a/mm/slab.c
+++ b/mm/slab.c
@@ -3448,8 +3448,9 @@ void *__kmem_cache_alloc_node(struct kmem_cache *cachep, struct list_lru *lru,
void *ret = slab_alloc_node(cachep, lru, flags, nodeid,
cachep->object_size, caller);

- trace_kmem_cache_alloc_node(caller, ret, cachep->object_size,
- cachep->size, flags, nodeid);
+ trace_kmem_cache_alloc_node(cachep->name, caller, ret,
+ cachep->object_size, cachep->size,
+ flags, nodeid);

return ret;
}
@@ -3518,7 +3519,7 @@ void *kmem_cache_alloc_node_trace(struct kmem_cache *cachep,
ret = slab_alloc_node(cachep, NULL, flags, nodeid, size, _RET_IP_);

ret = kasan_kmalloc(cachep, ret, size, flags);
- trace_kmalloc_node(_RET_IP_, ret,
+ trace_kmalloc_node(cachep->name, _RET_IP_, ret,
size, cachep->size,
flags, nodeid);
return ret;
@@ -3593,7 +3594,7 @@ void kmem_cache_free(struct kmem_cache *cachep, void *objp)
if (!cachep)
return;

- trace_kmem_cache_free(_RET_IP_, objp, cachep->name);
+ trace_kmem_cache_free(cachep->name, _RET_IP_, objp);
local_irq_save(flags);
debug_check_no_locks_freed(objp, cachep->object_size);
if (!(cachep->flags & SLAB_DEBUG_OBJECTS))
diff --git a/mm/slab.h b/mm/slab.h
index b864c5bc4c25..45ddb19df319 100644
--- a/mm/slab.h
+++ b/mm/slab.h
@@ -275,6 +275,10 @@ void create_kmalloc_caches(slab_flags_t);
struct kmem_cache *kmalloc_slab(size_t, gfp_t);
#endif

+/* cache names for tracepoints where it has no corresponding cache */
+#define KMALLOC_LARGE_NAME "kmalloc_large_node"
+#define KMALLOC_NAME "kmalloc_node"
+
gfp_t kmalloc_fix_flags(gfp_t flags);

/* Functions provided by the slab allocators */
diff --git a/mm/slab_common.c b/mm/slab_common.c
index 960cc07c3a91..416f0a1f17a6 100644
--- a/mm/slab_common.c
+++ b/mm/slab_common.c
@@ -956,10 +956,8 @@ void *kmalloc_large_node(size_t size, gfp_t flags, int node)
ptr = kasan_kmalloc_large(ptr, size, flags);
/* As ptr might get tagged, call kmemleak hook after KASAN. */
kmemleak_alloc(ptr, size, 1, flags);
- trace_kmalloc_node(_RET_IP_, ptr,
- size, PAGE_SIZE << order,
- flags, node);
-
+ trace_kmalloc_node(KMALLOC_LARGE_NAME, _RET_IP_, ptr, size,
+ PAGE_SIZE << order, flags, node);
return ptr;
}
EXPORT_SYMBOL(kmalloc_large_node);
diff --git a/mm/slob.c b/mm/slob.c
index 6c7c30845056..8abde6037d95 100644
--- a/mm/slob.c
+++ b/mm/slob.c
@@ -505,7 +505,7 @@ __do_kmalloc_node(size_t size, gfp_t gfp, int node, unsigned long caller)
*m = size;
ret = (void *)m + minalign;

- trace_kmalloc_node(caller, ret,
+ trace_kmalloc_node(KMALLOC_NAME, caller, ret,
size, size + minalign, gfp, node);
} else {
unsigned int order = get_order(size);
@@ -514,7 +514,7 @@ __do_kmalloc_node(size_t size, gfp_t gfp, int node, unsigned long caller)
gfp |= __GFP_COMP;
ret = slob_new_pages(gfp, order, node);

- trace_kmalloc_node(caller, ret,
+ trace_kmalloc_node(KMALLOC_LARGE_NAME, caller, ret,
size, PAGE_SIZE << order, gfp, node);
}

@@ -597,12 +597,12 @@ static void *slob_alloc_node(struct kmem_cache *c, gfp_t flags, int node,

if (c->size < PAGE_SIZE) {
b = slob_alloc(c->size, flags, c->align, node, 0);
- trace_kmem_cache_alloc_node(caller, b, c->object_size,
+ trace_kmem_cache_alloc_node(c->name, caller, b, c->object_size,
SLOB_UNITS(c->size) * SLOB_UNIT,
flags, node);
} else {
b = slob_new_pages(flags, get_order(c->size), node);
- trace_kmem_cache_alloc_node(caller, b, c->object_size,
+ trace_kmem_cache_alloc_node(c->name, caller, b, c->object_size,
PAGE_SIZE << get_order(c->size),
flags, node);
}
@@ -648,7 +648,7 @@ static void kmem_rcu_free(struct rcu_head *head)
void kmem_cache_free(struct kmem_cache *c, void *b)
{
kmemleak_free_recursive(b, c->flags);
- trace_kmem_cache_free(_RET_IP_, b, c->name);
+ trace_kmem_cache_free(c->name, _RET_IP_, b);
if (unlikely(c->flags & SLAB_TYPESAFE_BY_RCU)) {
struct slob_rcu *slob_rcu;
slob_rcu = b + (c->size - sizeof(struct slob_rcu));
diff --git a/mm/slub.c b/mm/slub.c
index 1dc9e8eebb62..de03fa1f5667 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -3215,7 +3215,7 @@ void *__kmem_cache_alloc_node(struct kmem_cache *s, struct list_lru *lru, gfp_t
{
void *ret = slab_alloc_node(s, lru, gfpflags, node, caller, s->object_size);

- trace_kmem_cache_alloc_node(caller, ret, s->object_size,
+ trace_kmem_cache_alloc_node(s->name, caller, ret, s->object_size,
s->size, gfpflags, node);

return ret;
@@ -3229,7 +3229,7 @@ void *kmem_cache_alloc_node_trace(struct kmem_cache *s,
{
void *ret = slab_alloc_node(s, NULL, gfpflags, node, _RET_IP_, size);

- trace_kmalloc_node(_RET_IP_, ret,
+ trace_kmalloc_node(s->name, _RET_IP_, ret,
size, s->size, gfpflags, node);

ret = kasan_kmalloc(s, ret, size, gfpflags);
@@ -3471,7 +3471,7 @@ void kmem_cache_free(struct kmem_cache *s, void *x)
s = cache_from_obj(s, x);
if (!s)
return;
- trace_kmem_cache_free(_RET_IP_, x, s->name);
+ trace_kmem_cache_free(s->name, _RET_IP_, x);
slab_free(s, virt_to_slab(x), x, NULL, 1, _RET_IP_);
}
EXPORT_SYMBOL(kmem_cache_free);
@@ -4352,7 +4352,7 @@ void *__kmalloc_node(size_t size, gfp_t flags, int node)

ret = slab_alloc_node(s, NULL, flags, node, _RET_IP_, size);

- trace_kmalloc_node(_RET_IP_, ret, size, s->size, flags, node);
+ trace_kmalloc_node(s->name, _RET_IP_, ret, size, s->size, flags, node);

ret = kasan_kmalloc(s, ret, size, flags);

@@ -4811,7 +4811,7 @@ void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
ret = slab_alloc_node(s, NULL, gfpflags, node, caller, size);

/* Honor the call site pointer we received. */
- trace_kmalloc_node(caller, ret, size, s->size, gfpflags, node);
+ trace_kmalloc_node(s->name, caller, ret, size, s->size, gfpflags, node);

return ret;
}
--
2.32.0


2022-05-02 13:52:04

by Hyeonggon Yoo

[permalink] [raw]
Subject: Re: [PATCH v2 14/23] mm/slab_common: print cache name in tracepoints

On Fri, Apr 29, 2022 at 04:05:14PM +0200, Vlastimil Babka wrote:
> On 4/14/22 10:57, Hyeonggon Yoo wrote:
> > Print cache name in tracepoints. If there is no corresponding cache
> > (kmalloc in SLOB or kmalloc_large_node), use KMALLOC_{,LARGE_}NAME
> > macro.
> >
> > Signed-off-by: Hyeonggon Yoo <[email protected]>
>
> Regarding tracepoints, I'm not sure it's a good idea to unify kmalloc and
> kmem_cache_alloc. I think the common use case is to trace kmalloc as there
> are many different callers, and then I'm not interested in kmem_cache_alloc
> callers much.

Make sense.

> What I would suggest instead is:
> - drop the _node versions, add node to normal versions
> - drop the kmem_alloc EVENT_CLASS, as kmalloc is different enough from
> kmem_cache_alloc (see next points), define separately as TRACE_EVENT().
> - printing cache_name makes sense to add for kmem_cache_alloc (also allows
> filtering events) but not for kmalloc.
> - kmem_cache_alloc with name can then drop the bytes_req, bytes_alloc as
> they are fixed for given name (and can be read from slabinfo).

Those suggestions makes sense to me.
kmalloc and kmem_cache_alloc are a bit different.

> Not using a common tracepoint will prevent some later unifications/cleanup
> (patch 21?), but hopefully not too much?

In v2 unification of __kmalloc_node()/kfree() somewhat depends on unified tracepoint.
I'll try to unify them with this approach in v3.

2022-05-03 01:17:58

by Vlastimil Babka

[permalink] [raw]
Subject: Re: [PATCH v2 14/23] mm/slab_common: print cache name in tracepoints

On 4/14/22 10:57, Hyeonggon Yoo wrote:
> Print cache name in tracepoints. If there is no corresponding cache
> (kmalloc in SLOB or kmalloc_large_node), use KMALLOC_{,LARGE_}NAME
> macro.
>
> Signed-off-by: Hyeonggon Yoo <[email protected]>

Regarding tracepoints, I'm not sure it's a good idea to unify kmalloc and
kmem_cache_alloc. I think the common use case is to trace kmalloc as there
are many different callers, and then I'm not interested in kmem_cache_alloc
callers much.

What I would suggest instead is:
- drop the _node versions, add node to normal versions
- drop the kmem_alloc EVENT_CLASS, as kmalloc is different enough from
kmem_cache_alloc (see next points), define separately as TRACE_EVENT().
- printing cache_name makes sense to add for kmem_cache_alloc (also allows
filtering events) but not for kmalloc.
- kmem_cache_alloc with name can then drop the bytes_req, bytes_alloc as
they are fixed for given name (and can be read from slabinfo).

Not using a common tracepoint will prevent some later unifications/cleanup
(patch 21?), but hopefully not too much?