From patchwork Wed Nov 6 02:44:51 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Fernandes X-Patchwork-Id: 11229141 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A9D4916B1 for ; Wed, 6 Nov 2019 02:45:00 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 466DF21D7F for ; Wed, 6 Nov 2019 02:45:00 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=joelfernandes.org header.i=@joelfernandes.org header.b="k4jGG7GZ" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 466DF21D7F Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=joelfernandes.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 73A4D6B0003; Tue, 5 Nov 2019 21:44:59 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 6C2B26B0006; Tue, 5 Nov 2019 21:44:59 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5643C6B0007; Tue, 5 Nov 2019 21:44:59 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0229.hostedemail.com [216.40.44.229]) by kanga.kvack.org (Postfix) with ESMTP id 397E26B0003 for ; Tue, 5 Nov 2019 21:44:59 -0500 (EST) Received: from smtpin07.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with SMTP id E64908249980 for ; Wed, 6 Nov 2019 02:44:58 +0000 (UTC) X-FDA: 76124310276.07.car01_2b6a675a0585e X-Spam-Summary: 2,0,0,fd58cdb70b831dd5,d41d8cd98f00b204,joel@joelfernandes.org,:linux-kernel@vger.kernel.org:joel@joelfernandes.org:ilkos@google.com:minchan@google.com:primiano@google.com:fmayer@google.com:hjd@google.com:joaodias@google.com:joelaf@google.com:lalitm@google.com:rslawik@google.com:sspatil@google.com:timmurray@google.com:akpm@linux-foundation.org:andriy.shevchenko@linux.intel.com:changbin.du@intel.com:mingo@redhat.com:joe@perches.com:keescook@chromium.org::mhocko@suse.com:pmladek@suse.com:rafael.j.wysocki@intel.com:sakari.ailus@linux.intel.com:sergey.senozhatsky@gmail.com:sfr@canb.auug.org.au:rostedt@goodmis.org,RULES_HIT:1:2:41:69:355:379:541:800:960:973:988:989:1260:1311:1314:1345:1437:1515:1605:1730:1747:1777:1792:2393:2559:2562:2892:2895:3138:3139:3140:3141:3142:3865:3866:3867:3868:3870:3871:3874:4050:4250:4321:4605:5007:6119:6120:6261:6653:6742:6743:7875:7901:7903:8568:8603:8957:10004:11026:11232:11473:11658:11914:12043:12291:12296:12297:12438:12517:12519:12 555:1267 X-HE-Tag: car01_2b6a675a0585e X-Filterd-Recvd-Size: 10662 Received: from mail-pf1-f193.google.com (mail-pf1-f193.google.com [209.85.210.193]) by imf44.hostedemail.com (Postfix) with ESMTP for ; Wed, 6 Nov 2019 02:44:58 +0000 (UTC) Received: by mail-pf1-f193.google.com with SMTP id p26so17674061pfq.8 for ; Tue, 05 Nov 2019 18:44:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=RSmJ0wuH4ZQI8dxqbp3iw/jPAFBBlQoY1aWhBRKrK+w=; b=k4jGG7GZ5IHSgDuA/PQlGInpwmgi466Uv+TZ3/0zGyX2MI2Zn6Bj7YIEFP2RnsHCjK Gcmnn19mbFxZLeTKDpGgRGq6oTf0LPMWkbZ8A0jyzsc3Gjh8TkVuPxX+imltJr0PVZvr Lxz7h10DhAVSXOkOrBkpLbgpwHK2IZIacN7eM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=RSmJ0wuH4ZQI8dxqbp3iw/jPAFBBlQoY1aWhBRKrK+w=; b=iu2BmLhRPKp8Tk3t1wKjg8q8IsIzGTZMvzdHdoaN2ijIF1JwOtGOJZuG8zSWqJpYDf x8oZdVLXoJ/7WHSjt2Hai/BcRfPDQV10BJgOtzlUj8LRumMvQxc133S71H+f1i6dC42Q xtSFf5dIH4f7sP17E5i++8TFljxFQ9bza/WbaISXSIiuDPXhorP8ve7IP5bPfBAhY1CF eOpje9c2OiyM7C6CnItpTiIaX/i4BHERuvt0CV9FiileUN+Rny4Aa0sgpnkNPpLqlQbN LkhdX3jOxEy111Orq8ouZte+Ksa0dN2Qp4HJeitwm1YevHCLJndJCMjIJvJxBM7cN1Fi xjxA== X-Gm-Message-State: APjAAAWDiIAlXFEawzt1VNJ4XmyFp8H0KRz/63xAOnYivwmp0TuV6ypl RQDy0A+9h8Sd8sZhE0BvFtAExw== X-Google-Smtp-Source: APXvYqyqLbhWq/w3O9r3GIC+QQQ1jLZp7H8ijRtbW72ZRwS/idm+Jf1u1OObqSoA9u/4e5iSDK1rVA== X-Received: by 2002:a63:d44a:: with SMTP id i10mr92555pgj.105.1573008296693; Tue, 05 Nov 2019 18:44:56 -0800 (PST) Received: from joelaf.cam.corp.google.com ([2620:15c:6:12:9c46:e0da:efbf:69cc]) by smtp.gmail.com with ESMTPSA id n62sm836902pjc.6.2019.11.05.18.44.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Nov 2019 18:44:56 -0800 (PST) From: "Joel Fernandes (Google)" To: linux-kernel@vger.kernel.org Cc: "Joel Fernandes (Google)" , Ioannis Ilkos , minchan@google.com, primiano@google.com, fmayer@google.com, hjd@google.com, joaodias@google.com, joelaf@google.com, lalitm@google.com, rslawik@google.com, sspatil@google.com, timmurray@google.com, Andrew Morton , Andy Shevchenko , Changbin Du , Ingo Molnar , Joe Perches , Kees Cook , linux-mm@kvack.org, Michal Hocko , Petr Mladek , "Rafael J. Wysocki" , Sakari Ailus , Sergey Senozhatsky , Stephen Rothwell , Steven Rostedt Subject: [PATCH] rss_stat: Add support to detect RSS updates of external mm Date: Tue, 5 Nov 2019 21:44:51 -0500 Message-Id: <20191106024452.81923-1-joel@joelfernandes.org> X-Mailer: git-send-email 2.24.0.rc1.363.gb1bccd3e3d-goog MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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. Reported-by: Ioannis Ilkos Signed-off-by: Joel Fernandes (Google) Acked-by: Petr Mladek # lib/vsprintf.c Signed-off-by: Joel Fernandes --- Based on top of the commit in linux-next: 8342d836dc7c ("mm: emit tracepoint when RSS changes") Google Bug: 140711541 Cc: minchan@google.com Cc: primiano@google.com Cc: ilkos@google.com Cc: fmayer@google.com Cc: hjd@google.com Cc: joaodias@google.com Cc: joelaf@google.com Cc: lalitm@google.com Cc: rslawik@google.com Cc: sspatil@google.com Cc: timmurray@google.com include/linux/mm.h | 8 ++++---- include/linux/string.h | 2 ++ include/trace/events/kmem.h | 32 +++++++++++++++++++++++++++++--- lib/vsprintf.c | 36 +++++++++++++++++++++++++----------- mm/memory.c | 4 ++-- 5 files changed, 62 insertions(+), 20 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 31d8cfb3d988..bfbe65ccffa3 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1643,27 +1643,27 @@ static inline unsigned long get_mm_counter(struct mm_struct *mm, int member) 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) { 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) { 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) { 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 */ diff --git a/include/linux/string.h b/include/linux/string.h index f516cec5277c..6b0e950701d0 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -261,6 +261,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, const void *from, size_t available); +int ptr_to_hashval(const void *ptr, unsigned long *hashval_out); + /** * strstarts - does @str start with @prefix? * @str: string to examine diff --git a/include/trace/events/kmem.h b/include/trace/events/kmem.h index 5a0666bfcf85..ad7e642bd497 100644 --- a/include/trace/events/kmem.h +++ b/include/trace/events/kmem.h @@ -316,24 +316,50 @@ TRACE_EVENT(mm_page_alloc_extfrag, __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, - TP_PROTO(int member, + TP_PROTO(struct mm_struct *mm, + int member, long count), - TP_ARGS(member, count), + TP_ARGS(mm, member, count), TP_STRUCT__entry( + __field(unsigned int, mm_id) + __field(unsigned int, curr) __field(int, member) __field(long, size) ), TP_fast_assign( + __entry->mm_id = mm_ptr_to_hash(mm); + __entry->curr = !!(current->mm == mm); __entry->member = member; __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->size) ); diff --git a/lib/vsprintf.c b/lib/vsprintf.c index dee8fc467fcf..401baaac1813 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -761,11 +761,34 @@ static int __init initialize_ptr_random(void) 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) +{ + const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)"; + unsigned long hashval; + + if (static_branch_unlikely(¬_filled_random_ptr_key)) + return -EAGAIN; + +#ifdef CONFIG_64BIT + hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key); + /* + * Mask off the first 32 bits, this makes explicit that we have + * modified the address (and 32 bits is plenty for a unique ID). + */ + hashval = hashval & 0xffffffff; +#else + hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key); +#endif + *hashval_out = hashval; + return 0; +} + 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)) { @@ -773,22 +796,13 @@ static char *ptr_to_id(char *buf, char *end, const void *ptr, return pointer_string(buf, end, (const void *)hashval, spec); } - if (static_branch_unlikely(¬_filled_random_ptr_key)) { + 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); } -#ifdef CONFIG_64BIT - hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key); - /* - * Mask off the first 32 bits, this makes explicit that we have - * modified the address (and 32 bits is plenty for a unique ID). - */ - hashval = hashval & 0xffffffff; -#else - hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key); -#endif return pointer_string(buf, end, (const void *)hashval, spec); } diff --git a/mm/memory.c b/mm/memory.c index 7596d625ebd1..d3c9784e6dc1 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -154,9 +154,9 @@ static int __init init_zero_pfn(void) } 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)