Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp620064lqp; Thu, 21 Mar 2024 10:23:15 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUlnDGBIl00Hx0TjWRNPgDbtoCSmcSTBd1LhoNoheEvB3yUUbrYhhI9uOPanD2qQScFaM6xFaka4TTJtmVEJ/Gkl5xr3ru+zs+jWRSiwQ== X-Google-Smtp-Source: AGHT+IGlwc1Cp6H87RBWUzj2MZoNH5N9w8Zcju2fJQHSOiZrtToVPc2vxb3OgtUkhx2ojhyUA6xx X-Received: by 2002:ae9:e914:0:b0:789:f2a6:4ba9 with SMTP id x20-20020ae9e914000000b00789f2a64ba9mr17002761qkf.51.1711041794736; Thu, 21 Mar 2024 10:23:14 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711041794; cv=pass; d=google.com; s=arc-20160816; b=lbuD8336ElXoaHtsSgsX//nqmKZ+mKxbkOrdchyXJw6SdLl3b7BVIrssyZsyBAt2/P jcBVs4C/GgJjxmfZZO1ZgV8WpAeQR7QJR6hJFkBaoivHF2xvqKTbmOrEoUlTVZ/NfR0O 7pyNNZVx7vaaVlq4s980ZVU7potvZ+k33WYSun1f+pWzbSEl+7vqJoFY02mu5uD7ZjL5 q4qySPWhumWwNKnGeVfg93CedpwAjwgK7uEuOOHHPl2k4+XMCos8nT24Ga9bTLQaxFS5 taaJe1gJY+aD/vRn+VX3L7SrRgTpwJqOoJi+bas2VFSSm0CHq6oq2SOK1CZQDkheol0f AW7A== 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=w4sCto2weX/tSraSWS0s5CjzL8cyi2xaSDvgaVtTzPM=; fh=tsAveS3ngdC5irwlp3bkaSdhv763V6j9++jF+G0SIK4=; b=GmEJKYx0wzUmTbCeJIEveLGYd4FM2jKyvTqKgSPGauPMqZoRetMRAEh747a0XUeaoh XePDtt1uYwX9D1/txrsjEe83CjxCa+iVyuO/CcV/2Nqm1GQWGvmB+07rpBJMQrgi849B 1UyONmPDab2nP02eoQs7J7q2QJG3eyYSNQTLtl8PC63HPi/D+vgAoqa8VOUw4AeeW+T7 pm26bVgbG8DbWm3iK4e24dJzCRLKXNauZyInvr1fC6C87D9zbDrl3HlvFJUgUJv58Y8A FlQK92mRzImFzxRcbbwS5bmqZmT4B5hZY7+RpqG2tIT35Z6nFmEyGn4CzTRwguymA++D 7ICg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="Wp4O/Vn1"; 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-110489-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-110489-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id s22-20020ae9f716000000b00789e98dc91fsi166653qkg.187.2024.03.21.10.23.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Mar 2024 10:23:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-110489-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="Wp4O/Vn1"; 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-110489-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-110489-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 673FF1C21C7C for ; Thu, 21 Mar 2024 17:23:14 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D963856768; Thu, 21 Mar 2024 17:23:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Wp4O/Vn1" Received: from mail-yb1-f172.google.com (mail-yb1-f172.google.com [209.85.219.172]) (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 0CE3B16410 for ; Thu, 21 Mar 2024 17:22:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711041780; cv=none; b=R/ct0St9+XNP7ZivWvZdf65SXP1+5NEflf45cCfdfwIlbhtempbTqoEIaM26fyvOi+4stF5xh3yfIimE9l2+KJ99OxPoKlNxQJHKRFBIbQbsTHCZDtYlWTttpKQrvvSG89+Mry5QiHgx8nYYLtTqBsvgaPcejkDNBeyPlvyjAdU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711041780; c=relaxed/simple; bh=Bla6ImeaxPC0l/wXS4fIkd5VYa4ZJlHCrM9gFW+aZB8=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=ha2RaaA5W/EHhlr5nt24EDJU4VNEbh6zlKk7eXoK0fEWRgF4JMHs1CB6m1Fsl1Tx/vy66sFHkyZ/JOK9+IVkNKmwN9Ezm3rdfMGRBj9NjzOmJjvAeu8y1XRXfzHdiZBNwlvmFfRPOm5yhdYlGFy+cZCcFJ6Je1R6dMJ2WlXhKCo= 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=Wp4O/Vn1; arc=none smtp.client-ip=209.85.219.172 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-f172.google.com with SMTP id 3f1490d57ef6-dc236729a2bso1169348276.0 for ; Thu, 21 Mar 2024 10:22:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1711041778; x=1711646578; 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=w4sCto2weX/tSraSWS0s5CjzL8cyi2xaSDvgaVtTzPM=; b=Wp4O/Vn1x+NjMVoyH1hqeGz5tXez/gLF44IpWIScGFPlcJyu5RJ7vJiJU/gQPl/4Dn htaniwwXNzQB2aWwR4yMm/3f9OHS4M1G8T1ih8wSy7E8R98XYDyIdy1jTczSEl4g7Tki 2K3rqQo1QM00/qrcEl7zDLRPvvZs4YkH89pwYltNrD0UKP1BKGxtDCd05YExk5woVyxA Kybp7/HSuoE9rKnv0dGGk54Y1+pnsiL0uYJR4mXC9UMEnzzDuoTz/6cp8K1CpYa0qZHa p639zE4maeZirBKWuTl3ZpCOmTXcQPxMrIlmbNfQbca/Zm3qv+NrJ2T9roplqehch6lG fW6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711041778; x=1711646578; 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=w4sCto2weX/tSraSWS0s5CjzL8cyi2xaSDvgaVtTzPM=; b=gyx8phL0nN5WDn5vmLaKams7yBGHocPFUP7eYLgRyDakn55X+ELnnnc2Rjj6ZEr30c qATvfN5oISddy34cQEe7RxZ/2qUO26LrIw4WdvUkGOh8dZNufqkbf7LTju2+NJTgxrnJ CSPCrg3vElXfHxjirodrWg1tGTnUJSTSuZoeu8dH+rglnidCseqkdIQ5dpNmiA9RvZE8 NGTDde88oUjY7CluhNp0004GzkZXQZVQoYQeCrDnqexuSkoxMI7foX5cAn7VyPOGMc6H a6tcIWs4F5vUbUJaqyI4a1kwSSfNOTN07Spq+9PeHtiu5qx+s5qq6Dtau8WL3lXh5ghY DW7A== X-Forwarded-Encrypted: i=1; AJvYcCWSLUDv0e2fSV9WrtOpfKIJBGjWbyCD6g/zcZPPRkC4V+EJ7qnVPYQBEvf6K1KZmqDP30iSPp4kkWmugsSPfzpzn1S00PUvKDOmZBrb X-Gm-Message-State: AOJu0YwDttY1t6HOkeOxZSvJSUOFXOMG3t03giiWmIPgep+/3CGJ8e7A /waOAuavDB/aaTYUbxTKDgW+W1oSTg/4SFDIeSWWD3nZ5yXGKCkCQ6P+Ajne0gI8NgJax8EfW1w /ideYjNtPsM41tb319lKjDMwM/QlbZiTR1PdS X-Received: by 2002:a25:3607:0:b0:dcc:323e:e1a4 with SMTP id d7-20020a253607000000b00dcc323ee1a4mr19870609yba.6.1711041777566; Thu, 21 Mar 2024 10:22:57 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240321163705.3067592-1-surenb@google.com> <20240321163705.3067592-21-surenb@google.com> In-Reply-To: From: Suren Baghdasaryan Date: Thu, 21 Mar 2024 10:22:46 -0700 Message-ID: Subject: Re: [PATCH v6 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, 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 Thu, Mar 21, 2024 at 10:19=E2=80=AFAM Suren Baghdasaryan wrote: > > On Thu, Mar 21, 2024 at 10:04=E2=80=AFAM Matthew Wilcox wrote: > > > > On Thu, Mar 21, 2024 at 04:48:53PM +0000, Matthew Wilcox wrote: > > > On Thu, Mar 21, 2024 at 09:36:42AM -0700, Suren Baghdasaryan wrote: > > > > +++ b/mm/page_alloc.c > > > > @@ -4700,12 +4700,15 @@ void __free_pages(struct page *page, unsign= ed int 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) > > > > + else if (!head) { > > > > + pgalloc_tag_sub_pages(tag, (1 << order) - 1); > > > > while (order-- > 0) > > > > free_the_page(page + (1 << order), order); > > > > + } > > > > > > Why do you need these new functions instead of just: > > > > > > + else if (!head) { > > > + pgalloc_tag_sub(page, (1 << order) - 1); > > > while (order-- > 0) > > > free_the_page(page + (1 << order), order); > > > + } > > > > Actually, I'm not sure this is safe (I don't fully understand codetags, > > so it may be safe). What can happen is that the put_page() can come in > > before the pgalloc_tag_sub(), and then that page can be allocated again= . > > Will that cause confusion? I indirectly answered your question in the reason #2 but to be clear, we obtain codetag before we do put_page() here, therefore it's valid. If another page is allocated and it points to the same codetag, then it will operate on the same codetag per-cpu counters and that should not be a problem. > > So, there are two reasons I unfortunately can't reuse pgalloc_tag_sub(): > > 1. We need to subtract `bytes` counter from the codetag but not the > `calls` counter, otherwise the final accounting will be incorrect. > This is because we effectively allocated multiple pages with one call > but freeing them with separate calls here. pgalloc_tag_sub_pages() > subtracts bytes but keeps calls counter the same. I mentioned this in > here: https://lore.kernel.org/all/CAJuCfpEgh1OiYNE_uKG-BqW2x97sOL9+AaTX4J= ct3=3DWHzAv+kg@mail.gmail.com/ > 2. The codetag object itself is stable, it's created at build time. > The exception is when we unload modules and the codetag section gets > freed but during module unloading we check that all module codetags > are not referenced anymore and we prevent unloading this section if > any of them are still referenced (should not normally happen). That > said, the reference to the codetag (in this case from the page_ext) > might change from under us and we have to make sure it's valid. We > ensure that here by getting the codetag itself with pgalloc_tag_get() > *before* calling put_page_testzero(), which ensures its stability. > > >