From patchwork Tue Oct 26 17:38:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pasha Tatashin X-Patchwork-Id: 12585331 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EA64CC433EF for ; Tue, 26 Oct 2021 17:38:36 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 9355C6109D for ; Tue, 26 Oct 2021 17:38:36 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 9355C6109D Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=soleen.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 3BFB294000D; Tue, 26 Oct 2021 13:38:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 39936940007; Tue, 26 Oct 2021 13:38:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 127FF94000D; Tue, 26 Oct 2021 13:38:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0208.hostedemail.com [216.40.44.208]) by kanga.kvack.org (Postfix) with ESMTP id F018F940007 for ; Tue, 26 Oct 2021 13:38:30 -0400 (EDT) Received: from smtpin18.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id AFDE32D233 for ; Tue, 26 Oct 2021 17:38:30 +0000 (UTC) X-FDA: 78739297980.18.AF4F2E9 Received: from mail-qt1-f172.google.com (mail-qt1-f172.google.com [209.85.160.172]) by imf30.hostedemail.com (Postfix) with ESMTP id A58A7E001999 for ; Tue, 26 Oct 2021 17:38:20 +0000 (UTC) Received: by mail-qt1-f172.google.com with SMTP id v17so14297971qtp.1 for ; Tue, 26 Oct 2021 10:38:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=NtsmbIV7d7zHsfeH2swrf3uQXR/7d3oxwiABUKv37gE=; b=PNV2XjJVxfXiWSL/tW8PwiWtR53RywlW1cPArNMtx+L/Wf1vtLvbcRfwXNq8iigtmF 0Ptzle5Oxy3TJccLaUNjqNcJAdOfvgeiZp0MzvZc3G8KohHo/VcPzjODLJx9o9kU/6cX GEULhx2Qx0G6F0zgk5YZdLlLLVBpM46DILOs4Xs2fAwaS0fiA5OBvXXrk29N7BMPRklt 55/AXI++VDHFF17oDDqnobsBMethvmOiV9b4iQbVZl3YnCeFa9nI3XnITsGID9Ki1gP0 866kW/ymqEYJwdxfD/RQL2NLzj7kKA2duuENsqob/TJ/sveDbLA8v1PgCxyNMzEsUUtN DE0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=NtsmbIV7d7zHsfeH2swrf3uQXR/7d3oxwiABUKv37gE=; b=fjamShFbMiJ2gfDncz/7oT5VC/1RA8oxhI64DWEezX9UNsoAv283ryE5murtD3ZF+9 rfZnzeqNv42womShcUVjlocKCWH9jqRardCnLD7kKoidLU8ENgvGmQ+D2/blOkcrmNe/ wlXl5aJlJMZ45ny+meerqHdIjoJmxBWlR6ISLrV8FAyPrL7aYYkCvmVDOgZ3i7xBy5LH uSjJLirgmRSNzyKHos1jcuXK0aVZPDPCCMHJcwBbpX2rtXmU5a5Dm7B09DzbBPvhm/Ik k0JGThKSK69vL5eqtO+ks22bkKWCfwwY/AUif6pJ3+EvW9v2dHGekQdDjpW5echeksF7 HNvw== X-Gm-Message-State: AOAM533HHUQ18+dI+id7cRYp0/Y7BY7Tp05KGTxRoy51RCElYEc0+QT5 YDczKxpNQJdpBgiEFPC1RUJsVQ== X-Google-Smtp-Source: ABdhPJwVq2CNV+SssZKgO5B05WCOSZ3H9k5Rw9dOZSjNBYaCWHeQIFOEPRQdXQLz2oXdhaGZXH612A== X-Received: by 2002:ac8:5707:: with SMTP id 7mr26700054qtw.397.1635269909790; Tue, 26 Oct 2021 10:38:29 -0700 (PDT) Received: from soleen.c.googlers.com.com (189.216.85.34.bc.googleusercontent.com. [34.85.216.189]) by smtp.gmail.com with ESMTPSA id bj37sm11001939qkb.49.2021.10.26.10.38.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Oct 2021 10:38:29 -0700 (PDT) From: Pasha Tatashin To: pasha.tatashin@soleen.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-m68k@lists.linux-m68k.org, anshuman.khandual@arm.com, willy@infradead.org, akpm@linux-foundation.org, william.kucharski@oracle.com, mike.kravetz@oracle.com, vbabka@suse.cz, geert@linux-m68k.org, schmitzmic@gmail.com, rostedt@goodmis.org, mingo@redhat.com, hannes@cmpxchg.org, guro@fb.com, songmuchun@bytedance.com, weixugc@google.com, gthelen@google.com Subject: [RFC 5/8] mm: avoid using set_page_count() when pages are freed into allocator Date: Tue, 26 Oct 2021 17:38:19 +0000 Message-Id: <20211026173822.502506-6-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.33.0.1079.g6e70778dc9-goog In-Reply-To: <20211026173822.502506-1-pasha.tatashin@soleen.com> References: <20211026173822.502506-1-pasha.tatashin@soleen.com> MIME-Version: 1.0 X-Stat-Signature: n5bsr4orhewjycf3kojzfzooy4cecuyn X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: A58A7E001999 Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=soleen.com header.s=google header.b=PNV2XjJV; dmarc=none; spf=pass (imf30.hostedemail.com: domain of pasha.tatashin@soleen.com designates 209.85.160.172 as permitted sender) smtp.mailfrom=pasha.tatashin@soleen.com X-HE-Tag: 1635269900-439415 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: When struct pages are first initialized the page->_refcount field is set 1. However, later when pages are freed into allocator we set _refcount to 0 via set_page_count(). Unconditionally resetting _refcount is dangerous. Instead use page_ref_dec_return(), and verify that the _refcount is what is expected. Signed-off-by: Pasha Tatashin --- mm/page_alloc.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 6af4596bddc2..9d18e5f9a85a 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -1659,6 +1659,7 @@ void __free_pages_core(struct page *page, unsigned int order) unsigned int nr_pages = 1 << order; struct page *p = page; unsigned int loop; + int refcnt; /* * When initializing the memmap, __init_single_page() sets the refcount @@ -1669,10 +1670,12 @@ void __free_pages_core(struct page *page, unsigned int order) for (loop = 0; loop < (nr_pages - 1); loop++, p++) { prefetchw(p + 1); __ClearPageReserved(p); - set_page_count(p, 0); + refcnt = page_ref_dec_return(p); + VM_BUG_ON_PAGE(refcnt, p); } __ClearPageReserved(p); - set_page_count(p, 0); + refcnt = page_ref_dec_return(p); + VM_BUG_ON_PAGE(refcnt, p); atomic_long_add(nr_pages, &page_zone(page)->managed_pages); @@ -2244,10 +2247,12 @@ void __init init_cma_reserved_pageblock(struct page *page) { unsigned i = pageblock_nr_pages; struct page *p = page; + int refcnt; do { __ClearPageReserved(p); - set_page_count(p, 0); + refcnt = page_ref_dec_return(p); + VM_BUG_ON_PAGE(refcnt, p); } while (++p, --i); set_pageblock_migratetype(page, MIGRATE_CMA);