From patchwork Fri Mar 25 01:09:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Morton X-Patchwork-Id: 12791110 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 65673C4332F for ; Fri, 25 Mar 2022 01:09:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ED8B08D0007; Thu, 24 Mar 2022 21:09:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E870D6B0078; Thu, 24 Mar 2022 21:09:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D29668D0007; Thu, 24 Mar 2022 21:09:07 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0133.hostedemail.com [216.40.44.133]) by kanga.kvack.org (Postfix) with ESMTP id C22A16B0075 for ; Thu, 24 Mar 2022 21:09:07 -0400 (EDT) Received: from smtpin17.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 86C73181B0495 for ; Fri, 25 Mar 2022 01:09:07 +0000 (UTC) X-FDA: 79281124734.17.4890F3D Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf16.hostedemail.com (Postfix) with ESMTP id 0F37E18002D for ; Fri, 25 Mar 2022 01:09:06 +0000 (UTC) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 61781617ED; Fri, 25 Mar 2022 01:09:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BC650C340EC; Fri, 25 Mar 2022 01:09:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1648170545; bh=6LWfCzLrPgNVafpJLonBr1p+EEPnw+GvRm+9q6c9V/c=; h=Date:To:From:In-Reply-To:Subject:From; b=R/BOpdSd5tlqvXyuyDEibdgyYCHX4F6GVffNvsgj0PsTnSo24PkfiB72dyOqqHhjK ywUu8kpQnJvXcFlDQRBI9sXg9jkavds9c7Djw+vS5eJ1RBZP6bOd7RAaWxFMpwb+NC Ze3jVJULRCFFCpv2y3SKrWaUTpqoHpYqKFoUG9Ik= Date: Thu, 24 Mar 2022 18:09:05 -0700 To: vdavydov.dev@gmail.com,senozhatsky@chromium.org,rppt@linux.ibm.com,rostedt@goodmis.org,roman.gushchin@linux.dev,rientjes@google.com,pmladek@suse.com,mhocko@kernel.org,linux@rasmusvillemoes.dk,ira.weiny@intel.com,hannes@cmpxchg.org,aquini@redhat.com,andriy.shevchenko@linux.intel.com,longman@redhat.com,akpm@linux-foundation.org,patches@lists.linux.dev,linux-mm@kvack.org,mm-commits@vger.kernel.org,torvalds@linux-foundation.org,akpm@linux-foundation.org From: Andrew Morton In-Reply-To: <20220324180758.96b1ac7e17675d6bc474485e@linux-foundation.org> Subject: [patch 010/114] mm/page_owner: use scnprintf() to avoid excessive buffer overrun check Message-Id: <20220325010905.BC650C340EC@smtp.kernel.org> X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 0F37E18002D X-Rspam-User: Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b="R/BOpdSd"; dmarc=none; spf=pass (imf16.hostedemail.com: domain of akpm@linux-foundation.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org X-Stat-Signature: fksywumr57bw5bptxr1a7whsidw7uj7x X-HE-Tag: 1648170546-545434 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: From: Waiman Long Subject: mm/page_owner: use scnprintf() to avoid excessive buffer overrun check 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(). Link: https://lkml.kernel.org/r/20220202203036.744010-3-longman@redhat.com Signed-off-by: Waiman Long Acked-by: David Rientjes Reviewed-by: Sergey Senozhatsky Acked-by: Rafael Aquini Acked-by: Mike Rapoport Cc: Andy Shevchenko Cc: Ira Weiny Cc: Johannes Weiner Cc: Michal Hocko Cc: Petr Mladek Cc: Rasmus Villemoes Cc: Roman Gushchin Cc: Steven Rostedt (Google) Cc: Vladimir Davydov Signed-off-by: Andrew Morton --- mm/page_owner.c | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) --- a/mm/page_owner.c~mm-page_owner-use-scnprintf-to-avoid-excessive-buffer-overrun-check +++ a/mm/page_owner.c @@ -338,19 +338,16 @@ print_page_owner(char __user *buf, size_ 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_ 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");