Files
linux/include/trace/events/memcg.h
T
Qi Zheng 85358bad68 mm: memcontrol: change val type to long in __mod_memcg_{lruvec_}state()
The __mod_memcg_state() and __mod_memcg_lruvec_state() functions are also
used to reparent non-hierarchical stats.  In this scenario, the values
passed to them are accumulated statistics that might be extremely large
and exceed the upper limit of a 32-bit integer.

Change the val parameter type from int to long in these functions and
their corresponding tracepoints (memcg_rstat_stats) to prevent potential
overflow issues.

After that, in memcg_state_val_in_pages(), if the passed val is negative,
the expression val * unit / PAGE_SIZE could be implicitly converted to a
massive positive number when compared with 1UL in the max() macro.  This
leads to returning an incorrect massive positive value.

Fix this by using abs(val) to calculate the magnitude first, and then
restoring the sign of the value before returning the result. 
Additionally, use mult_frac() to prevent potential overflow during the
multiplication of val and unit.

Link: https://lore.kernel.org/70a9440e49c464b4dca88bcabc6b491bd335c9f0.1774604356.git.zhengqi.arch@bytedance.com
Signed-off-by: Qi Zheng <[email protected]>
Reported-by: Harry Yoo (Oracle) <[email protected]>
Reviewed-by: Lorenzo Stoakes (Oracle) <[email protected]>
Reviewed-by: Harry Yoo (Oracle) <[email protected]>
Cc: Allen Pais <[email protected]>
Cc: Axel Rasmussen <[email protected]>
Cc: Baoquan He <[email protected]>
Cc: David Hildenbrand <[email protected]>
Cc: Hamza Mahfooz <[email protected]>
Cc: Hugh Dickins <[email protected]>
Cc: Imran Khan <[email protected]>
Cc: Johannes Weiner <[email protected]>
Cc: Kamalesh Babulal <[email protected]>
Cc: Lance Yang <[email protected]>
Cc: Michal Hocko <[email protected]>
Cc: Michal Koutný <[email protected]>
Cc: Muchun Song <[email protected]>
Cc: Roman Gushchin <[email protected]>
Cc: Shakeel Butt <[email protected]>
Cc: Usama Arif <[email protected]>
Cc: Wei Xu <[email protected]>
Cc: Yuanchu Xie <[email protected]>
Cc: Zi Yan <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
2026-04-18 00:10:48 -07:00

107 lines
2.2 KiB
C

/* SPDX-License-Identifier: GPL-2.0 */
#undef TRACE_SYSTEM
#define TRACE_SYSTEM memcg
#if !defined(_TRACE_MEMCG_H) || defined(TRACE_HEADER_MULTI_READ)
#define _TRACE_MEMCG_H
#include <linux/memcontrol.h>
#include <linux/tracepoint.h>
DECLARE_EVENT_CLASS(memcg_rstat_stats,
TP_PROTO(struct mem_cgroup *memcg, int item, long val),
TP_ARGS(memcg, item, val),
TP_STRUCT__entry(
__field(u64, id)
__field(int, item)
__field(long, val)
),
TP_fast_assign(
__entry->id = cgroup_id(memcg->css.cgroup);
__entry->item = item;
__entry->val = val;
),
TP_printk("memcg_id=%llu item=%d val=%ld",
__entry->id, __entry->item, __entry->val)
);
DEFINE_EVENT(memcg_rstat_stats, mod_memcg_state,
TP_PROTO(struct mem_cgroup *memcg, int item, long val),
TP_ARGS(memcg, item, val)
);
DEFINE_EVENT(memcg_rstat_stats, mod_memcg_lruvec_state,
TP_PROTO(struct mem_cgroup *memcg, int item, long val),
TP_ARGS(memcg, item, val)
);
DECLARE_EVENT_CLASS(memcg_rstat_events,
TP_PROTO(struct mem_cgroup *memcg, int item, unsigned long val),
TP_ARGS(memcg, item, val),
TP_STRUCT__entry(
__field(u64, id)
__field(int, item)
__field(unsigned long, val)
),
TP_fast_assign(
__entry->id = cgroup_id(memcg->css.cgroup);
__entry->item = item;
__entry->val = val;
),
TP_printk("memcg_id=%llu item=%d val=%lu",
__entry->id, __entry->item, __entry->val)
);
DEFINE_EVENT(memcg_rstat_events, count_memcg_events,
TP_PROTO(struct mem_cgroup *memcg, int item, unsigned long val),
TP_ARGS(memcg, item, val)
);
TRACE_EVENT(memcg_flush_stats,
TP_PROTO(struct mem_cgroup *memcg, s64 stats_updates,
bool force, bool needs_flush),
TP_ARGS(memcg, stats_updates, force, needs_flush),
TP_STRUCT__entry(
__field(u64, id)
__field(s64, stats_updates)
__field(bool, force)
__field(bool, needs_flush)
),
TP_fast_assign(
__entry->id = cgroup_id(memcg->css.cgroup);
__entry->stats_updates = stats_updates;
__entry->force = force;
__entry->needs_flush = needs_flush;
),
TP_printk("memcg_id=%llu stats_updates=%lld force=%d needs_flush=%d",
__entry->id, __entry->stats_updates,
__entry->force, __entry->needs_flush)
);
#endif /* _TRACE_MEMCG_H */
/* This part must be outside protection */
#include <trace/define_trace.h>