From patchwork Tue Apr 9 19:22:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13623122 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3314BC67861 for ; Tue, 9 Apr 2024 19:24:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C1DF36B009C; Tue, 9 Apr 2024 15:24:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BF5406B009D; Tue, 9 Apr 2024 15:24:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ABCEC6B009E; Tue, 9 Apr 2024 15:24:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 8CF776B009C for ; Tue, 9 Apr 2024 15:24:44 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 5E4CDA0520 for ; Tue, 9 Apr 2024 19:24:44 +0000 (UTC) X-FDA: 81990970488.22.65AEBCD Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf20.hostedemail.com (Postfix) with ESMTP id 9EE881C001B for ; Tue, 9 Apr 2024 19:24:42 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=dtJf3PMj; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf20.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712690682; a=rsa-sha256; cv=none; b=8EdHN/9o/PRgeQn0tKf0YgcBF6yIgHKXVVMiB5AAxPVNP6O9dN7DdVVIPyGEEmhHSp+1jz wjAzQnkhYQ7uM701TY+ArU3JL4KLbIFp3IaIf+AH5mcAdtt7oiIBeyNx/0/lPhp+GsmS89 HE3rg5OHq3H8cuQzgulPIb/Q2O8k9J8= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=dtJf3PMj; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf20.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712690682; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Uy2oRB9rRnFFfSxz/BmLFL5vpaMUgfQlIINZ/k73n3E=; b=o3U3jKjgJtcRoxif2nyrVH2HXrO1PfVEf4sgCB+VJVTX5EiVlJkMzmNmK/JqgZCICJKKlm vXMAAYiXWvWQxP2Lqu/gbIq/zAMnZksCdHqO0NdL6mFL26ZUvJFWU1emTHnti4aOzk9VSb hOHHoNiU10fdqBTvmV/tlP9hkgz8KRk= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712690682; 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=Uy2oRB9rRnFFfSxz/BmLFL5vpaMUgfQlIINZ/k73n3E=; b=dtJf3PMjv3+mkwEe37uNcgN5b9xzFYM5gwZWcnsY6ZXKakdk83cN8eEMzvbRDVRewZda1U msEjlHQy1wnVbxMJj4Kxj0cGi+88ymYhSfKRPiGRKeqvJgBHqwgNtYpRpfE51PrMIQ8OT9 95q2uuhpAQaBHZ0fJFhYhQrhukPwTEE= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-294-y1kOx-irPBuEYteLZHDYxQ-1; Tue, 09 Apr 2024 15:24:35 -0400 X-MC-Unique: y1kOx-irPBuEYteLZHDYxQ-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8046C380009F; Tue, 9 Apr 2024 19:24:33 +0000 (UTC) Received: from t14s.redhat.com (unknown [10.39.192.106]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1167E40B4979; Tue, 9 Apr 2024 19:24:22 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linux-doc@vger.kernel.org, cgroups@vger.kernel.org, linux-sh@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, David Hildenbrand , Andrew Morton , "Matthew Wilcox (Oracle)" , Peter Xu , Ryan Roberts , Yin Fengwei , Yang Shi , Zi Yan , Jonathan Corbet , Hugh Dickins , Yoshinori Sato , Rich Felker , John Paul Adrian Glaubitz , Chris Zankel , Max Filippov , Muchun Song , Miaohe Lin , Naoya Horiguchi , Richard Chang Subject: [PATCH v1 05/18] mm: improve folio_likely_mapped_shared() using the mapcount of large folios Date: Tue, 9 Apr 2024 21:22:48 +0200 Message-ID: <20240409192301.907377-6-david@redhat.com> In-Reply-To: <20240409192301.907377-1-david@redhat.com> References: <20240409192301.907377-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 9EE881C001B X-Stat-Signature: bcqcsrhhi4moi9z6iagmcnqofijxukwj X-HE-Tag: 1712690682-292842 X-HE-Meta: U2FsdGVkX18N5ePqHEXoCrGHh60YQagCT4JRvVjVURxDniaJ5TSzCjag8ZRTWG47DvhpRSe4pZdQVEVQcHzOJFQHOGWJE90BFOQeSs7wCLpYDU5seW0FQTfmDcJ5Mj7Nhh33bOf2PExbUoZ70OagcuROi1Z5s/dQbr5G4Qw1YiwAx4Vlq/vJnnmEMuuqRBSBpIGpdp9unbZEwpF3T3f4kkrPqNLzQ1KnoDVQr7ihDJjGjI4KToaC5vX9DymHPScSdNdEH2Fkl02FL6hws2xiXv3jeTfILMop6lbWA6pmftM+WsjTSXJW3MEgU85dB0UZpo2OKDO6n9Xl0d/PQ6r6/x0Z9tv1EayFzxT53oVSJolY1vj3d44CoAmMrppiwKOF1NJ2+soC+81gN/azNlh/H6LcRMI9oZKaHaqtYT9YBalqO7JBgMOzB6NVxClyJG9NfgxLH8eMaKofDhwNmgQy8oCkyNQ64garpX+HDOdXjHjrOxGcLH773021qY3PGHtgtiH2ma4tF1683J+JuhbzVKDVKHWS46+kNiXUrrPUS74KDLJUSIuRRvNw7Q1cs5GIbvqB3w0Z5ZsBvIeusrge1tQzXysuwYEXCwTDT/jyrXUZP5tcDRu4ksgzCH7pbK0pquVUyl9dST5bVhJajfowpppLwAuIfgGHUcVbt/65nKEo17I9SjbpvpLIbRbEvxtuDPdylYw5sCuSzJoAULeafXrCMQe+Rdm77TkCkZWrrfvk+qyrcIF+cl9iQHMK11OQz+YTcXZUcSwoOjJUSqcJRKcjgZe5NBXcoqD0L5TCCuxchLV5K6EBBd5CM+z8Vd9WxhTz+pxd0psdaejfwOflyTr8sTUVMiBZCzNgacX1Li5v0xvMWhh82oPNXYTczynSGWNhr1zV1OzShSkDpP97NKlJDfaQVUR+Frewq59yruDPKrwTURtmWmlXj3QvKzUnJsbvSQOCQJTfm18BatY 1SRJHo0A mpItasECDwOwx9mn7BH8YvOCl+JI3SVDDfloqJrivbzMOoJ86LfwqNEI2pVzYq/Ba4S9rEs7S+D9uQS/MguO13XHvXCcLnZ/6CpXvHwWZLohsdCWRmKDMIg1ZlYxpfYsjvePJy5bTo3VjqdhHwQDt7nS8vshW6is4AGXmjoDheuM4vTTk8EvkvCmQWmH2RKw0Ai0XJJYGOoL4zLYy5o7YQLbBgFI+I5YTxf1cg1i/2ijvcTZTdb0IGPxrXUBxjTDFiIlS X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: We can now read the mapcount of large folios very efficiently. Use it to improve our handling of partially-mappable folios, falling back to making a guess only in case the folio is not "obviously mapped shared". We can now better detect partially-mappable folios where the first page is not mapped as "mapped shared", reducing "false negatives"; but false negatives are still possible. While at it, fixup a wrong comment (false positive vs. false negative) for KSM folios. Signed-off-by: David Hildenbrand Reviewed-by: Yin Fengwei --- include/linux/mm.h | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 1862a216af15..daf687f0e8e5 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2183,7 +2183,7 @@ static inline size_t folio_size(struct folio *folio) * indicate "mapped shared" (false positive) when two VMAs in the same MM * cover the same file range. * #. For (small) KSM folios, the return value can wrongly indicate "mapped - * shared" (false negative), when the folio is mapped multiple times into + * shared" (false positive), when the folio is mapped multiple times into * the same MM. * * Further, this function only considers current page table mappings that @@ -2200,7 +2200,22 @@ static inline size_t folio_size(struct folio *folio) */ static inline bool folio_likely_mapped_shared(struct folio *folio) { - return page_mapcount(folio_page(folio, 0)) > 1; + int mapcount = folio_mapcount(folio); + + /* Only partially-mappable folios require more care. */ + if (!folio_test_large(folio) || unlikely(folio_test_hugetlb(folio))) + return mapcount > 1; + + /* A single mapping implies "mapped exclusively". */ + if (mapcount <= 1) + return false; + + /* If any page is mapped more than once we treat it "mapped shared". */ + if (folio_entire_mapcount(folio) || mapcount > folio_nr_pages(folio)) + return true; + + /* Let's guess based on the first subpage. */ + return atomic_read(&folio->_mapcount) > 0; } #ifndef HAVE_ARCH_MAKE_PAGE_ACCESSIBLE