From nobody Mon Jun 29 23:57:34 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C213EC433F5 for ; Sat, 29 Jan 2022 20:53:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353170AbiA2Ux4 (ORCPT ); Sat, 29 Jan 2022 15:53:56 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:31921 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353168AbiA2Uxy (ORCPT ); Sat, 29 Jan 2022 15:53:54 -0500 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=nX58UmfyCZKrrdzehrq1UxMQowz9BiWVJRz/V9rRqyY=; b=C2ZndTDzUlpErAV7xSAIPMVSylVdoiGjoa/eaPsZ1wGf6F7Em6blE5/QBbuK1+cPQjD+oG Ta98N12Xd7IWj5q/PsEAHoXASg+4uRpiSnChsqe0ZnPeqnXUpci/ncjw3WzDcIsDG0EFoy GaiNZNsZjoDjI6bNl5ENIWZZhRmklzg= 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 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 Acked-by: Roman Gushchin Reviewed-by: Sergey Senozhatsky --- 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; =20 + if (!size) + return 0; + i =3D vsnprintf(buf, size, fmt, args); =20 if (likely(i < size)) return i; - if (size !=3D 0) - return size - 1; - return 0; + + return size - 1; } EXPORT_SYMBOL(vscnprintf); =20 --=20 2.27.0 From nobody Mon Jun 29 23:57:34 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1440FC433F5 for ; Sat, 29 Jan 2022 20:54:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353205AbiA2UyA (ORCPT ); Sat, 29 Jan 2022 15:54:00 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:49215 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353175AbiA2Uxz (ORCPT ); Sat, 29 Jan 2022 15:53:55 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643489635; 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=D8ve9czQ5s39VjA26+8tmFbJG/nE2o4ZUAh5SOloNCZSmd+nDiIu2KXdRMkxXitnIrPizZ 4iw6y9vgZWnA2OtkHo3yg30en0UXqSFn6whrO2LxFq7qQkp3SxFP+pMBYVkzw+NwwoweYN nL7/fjN0V5HLsQWzfXVoGW2Hr9a4lSo= 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 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 --- 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, unsi= gned long pfn, if (!kbuf) return -ENOMEM; =20 - ret =3D snprintf(kbuf, count, + ret =3D 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); =20 - if (ret >=3D count) - goto err; - /* Print information relevant to grouping pages by mobility */ pageblock_mt =3D get_pageblock_migratetype(page); page_mt =3D gfp_migratetype(page_owner->gfp_mask); - ret +=3D snprintf(kbuf + ret, count - ret, + ret +=3D 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, unsi= gned long pfn, migratetype_names[pageblock_mt], &page->flags); =20 - if (ret >=3D count) - goto err; - ret +=3D stack_depot_snprint(handle, kbuf + ret, count - ret, 0); if (ret >=3D count) goto err; =20 if (page_owner->last_migrate_reason !=3D -1) { - ret +=3D snprintf(kbuf + ret, count - ret, + ret +=3D scnprintf(kbuf + ret, count - ret, "Page has been migrated, last migrate reason: %s\n", migrate_reason_names[page_owner->last_migrate_reason]); - if (ret >=3D count) - goto err; } =20 ret +=3D snprintf(kbuf + ret, count - ret, "\n"); --=20 2.27.0 From nobody Mon Jun 29 23:57:34 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2B916C433FE for ; Sat, 29 Jan 2022 20:54:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353196AbiA2Ux6 (ORCPT ); Sat, 29 Jan 2022 15:53:58 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:47428 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353171AbiA2Uxz (ORCPT ); Sat, 29 Jan 2022 15:53:55 -0500 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=poPjjcFexMGp0K40SsFnRADO8VbhRo0jK19whIZnUJQ=; b=Zy/Z3ihjhNeS8iwaQHCQWt7LNXmR5CSO9Oigf0fs5yz5AA0AH8/HVoJZWrbXlCErFc7hO/ A7PxOmpGpL4YDJKY4wqIAPOISXovKhZcQaLtGlYGmav55JLS6p1+bOtgYoL3p5SEIywVo0 vslmjBbWCKBOoL0eQKdjzGElckzyulg= 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 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 =20 #include "internal.h" @@ -331,6 +332,7 @@ print_page_owner(char __user *buf, size_t count, unsign= ed long pfn, depot_stack_handle_t handle) { int ret, pageblock_mt, page_mt; + unsigned long __maybe_unused memcg_data; char *kbuf; =20 count =3D min_t(size_t, count, PAGE_SIZE); @@ -365,6 +367,35 @@ print_page_owner(char __user *buf, size_t count, unsig= ned long pfn, migrate_reason_names[page_owner->last_migrate_reason]); } =20 +#ifdef CONFIG_MEMCG + /* + * Look for memcg information and print it out + */ + memcg_data =3D READ_ONCE(page->memcg_data); + if (memcg_data) { + struct mem_cgroup *memcg =3D page_memcg_check(page); + bool onlined; + char name[80]; + + if (memcg_data & MEMCG_DATA_OBJCGS) + ret +=3D scnprintf(kbuf + ret, count - ret, + "Slab cache page\n"); + + if (!memcg) + goto copy_out; + + onlined =3D (memcg->css.flags & CSS_ONLINE); + cgroup_name(memcg->css.cgroup, name, sizeof(name)); + ret +=3D scnprintf(kbuf + ret, count - ret, + "Charged %sto %smemcg %s\n", + PageMemcgKmem(page) ? "(via objcg) " : "", + onlined ? "" : "offlined ", + name); + } + +copy_out: +#endif + ret +=3D snprintf(kbuf + ret, count - ret, "\n"); if (ret >=3D count) goto err; --=20 2.27.0