Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp630702lqp; Thu, 21 Mar 2024 10:40:53 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXLvTMV08nH2WmPC42ME4DhwmAf5W8EW36TIVtqBDHLSvmgeVqO1oKOiK4JF7QgLur0e3a7E0rPfwgC/gdtVmr3WKA3k1+0CMkcHHX3ng== X-Google-Smtp-Source: AGHT+IF2NjI7wOGVNNobr/2s8kJ/QFhJhiFFDNCdzJDAXSEmZvPixR7I71zv8EN8o8USblD2P38w X-Received: by 2002:a17:902:c401:b0:1e0:2e8b:f4b with SMTP id k1-20020a170902c40100b001e02e8b0f4bmr99981plk.2.1711042853489; Thu, 21 Mar 2024 10:40:53 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711042853; cv=pass; d=google.com; s=arc-20160816; b=NcvSJR7jgDZVzFWHtziHbXNIMKjnv7bx96UIqnjjLNimFUGTSp5d6wdXYsONCq3Zip l07YknSMG1YuQMFhWZ9QlPfMLWqEXtHt/Fz3OfEC9uLHqgnr9j6od5P9ye9CJSsSh9Uj z+HkKaeN4oDUeRzYi5JKMSrBw6lG4m8iIPBm94ArnIuXlW08bu+xVjcSR/z0Xu+u0rw0 95lHOeXktx3ebYt3KN46e2zm0SdeWvJUG7gUAlmr7wMj4yN4gXLR3IGK+nNdzfaTJpGY lSVLx7dCM2lVVXpFXEXr93rRSeHYqE/1KGH/7AIqHOWLT4bnT4HRbIsU4dfpJP6v43kd lAFg== 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=tqvfQSGh+Xzm8qjoxKwBWCT5Q1J0hViT+VZ+PA5jO/g=; fh=/cRl0GQalULbdw7yoU9AtJ+mVKbguqhKBYRxbb90Guw=; b=Mkpi1EiHCuRyESrPSpR4oQXXSaU/BNF+PVdoHKM2RJO917EnCDo7WeKfmoYUPA78yR SA3Zdc8wUqYPQBGRTVnzBGMn8J0eznDAnqAPyqf/5QMs0wXCW2k3e/+lgPDzgC7Kvvlv jPREymihoPvymsg7uhMOhI1Dw3AlParD6V1D5AiDbQnXFMBhE7PQPqvOXk5B05I00C0z wsjYeDWz7c3+eP8mCIvy7zB8mBdguoF9eB9KnAPXGtqZv9rMNts2ICC9IGjLKqRHICg9 2XukY9wy+quEeP3J6k6jU3T5BV+y2IGNTQsMbHRE6FIFDz7EycrVzL7kXbhdyccs4dEQ 49MQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=wQGswcMD; 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-110483-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-110483-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id n7-20020a170902f60700b001dee5f229adsi160298plg.122.2024.03.21.10.40.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Mar 2024 10:40:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-110483-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=wQGswcMD; 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-110483-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-110483-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id DB0BAB25F0E for ; Thu, 21 Mar 2024 17:20:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C737817590; Thu, 21 Mar 2024 17:19:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="wQGswcMD" Received: from mail-yb1-f170.google.com (mail-yb1-f170.google.com [209.85.219.170]) (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 F40FB132C37 for ; Thu, 21 Mar 2024 17:19:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.170 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711041583; cv=none; b=Of8Nvz30L/vdNNfCNr56bfoGOAwrA50eGLLOLJDfyWkfaqbj7Pb1tIgWz2onX1I7uu/vtA3/hl+FH6DKkljKE9zFboQpeEFMayAhhEq1bzKNHfqdHX23WQwMvDZJiQtnTx+yQYrsf+S+w01aYcPueURK76dbMnSTjyCjZV3Z3bs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711041583; c=relaxed/simple; bh=H+LJEeBQNsEayEy62NES0fpvH5c8qtUySXf9WjZsL7Q=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=OU6bpo8bkQYYIV90womA7wHCGo5zYPOtAPPTLQFtk2a9VnRxT+OssaNlxPS0xSfG0LoJTN+/p9vj7iPBtrF7AukrUnTlKz1kV64OgQuDINy4igli5nDKVPs2qKx9BReUqQoo5J7PQNLum8nRJhj+CJh/5iCnQzwBVeajPG4sVh4= 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=wQGswcMD; arc=none smtp.client-ip=209.85.219.170 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-f170.google.com with SMTP id 3f1490d57ef6-dcc73148611so1351008276.3 for ; Thu, 21 Mar 2024 10:19:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1711041581; x=1711646381; 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=tqvfQSGh+Xzm8qjoxKwBWCT5Q1J0hViT+VZ+PA5jO/g=; b=wQGswcMDuplyRs8fUGwaOmGwqP4mg/LWEXFC8ArimCNwJDM+AN5v9S0xWnckBn0jk8 UxqKp7QvM/RCvybCB0nL8IghAOSh6pPPc5flnZHOxIi3oS0qIFM/vX/GhEOZmV0vKSqf mIgHHK4zqQdRHGVqjgW7FF2HNkdsg8yHHp50lmbqURSFPylRlmaCEvgqJNAWffpiiJbU 8X52om8QSE2Sm1Q3lQD0PE3FHALTwhYilwJ0SK6BLYDJrwvFestuWd7Un0QefYLohj2j PXDnP/+iBba3Y7kqD0gRUqnetLQaKGYWxaCyZsYmmazdgsNKXt5CQVg2pgOCfz5lWphV r/LQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711041581; x=1711646381; 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=tqvfQSGh+Xzm8qjoxKwBWCT5Q1J0hViT+VZ+PA5jO/g=; b=pWisxHtWf0mKxR26StVsIZBrX0k4UV8M58m8RfM/FAawDjIYb+nFs4kn5narmBhnLI P7Su0OlyfEQAIeixg+XLouzH/OCrcXQJLgGp1HIqyMpK7KrPmlgvS+eMtzQR7uWMIIwY 8TFQsYdp5iV+6Ippv/0f//QzD3MqRm8V62Mo7ozE3rQBS1y8HVnZapiotOhiI42s0HnX 5Bd7Po9/nW+mLIh3APvujX2WCxeR4rSLmY+XHMqVMl9Nbod4HkqJH+/4DomS9K4W63WN SIUbuYa7m/RsrhQQnzOnbhxj6lVn8vOWvA9M602KHHOz/dnsMkpUd+pGYsAjRBJspkat tsTw== X-Forwarded-Encrypted: i=1; AJvYcCUVexj7mtVgghmQzVGuWKphfSKQLoF74QJ2PN2P/UFp8xJBmeMQW4YnPY4q5jYaTjg4F6hM+Vo/gjfWfVutBEXE/QWp186sF5pZjmhz X-Gm-Message-State: AOJu0YxjQKsplShsNQnSSbT935KAw/heNbCtzZM6uBwhO/gZTZFR+WND ncoZdRJ3/fcZbufMIBGK8vXiLRElduVkmwGMGAZo6EYaPLiwzL0bQph+PZTeO8J+R2Y9ilDJQp/ 2oUnqnZP0n9SpXioCRG0pPGj+4xPjXAPK1xJK X-Received: by 2002:a25:dc4a:0:b0:dcd:4e54:9420 with SMTP id y71-20020a25dc4a000000b00dcd4e549420mr19795790ybe.5.1711041580565; Thu, 21 Mar 2024 10:19:40 -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:19:28 -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: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, unsigned= 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? 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+AaTX4Jct= 3=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. >