From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-175.mta1.migadu.com (out-175.mta1.migadu.com [95.215.58.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2CF423E5EC0 for ; Wed, 5 Aug 2026 07:54:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.175 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785916498; cv=none; b=YKp4FlssDzCrvA3aflRCFIiSwGgySjGEAoqsQmNNR2Z98xW/nDhjssfs3zW1ME5HRqXMgD7LdM9hBEGlBDNeJ0EufnoB/R7KIw3mqYGBezc8gAc9IofV2JW8k+Il4qLBtbRMUGuV17w8xNUUfgS6gtVBBebRoNajYg/ZqgxxmuE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785916498; c=relaxed/simple; bh=RejxSqib9aBuLsvfbPxFQxtz4EFWAblNB5sG5n0YL+c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type:Content-type; b=ZqZLYlE4n4Rkiltto8nmwxwErPvAhT31iO1Gd1th/FxCqg2/Xo9gUuOaEhJA4eX/SZIR3nu58DCGIxYkl3qHyYwoCWpUae0l7q7+i/VmQkq6ejMcaxWGqxBd4PChVHo0PBrFoMUiyPXLhfWHP4zCGlh/AnfaLwQg81N5WPxjhw4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=WTf6PIsg; arc=none smtp.client-ip=95.215.58.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="WTf6PIsg" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1785916493; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KQPF9NpoXms5JqUk+wWG0b5ssSUK6sTaQPnJLeO/Wgc=; b=WTf6PIsgJKrF3nj0UfO/kXgdoFHWcAZIMMiQtM5TabV1fluZmpgG0uKdbdZKRSg62tLfXT sFrrnvfe5jPh5tVLZVdGvzdHlRyVVhmPUwQeLsbGawGSKzWB+6MVaKMxIW99TXl3MxwIJM ZnAaw97ElGJcefECReEWYaPZmZChrqU= From: Baoquan He To: linux-mm@kvack.org Cc: chrisl@kernel.org, nphamcs@gmail.com, kasong@tencent.com, baohua@kernel.org, youngjun.park@lge.com, hannes@cmpxchg.org, yosry@kernel.org, david@kernel.org, shikemeng@huaweicloud.com, chengming.zhou@linux.dev, linux-kernel@vger.kernel.org, Baoquan He Subject: [RFC PATCH v2 06/10] mm, swap: free backing pages in xswap_unmap_clusters Date: Wed, 5 Aug 2026 15:53:29 +0800 Message-ID: <20260805075336.3579395-7-baoquan.he@linux.dev> In-Reply-To: <20260805075336.3579395-1-baoquan.he@linux.dev> References: <20260805075336.3579395-1-baoquan.he@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-type: text/plain Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT vm_area_unmap_pages() only clears PTEs and frees intermediate page table pages — it does not free the backing physical pages allocated by xswap_map_clusters(). Fix this by walking the page table with apply_to_existing_page_range() before the unmap to collect all struct pages in the range. After vunmap_range() clears the PTEs, free the collected pages via __free_page(). Use a simple xswap_page_data collector callback: for each present PTE, collect pte_page() into a dynamically allocated array. The array is freed after the pages are released. Signed-off-by: Baoquan He --- mm/swapfile.c | 52 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 0d1d0c5e24c6..c43c8746378e 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -3796,6 +3796,23 @@ static int xswap_map_clusters(struct swap_info_struct *si, return -ENOMEM; } +struct xswap_page_data { + struct page **pages; + int nr; + int max; +}; + +static int xswap_collect_page(pte_t *pte, unsigned long addr, void *data) +{ + struct xswap_page_data *xpd = data; + + if (!pte_present(*pte)) + return 0; + if (xpd->nr < xpd->max) + xpd->pages[xpd->nr++] = pte_page(*pte); + return 0; +} + static void xswap_unmap_clusters(struct swap_info_struct *si, unsigned long start_idx, unsigned long nr) { @@ -3805,11 +3822,15 @@ static void xswap_unmap_clusters(struct swap_info_struct *si, /* * Round to page boundaries: start up (skip partial page that may * contain clusters still in use before start_idx), end up so the - * entire range is covered. vm_area_unmap_pages() operates on - * whole pages. + * entire range is covered. vm_area_unmap_pages() and + * apply_to_existing_page_range() operate on whole pages. */ unsigned long vm_start = PAGE_ALIGN(start_addr); unsigned long vm_end = PAGE_ALIGN(end_addr); + unsigned long size; + unsigned long npages; + struct xswap_page_data xpd; + int i; mutex_lock(&si->xswap_lock); @@ -3818,12 +3839,31 @@ static void xswap_unmap_clusters(struct swap_info_struct *si, goto out; } - vm_area_unmap_pages(si->cluster_vm, vm_start, vm_end); + size = vm_end - vm_start; + npages = size >> PAGE_SHIFT; + /* - * vm_area_unmap_pages() only clears PTEs; it does not free the - * physical pages. Walk the page table to find and free them. + * Walk the page table to collect physical pages before unmapping. + * vm_area_unmap_pages() only clears PTEs and frees intermediate + * page table pages — it does not free backing pages. */ - /* TODO: free backing pages via page table walk or tracking bitmap */ + xpd.pages = kmalloc_array(npages, sizeof(*xpd.pages), GFP_KERNEL); + if (xpd.pages) { + xpd.nr = 0; + xpd.max = npages; + apply_to_existing_page_range(&init_mm, vm_start, size, + xswap_collect_page, &xpd); + } + + vm_area_unmap_pages(si->cluster_vm, vm_start, vm_end); + + /* Free the collected backing pages */ + if (xpd.pages) { + for (i = 0; i < xpd.nr; i++) + __free_page(xpd.pages[i]); + kfree(xpd.pages); + } + mutex_unlock(&si->xswap_lock); out: -- 2.54.0