From patchwork Sat Jan 29 20:53:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Waiman Long X-Patchwork-Id: 12729637 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6F651C433EF for ; Sat, 29 Jan 2022 20:53:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7A6A06B007B; Sat, 29 Jan 2022 15:53:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7090B6B007D; Sat, 29 Jan 2022 15:53:53 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4E8B06B007E; Sat, 29 Jan 2022 15:53:53 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0151.hostedemail.com [216.40.44.151]) by kanga.kvack.org (Postfix) with ESMTP id 312396B007B for ; Sat, 29 Jan 2022 15:53:53 -0500 (EST) Received: from smtpin18.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id CC08280FFEEF for ; Sat, 29 Jan 2022 20:53:52 +0000 (UTC) X-FDA: 79084526304.18.318DA0A Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf04.hostedemail.com (Postfix) with ESMTP id 5518940002 for ; Sat, 29 Jan 2022 20:53:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643489631; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=nX58UmfyCZKrrdzehrq1UxMQowz9BiWVJRz/V9rRqyY=; b=OTyzXJq5aw/I+PxhklijLlH465QQaxedwncC5Fov+hB2t8PT3rnjo4vJD7FIeo40LUSnwa NxAEgr4b01WtG+6hpRIBfvYat2dvLGie2UmAMojR3WJLcVGok006bbf+jIUpuGFrVFm9sl i3BefYgqimNa5hVcOFNZJU+s7O3qx1k= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-511-h1czgUQNPdOvrroX67Un8A-1; Sat, 29 Jan 2022 15:53:48 -0500 X-MC-Unique: h1czgUQNPdOvrroX67Un8A-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 0772983DD21; Sat, 29 Jan 2022 20:53:46 +0000 (UTC) Received: from llong.com (unknown [10.22.16.96]) by smtp.corp.redhat.com (Postfix) with ESMTP id 05BDA5DF2E; Sat, 29 Jan 2022 20:53:43 +0000 (UTC) From: Waiman Long To: Johannes Weiner , Michal Hocko , Vladimir Davydov , Andrew Morton , Petr Mladek , Steven Rostedt , Sergey Senozhatsky , Andy Shevchenko , Rasmus Villemoes Cc: linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, Ira Weiny , Rafael Aquini , Waiman Long Subject: [PATCH v2 1/3] lib/vsprintf: Avoid redundant work with 0 size Date: Sat, 29 Jan 2022 15:53:13 -0500 Message-Id: <20220129205315.478628-2-longman@redhat.com> In-Reply-To: <20220129205315.478628-1-longman@redhat.com> References: <20220129205315.478628-1-longman@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 5518940002 X-Stat-Signature: smz5xtn9qxgoge6g6yp9gja4h7oqpd9p Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=OTyzXJq5; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf04.hostedemail.com: domain of longman@redhat.com has no SPF policy when checking 170.10.129.124) smtp.mailfrom=longman@redhat.com X-Rspam-User: nil X-HE-Tag: 1643489631-502284 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: For *scnprintf(), vsnprintf() is always called even if the input size is 0. That is a waste of time, so just return 0 in this case. Signed-off-by: Waiman Long Reviewed-by: Sergey Senozhatsky Acked-by: Roman Gushchin --- lib/vsprintf.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 3b8129dd374c..a65df546fb06 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -2895,13 +2895,15 @@ int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) { int i; + if (!size) + return 0; + i = vsnprintf(buf, size, fmt, args); if (likely(i < size)) return i; - if (size != 0) - return size - 1; - return 0; + + return size - 1; } EXPORT_SYMBOL(vscnprintf); From patchwork Sat Jan 29 20:53:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Waiman Long X-Patchwork-Id: 12729639 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 43E45C433EF for ; Sat, 29 Jan 2022 20:53:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A42BA6B007E; Sat, 29 Jan 2022 15:53:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 97E3F6B0080; Sat, 29 Jan 2022 15:53:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 77DAB6B0081; Sat, 29 Jan 2022 15:53:55 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0098.hostedemail.com [216.40.44.98]) by kanga.kvack.org (Postfix) with ESMTP id 673416B0080 for ; Sat, 29 Jan 2022 15:53:55 -0500 (EST) Received: from smtpin21.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 26A31181C49B4 for ; Sat, 29 Jan 2022 20:53:55 +0000 (UTC) X-FDA: 79084526430.21.6C0A3CC Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf14.hostedemail.com (Postfix) with ESMTP id 7DD58100004 for ; Sat, 29 Jan 2022 20:53:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643489634; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=f1x6j7RB7xRENYfNa+FT/BlT6fNQP5UqbN6D/s+vZCU=; b=JXdYJsZ/9Snk3o2pGZVDa3w6gisUPYljGrTqYYYHC9/dfZVtVXcaZFLJL3+7IxGVha3toA YuVTgtSwJ+jAAYnf39CWh0kk9o6WTWs53ICr06DZsp8UO88oz3+8xtrP3IPp4JJVj5EYKO dCOPFIdbT68fetxhxeelAU7W+c+Tu6Y= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-220-y4KFQNNXPxewucIo98fB5w-1; Sat, 29 Jan 2022 15:53:49 -0500 X-MC-Unique: y4KFQNNXPxewucIo98fB5w-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id BF0101006AA0; Sat, 29 Jan 2022 20:53:47 +0000 (UTC) Received: from llong.com (unknown [10.22.16.96]) by smtp.corp.redhat.com (Postfix) with ESMTP id 286535DF3A; Sat, 29 Jan 2022 20:53:46 +0000 (UTC) From: Waiman Long To: Johannes Weiner , Michal Hocko , Vladimir Davydov , Andrew Morton , Petr Mladek , Steven Rostedt , Sergey Senozhatsky , Andy Shevchenko , Rasmus Villemoes Cc: linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, Ira Weiny , Rafael Aquini , Waiman Long Subject: [PATCH v2 2/3] mm/page_owner: Use scnprintf() to avoid excessive buffer overrun check Date: Sat, 29 Jan 2022 15:53:14 -0500 Message-Id: <20220129205315.478628-3-longman@redhat.com> In-Reply-To: <20220129205315.478628-1-longman@redhat.com> References: <20220129205315.478628-1-longman@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Stat-Signature: oxjnoawudcifpnp5ktaef5coahmtoajm X-Rspam-User: nil Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="JXdYJsZ/"; spf=none (imf14.hostedemail.com: domain of longman@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=longman@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 7DD58100004 X-HE-Tag: 1643489634-245154 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: The snprintf() function can return a length greater than the given input size. That will require a check for buffer overrun after each invocation of snprintf(). scnprintf(), on the other hand, will never return a greater length. By using scnprintf() in selected places, we can avoid some buffer overrun checks except after stack_depot_snprint() and after the last snprintf(). Signed-off-by: Waiman Long Reviewed-by: Sergey Senozhatsky --- mm/page_owner.c | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/mm/page_owner.c b/mm/page_owner.c index 99e360df9465..28dac73e0542 100644 --- a/mm/page_owner.c +++ b/mm/page_owner.c @@ -338,19 +338,16 @@ print_page_owner(char __user *buf, size_t count, unsigned long pfn, if (!kbuf) return -ENOMEM; - ret = snprintf(kbuf, count, + ret = scnprintf(kbuf, count, "Page allocated via order %u, mask %#x(%pGg), pid %d, ts %llu ns, free_ts %llu ns\n", page_owner->order, page_owner->gfp_mask, &page_owner->gfp_mask, page_owner->pid, page_owner->ts_nsec, page_owner->free_ts_nsec); - if (ret >= count) - goto err; - /* Print information relevant to grouping pages by mobility */ pageblock_mt = get_pageblock_migratetype(page); page_mt = gfp_migratetype(page_owner->gfp_mask); - ret += snprintf(kbuf + ret, count - ret, + ret += scnprintf(kbuf + ret, count - ret, "PFN %lu type %s Block %lu type %s Flags %pGp\n", pfn, migratetype_names[page_mt], @@ -358,19 +355,14 @@ print_page_owner(char __user *buf, size_t count, unsigned long pfn, migratetype_names[pageblock_mt], &page->flags); - if (ret >= count) - goto err; - ret += stack_depot_snprint(handle, kbuf + ret, count - ret, 0); if (ret >= count) goto err; if (page_owner->last_migrate_reason != -1) { - ret += snprintf(kbuf + ret, count - ret, + ret += scnprintf(kbuf + ret, count - ret, "Page has been migrated, last migrate reason: %s\n", migrate_reason_names[page_owner->last_migrate_reason]); - if (ret >= count) - goto err; } ret += snprintf(kbuf + ret, count - ret, "\n"); From patchwork Sat Jan 29 20:53:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Waiman Long X-Patchwork-Id: 12729638 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 10113C433EF for ; Sat, 29 Jan 2022 20:53:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4C0EE6B007D; Sat, 29 Jan 2022 15:53:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 449D46B007E; Sat, 29 Jan 2022 15:53:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 274896B0080; Sat, 29 Jan 2022 15:53:55 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0163.hostedemail.com [216.40.44.163]) by kanga.kvack.org (Postfix) with ESMTP id 168A06B007D for ; Sat, 29 Jan 2022 15:53:55 -0500 (EST) Received: from smtpin26.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id C2A8B181C49B4 for ; Sat, 29 Jan 2022 20:53:54 +0000 (UTC) X-FDA: 79084526388.26.D580A9C Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf29.hostedemail.com (Postfix) with ESMTP id 609BC120004 for ; Sat, 29 Jan 2022 20:53:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643489633; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=poPjjcFexMGp0K40SsFnRADO8VbhRo0jK19whIZnUJQ=; b=TrUj5oDwFDn3RY+GjDzLeGQEiAGwiiby4yiC7r21XVZfgxs7IrjD3V7DiHcl9Kkxa0oU4s g47STfl3MNxH86Znr/qXj3+aJgrD5CTPhKQP9WQJEC7zAc2CHWrFbVnhsxUrXlvRb9YO6M eZB8Xqp8usvOGaXLAmsFbcCTuGsWfDo= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-240-tfjAvJFGOhmoKcB5pFVqXA-1; Sat, 29 Jan 2022 15:53:51 -0500 X-MC-Unique: tfjAvJFGOhmoKcB5pFVqXA-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 40870343CA; Sat, 29 Jan 2022 20:53:49 +0000 (UTC) Received: from llong.com (unknown [10.22.16.96]) by smtp.corp.redhat.com (Postfix) with ESMTP id E0C605DF2E; Sat, 29 Jan 2022 20:53:47 +0000 (UTC) From: Waiman Long To: Johannes Weiner , Michal Hocko , Vladimir Davydov , Andrew Morton , Petr Mladek , Steven Rostedt , Sergey Senozhatsky , Andy Shevchenko , Rasmus Villemoes Cc: linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, Ira Weiny , Rafael Aquini , Waiman Long Subject: [PATCH v2 3/3] mm/page_owner: Dump memcg information Date: Sat, 29 Jan 2022 15:53:15 -0500 Message-Id: <20220129205315.478628-4-longman@redhat.com> In-Reply-To: <20220129205315.478628-1-longman@redhat.com> References: <20220129205315.478628-1-longman@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=TrUj5oDw; spf=none (imf29.hostedemail.com: domain of longman@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=longman@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspam-User: nil X-Rspamd-Queue-Id: 609BC120004 X-Stat-Signature: mjyintdr1bh5csetr63dygrd9wnb6zg4 X-Rspamd-Server: rspam12 X-HE-Tag: 1643489634-844437 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: It was found that a number of offlined memcgs were not freed because they were pinned by some charged pages that were present. Even "echo 1 > /proc/sys/vm/drop_caches" wasn't able to free those pages. These offlined but not freed memcgs tend to increase in number over time with the side effect that percpu memory consumption as shown in /proc/meminfo also increases over time. In order to find out more information about those pages that pin offlined memcgs, the page_owner feature is extended to dump memory cgroup information especially whether the cgroup is offlined or not. Signed-off-by: Waiman Long --- mm/page_owner.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/mm/page_owner.c b/mm/page_owner.c index 28dac73e0542..8dc5cd0fa227 100644 --- a/mm/page_owner.c +++ b/mm/page_owner.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include "internal.h" @@ -331,6 +332,7 @@ print_page_owner(char __user *buf, size_t count, unsigned long pfn, depot_stack_handle_t handle) { int ret, pageblock_mt, page_mt; + unsigned long __maybe_unused memcg_data; char *kbuf; count = min_t(size_t, count, PAGE_SIZE); @@ -365,6 +367,35 @@ print_page_owner(char __user *buf, size_t count, unsigned long pfn, migrate_reason_names[page_owner->last_migrate_reason]); } +#ifdef CONFIG_MEMCG + /* + * Look for memcg information and print it out + */ + memcg_data = READ_ONCE(page->memcg_data); + if (memcg_data) { + struct mem_cgroup *memcg = page_memcg_check(page); + bool onlined; + char name[80]; + + if (memcg_data & MEMCG_DATA_OBJCGS) + ret += scnprintf(kbuf + ret, count - ret, + "Slab cache page\n"); + + if (!memcg) + goto copy_out; + + onlined = (memcg->css.flags & CSS_ONLINE); + cgroup_name(memcg->css.cgroup, name, sizeof(name)); + ret += scnprintf(kbuf + ret, count - ret, + "Charged %sto %smemcg %s\n", + PageMemcgKmem(page) ? "(via objcg) " : "", + onlined ? "" : "offlined ", + name); + } + +copy_out: +#endif + ret += snprintf(kbuf + ret, count - ret, "\n"); if (ret >= count) goto err;