From patchwork Wed Mar 6 18:24:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13584430 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 BC6B5C5475B for ; Wed, 6 Mar 2024 18:25:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 162DF6B00AB; Wed, 6 Mar 2024 13:25:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0C2F96B00AC; Wed, 6 Mar 2024 13:25:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E0B1F6B00AD; Wed, 6 Mar 2024 13:25:30 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id C7CF76B00AB for ; Wed, 6 Mar 2024 13:25:30 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id A6295160A25 for ; Wed, 6 Mar 2024 18:25:30 +0000 (UTC) X-FDA: 81867442020.04.1AEB0F7 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf30.hostedemail.com (Postfix) with ESMTP id EB3E48001E for ; Wed, 6 Mar 2024 18:25:28 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=qtuYRcdA; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf30.hostedemail.com: domain of 3F7XoZQYKCFoKMJ6F38GG8D6.4GEDAFMP-EECN24C.GJ8@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3F7XoZQYKCFoKMJ6F38GG8D6.4GEDAFMP-EECN24C.GJ8@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1709749529; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=EvAS1Tk9rG7Nm0pd+ehjWtNqiQusCGfZiqvBjL56bfE=; b=ypz553gZIy2HsnCPF3uHiwOB6nE5t54EflLPTnPhJutnwxNQO5aWWiIHBFmRjnac5xT9QM sns1g8GugsmijnC/gbKEjcysWzSdIrBL8fjMlegwanVy0NjgAm4KPKnlXOm3ezXwG9XcUn R8eNPNo/sSUzwedTCro4bnyM0p9/2qc= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=qtuYRcdA; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf30.hostedemail.com: domain of 3F7XoZQYKCFoKMJ6F38GG8D6.4GEDAFMP-EECN24C.GJ8@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3F7XoZQYKCFoKMJ6F38GG8D6.4GEDAFMP-EECN24C.GJ8@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1709749529; a=rsa-sha256; cv=none; b=r01+EGApfPPbdYgL+B/fRAGb/ZpI4O2WqN+F7rJ96X4VVC0eBgH2EuJ4+sBYPBUFC0Tnpm sMqoy10WoLtKGw/uR65JeVoKUat3QADanorjQgbl602nXkf/WgCNGq/zoqJDk/Ev5AYiSm /i/B5JcQQnKviNfj78hzTPZcinMgsfM= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-609db871b90so316697b3.1 for ; Wed, 06 Mar 2024 10:25:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709749528; x=1710354328; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=EvAS1Tk9rG7Nm0pd+ehjWtNqiQusCGfZiqvBjL56bfE=; b=qtuYRcdA7jqpGk8UEpdcybZVQQB0+mVwLbMGWvh+9dmsrmOkpODSd8kpVTBAY8A1sC 5MHD5Ss2aZMXWain6LsBei7G27b9ELavncvpNBqmIlyXe5b3CMQsO/b4B7qzP2QGlO4A EPDOTo1R6n/PJ9iwAa2OVjC5WrAenRV1S3WbKKnJvBhPJ65Vk8xTa/Imk1byKafzbJ9o GTTzW4UpN11MFXFby1e5qHFvrPkb5fOIcnjq0D1FNYUZW3BhcClC3o8S83IlC0wecnui mJmFumIKXz2uLf7omjbDCJOQ2iTUMF3sBbA7JAP6/wH0B6A5AJ3PT+D5uOR9ShnSm8Uk fTQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709749528; x=1710354328; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=EvAS1Tk9rG7Nm0pd+ehjWtNqiQusCGfZiqvBjL56bfE=; b=dqNDt5ypsMoYoqYW5KDrBMtDqspwt+3VwNxQJRi5ES2L+UPGwEO/HHwEa4d2m4Gh6Z eCfoVEtRwJxNMv02A4bbSghwJ0rjhGLlCcX0cfaBW94rzBc6ecM+cyreEB/r47+xae1i py4Q5qva7mfCuUCxZQQTf6Rfm5OGF0TRMYdxPs0Felp9IK8qv0W2Bd4zwr39Yt3ZG9Xa lgwLYDzzD/Wt72VA8oub8mbOJVknlkHwJmO06J8BLKSU+eBMd6h4s2EcMYK0l/OlaL3U KAvcCSlDu4XgDfu5Q3Ah53Wy2v1aFyXemVBj6slv4rIpA/7uhJIGotfBP0FBQZPrCqru 1fjw== X-Forwarded-Encrypted: i=1; AJvYcCX2nJM1gd6NCVCH3l5dv1w4jEslUjn1yym3vY/7wbGitP+ONOJVN1GVDP/BC66rAp43SyBIdwM/Jl033tGWzboV3fI= X-Gm-Message-State: AOJu0Ywuz3/fMhgSAW3sOP3m2cftpWuGRwJNpCihbWwVhCOo1v/owdFq p1NjXw+rjLEjln8hWvBsJX52G1i7YWnaZehldokhR6dxzeLFIbeyIC9FVzafs22+aCsSubTDIbD +xA== X-Google-Smtp-Source: AGHT+IFQIptCkWW78zp6nNtKkGuc5SIQRmDLhLOqGJHxbi7rcmsMoDf48+EiK0QB4NL+su2PCoHiqX08ucE= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:85f0:e3db:db05:85e2]) (user=surenb job=sendgmr) by 2002:a05:690c:fcd:b0:609:33af:cca8 with SMTP id dg13-20020a05690c0fcd00b0060933afcca8mr4422200ywb.2.1709749527811; Wed, 06 Mar 2024 10:25:27 -0800 (PST) Date: Wed, 6 Mar 2024 10:24:18 -0800 In-Reply-To: <20240306182440.2003814-1-surenb@google.com> Mime-Version: 1.0 References: <20240306182440.2003814-1-surenb@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Message-ID: <20240306182440.2003814-21-surenb@google.com> Subject: [PATCH v5 20/37] mm: fix non-compound multi-order memory accounting in __free_pages From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: kent.overstreet@linux.dev, mhocko@suse.com, vbabka@suse.cz, hannes@cmpxchg.org, roman.gushchin@linux.dev, mgorman@suse.de, dave@stgolabs.net, willy@infradead.org, liam.howlett@oracle.com, penguin-kernel@i-love.sakura.ne.jp, corbet@lwn.net, void@manifault.com, peterz@infradead.org, juri.lelli@redhat.com, catalin.marinas@arm.com, will@kernel.org, arnd@arndb.de, tglx@linutronix.de, mingo@redhat.com, dave.hansen@linux.intel.com, x86@kernel.org, peterx@redhat.com, david@redhat.com, axboe@kernel.dk, mcgrof@kernel.org, masahiroy@kernel.org, nathan@kernel.org, dennis@kernel.org, jhubbard@nvidia.com, tj@kernel.org, muchun.song@linux.dev, rppt@kernel.org, paulmck@kernel.org, pasha.tatashin@soleen.com, yosryahmed@google.com, yuzhao@google.com, dhowells@redhat.com, hughd@google.com, andreyknvl@gmail.com, keescook@chromium.org, ndesaulniers@google.com, vvvvvv@google.com, gregkh@linuxfoundation.org, ebiggers@google.com, ytcoode@gmail.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, bristot@redhat.com, vschneid@redhat.com, cl@linux.com, penberg@kernel.org, iamjoonsoo.kim@lge.com, 42.hyeyoo@gmail.com, glider@google.com, elver@google.com, dvyukov@google.com, shakeelb@google.com, songmuchun@bytedance.com, jbaron@akamai.com, aliceryhl@google.com, rientjes@google.com, minchan@google.com, kaleshsingh@google.com, surenb@google.com, kernel-team@android.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, iommu@lists.linux.dev, linux-arch@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, kasan-dev@googlegroups.com, cgroups@vger.kernel.org X-Rspamd-Queue-Id: EB3E48001E X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: oc4d8fue4yh8kdkm64bzxdx9ouqnzrmt X-HE-Tag: 1709749528-980489 X-HE-Meta: U2FsdGVkX1/UuPniS8rW5x6B0N1b8yD/arzoDTKvc+HmClZ2bww3QSl5hzayW8nQCtqsx6aD6ErpULoD7CoQeQMRM6B+jOri+Pds8cHlZfYxxAox702levOnD7YtZDfAvbdwlphXaP+IFkMZzEAsZezEG4+LFU7i+tVTu5eboBZwHv4gDJZGVBX2FgShiGhP2zhlHpEvouJFIRVxqeWYzgsCjqHJepQ16r87yDBKbyWMR/InIDj6Dv3924mZ1FkEx0q1wLLi1RVgAEzM0bEh1BSHef/dg35vpnl2G6ufcwYuOGGkMtUI6AzIIcmfwxs0fLJ9S9is+h4n7Gy2pXFJFAb9JU2eZHuHfsjeeXuRq43cHSRlqM9iAFeAShykrLTG+S+2yFg3QWmNxPhiGhsf5SzdSuqeQWaBTP9si8wOX7mOjgTUcuR5UvkXhZ5YhiPuADiAI1othfRRvANbO59bGO3MtNZmO59QFlnOYd3HW6lha0/GoxvvTGw0gcad4oAPKYtzcV66LRWED9yTkTXeVpPUL/CmQG6GJYUxb46tBxWbjsg1SS56Ykl/b5g9wR0k5CzhsiFwgHTvc09rasPQePpDVsgGUCh+gEvuag4WzP5peoil1KcRyVjGFvQZjxzdRfJ3D+frubtuGlW3ai/gimPnE+5YDUg1TC7YOx5jZntlZN38ExXOwQdSoPdXwMbcFZyfHUJdBQnIAWucMr3+fNoutOixA4mKtTvIajXlTWg5laytc+7ySRvIBSnzrhZaJNg+eEH47NQRk7RRdd7VEWrItbuKFoDg+Xs0rwSB29ylg81yzsrKR17xFVo/jp5X+vGos1OmgvrSlPntV7sW/bPb0qf59wtEV1qDq2nR3DGBL4az+CT+VcbAew8b1Pb309aDvMACyWxSXWTKDciyN+SII2sOSLO06DDOHYDVtEz6Qm0edqKCRLovdlI5dS4SKW5jVDBQjuH494DnEiG Rp1BaIFP pPPwNMQ280zWUtS9qOa4BX60qaEPcxGOmD0vnbYVgfTb5mAXyh9vPu51pHcYYKVPkRmkievh69UVW1O78G112ageqCozFz0h/8+Uk7MHZz3gAZ2cNs9tnI/iaKRJ/HHwpnHBAXsoPhxae8co9WBwslczF4v9GZheLh0SAOU7edjj/H1bhsONOot5VUgsmoSG8gU43ysCuFgQrOvzFk+MTvOBWzncYWVdACX2Xrf3JxWpHqABOwdWjQnt2Vouj7dwOvQPX8G6tLDM6u1p+bDX3+1s5wKHREuBTzHnI2tPB9U8PJkR6yEnflB9ggKPvVWxlElzAy/P5IlGw628BRzt5Vyvux/WFE7923aWuv0FhPR3gJqfICsR0e231k+AAX9lSe1PtUmo4apyYDiLyTWUNm36I+iRvtsaGYPn8GsmY1QJeDjnLBaqOVSUL8QJDhR1Zxb5oLWZ/xjqO0OqxX2qbj2268w== 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: List-Subscribe: List-Unsubscribe: When a non-compound multi-order page is freed, it is possible that a speculative reference keeps the page pinned. In this case we free all pages except for the first page, which will be freed later by the last put_page(). However put_page() ignores the order of the page being freed, treating it as a 0-order page. This creates a memory accounting imbalance because the pages freed in __free_pages() do not have their own alloc_tag and their memory was accounted to the first page. To fix this the first page should adjust its allocation size counter when "tail" pages are freed. Reported-by: Vlastimil Babka Signed-off-by: Suren Baghdasaryan Reviewed-by: Vlastimil Babka --- include/linux/pgalloc_tag.h | 24 ++++++++++++++++++++++++ mm/page_alloc.c | 11 ++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/include/linux/pgalloc_tag.h b/include/linux/pgalloc_tag.h index 9e6ad8e0e4aa..59de43172cc2 100644 --- a/include/linux/pgalloc_tag.h +++ b/include/linux/pgalloc_tag.h @@ -96,12 +96,36 @@ static inline void pgalloc_tag_split(struct page *page, unsigned int nr) page_ext_put(page_ext); } +static inline struct alloc_tag *pgalloc_tag_get(struct page *page) +{ + struct alloc_tag *tag = NULL; + + if (mem_alloc_profiling_enabled()) { + union codetag_ref *ref = get_page_tag_ref(page); + + alloc_tag_sub_check(ref); + if (ref && ref->ct) + tag = ct_to_alloc_tag(ref->ct); + put_page_tag_ref(ref); + } + + return tag; +} + +static inline void pgalloc_tag_sub_bytes(struct alloc_tag *tag, unsigned int order) +{ + if (mem_alloc_profiling_enabled() && tag) + this_cpu_sub(tag->counters->bytes, PAGE_SIZE << order); +} + #else /* CONFIG_MEM_ALLOC_PROFILING */ static inline void pgalloc_tag_add(struct page *page, struct task_struct *task, unsigned int order) {} static inline void pgalloc_tag_sub(struct page *page, unsigned int order) {} static inline void pgalloc_tag_split(struct page *page, unsigned int nr) {} +static inline struct alloc_tag *pgalloc_tag_get(struct page *page) { return NULL; } +static inline void pgalloc_tag_sub_bytes(struct alloc_tag *tag, unsigned int order) {} #endif /* CONFIG_MEM_ALLOC_PROFILING */ diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 39dc4dcf14f5..b402149a795f 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -4697,12 +4697,21 @@ void __free_pages(struct page *page, unsigned int order) { /* get PageHead before we drop reference */ int head = PageHead(page); + struct alloc_tag *tag = pgalloc_tag_get(page); if (put_page_testzero(page)) free_the_page(page, order); else if (!head) - while (order-- > 0) + while (order-- > 0) { free_the_page(page + (1 << order), order); + /* + * non-compound multi-order page accounts all allocations + * to the first page (just like compound one), therefore + * we need to adjust the allocation size of the first + * page as its order is ignored when put_page() frees it. + */ + pgalloc_tag_sub_bytes(tag, order); + } } EXPORT_SYMBOL(__free_pages);