From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from va-1-114.ptr.blmpb.com (va-1-114.ptr.blmpb.com [209.127.230.114]) (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 C4120346E4A for ; Wed, 3 Jun 2026 08:04:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.127.230.114 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780473872; cv=none; b=dK65x3VbQ04htwcIsG+UWPgyxC9vqfqSS9hDuYBaAFSfvJLath9Ut6fMnMNjidpaBxW3r5ZYnGKYmzma8Wk7kyI9JvpjHgdA1er0bonLvyVvXElxnRHBYtl8ILFAnVi6H4Ps9I9Lvo/1BwX4xIolrxWMfdm3KyIfB8tXRvPRw0k= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780473872; c=relaxed/simple; bh=zEwM4ExvxDP39KFMRfs9oCMwfWnr8A/tcgD3Hi6W860=; h=Subject:References:Message-Id:Cc:From:Mime-Version:To:Date: In-Reply-To:Content-Type; b=G0UA+KRz1d+h2wlqn70U57SA3TksqxynuSF6qktM+dqkGhx9VjjIFyMlB1IMJwswj/GQsEz07V1JrB95slUotick6lIEqY7LpmmCUvJsX65Zhh/5dXa3lWEqomJJUSCnw+0JjvZar6lHxI0KI1BZTf/GHzk6upMOeItw1rcOXwI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=nXc4Z1Jj; arc=none smtp.client-ip=209.127.230.114 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="nXc4Z1Jj" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=2212171451; d=bytedance.com; t=1780473860; h=from:subject: mime-version:from:date:message-id:subject:to:cc:reply-to:content-type: mime-version:in-reply-to:message-id; bh=PAoGwOogI0538Jjz3Jr5mPaMM02ePyByPYJiX1kFuRI=; b=nXc4Z1Jju0zHBJOvEeYJbZHEg9IWLUkiIUT9Y5bIJxEQsqz/L2DmvHPWjZckPV8v+RMxKa OgET/x03w4pmPnmfN8PEr4I6+BDxFDg1o4b1j/WnvD1K8gtEMM5snPrJHDP/5Z8fx9/iyo 7Yp0fMCsBr9c9Zb7ftKJ/sJ+cIB5f/dUHLpmO/F1lIpwXL0rC9UvcU/vqdupPDFbJ+gZa1 svfmi2VZZdjyoM+OzAjHWHjMgNkjq1gpl9/vIaQ9bGCHyye2h3vUh4UBalVqJLAikEIfs7 nXlzMu+uxtMELsyyP/8iAMJ/gI9TGdtQJJzXFI+nEmYs9qjsSykorhFplaSpwA== Subject: [PATCH v4 2/8] mm: factor zone-device page init helpers out of __init_zone_device_page References: <20260603080152.64728-1-lizhe.67@bytedance.com> Message-Id: <20260603080152.64728-3-lizhe.67@bytedance.com> Cc: , , , , , From: "Li Zhe" Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Transfer-Encoding: 7bit X-Original-From: Li Zhe To: , , , , , , , , , Date: Wed, 3 Jun 2026 16:01:46 +0800 X-Lms-Return-Path: In-Reply-To: <20260603080152.64728-1-lizhe.67@bytedance.com> X-Mailer: git-send-email 2.45.2 Content-Type: text/plain; charset=UTF-8 memmap_init_zone_device() currently mixes refcount policy, core ZONE_DEVICE page setup, and pageblock metadata handling in a single helper. Factor the refcount-reset predicate into pagemap_resets_refcount(), move the common page initialization into __zone_device_page_init(), split pageblock handling into zone_device_page_init_pageblock(), and wrap the existing slow path in zone_device_page_init_slow(). This keeps the slow-path behaviour unchanged and gives later patches reusable helper boundaries. No functional change intended. Signed-off-by: Li Zhe Reviewed-by: Mike Rapoport (Microsoft) --- mm/mm_init.c | 62 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 19 deletions(-) diff --git a/mm/mm_init.c b/mm/mm_init.c index 35de3b6a186d..2e5899c5cf35 100644 --- a/mm/mm_init.c +++ b/mm/mm_init.c @@ -987,11 +987,38 @@ static void __init memmap_init(void) } #ifdef CONFIG_ZONE_DEVICE -static void __ref __init_zone_device_page(struct page *page, unsigned long pfn, +/* + * Return true when the free path for this pagemap type restores the page + * refcount to 1, so memmap_init_zone_device() can keep the count set by + * __init_single_page(). Otherwise initialize the refcount to 0 and leave + * it to the allocator or pgmap callbacks to raise it when the page is + * handed out again. + */ +static inline bool pagemap_resets_refcount(const struct dev_pagemap *pgmap) +{ + /* + * MEMORY_DEVICE_GENERIC pages regain a refcount of 1 in the free + * path. The remaining ZONE_DEVICE types start from 0 here and raise + * the count again when the allocator or driver hands the page out. + */ + switch (pgmap->type) { + case MEMORY_DEVICE_FS_DAX: + case MEMORY_DEVICE_PRIVATE: + case MEMORY_DEVICE_COHERENT: + case MEMORY_DEVICE_PCI_P2PDMA: + return false; + case MEMORY_DEVICE_GENERIC: + return true; + default: + WARN_ONCE(1, "Unknown memory type!"); + return true; + } +} + +static void __ref __zone_device_page_init(struct page *page, unsigned long pfn, unsigned long zone_idx, int nid, struct dev_pagemap *pgmap) { - __init_single_page(page, pfn, zone_idx, nid); /* @@ -1010,7 +1037,11 @@ static void __ref __init_zone_device_page(struct page *page, unsigned long pfn, */ page_folio(page)->pgmap = pgmap; page->zone_device_data = NULL; +} +static void __ref zone_device_page_init_pageblock(struct page *page, + unsigned long pfn) +{ /* * Mark the block movable so that blocks are reserved for * movable at startup. This will force kernel allocations @@ -1025,23 +1056,16 @@ static void __ref __init_zone_device_page(struct page *page, unsigned long pfn, init_pageblock_migratetype(page, MIGRATE_MOVABLE, false); cond_resched(); } +} - /* - * MEMORY_DEVICE_GENERIC pages regain a refcount of 1 in the free - * path. The remaining ZONE_DEVICE types start from 0 here and raise - * the count again when the allocator or driver hands the page out. - */ - switch (pgmap->type) { - case MEMORY_DEVICE_FS_DAX: - case MEMORY_DEVICE_PRIVATE: - case MEMORY_DEVICE_COHERENT: - case MEMORY_DEVICE_PCI_P2PDMA: +static void __ref zone_device_page_init_slow(struct page *page, + unsigned long pfn, unsigned long zone_idx, int nid, + struct dev_pagemap *pgmap) +{ + __zone_device_page_init(page, pfn, zone_idx, nid, pgmap); + if (!pagemap_resets_refcount(pgmap)) set_page_count(page, 0); - break; - - case MEMORY_DEVICE_GENERIC: - break; - } + zone_device_page_init_pageblock(page, pfn); } /* @@ -1080,7 +1104,7 @@ static void __ref memmap_init_compound(struct page *head, for (pfn = head_pfn + 1; pfn < end_pfn; pfn++) { struct page *page = pfn_to_page(pfn); - __init_zone_device_page(page, pfn, zone_idx, nid, pgmap); + zone_device_page_init_slow(page, pfn, zone_idx, nid, pgmap); prep_compound_tail(page, head, order); set_page_count(page, 0); } @@ -1116,7 +1140,7 @@ void __ref memmap_init_zone_device(struct zone *zone, for (pfn = start_pfn; pfn < end_pfn; pfn += pfns_per_compound) { struct page *page = pfn_to_page(pfn); - __init_zone_device_page(page, pfn, zone_idx, nid, pgmap); + zone_device_page_init_slow(page, pfn, zone_idx, nid, pgmap); if (pfns_per_compound == 1) continue; -- 2.20.1