UPSTREAM: rss_stat: Add support to detect RSS updates of external mm

When a process updates the RSS of a different process, the rss_stat
tracepoint appears in the context of the process doing the update. This
can confuse userspace that the RSS of process doing the update is
updated, while in reality a different process's RSS was updated.

This issue happens in reclaim paths such as with direct reclaim or
background reclaim.

This patch adds more information to the tracepoint about whether the mm
being updated belongs to the current process's context (curr field). We
also include a hash of the mm pointer so that the process who the mm
belongs to can be uniquely identified (mm_id field).

Also vsprintf.c is refactored a bit to allow reuse of hashing code.

Change-Id: Ic87af93af608c83be0b08757aed99d2b9c2c01d8
Reported-by: Ioannis Ilkos <ilkos@google.com>
Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org>
Acked-by: Petr Mladek <pmladek@suse.com> # lib/vsprintf.c
Signed-off-by: Joel Fernandes <joelaf@google.com>
This commit is contained in:
Joel Fernandes
2019-11-18 14:02:15 -05:00
parent 77510796c6
commit f6cc86c7bd
5 changed files with 66 additions and 25 deletions

View File

@@ -1643,27 +1643,27 @@ static inline unsigned long get_mm_counter(struct mm_struct *mm, int member)
return (unsigned long)val; return (unsigned long)val;
} }
void mm_trace_rss_stat(int member, long count); void mm_trace_rss_stat(struct mm_struct *mm, int member, long count);
static inline void add_mm_counter(struct mm_struct *mm, int member, long value) static inline void add_mm_counter(struct mm_struct *mm, int member, long value)
{ {
long count = atomic_long_add_return(value, &mm->rss_stat.count[member]); long count = atomic_long_add_return(value, &mm->rss_stat.count[member]);
mm_trace_rss_stat(member, count); mm_trace_rss_stat(mm, member, count);
} }
static inline void inc_mm_counter(struct mm_struct *mm, int member) static inline void inc_mm_counter(struct mm_struct *mm, int member)
{ {
long count = atomic_long_inc_return(&mm->rss_stat.count[member]); long count = atomic_long_inc_return(&mm->rss_stat.count[member]);
mm_trace_rss_stat(member, count); mm_trace_rss_stat(mm, member, count);
} }
static inline void dec_mm_counter(struct mm_struct *mm, int member) static inline void dec_mm_counter(struct mm_struct *mm, int member)
{ {
long count = atomic_long_dec_return(&mm->rss_stat.count[member]); long count = atomic_long_dec_return(&mm->rss_stat.count[member]);
mm_trace_rss_stat(member, count); mm_trace_rss_stat(mm, member, count);
} }
/* Optimized variant when page is already known not to be PageAnon */ /* Optimized variant when page is already known not to be PageAnon */

View File

@@ -216,6 +216,8 @@ int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) __printf(3, 4);
extern ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos, extern ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
const void *from, size_t available); const void *from, size_t available);
int ptr_to_hashval(const void *ptr, unsigned long *hashval_out);
/** /**
* strstarts - does @str start with @prefix? * strstarts - does @str start with @prefix?
* @str: string to examine * @str: string to examine

View File

@@ -316,24 +316,50 @@ TRACE_EVENT(mm_page_alloc_extfrag,
__entry->change_ownership) __entry->change_ownership)
); );
/*
* Required for uniquely and securely identifying mm in rss_stat tracepoint.
*/
#ifndef __PTR_TO_HASHVAL
static unsigned int __maybe_unused mm_ptr_to_hash(const void *ptr)
{
int ret;
unsigned long hashval;
ret = ptr_to_hashval(ptr, &hashval);
if (ret)
return 0;
/* The hashed value is only 32-bit */
return (unsigned int)hashval;
}
#define __PTR_TO_HASHVAL
#endif
TRACE_EVENT(rss_stat, TRACE_EVENT(rss_stat,
TP_PROTO(int member, TP_PROTO(struct mm_struct *mm,
int member,
long count), long count),
TP_ARGS(member, count), TP_ARGS(mm, member, count),
TP_STRUCT__entry( TP_STRUCT__entry(
__field(unsigned int, mm_id)
__field(unsigned int, curr)
__field(int, member) __field(int, member)
__field(long, size) __field(long, size)
), ),
TP_fast_assign( TP_fast_assign(
__entry->mm_id = mm_ptr_to_hash(mm);
__entry->curr = !!(current->mm == mm);
__entry->member = member; __entry->member = member;
__entry->size = (count << PAGE_SHIFT); __entry->size = (count << PAGE_SHIFT);
), ),
TP_printk("member=%d size=%ldB", TP_printk("mm_id=%u curr=%d member=%d size=%ldB",
__entry->mm_id,
__entry->curr,
__entry->member, __entry->member,
__entry->size) __entry->size)
); );

View File

@@ -739,24 +739,12 @@ static int __init initialize_ptr_random(void)
} }
early_initcall(initialize_ptr_random); early_initcall(initialize_ptr_random);
/* Maps a pointer to a 32 bit unique identifier. */ int ptr_to_hashval(const void *ptr, unsigned long *hashval_out)
static char *ptr_to_id(char *buf, char *end, const void *ptr,
struct printf_spec spec)
{ {
const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)";
unsigned long hashval; unsigned long hashval;
/* When debugging early boot use non-cryptographically secure hash. */ if (static_branch_unlikely(&not_filled_random_ptr_key))
if (unlikely(debug_boot_weak_hash)) { return -EAGAIN;
hashval = hash_long((unsigned long)ptr, 32);
return pointer_string(buf, end, (const void *)hashval, spec);
}
if (static_branch_unlikely(&not_filled_random_ptr_key)) {
spec.field_width = 2 * sizeof(ptr);
/* string length must be less than default_width */
return error_string(buf, end, str, spec);
}
#ifdef CONFIG_64BIT #ifdef CONFIG_64BIT
hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key); hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
@@ -768,6 +756,31 @@ static char *ptr_to_id(char *buf, char *end, const void *ptr,
#else #else
hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key); hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
#endif #endif
*hashval_out = hashval;
return 0;
}
/* Maps a pointer to a 32 bit unique identifier. */
static char *ptr_to_id(char *buf, char *end, const void *ptr,
struct printf_spec spec)
{
const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)";
unsigned long hashval;
int ret;
/* When debugging early boot use non-cryptographically secure hash. */
if (unlikely(debug_boot_weak_hash)) {
hashval = hash_long((unsigned long)ptr, 32);
return pointer_string(buf, end, (const void *)hashval, spec);
}
ret = ptr_to_hashval(ptr, &hashval);
if (ret) {
spec.field_width = 2 * sizeof(ptr);
/* string length must be less than default_width */
return error_string(buf, end, str, spec);
}
return pointer_string(buf, end, (const void *)hashval, spec); return pointer_string(buf, end, (const void *)hashval, spec);
} }

View File

@@ -142,9 +142,9 @@ static int __init init_zero_pfn(void)
} }
core_initcall(init_zero_pfn); core_initcall(init_zero_pfn);
void mm_trace_rss_stat(int member, long count) void mm_trace_rss_stat(struct mm_struct *mm, int member, long count)
{ {
trace_rss_stat(member, count); trace_rss_stat(mm, member, count);
} }
#if defined(SPLIT_RSS_COUNTING) #if defined(SPLIT_RSS_COUNTING)