From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-181.mta0.migadu.com (out-181.mta0.migadu.com [91.218.175.181]) (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 2B08C3FE640 for ; Tue, 2 Jun 2026 14:27:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.181 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780410426; cv=none; b=k62T70hR6RHt8+zF/+20cZoaIbg00GtmOCNVya5IDVXstnU7XuM160lnXiyWBZllSpVqatu3Hkp9RgpiZpwQaC2PsqSQf8jxD3KBYk8dUQf5H6uxtiaG8iBlY36InD0o44IPquKij3jfT81alHZS0eE4MvW97Ep/qI8IMEDh30A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780410426; c=relaxed/simple; bh=XpZXmWOLKdEl0ky1Wups9KNcUWfZu/4dTSGfdTcbrFk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=inMtfWZlSbAf9qwhMqRpQk9AaRY41BIcGKyeMIWzKlatjKmWVX/BnXeIh5f/5IjOR2w4a5MSxupauhuqk4I/E+JjpxTlmY6dKZWMLb9s7L6AcY+tfRVgsBtNPPDzqSZYQ2zrkg9YWIvTc+GmHM6Z73J28B4djWeqVGS8oF2s8jo= 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=hkVMII0r; arc=none smtp.client-ip=91.218.175.181 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="hkVMII0r" 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=1780410423; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=MblwLlwqCbAerbDko1BchwsCrXD3VZb6izB3pry9d1g=; b=hkVMII0r5J58OUGsynBUD9a4EDBaYUk8vlKTh8qkbpS9Wc6wyGhDqShzR0iau+RmYStX7h 1VlDt4g5kmDI1tWhdYlLPRn5V/JSneJu6i+TzOVl8CwV5De1zbrWpxGdFEbcORVEYpKJr6 Ug+LzRVHMjuIsMYNKJ2j1+bqNQ21mBA= From: Usama Arif To: Andrew Morton , david@kernel.org, chrisl@kernel.org, kasong@tencent.com, ljs@kernel.org, ziy@nvidia.com Cc: ying.huang@linux.alibaba.com, Baoquan He , willy@infradead.org, youngjun.park@lge.com, hannes@cmpxchg.org, riel@surriel.com, shakeel.butt@linux.dev, alex@ghiti.fr, kas@kernel.org, baohua@kernel.org, dev.jain@arm.com, baolin.wang@linux.alibaba.com, npache@redhat.com, Liam R. Howlett , ryan.roberts@arm.com, Vlastimil Babka , lance.yang@linux.dev, linux-kernel@vger.kernel.org, nphamcs@gmail.com, shikemeng@huaweicloud.com, kernel-team@meta.com, Usama Arif Subject: [v2 12/16] mm: handle PMD swap entries in MADV_WILLNEED Date: Tue, 2 Jun 2026 07:24:20 -0700 Message-ID: <20260602142537.198755-13-usama.arif@linux.dev> In-Reply-To: <20260602142537.198755-1-usama.arif@linux.dev> References: <20260602142537.198755-1-usama.arif@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT swapin_walk_pmd_entry() walks PTEs and skips non-present PMDs, so MADV_WILLNEED is a no-op on a PMD swap entry. Read the whole 2 MB folio in at PMD order via swapin_sync(BIT(HPAGE_PMD_ORDER)) so the subsequent fault hits do_huge_pmd_swap_page() and restores the THP mapping; an order-0 read-ahead would force the fault to split. Signed-off-by: Usama Arif --- mm/madvise.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/mm/madvise.c b/mm/madvise.c index 00539022f804..25f40542b951 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -193,6 +193,46 @@ static int swapin_walk_pmd_entry(pmd_t *pmd, unsigned long start, spinlock_t *ptl; unsigned long addr; + ptl = pmd_trans_huge_lock(pmd, vma); + if (ptl) { + pmd_t pmdval = *pmd; + + if (pmd_is_swap_entry(pmdval)) { + softleaf_t entry = softleaf_from_pmd(pmdval); + struct vm_fault vmf = { + .vma = vma, + .address = start, + .real_address = start, + .pmd = pmd, + }; + struct swap_info_struct *si; + struct folio *folio; + + /* + * Pin the swap device under the PMD lock so the + * lookup is atomic with the PMD-swap-entry + * observation; swapin_sync() requires its caller to + * keep the device valid for the duration of the call. + */ + si = get_swap_device(entry); + spin_unlock(ptl); + if (!si) { + cond_resched(); + return 0; + } + + folio = swapin_sync(entry, GFP_HIGHUSER_MOVABLE, + BIT(HPAGE_PMD_ORDER), &vmf, + NULL, 0); + if (!IS_ERR_OR_NULL(folio)) + folio_put(folio); + put_swap_device(si); + cond_resched(); + return 0; + } + spin_unlock(ptl); + } + for (addr = start; addr < end; addr += PAGE_SIZE) { pte_t pte; softleaf_t entry; -- 2.52.0