Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp607114lqp; Thu, 21 Mar 2024 10:04:28 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXisQEOdUrcOduSiV26dS8ikzXxOJ+sM2GdmgBKsJ6iPndBjTf5L+Be83FrzXsJUC/16Bwi0OKr6gNv+57YVP3VObebr08yWKIghT9wLQ== X-Google-Smtp-Source: AGHT+IHu27BPxCHPzwRcdwea5KbUb4hsnWgk2mq4cbt85GXa/zm2r5SavNHu9rPwvKJvcU3vnxqc X-Received: by 2002:a92:d185:0:b0:365:d2be:3de6 with SMTP id z5-20020a92d185000000b00365d2be3de6mr124820ilz.2.1711040668019; Thu, 21 Mar 2024 10:04:28 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711040667; cv=pass; d=google.com; s=arc-20160816; b=iI9jysGF3+GrI4qV8S4oxmOPgquKj21pEzjNjZcf7M/L0ngrSTs/mMWTVSw2wbiNWq UAanmqirVD9IqwMAsU29bHF2PnBZuiw/NDLvggzwnkKPSXUsTKAmIuS4RlamXKbeRNbi AzRWK2dcVGHepjN1xS+RW9LdlBGwtHXFYb+YTZ5avrVOMOeSFVTD6zUD8DNUS8Ss5k8r JWX75k5AIrIvf9d1+L+vPEaLhgA3JSOk6g7WDp+iple5gc6/R08cpi0qs/N0sjU8CnBj 5xJt3qYsM881yWzvEU0nNqRpLwKEjNq3lNcFvNDd43De8REkMGojm7pHT6aSWtNV6pfn 2OlA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=/zyX0GCVbwAVqAn+thP4TQZbJ1DZ8xPrkSKdxK+srAs=; fh=3aFccfJv0OgWv2rov7+7prrNVw+0rlGz8qprZXQiXAM=; b=W1jrgOxTSvAUXaXVcnH1WabaGXW6UE38O4+EPsMtcV+MoqvUJhbTN4F3fhP1yfNkSq aHyUkza2cL+VvGyt5MIjqTdPBX2MB3+2W8s12LP4qCGtf2acMRoHUm8eBGedRTerYtPR 4Ox59XRBYMugmS60jhch6Er3qxpOmMu4o8qvZFmU2Tk7MjgnSJcBaw3qWfmtPW1OWMoD pbdDxsg3ld974PM+tNPdfbwfTHtC7hikvH2+3U6drEmIEdEYkyM7qe4ERY/9tjkU//N0 Qeiq6QB37PHyOKPKXB2qRDHaIrwQb7E5w7nolptTdYg0IKhZ+rT3ehS2eK2DkuFIOfEe sOUA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="H/nZHpnq"; arc=pass (i=1 spf=pass spfdomain=flex--surenb.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-110375-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-110375-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. [147.75.48.161]) by mx.google.com with ESMTPS id y10-20020a63fa0a000000b005d81b96abb9si121317pgh.888.2024.03.21.10.04.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Mar 2024 10:04:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-110375-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="H/nZHpnq"; arc=pass (i=1 spf=pass spfdomain=flex--surenb.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-110375-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-110375-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 3A226B270FC for ; Thu, 21 Mar 2024 16:45:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3D89D13175D; Thu, 21 Mar 2024 16:37:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="H/nZHpnq" Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (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 7E8B513957A for ; Thu, 21 Mar 2024 16:37:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711039077; cv=none; b=fXRM+egoL50H79nfC4DOpR6fEk0/eLhAD0RztXvmFJvMMzm78baS9iOYX19GB2YvwykT9Rv1vjkSJ1hXoiyKtGRbg2Zh4ocumZvBAUEqTB1ZWeORjkBFonXjKr8HR4KSzuSw4Gyb+UhmkvdOZOF38NCoZC4upejl6I5G2HDczpE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711039077; c=relaxed/simple; bh=q3rjzWyhmG5cuSNHeqibvv4qQMuWF17WVodXZAuRHsE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=BVN6cMQN69soF//354Nk8S2UEMyGkNrlB9QAq4svJLg9faSr1KEdOkPcINN1bnQ1Uq1ngV7NPAXncGjh4lMDYrYcYhmKCM/Xop4T0zCoakxl+gisDRGiU43okzHJ6e8s1S0F9zNM+hkqbxrSQMyHuawGxz6SldpPSyroeYQEmF4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=H/nZHpnq; arc=none smtp.client-ip=209.85.128.202 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=flex--surenb.bounces.google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-608e4171382so19905797b3.3 for ; Thu, 21 Mar 2024 09:37:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1711039074; x=1711643874; darn=vger.kernel.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=/zyX0GCVbwAVqAn+thP4TQZbJ1DZ8xPrkSKdxK+srAs=; b=H/nZHpnqnh56xgMrLxTP7kIlE7MiJsmMHgNm1xlFsNrulPEvtmLpDd/AIg1Txm8ByW lJ/eOl/B5agvQIwBRDLo9KZ0VefLTkr+qyBkEUKkn30RwzIKzrqpIbE45N/RhEfiuLje ywvjWuCgNyGsKyHt15KVIZX2HR37MdCmAySvdRlGsrr0q+zP+ASFFIYoQMcRwmKRhEAl 3pmKPWdTOOktySV1o3I7GQF9odLyePM7rT5Xqk+7rSDMXWaM4vE8NkSTGrj/LX2LbrYw NrwwbwExWXAs4BqV6P2OjhOTaqNL0NgTBci8BnP/OvN8Aa6vwyXmm1y9/xnlgBhqAXFp h/xw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711039074; x=1711643874; 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=/zyX0GCVbwAVqAn+thP4TQZbJ1DZ8xPrkSKdxK+srAs=; b=slTD1GCtqCyJeSZ1YenOfyr2jsg5kBIVGm6vCOBiR8wUYz3g/6Rnh6yM6u4RBj3SPj 5feo0BInoXhI5qjvZhwe5jqrD3Mi0W6ZxPYelUjQtErG6AjCWTCEOzMsT+FLB/+wpr7M 2aG1wVxbSQIE+HrNyxPpaBrDTYlWVK4Nev52o3nvPoDA6Td+dITc7n6xID68d504uLiN KttwyR+eGOXzvCpGskMzZ/biSO6g+TU7bIv/M8T+s96dcU6rrRCQnrn9Ij+zSXNe91vC il/gIq0X3RvOPICQk9YF02BiCxF/uOl7n5ay9Yf+NB1bDC1piEVHEfwHIvH3G++1gH2H 6uAA== X-Forwarded-Encrypted: i=1; AJvYcCWMNga+ScmIKLaKrwfvta2YR/nSe8sz1l/aD5e9hD+INdajShwyxYHC89f9xuD9RghnY4J5C7qSD6K9y/HvutUXY405fERPP4jnK5Nh X-Gm-Message-State: AOJu0Yz608JuhdY90WUt+1vuyYhWJE7shI2L1SGId4bApGiaqSXCdXZC v4zKP2rWKV1KQ8Pff0K5qpXA6dCj7cC8qwZWf9BeVhbnszNIkzL8+V2WzjfA3ibZfB7aoyU3mAt Upg== X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:a489:6433:be5d:e639]) (user=surenb job=sendgmr) by 2002:a05:6902:218c:b0:dcc:4785:b51e with SMTP id dl12-20020a056902218c00b00dcc4785b51emr980431ybb.12.1711039073381; Thu, 21 Mar 2024 09:37:53 -0700 (PDT) Date: Thu, 21 Mar 2024 09:36:42 -0700 In-Reply-To: <20240321163705.3067592-1-surenb@google.com> 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> X-Mailer: git-send-email 2.44.0.291.gc1ea87d7ee-goog Message-ID: <20240321163705.3067592-21-surenb@google.com> Subject: [PATCH v6 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, 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 Content-Type: text/plain; charset="UTF-8" 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 the page passed to put_page() is indistinguishable 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. Reported-by: Vlastimil Babka Signed-off-by: Suren Baghdasaryan --- include/linux/pgalloc_tag.h | 24 ++++++++++++++++++++++++ mm/page_alloc.c | 5 ++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/include/linux/pgalloc_tag.h b/include/linux/pgalloc_tag.h index 093edf98c3d7..50d212330bbb 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_pages(struct alloc_tag *tag, unsigned int nr) +{ + if (mem_alloc_profiling_enabled() && tag) + this_cpu_sub(tag->counters->bytes, PAGE_SIZE * nr); +} + #else /* CONFIG_MEM_ALLOC_PROFILING */ static inline void pgalloc_tag_add(struct page *page, struct task_struct *task, unsigned int nr) {} static inline void pgalloc_tag_sub(struct page *page, unsigned int nr) {} 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_pages(struct alloc_tag *tag, unsigned int nr) {} #endif /* CONFIG_MEM_ALLOC_PROFILING */ diff --git a/mm/page_alloc.c b/mm/page_alloc.c index fd1cc5b80a56..00e0ae4cbf2d 100644 --- a/mm/page_alloc.c +++ 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 = PageHead(page); + struct alloc_tag *tag = 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); + } } EXPORT_SYMBOL(__free_pages); -- 2.44.0.291.gc1ea87d7ee-goog