Received: by 2002:ab2:2997:0:b0:1ec:cbc4:63fb with SMTP id n23csp372579lqb; Thu, 29 Feb 2024 03:45:18 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCV+CJKCgG72ViXdIB3YuYBIPUFb+LLAISrMcprioVsA4ZI/bIjKHsH1zQL+UlM7KKsosI9iXNZteBtffWZf38Q7uB4bUHhVBHcAXb8Csw== X-Google-Smtp-Source: AGHT+IG61NYWkArHkMJfp4U0jv8K0Nl7wW9ZZNfZ6sGK6rChjLLf0eOFlXTq3c4+yykuK9L+J+NJ X-Received: by 2002:a17:906:884:b0:a44:16e:2618 with SMTP id n4-20020a170906088400b00a44016e2618mr1365821eje.48.1709207118585; Thu, 29 Feb 2024 03:45:18 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709207118; cv=pass; d=google.com; s=arc-20160816; b=wZ4iOUt38BqaAVcPR0/BaiWyF5bGwpCMON4KR8f3ZIeZHXhP3DV5BpWuZWNxW+AqtH pfksJuLd3UI6do+Py2vIdVQ6qj2ALvKZ84mMYtKHqfDCdxJoDGL1XTyOuL8019RBB5+t 4pv6OXlzQ5hee6LKKfST66ITgVpJPLWpQ1p+Ygr7dJd7KcUjr8vmttMnDFN1huzXaSzf JqbLy6LeeGon2l2HW+SaHDHDIAwfC0HTAD7nZa8eJQaaEbKvTFb5F0Loc6K9pi3Uguy8 FolohSm3uas/I8sxD//DeqhhWK0dZp1aCdcP8AJLNzW/y6NinOaoPT5kC/+vFaD1MmHU Ysgw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=N0ZpY65VFg+E6A8qBifiwnosnbkH2ehqTRFWLh41Zio=; fh=+CFHRYSq3N02kCKYwOFPJkBd8sCtb5/TmZ2E4dJTFE0=; b=erB4bs9jMHFxs56fdkMUMK+En5By4CJqsAh3sy3R7167AgtKQSiO56Rcf7ap8mKwZm Zshj3mSotS/DDIg0mx1FMyZkFeFhT03jAmsJr6dBRl+bUoc6uLt0aA9DztG/f7EbhkDP f8x7pRhYoUzAYnerj2caRLtE73E79Hbljc+7O4Y/Tn+xox55xJTfvQ1GxGQqjPfnD+ft ZexcEsVEydLNEh4S+BbIAEvEcF9iJWnDTDnz3oFdrZ80OwNNj6gdq+o6wcOoM8AKXle5 nJ5D3k08hthq6YAlL0YMnPJowmDuRcB/zJJ9bsN1gq6bUJziJxfvP5jlT4MP5sqpgaa2 qqOQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-86646-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-86646-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id c13-20020a170906528d00b00a3e4577ac9dsi504163ejm.859.2024.02.29.03.45.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Feb 2024 03:45:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-86646-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-86646-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-86646-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 27B481F23FDA for ; Thu, 29 Feb 2024 11:45:18 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 11A8F7C6CD; Thu, 29 Feb 2024 11:45:11 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B04DE7C6C1 for ; Thu, 29 Feb 2024 11:45:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709207110; cv=none; b=rDdzy/ZrsE8kdXBtJd2iUbzrsFaRT+IYl4zaTFUwvLR4ewdG+jrI3jubp1fnOqjphn6pDd0dNwpl/UEBgOgKd2Crj39mxcgMrGSUv4/yUnMHAtZBRqmAC/Jsi2C5wsg1cvgqjtbAQI0P+l/SqjIYXtEqvjv3+WTL/Uct/mO0gJw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709207110; c=relaxed/simple; bh=ZMvT27zZmRa7OAu43iihzVGaDwsXtfM9wdl37+5gSbU=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=jGvjwEG84laA5LTIrUNDROdYq1E8tgwO8Y/QUe4eY/GEJTgaqDbZfkUgdHbKtf2Qq+u6h3R3mRs9W85Lrmzas/+R1ERBr5P0I4jsH2Ju0uxv8ngZBTdS9l3skmIVZAM4K51r3UuILNr4+VsAQw4ah5lD19qlbiMprPs2+nMVMHw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 019FE1FB; Thu, 29 Feb 2024 03:45:46 -0800 (PST) Received: from [10.57.68.58] (unknown [10.57.68.58]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 225483F762; Thu, 29 Feb 2024 03:45:05 -0800 (PST) Message-ID: <58c4726d-73ec-40e5-8f1d-e00c37532af9@arm.com> Date: Thu, 29 Feb 2024 11:45:04 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v1] mm: convert folio_estimated_sharers() to folio_likely_mapped_shared() Content-Language: en-GB To: David Hildenbrand , linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, Barry Song , Vishal Moola References: <20240227201548.857831-1-david@redhat.com> From: Ryan Roberts In-Reply-To: <20240227201548.857831-1-david@redhat.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 27/02/2024 20:15, David Hildenbrand wrote: > Callers of folio_estimated_sharers() only care about "mapped shared vs. > mapped exclusively", not the exact estimate of sharers. Let's consolidate > and unify the condition users are checking. While at it clarify the > semantics and extend the discussion on the fuzziness. > > Use the "likely mapped shared" terminology to better express what the > (adjusted) function actually checks. > > Whether a partially-mappable folio is more likely to not be partially > mapped than partially mapped is debatable. In the future, we might be able > to improve our estimate for partially-mappable folios, though. > > Note that we will now consistently detect "mapped shared" only if the > first subpage is actually mapped multiple times. When the first subpage > is not mapped, we will consistently detect it as "mapped exclusively". > This change should currently only affect the usage in > madvise_free_pte_range() and queue_folios_pte_range() for large folios: if > the first page was already unmapped, we would have skipped the folio. > > Cc: Barry Song > Cc: Vishal Moola (Oracle) > Cc: Ryan Roberts > Signed-off-by: David Hildenbrand Reviewed-by: Ryan Roberts > --- > include/linux/mm.h | 46 ++++++++++++++++++++++++++++++++++++---------- > mm/huge_memory.c | 2 +- > mm/madvise.c | 6 +++--- > mm/memory.c | 2 +- > mm/mempolicy.c | 14 ++++++-------- > mm/migrate.c | 8 ++++---- > 6 files changed, 51 insertions(+), 27 deletions(-) > > diff --git a/include/linux/mm.h b/include/linux/mm.h > index 6f4825d829656..795c89632265f 100644 > --- a/include/linux/mm.h > +++ b/include/linux/mm.h > @@ -2147,21 +2147,47 @@ static inline size_t folio_size(struct folio *folio) > } > > /** > - * folio_estimated_sharers - Estimate the number of sharers of a folio. > + * folio_likely_mapped_shared - Estimate if the folio is mapped into the page > + * tables of more than one MM > * @folio: The folio. > * > - * folio_estimated_sharers() aims to serve as a function to efficiently > - * estimate the number of processes sharing a folio. This is done by > - * looking at the precise mapcount of the first subpage in the folio, and > - * assuming the other subpages are the same. This may not be true for large > - * folios. If you want exact mapcounts for exact calculations, look at > - * page_mapcount() or folio_total_mapcount(). > + * This function checks if the folio is currently mapped into more than one > + * MM ("mapped shared"), or if the folio is only mapped into a single MM > + * ("mapped exclusively"). > * > - * Return: The estimated number of processes sharing a folio. > + * As precise information is not easily available for all folios, this function > + * estimates the number of MMs ("sharers") that are currently mapping a folio > + * using the number of times the first page of the folio is currently mapped > + * into page tables. > + * > + * For small anonymous folios (except KSM folios) and anonymous hugetlb folios, > + * the return value will be exactly correct, because they can only be mapped > + * at most once into an MM, and they cannot be partially mapped. > + * > + * For other folios, the result can be fuzzy: > + * (a) For partially-mappable large folios (THP), the return value can wrongly > + * indicate "mapped exclusively" (false negative) when the folio is > + * only partially mapped into at least one MM. > + * (b) For pagecache folios (including hugetlb), the return value can wrongly > + * indicate "mapped shared" (false positive) when two VMAs in the same MM > + * cover the same file range. > + * (c) For (small) KSM folios, the return value can wrongly indicate "mapped > + * shared" (false negative), when the folio is mapped multiple times into > + * the same MM. > + * > + * Further, this function only considers current page table mappings that > + * are tracked using the folio mapcount(s). It does not consider: > + * (1) If the folio might get mapped in the (near) future (e.g., swapcache, > + * pagecache, temporary unmapping for migration). > + * (2) If the folio is mapped differently (VM_PFNMAP). > + * (3) If hugetlb page table sharing applies. Callers might want to check > + * hugetlb_pmd_shared(). > + * > + * Return: Whether the folio is estimated to be mapped into more than one MM. > */ > -static inline int folio_estimated_sharers(struct folio *folio) > +static inline bool folio_likely_mapped_shared(struct folio *folio) > { > - return page_mapcount(folio_page(folio, 0)); > + return page_mapcount(folio_page(folio, 0)) > 1; > } > > #ifndef HAVE_ARCH_MAKE_PAGE_ACCESSIBLE > diff --git a/mm/huge_memory.c b/mm/huge_memory.c > index 50d146eb248ff..4d10904fef70c 100644 > --- a/mm/huge_memory.c > +++ b/mm/huge_memory.c > @@ -1829,7 +1829,7 @@ bool madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, > * If other processes are mapping this folio, we couldn't discard > * the folio unless they all do MADV_FREE so let's skip the folio. > */ > - if (folio_estimated_sharers(folio) != 1) > + if (folio_likely_mapped_shared(folio)) > goto out; > > if (!folio_trylock(folio)) > diff --git a/mm/madvise.c b/mm/madvise.c > index 44a498c94158c..32a534d200219 100644 > --- a/mm/madvise.c > +++ b/mm/madvise.c > @@ -366,7 +366,7 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, > folio = pfn_folio(pmd_pfn(orig_pmd)); > > /* Do not interfere with other mappings of this folio */ > - if (folio_estimated_sharers(folio) != 1) > + if (folio_likely_mapped_shared(folio)) > goto huge_unlock; > > if (pageout_anon_only_filter && !folio_test_anon(folio)) > @@ -453,7 +453,7 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, > if (folio_test_large(folio)) { > int err; > > - if (folio_estimated_sharers(folio) > 1) > + if (folio_likely_mapped_shared(folio)) > break; > if (pageout_anon_only_filter && !folio_test_anon(folio)) > break; > @@ -677,7 +677,7 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr, > if (folio_test_large(folio)) { > int err; > > - if (folio_estimated_sharers(folio) != 1) > + if (folio_likely_mapped_shared(folio)) > break; > if (!folio_trylock(folio)) > break; > diff --git a/mm/memory.c b/mm/memory.c > index 1c45b6a42a1b9..8394a9843ca06 100644 > --- a/mm/memory.c > +++ b/mm/memory.c > @@ -5173,7 +5173,7 @@ static vm_fault_t do_numa_page(struct vm_fault *vmf) > * Flag if the folio is shared between multiple address spaces. This > * is later used when determining whether to group tasks together > */ > - if (folio_estimated_sharers(folio) > 1 && (vma->vm_flags & VM_SHARED)) > + if (folio_likely_mapped_shared(folio) && (vma->vm_flags & VM_SHARED)) > flags |= TNF_SHARED; > > nid = folio_nid(folio); > diff --git a/mm/mempolicy.c b/mm/mempolicy.c > index f60b4c99f1302..0b92fde395182 100644 > --- a/mm/mempolicy.c > +++ b/mm/mempolicy.c > @@ -642,12 +642,11 @@ static int queue_folios_hugetlb(pte_t *pte, unsigned long hmask, > * Unless MPOL_MF_MOVE_ALL, we try to avoid migrating a shared folio. > * Choosing not to migrate a shared folio is not counted as a failure. > * > - * To check if the folio is shared, ideally we want to make sure > - * every page is mapped to the same process. Doing that is very > - * expensive, so check the estimated sharers of the folio instead. > + * See folio_likely_mapped_shared() on possible imprecision when we > + * cannot easily detect if a folio is shared. > */ > if ((flags & MPOL_MF_MOVE_ALL) || > - (folio_estimated_sharers(folio) == 1 && !hugetlb_pmd_shared(pte))) > + (!folio_likely_mapped_shared(folio) && !hugetlb_pmd_shared(pte))) > if (!isolate_hugetlb(folio, qp->pagelist)) > qp->nr_failed++; > unlock: > @@ -1032,11 +1031,10 @@ static bool migrate_folio_add(struct folio *folio, struct list_head *foliolist, > * Unless MPOL_MF_MOVE_ALL, we try to avoid migrating a shared folio. > * Choosing not to migrate a shared folio is not counted as a failure. > * > - * To check if the folio is shared, ideally we want to make sure > - * every page is mapped to the same process. Doing that is very > - * expensive, so check the estimated sharers of the folio instead. > + * See folio_likely_mapped_shared() on possible imprecision when we > + * cannot easily detect if a folio is shared. > */ > - if ((flags & MPOL_MF_MOVE_ALL) || folio_estimated_sharers(folio) == 1) { > + if ((flags & MPOL_MF_MOVE_ALL) || !folio_likely_mapped_shared(folio)) { > if (folio_isolate_lru(folio)) { > list_add_tail(&folio->lru, foliolist); > node_stat_mod_folio(folio, > diff --git a/mm/migrate.c b/mm/migrate.c > index 73a052a382f13..35d376969f8b9 100644 > --- a/mm/migrate.c > +++ b/mm/migrate.c > @@ -2568,11 +2568,11 @@ int migrate_misplaced_folio(struct folio *folio, struct vm_area_struct *vma, > /* > * Don't migrate file folios that are mapped in multiple processes > * with execute permissions as they are probably shared libraries. > - * To check if the folio is shared, ideally we want to make sure > - * every page is mapped to the same process. Doing that is very > - * expensive, so check the estimated mapcount of the folio instead. > + * > + * See folio_likely_mapped_shared() on possible imprecision when we > + * cannot easily detect if a folio is shared. > */ > - if (folio_estimated_sharers(folio) != 1 && folio_is_file_lru(folio) && > + if (folio_likely_mapped_shared(folio) && folio_is_file_lru(folio) && > (vma->vm_flags & VM_EXEC)) > goto out; >