Received: by 2002:ab2:710b:0:b0:1ef:a325:1205 with SMTP id z11csp1802018lql; Wed, 13 Mar 2024 08:31:57 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCU4PLRzBaqfZSMhuESfPrVEKrawJyi0C2+f+eLPq7sv+RuS/hR6NNNWqwJuYtRrh9AkIs3N3HphL50sf4ZRUv0ahZtYsIEZGIGLx6IjSw== X-Google-Smtp-Source: AGHT+IGtVnSU6QaaA9yqbrZ4Ybp1CsQBxh8Qx6Jt22gZ25devyJVOk+TMyD5V5/XKCLB7kP9PtTh X-Received: by 2002:a05:6a00:4fcb:b0:6e6:7af6:2201 with SMTP id le11-20020a056a004fcb00b006e67af62201mr2676157pfb.8.1710343916820; Wed, 13 Mar 2024 08:31:56 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710343916; cv=pass; d=google.com; s=arc-20160816; b=oYN1PznyTZGLs0HRON/6X6Ey9P4lkFVriEJ6t0LYSIQlqUC2H0/JoTe9FTG0Rx26nU l3mEl2c5YfHmX0t1MrEJz9LlVFNC31NW+M23gfxwmRDFjiZBwvKCNwKcbzhvrd6VUIHx 8YtGmkC+0GEMhIPy3NO6EB5iCYmX7XdMrSNvh3FxEeDdx1tDUKhcmW3xR79J2CiiRS9U ZfFH5IWwUY5xdqfmATY8VntgoM9yAY8OJq+2hThUy32kq9drFSYK4FiLqoII7+J5FxYI OI6xhH7NodvWNQa6MwxRedoUqNJv3CJhxb0pWa0uFIJ9yAFVYSkY70LbxX8b+AA7rINm cW6w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=JtMw74U66kmsHyXAV7bBZ7SbbkNSfFBApfqkzQBmCrI=; fh=bG7ICAqBl3SqmAtEZF2WP6xXjXbSoUSsiPHymQUqv00=; b=hUT9yprYR/LUUb917h2mlu0WdMCUF4BPOf+UCcu8NEheb7BBxfkG6yahj24o1OdfsJ kNMDPhfcoBuiuMl52/16nWyuprjLaW0qtqEG5ju0LsDD7mBYd+//KY5r4OKzlx+Jtg5T uFzl6fKwfHADc7P4zobymUDcpnmh7wX5p2TaNYWuzijt3Hkez7mT3vgv/qSDUDlh84+v vItdNgPQF91Cwm6270YHArcD2bRPgwIH24gGhG+U8az4wDD1WmeOVHqqdpGjm11r1mJ5 8mxKJEmlMkYj6A1N+HbaP1CnXI+bLD/KD/gceCE6UrjGOJ8SS4sS1hc6N4g8/e7frgcS lwWQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=nHy1XTA7; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-101681-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-101681-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id jo35-20020a056a0090a300b006e6b2b2b0f8si2463470pfb.354.2024.03.13.08.31.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Mar 2024 08:31:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-101681-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=nHy1XTA7; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-101681-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-101681-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 7B7F7284FA2 for ; Wed, 13 Mar 2024 15:31:56 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8EC2E47779; Wed, 13 Mar 2024 15:31:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="nHy1XTA7" Received: from mail-yb1-f173.google.com (mail-yb1-f173.google.com [209.85.219.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7FE4947F59 for ; Wed, 13 Mar 2024 15:31:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.173 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710343895; cv=none; b=nPSOSOL1U62Vxvx8zVoNMAzlcH+xnXfcnk2b09P+3Zpv9Z6VLTPEEmdWIvPF8Ipf97uoaOJAO64+z6eAWeqQAeapS59nXYW170JsbCUTd8juSrusjNMmhFFVTJFw2QRy2SZKYkAFIWGTr9kInBSPIJvnVsF/GH19xzIB1L6Lyz0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710343895; c=relaxed/simple; bh=Zq4RfZOjcqNtcLFUzAg0QuQxZvQrvC1IKPOXZvtCEr8=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=CdGlPWUiRIyMbQ5T8Rpkt+m0BlgvuvmVauLOtr658Wmb4rdC6GhJd/QPhMqP3Rn43Ojez1fkJHrpMtTMKepo//p6OLjLyl6TwU20cBSM9xQSMZ2CmF648w7joaDBOwkx/m1Nb6U7CSuOz1cqTwBf6OBtKAr6QP4aAw/KXKwYLj4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=nHy1XTA7; arc=none smtp.client-ip=209.85.219.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-yb1-f173.google.com with SMTP id 3f1490d57ef6-dcc7cdb3a98so1180180276.2 for ; Wed, 13 Mar 2024 08:31:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1710343891; x=1710948691; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=JtMw74U66kmsHyXAV7bBZ7SbbkNSfFBApfqkzQBmCrI=; b=nHy1XTA7EB89TkqAKPt3CSMUwffHkk/CAuX5IYEt3/EzeN/XRhGq2DKnf7K6hxBuX2 lizjW6ABzbMxvs9EVRQPfIEdfnzXbICsxanCJ313DaPFFsn3XgjJXutzC0m8X+uGp4Z5 Om3PINc9nBM2T9TOBrh3Fdfv+4E3X3TozhN1tc8/oU8w+/Zy+NQckLE5J7e2sPXxNlyr Yg6YbyXesZbqI/nCi6EKIOgO914R0yfPTAzB91Id3Q95jZ4U6SQbdb1pjfCT0GK05Dcl tPLny3Iv8ZJxV34iyTvblGo8lJ5DISojziQxMAi9f+np2LqJCj/YFpKveTEQzzcE0BjG 1ivg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710343891; x=1710948691; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=JtMw74U66kmsHyXAV7bBZ7SbbkNSfFBApfqkzQBmCrI=; b=dgp7tvVNRK1Eiyx9M0GfioCEUt2VUoBpo4vK/lf2yPE0x9x8Xszy/5IRhVHrIYWbzy iCpLWtDEj60UUuRCRLR1PwSZHRjxTWXjnw8MJwSNiLbeZjdn2HpRwr7pmqxFNObFdM8H i7Nre8Q1BZMJTccJ2X+XqQDmkayIiUfG1PLxKc/VyRoDFIT+sHaV2t4z5JDpKuE7cxOf ZTjPWB8FEhnZPUo39uZumXttgSYdLQHrMOZ+fFgUzll5kvAqwIHbuhhWg+ZMDTk3EPp5 v8yMVaE8e+96ZSVnVS0Xk+GrvRQRJ/IeA507NGRg9VAs0JPSTB7khzH+affu8rzcNOvS BLPQ== X-Forwarded-Encrypted: i=1; AJvYcCUuqRAXhGDJdfisPaJ71YuKqRt/LCyn34t6TBer9uNoPltumustUVFfqwzoVy4A3PPzkVXraLAXCB0ippMNiXlCKCtsuCKwz9Un0GLw X-Gm-Message-State: AOJu0Yzfcr05jRseovlq1nRVj0rfkhb5k/wO6FTUx4ZAUOz+EUdOCOQk MSYHZd3xcHvtSLI02XEdPI0HJ3Tcu9bCWPkRJo7i2wiUBhyL3R4rXJwDrI4+bR37AWkVJ7ywbBV 9Ehj4lQVITHyiWApxOXmioEqOV5Vo8DRD5n1W X-Received: by 2002:a5b:706:0:b0:dcb:abbc:f597 with SMTP id g6-20020a5b0706000000b00dcbabbcf597mr2798485ybq.54.1710343891190; Wed, 13 Mar 2024 08:31:31 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240306182440.2003814-1-surenb@google.com> <20240306182440.2003814-21-surenb@google.com> In-Reply-To: From: Suren Baghdasaryan Date: Wed, 13 Mar 2024 15:31:18 +0000 Message-ID: Subject: Re: [PATCH v5 20/37] mm: fix non-compound multi-order memory accounting in __free_pages To: Matthew Wilcox Cc: akpm@linux-foundation.org, kent.overstreet@linux.dev, mhocko@suse.com, vbabka@suse.cz, hannes@cmpxchg.org, roman.gushchin@linux.dev, mgorman@suse.de, dave@stgolabs.net, 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, 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Mar 13, 2024 at 3:04=E2=80=AFPM Matthew Wilcox wrote: > > On Wed, Mar 06, 2024 at 10:24:18AM -0800, Suren Baghdasaryan wrote: > > 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 free= d, > > treating it as a 0-order page. This creates a memory accounting imbalan= ce > > because the pages freed in __free_pages() do not have their own alloc_t= ag > > 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 fr= eed. > > It's not "ignored". It's not available! > > Better wording: > > However the page passed to put_page() is indisinguishable from an > order-0 page, so it cannot do the accounting, just as it cannot free > the subsequent pages. Do the accounting here, where we free the pages. > > (I'm sure further improvements are possible) > > > +static inline void pgalloc_tag_sub_bytes(struct alloc_tag *tag, unsign= ed int order) > > +{ > > + if (mem_alloc_profiling_enabled() && tag) > > + this_cpu_sub(tag->counters->bytes, PAGE_SIZE << order); > > +} > > This is a terribly named function. And it's not even good for what we > want to use it for. > > static inline void pgalloc_tag_sub_pages(struct alloc_tag *tag, unsigned = int nr) > { > if (mem_alloc_profiling_enabled() && tag) > this_cpu_sub(tag->counters->bytes, PAGE_SIZE * nr); > } > > > +++ b/mm/page_alloc.c > > @@ -4697,12 +4697,21 @@ void __free_pages(struct page *page, unsigned i= nt order) > > { > > /* get PageHead before we drop reference */ > > int head =3D PageHead(page); > > + struct alloc_tag *tag =3D 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 all= ocations > > + * to the first page (just like compound one), th= erefore > > + * we need to adjust the allocation size of the f= irst > > + * page as its order is ignored when put_page() f= rees it. > > + */ > > + pgalloc_tag_sub_bytes(tag, order); > > - else if (!head > + else if (!head) { > + pgalloc_tag_sub_pages(1 << order - 1); > while (order-- > 0) > free_the_page(page + (1 << order), order); > + } > > It doesn't need a comment, it's obvious what you're doing. All suggestions seem fine to me. I'll adjust the next version accordingly. Thanks for reviewing and the feedback! >