From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-172.mta1.migadu.com (out-172.mta1.migadu.com [95.215.58.172]) (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 23C5827603C for ; Fri, 12 Jun 2026 15:21:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781277712; cv=none; b=cin/s1eIJ4yXX/gh+tGYQtpqt9iHblIAjiNLTZfpeMnJyBtl28JfFqTs6sS67NfDAQGimRwud7syeB025sSY4mhU+o1hynep5JhoUepWZ6NH8D/uSXLFPcMNwn4vHO2YZIjuXCdzPDg35jv3A4iWUy3pxOJtrInmD6ipQHoSIh0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781277712; c=relaxed/simple; bh=UPE3dIpJkdb5qVhvKNKZL9DN5t0Pbi8KEoH5X+Ny7RQ=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=R/shVNKndXUflv7tBh8qLC5DFnIHUWd2nlcbY6IwUX9VfJR9BQ5QtEEMTUuk7EN1ddzq52TZc1tWVoLJ9XysTdAMNhsJLlx/V7FDcAwPU50eZYWdcFKmcuc5kAU4S7I8Go0hpkIn2CM9pPRnpqoZPriahu+5KEb911WDIxsoxu8= 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=nnd7OEnc; arc=none smtp.client-ip=95.215.58.172 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="nnd7OEnc" Message-ID: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1781277707; 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=CP/ACqUrsN+vZNBxbVQZ0F5oAZ6jzKbJcdO1MIKShNA=; b=nnd7OEnc1hRa7QOfAG9NJnAYLlCmiR8N8ms8Wpwg49RKxp2/4Ln9J5XO5hwDz0UkgmEWdj qFGtJxZhvPvHbVoeE45Ihef4/N9pPVDZyAmWpKCGY6hcBubKQz/qRnhLIgF0wRjcSCAoaG 53S9KtpP4EctX/kY5Ny+HGKFwvGDc4Q= Date: Fri, 12 Jun 2026 23:21:22 +0800 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 Content-Language: en-US To: Usama Arif 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> X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Lance Yang In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_OUT On 2026/6/12 23:05, Usama Arif wrote: > > > 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. Cool. >> >> 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: Just skimmed it. That should do the trick. Will go through it properly in v3 :) Thanks, Lance > > 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; >