From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-176.mta1.migadu.com (out-176.mta1.migadu.com [95.215.58.176]) (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 30DC235E1BA for ; Fri, 12 Jun 2026 15:06:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.176 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781276774; cv=none; b=qb/+bYjEzoOoQpp5zpV+FUqRKDrix4M7BaMLtI3mq5fAXDlNlmLUusVNzjVyWqZEpIx9PjIFxmB31RL/UIHMQgSBMZDljrFuplWykRbuCEYgkDX5thqndDSMgT7v22wjQApRgBY0y4n5dfDVab2SjTsfciISoVovaSEDxvoThH4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781276774; c=relaxed/simple; bh=Epx6HPoYKHpgd6YtpNdO2p2ikoUjlHD1qQ5UMVIVHoU=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=cg3vZUSwMdgmtRgWacacFz6l3flrPHkjxGvhjjhMi4VhtAVC/uTopa4X8Z58Q2oPzhKdpqee9B91isWzYCR7x9ZGbA7D2+jdqAyPsxk1Io9WmxHZPrOzQa9VFHZue8ny9+0DN6u+mn77EENAT3eGD3Md7YKOTGwUcEJXslVZSQ0= 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=K1DEFj4I; arc=none smtp.client-ip=95.215.58.176 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="K1DEFj4I" Message-ID: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1781276769; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QYbxVxLJkPeVlJemR6VoxtOTsg3NPDGQmg7k2Z6F9O8=; b=K1DEFj4I8vun/tIJGxrSH1strQ/FHcGgv+bYplJ7fQHx1/imO6zavfvp5urYX2DRyM+yW/ AKGuWIk3u6++W+Eu7SJMHGk996Yv+qt4ZTK82bfqRs/B7yWOdDqPgxe5AuiSxm3ryjkjNR i5SeaBHFvZnlU/AfzALkg32L1lbCgnM= Date: Fri, 12 Jun 2026 16:05:57 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Subject: Re: [v2 11/16] mm: handle PMD swap entries in non-present PMD walkers To: Lance Yang Cc: akpm@linux-foundation.org, david@kernel.org, chrisl@kernel.org, kasong@tencent.com, ljs@kernel.org, ziy@nvidia.com, ying.huang@linux.alibaba.com, baoquan.he@linux.dev, 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@infradead.org, ryan.roberts@arm.com, vbabka@kernel.org, linux-kernel@vger.kernel.org, nphamcs@gmail.com, shikemeng@huaweicloud.com, kernel-team@meta.com, linux-mm@kvack.org References: <20260602142537.198755-12-usama.arif@linux.dev> <20260612064550.54968-1-lance.yang@linux.dev> Content-Language: en-US X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Usama Arif In-Reply-To: <20260612064550.54968-1-lance.yang@linux.dev> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_OUT On 12/06/2026 07:45, Lance Yang wrote: > +Cc linux-mm > > Please Cc linux-mm next time. Pretty clearly MM work ... Yes, thanks for this! I forgot, will be careful in v3. > > On Tue, Jun 02, 2026 at 07:24:19AM -0700, Usama Arif wrote: > [...] >> diff --git a/mm/mincore.c b/mm/mincore.c >> index e5d13eea9234..3fee8a7b9d9d 100644 >> --- a/mm/mincore.c >> +++ b/mm/mincore.c >> @@ -172,7 +172,19 @@ static int mincore_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, >> >> ptl = pmd_trans_huge_lock(pmd, vma); >> if (ptl) { >> - memset(vec, 1, nr); >> + if (pmd_present(*pmd)) { >> + memset(vec, 1, nr); >> + } else { >> + /* >> + * Non-present PMD: migration, device-private, or PMD >> + * swap entry. Route through mincore_swap() the same way >> + * the PTE path does -- the swap entry covers all 512 >> + * slots, so the whole vec gets the same answer. >> + */ >> + softleaf_t entry = softleaf_from_pmd(*pmd); >> + >> + memset(vec, mincore_swap(entry, false), nr); > > Looks buggy ... > > That assumes one swap-cache lookup is enough for whole PMD-sized range. > I don't think that always holds ... > > See do_huge_pmd_swap_page(): > > ---8<--- > folio = swap_cache_get_folio(swp_entry); > [...] > /* > * Folio should be PMD-sized; if not (e.g. split in swap cache), > * split the PMD swap entry and retry at PTE level. > */ > if (folio_nr_pages(folio) != HPAGE_PMD_NR) { > folio_unlock(folio); > folio_put(folio); > goto split_fallback; > } > --- > > it handles the case where swap_cache_get_folio() returns a folio that > is no longer PMD-sized. E.g. because it was split in the swap cache > while the PMD swap entry was installed. Then it split the PMD swap entry > and retries at PTE level :) > > unuse_pmd_entry() has the same fallback. Can mincore hit that case? > > Maybe the comment right above should say something like: > > " > One lookup is enough for a PMD-sized swapcache folio. If the swapcache > was split, check the per-page swap slots. > " > > Hopefully, I'm not missing something here :D > > Cheers, Lance Good catch! Thanks for pointing this out. I think the below diff over this commit should be ok. I will add it to the next revision. Its slower, but it shouldn't be an issue as its just mincore: diff --git a/mm/mincore.c b/mm/mincore.c index 3fee8a7b9d9d..975513fff336 100644 --- a/mm/mincore.c +++ b/mm/mincore.c @@ -175,15 +175,42 @@ static int mincore_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, if (pmd_present(*pmd)) { memset(vec, 1, nr); } else { - /* - * Non-present PMD: migration, device-private, or PMD - * swap entry. Route through mincore_swap() the same way - * the PTE path does -- the swap entry covers all 512 - * slots, so the whole vec gets the same answer. - */ softleaf_t entry = softleaf_from_pmd(*pmd); - memset(vec, mincore_swap(entry, false), nr); + /* + * Non-present PMD: migration, device-private, or + * PMD swap entry. Migration / device-private cover + * the whole PMD range with a single answer. + */ + if (!softleaf_is_swap(entry)) { + memset(vec, mincore_swap(entry, false), nr); + } else { + struct folio *folio = swap_cache_get_folio(entry); + + /* + * One lookup is enough for a PMD-sized + * swapcache folio. If the swapcache was split + * (e.g. by deferred_split_scan() or + * memory_failure()) while the PMD swap entry + * was installed, check the per-page swap slots. + */ + if (folio && folio_nr_pages(folio) == HPAGE_PMD_NR) { + memset(vec, folio_test_uptodate(folio), nr); + folio_put(folio); + } else { + unsigned long haddr = addr & HPAGE_PMD_MASK; + pgoff_t off = swp_offset(entry) + + ((addr - haddr) >> PAGE_SHIFT); + + if (folio) + folio_put(folio); + for (i = 0; i < nr; i++) + vec[i] = mincore_swap( + swp_entry(swp_type(entry), + off + i), + false); + } + } } spin_unlock(ptl); goto out;