Received: by 2002:a05:7208:31d3:b0:81:e143:7c29 with SMTP id v19csp2633467rbd; Tue, 9 Apr 2024 12:25:39 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVCLYcc2CQM6IfbNbkKE7gcL7r0Z14miaDYRRG43al5VKL4bDfkYISxVeuAJnWJC4RSD2xnSX4OiNYcDl5R95KvWXVN2+WlfN6Vx3Y8cw== X-Google-Smtp-Source: AGHT+IEvtqjrG+mGWYsGqm7xVUU7Nl1+kUTNFG0be6p6XU9C5qAx7gOd9nWAXcfV0vdJrd2GmBYA X-Received: by 2002:a17:906:3bd1:b0:a46:65fd:969d with SMTP id v17-20020a1709063bd100b00a4665fd969dmr195100ejf.71.1712690739196; Tue, 09 Apr 2024 12:25:39 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712690739; cv=pass; d=google.com; s=arc-20160816; b=qgqVT3Vz9M23siHKJhuQObJ2bVXvIVaFGKbdMfeTmoqL5GH9urh50LR/voOJUS6zGo GjshmA9/26+AoEwJh/F2R4ZQZ74NMRkuXwFdC6oPj5R8T7DegzSAsnl5annAtxZSMpI1 ICE0jLHM9pIc3X9R8QgPv+MQk8R4X7JQcvUrrW1Q+a2JmqFQ1v+xh06lAvjooLFcESGn UQxgsl4K7CN2eFxllmaM3j5CmJKUTwfhGzW+Y867xw13lymTXTq2deuH1aOB9zvpXnXS IP9fdszVfBNVTO9G3YrYicbvzZjOArqbS5gmjaL8y7DIh+xVr5Vy7PWVHhB5An24pYdz 9CdA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=Uy2oRB9rRnFFfSxz/BmLFL5vpaMUgfQlIINZ/k73n3E=; fh=6UXOkRl8PLWnLRlJFkZ1FKxRpZrfO06mwvecZ/lFyqY=; b=oEBl4tSE0KJZnVpAwKxrSOUIaS5IIJuK4zrY2DCqPGquCQWfFPqr7jgoX8GIy+kJmL J0svvqep2J9rtxNGNOB99LY2UlqL5mx3SKcvNkiZopHdmC4/ZpWqAq05BJJ5tdGKugvd zS++UHw56+LKj1LvNGAe4PbUGR6DOLBSQdIOmFObCvl2QGQFyr/ZoF/xqJ1GtE3g/Uo6 I5grChsWbM9vglJxc3hxT6+MLQ/UWlwfTc7sMinAiqWM8QG+NZJabi+4yQPNIpoouaZq VE6odX6iEiWBdd8IfgR6MNAJWFNs29EVMMifxUwyznyh7UE38ZvVe5JjNhP0F9VnI2yN zPDg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=AGaeMGsV; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-137521-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-137521-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id hs40-20020a1709073ea800b00a51deebdcbcsi1869162ejc.410.2024.04.09.12.25.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Apr 2024 12:25:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-137521-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=AGaeMGsV; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-137521-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-137521-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.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 B225B1F22C32 for ; Tue, 9 Apr 2024 19:25:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 58C931581EB; Tue, 9 Apr 2024 19:24:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="AGaeMGsV" 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 AFE45157A79 for ; Tue, 9 Apr 2024 19:24:41 +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=1712690684; cv=none; b=E3cvxiALuQS9M1VQXzr0lqqQsUmOZzovnEUyVDC+2Yrwlp6sf67GP5bvzf2VlB8tNkT3nZNvLZPWAhmxGMgGL4aK50PWzqJ1vO74SIUtxx+ZA/IFSJFxdoVUuXXT+0LbYRSJyMiMxURncygzFmIrvQpNF24hYQUV4DinJ5CfZuA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712690684; c=relaxed/simple; bh=6G3a0Az5RUxYjW0MeAONrOSx900CkrCCjkjSacXNONc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nGKRNX6dGylfXrNDxvZ9EhObuzlwtpJC4Kl7x4E8ciQvgNzIjtoclNjeDZB5ujDGm1ZHllQ+Eg+gWN0nUQbamw7g8yNc1OIiGZ0PbA3+RrtH3kzJDj3eaZ/EMgGMqju2xeK6ufKqiPAwvG52k2LezVBEfgwQ3Z8hf/mDU04YnGs= 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=AGaeMGsV; 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712690680; 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=AGaeMGsV7044dtgbDXM7W4kg6MCu4pxSitZzG06rpN/NNR99Xz4HEyBZExE1FCSQxk9u0f q3ayjbugyt61H/Q8bDxpdPsVjWvtOkQJr0S+Or8p6AJD4kbo/S/nZ1d2m7BSOrCvcNt46I WilM2XIeESYXG+/UOkdGbcdB2K3gids= 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 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 --- 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 -- 2.44.0