From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 2B6AD198822 for ; Mon, 24 Feb 2025 21:10:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740431424; cv=none; b=MjNJxoAeuESduE3BqRGl5CkjMqS6KtBlzoJ8Rt86Y2H9rqiIpIroxW8d63A1ma9CLAjG5VqHHVUqTJwZ7/8JHLXzhzylgx8v7JMTO9dTI1g//Uy2csbYuTuSh+wKhFSt9AZDJEmXxIOy8V1qroLgf2TLlm8lnCKy7m8/jV4paZs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740431424; c=relaxed/simple; bh=8ShZRzfgtCGLnrqMvTxM31tyjSi2afUVsge5F4qJXAE=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=KKCW7wuPOUHoromuyzlNFRLTXY09w65YPo1c0vqlNug08o/1dUVfcDRAaLicBeqdB45W28PFwFuyUi3RF5ipcHX3uIKCSwS0wDXWkVPrJb/yIMUO/UtYg4Z5QQzkKLiGD+QI5FAuL9fCnv1iRvRMca8iYh0Bi/jyWZwXB0avLKo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=OhcrhRoq; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="OhcrhRoq" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740431422; 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:autocrypt:autocrypt; bh=fzSTxledaSV06nALv2dcRKnCIBvTt88ZTAw50245vSk=; b=OhcrhRoqgfruin7bTHpf0JGl43sIspaIIMF2Y9bfKm9bc4C09td/3+Pr5oeKwOKJ6158KT EZv+zb+Jnlkzm5S75cFSnKDjCNZzSmkugg55FLJTvBkTgKv6j2NPw6B3m9V0MHFb2dQbZb Et5SnJ13Y7eGFJpFHIIXoO5WUJIM2nU= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-500-4ea9ILXmOlCPpBVLUN2y5w-1; Mon, 24 Feb 2025 16:10:20 -0500 X-MC-Unique: 4ea9ILXmOlCPpBVLUN2y5w-1 X-Mimecast-MFC-AGG-ID: 4ea9ILXmOlCPpBVLUN2y5w_1740431419 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-38f455a8e43so1848506f8f.0 for ; Mon, 24 Feb 2025 13:10:20 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740431419; x=1741036219; h=content-transfer-encoding:in-reply-to:organization:autocrypt :content-language:references:cc:to:from:subject:user-agent :mime-version:date:message-id:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=fzSTxledaSV06nALv2dcRKnCIBvTt88ZTAw50245vSk=; b=LrjTrC/67KkrGvAjTvv+QMuWgLXDmmvM1INO64/lLDAhiyCK/pYLrf8QOqAEylxlvF 3DehgdfhRaNAK6P4XSbNf9ur3pWBVI+8pp5ZEDBdBs/PCC1jn0f32atzbyeKqTvIenTz skhnfyD6OrTGEgN0Oqsi1AEJDVBFKlaxAgZgl2jZIDNIjS7KBRE+AV0zRXvKRXkHElJL GKYT4c4pUeRQvCJmg0p/Phx7gO6u2ba6GAHmDZBvFXHawHCfaFF0pQD6pbC2p3MADkLC NBj7tf4k+2pdZnOVAYe1QjvJ66tEzvR1bB0CjkF4Ab96/DzwxWTOpe6KttVoSqojbo4d klTw== X-Gm-Message-State: AOJu0YxWU60/ihr71CfMmxWU0Wz5WXarf5L07ayd/omkRpv6CeWTzBHB P9SAUvKvYE2J3atk5FoAxZgpvP1l1ADhSL8ZUzhXn2JKsNyI2AxTwS1Qb1mPF6sqabFTC/e8UFb f1plJO7mcYGZXgw8Wv3HjTJ7kW0kx0C8f91PLEB+1BmTHAqDlZg43mjcnYA== X-Gm-Gg: ASbGncvnNtvyoOdgNc7pVBf/sj9tvRve6228j2Yq68ReLSQXH9Cd/Wqv9LwFvsXYHsR 4d4PMWguhB4ILpAGpp16G2fsXmO/QjcuzLZ6tgaesKr0mRWqUCqzalI1Jne9iI9K8b9Gb+DCz1L lxlLw7NanT8P/bVmBSlbbgX0X0SmNijWVzStoWffY4ICo371ffM7YrgeoydTOB2vI4IMC9pB8u6 0zaDU0e1YylPjTdBoma3zEUyMG3rqUAuu6a9gtqYPM3U7567HvdDM9+opV10e0I9duZWrAMNlt1 ifmYHrUiM3H94tUTfkZnHRvQ2XXEy8jaDitLGdpB+Y23HAaWdChDqrJymRbHTBy8c1sa77L2HnS hpK7wgBdK916JZp0xoZkxijyeyiBA8broNnNfV9qff9E= X-Received: by 2002:a05:6000:154b:b0:38f:2726:bc0e with SMTP id ffacd0b85a97d-390cc632329mr508255f8f.44.1740431419241; Mon, 24 Feb 2025 13:10:19 -0800 (PST) X-Google-Smtp-Source: AGHT+IH0pfDUvAW4Xj762TXmpEfVTdueKZApc8ywd+tnC/XOKYVKmUOws9JdRuwbCwap5XWge/5ONw== X-Received: by 2002:a05:6000:154b:b0:38f:2726:bc0e with SMTP id ffacd0b85a97d-390cc632329mr508228f8f.44.1740431418811; Mon, 24 Feb 2025 13:10:18 -0800 (PST) Received: from ?IPV6:2003:cb:c735:1900:ac8b:7ae5:991f:54fc? (p200300cbc7351900ac8b7ae5991f54fc.dip0.t-ipconnect.de. [2003:cb:c735:1900:ac8b:7ae5:991f:54fc]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-439b02ce60asm119493485e9.7.2025.02.24.13.10.15 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 24 Feb 2025 13:10:17 -0800 (PST) Message-ID: <9466df6c-b169-4b98-8721-5722ff4284a6@redhat.com> Date: Mon, 24 Feb 2025 22:10:15 +0100 Precedence: bulk X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2 16/20] fs/proc/page: remove per-page mapcount dependency for /proc/kpagecount (CONFIG_NO_PAGE_MAPCOUNT) From: David Hildenbrand To: Zi Yan , linux-kernel@vger.kernel.org Cc: linux-doc@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, Andrew Morton , "Matthew Wilcox (Oracle)" , Tejun Heo , Zefan Li , Johannes Weiner , =?UTF-8?Q?Michal_Koutn=C3=BD?= , Jonathan Corbet , Andy Lutomirski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Muchun Song , "Liam R. Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn , owner-linux-mm@kvack.org References: <20250224165603.1434404-1-david@redhat.com> <20250224165603.1434404-17-david@redhat.com> <8a5e94a2-8cd7-45f5-a2be-525242c0cd16@redhat.com> Content-Language: en-US Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr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Kv AxBQXWeXhJI9XQssfrf4Gusdqx8nPEpfOqCtbbwJMATbHyqLt7/oz/5deGuwxgb65pWIzufa N7eop7uh+6bezi+rugUI+w6DABEBAAHCwXwEGAEIACYCGwwWIQQb2cqtc1xMOkYN/MpN3hD3 AP+DWgUCXw7HsgUJEqkpoQAKCRBN3hD3AP+DWrrpD/4qS3dyVRxDcDHIlmguXjC1Q5tZTwNB boaBTPHSy/Nksu0eY7x6HfQJ3xajVH32Ms6t1trDQmPx2iP5+7iDsb7OKAb5eOS8h+BEBDeq 3ecsQDv0fFJOA9ag5O3LLNk+3x3q7e0uo06XMaY7UHS341ozXUUI7wC7iKfoUTv03iO9El5f XpNMx/YrIMduZ2+nd9Di7o5+KIwlb2mAB9sTNHdMrXesX8eBL6T9b+MZJk+mZuPxKNVfEQMQ a5SxUEADIPQTPNvBewdeI80yeOCrN+Zzwy/Mrx9EPeu59Y5vSJOx/z6OUImD/GhX7Xvkt3kq Er5KTrJz3++B6SH9pum9PuoE/k+nntJkNMmQpR4MCBaV/J9gIOPGodDKnjdng+mXliF3Ptu6 3oxc2RCyGzTlxyMwuc2U5Q7KtUNTdDe8T0uE+9b8BLMVQDDfJjqY0VVqSUwImzTDLX9S4g/8 kC4HRcclk8hpyhY2jKGluZO0awwTIMgVEzmTyBphDg/Gx7dZU1Xf8HFuE+UZ5UDHDTnwgv7E th6RC9+WrhDNspZ9fJjKWRbveQgUFCpe1sa77LAw+XFrKmBHXp9ZVIe90RMe2tRL06BGiRZr jPrnvUsUUsjRoRNJjKKA/REq+sAnhkNPPZ/NNMjaZ5b8Tovi8C0tmxiCHaQYqj7G2rgnT0kt WNyWQQ== Organization: Red Hat In-Reply-To: <8a5e94a2-8cd7-45f5-a2be-525242c0cd16@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 24.02.25 22:02, David Hildenbrand wrote: > On 24.02.25 21:40, Zi Yan wrote: >> On Mon Feb 24, 2025 at 11:55 AM EST, David Hildenbrand wrote: >>> Let's implement an alternative when per-page mapcounts in large folios >>> are no longer maintained -- soon with CONFIG_NO_PAGE_MAPCOUNT. >>> >>> For large folios, we'll return the per-page average mapcount within the >>> folio, except when the average is 0 but the folio is mapped: then we >>> return 1. >>> >>> For hugetlb folios and for large folios that are fully mapped >>> into all address spaces, there is no change. >>> >>> As an alternative, we could simply return 0 for non-hugetlb large folios, >>> or disable this legacy interface with CONFIG_NO_PAGE_MAPCOUNT. >>> >>> But the information exposed by this interface can still be valuable, and >>> frequently we deal with fully-mapped large folios where the average >>> corresponds to the actual page mapcount. So we'll leave it like this for >>> now and document the new behavior. >>> >>> Note: this interface is likely not very relevant for performance. If >>> ever required, we could try doing a rather expensive rmap walk to collect >>> precisely how often this folio page is mapped. >>> >>> Signed-off-by: David Hildenbrand >>> --- >>> Documentation/admin-guide/mm/pagemap.rst | 7 +++++- >>> fs/proc/internal.h | 31 ++++++++++++++++++++++++ >>> fs/proc/page.c | 19 ++++++++++++--- >>> 3 files changed, 53 insertions(+), 4 deletions(-) >>> >>> diff --git a/Documentation/admin-guide/mm/pagemap.rst b/Documentation/admin-guide/mm/pagemap.rst >>> index caba0f52dd36c..49590306c61a0 100644 >>> --- a/Documentation/admin-guide/mm/pagemap.rst >>> +++ b/Documentation/admin-guide/mm/pagemap.rst >>> @@ -42,7 +42,12 @@ There are four components to pagemap: >>> skip over unmapped regions. >>> >>> * ``/proc/kpagecount``. This file contains a 64-bit count of the number of >>> - times each page is mapped, indexed by PFN. >>> + times each page is mapped, indexed by PFN. Some kernel configurations do >>> + not track the precise number of times a page part of a larger allocation >>> + (e.g., THP) is mapped. In these configurations, the average number of >>> + mappings per page in this larger allocation is returned instead. However, >>> + if any page of the large allocation is mapped, the returned value will >>> + be at least 1. >>> >>> The page-types tool in the tools/mm directory can be used to query the >>> number of times a page is mapped. >>> diff --git a/fs/proc/internal.h b/fs/proc/internal.h >>> index 1695509370b88..16aa1fd260771 100644 >>> --- a/fs/proc/internal.h >>> +++ b/fs/proc/internal.h >>> @@ -174,6 +174,37 @@ static inline int folio_precise_page_mapcount(struct folio *folio, >>> return mapcount; >>> } >>> >>> +/** >>> + * folio_average_page_mapcount() - Average number of mappings per page in this >>> + * folio >>> + * @folio: The folio. >>> + * >>> + * The average number of present user page table entries that reference each >>> + * page in this folio as tracked via the RMAP: either referenced directly >>> + * (PTE) or as part of a larger area that covers this page (e.g., PMD). >>> + * >>> + * Returns: The average number of mappings per page in this folio. 0 for >>> + * folios that are not mapped to user space or are not tracked via the RMAP >>> + * (e.g., shared zeropage). >>> + */ >>> +static inline int folio_average_page_mapcount(struct folio *folio) >>> +{ >>> + int mapcount, entire_mapcount; >>> + unsigned int adjust; >>> + >>> + if (!folio_test_large(folio)) >>> + return atomic_read(&folio->_mapcount) + 1; >>> + >>> + mapcount = folio_large_mapcount(folio); >>> + entire_mapcount = folio_entire_mapcount(folio); >>> + if (mapcount <= entire_mapcount) >>> + return entire_mapcount; >>> + mapcount -= entire_mapcount; >>> + >>> + adjust = folio_large_nr_pages(folio) / 2; > > Thanks for the review! > >> >> Is there any reason for choosing this adjust number? A comment might be >> helpful in case people want to change it later, either with some reasoning >> or just saying it is chosen empirically. > > We're dividing by folio_large_nr_pages(folio) (shifting by > folio_large_order(folio)), so this is not a magic number at all. > > So this should be "ordinary" rounding. > > Assume nr_pages = 512. > > With 255 we want to round down, with 256 we want to round up. > > 255 / 512 = 0 :) > 256 / 512 = 0 :( > > Compared to: > > (255 + (512 / 2)) / 512 = (255 + 256) / 512 = 0 :) > (256 + (512 / 2)) / 512 = (256 + 256) / 512 = 1 :) I think adding to the function doc: "The average is calculated by rounding to the nearest integer." might make it clearer. -- Cheers, David / dhildenb