From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from va-1-111.ptr.blmpb.com (va-1-111.ptr.blmpb.com [209.127.230.111]) (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 69402314B8C for ; Thu, 21 May 2026 04:03:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.127.230.111 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779336189; cv=none; b=Z00KzoIpuZb13lIlnabPT3eQVZ6j+jSNND7T+yxAXy+M/+916zVxIpTXtxWxqCAs2fmQ5tICD2USq3fkw9OcF768+KfRrnSy8UmUNGaujVeusj0+3TvahDCqjx1145rxHSb1aLMM2Wv7+OFEURkvK4RIoaLOSlQv74+MggVlaJU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779336189; c=relaxed/simple; bh=GWyeI+m+rFWI8ArYKyE1ilMxvNLhqzxY8+cdQAg4Wjg=; h=Subject:Content-Type:Message-Id:References:Date:In-Reply-To:To:Cc: From:Mime-Version; b=Rx8Pz0bq6tdrfaKgN1IHKAdi5ZjEKyESqmd5nQ0ylnfvQBpigxydDRgHwXw/nG36i3D7dHTyeh+NnQyPYpgVoPKHve++SrsVWnvvbdKBBKUOlyzotU6PT/EbkT+IjPUKW5cVx1LOfp2jLXuIS6KXdx52CfhpTNclmefcly4qgCA= 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=nN5kh5pf; arc=none smtp.client-ip=209.127.230.111 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="nN5kh5pf" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=2212171451; d=bytedance.com; t=1779336182; h=from:subject: mime-version:from:date:message-id:subject:to:cc:reply-to:content-type: mime-version:in-reply-to:message-id; bh=r8qhTx7yx3vMMJAHefyPo3ETAaMxzAsg7BKGCu+RwJg=; b=nN5kh5pfL9Zv5Rs68RqL8v/tzSR9cbPc9Ahep90oEPq2WAuLw3HRCUIOczDU7zuo2HXn4Q gW5eEyG+XzrZ+S/AwwjMzJayY4LHpZ9ueORqRbF94qX6jtKsOjTF9YUSOFtKzMGkk9Ikvh KCLrJ9h6nXSV60UxS90tqJmH6ARyqmniszXZe8fm9vsV5hu6004uy1jrj49i4BGs5GQsbp fpjvPTKEzYVeoNDeQtDxDBfLQhEkhHKdXZNd+hVOrqC9E+fmt9coGOOBWHpoC3cPbxxKTj frlC7hknG0Yrxy8HxyfKl4cKCfHKTu3q8ZVzSojPLIO5Km3KSHBM7BYvXHtr5w== Subject: [PATCH v2 1/7] mm: factor zone-device page init helpers out of __init_zone_device_page X-Original-From: Li Zhe Content-Type: text/plain; charset=UTF-8 Message-Id: <20260521040124.10608-2-lizhe.67@bytedance.com> References: <20260521040124.10608-1-lizhe.67@bytedance.com> X-Mailer: git-send-email 2.45.2 Date: Thu, 21 May 2026 12:01:18 +0800 In-Reply-To: <20260521040124.10608-1-lizhe.67@bytedance.com> Content-Transfer-Encoding: 7bit X-Lms-Return-Path: To: , , , , , , , , , Cc: , , , , , From: "Li Zhe" Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 memmap_init_zone_device() currently mixes refcount policy, core ZONE_DEVICE page setup, and pageblock metadata handling in a single helper. Factor the refcount policy into zone_device_page_init_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 --- mm/mm_init.c | 64 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 23 deletions(-) diff --git a/mm/mm_init.c b/mm/mm_init.c index f9f8e1af921c..4ba506df93bc 100644 --- a/mm/mm_init.c +++ b/mm/mm_init.c @@ -987,11 +987,36 @@ static void __init memmap_init(void) } #ifdef CONFIG_ZONE_DEVICE -static void __ref __init_zone_device_page(struct page *page, unsigned long pfn, +static inline int zone_device_page_init_refcount( + const struct dev_pagemap *pgmap) +{ + /* + * ZONE_DEVICE pages other than MEMORY_TYPE_GENERIC are released + * directly to the driver page allocator which will set the page count + * to 1 when allocating the page. + * + * MEMORY_TYPE_GENERIC and MEMORY_TYPE_FS_DAX pages automatically have + * their refcount reset to one whenever they are freed (ie. after + * their refcount drops to 0). + */ + switch (pgmap->type) { + case MEMORY_DEVICE_FS_DAX: + case MEMORY_DEVICE_PRIVATE: + case MEMORY_DEVICE_COHERENT: + case MEMORY_DEVICE_PCI_P2PDMA: + return 0; + case MEMORY_DEVICE_GENERIC: + return 1; + default: + WARN_ONCE(1, "Unknown memory type!"); + return 1; + } +} + +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 +1035,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,27 +1054,16 @@ static void __ref __init_zone_device_page(struct page *page, unsigned long pfn, init_pageblock_migratetype(page, MIGRATE_MOVABLE, false); cond_resched(); } +} - /* - * ZONE_DEVICE pages other than MEMORY_TYPE_GENERIC are released - * directly to the driver page allocator which will set the page count - * to 1 when allocating the page. - * - * MEMORY_TYPE_GENERIC and MEMORY_TYPE_FS_DAX pages automatically have - * their refcount reset to one whenever they are freed (ie. after - * their refcount drops to 0). - */ - 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 (!zone_device_page_init_refcount(pgmap)) set_page_count(page, 0); - break; - - case MEMORY_DEVICE_GENERIC: - break; - } + zone_device_page_init_pageblock(page, pfn); } /* @@ -1084,7 +1102,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); } @@ -1120,7 +1138,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