From patchwork Tue Apr 16 17:25:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13632252 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 43C95C04FF6 for ; Tue, 16 Apr 2024 17:26:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CE0286B0095; Tue, 16 Apr 2024 13:26:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C90C16B0096; Tue, 16 Apr 2024 13:26:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B58776B0098; Tue, 16 Apr 2024 13:26:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 96DFD6B0095 for ; Tue, 16 Apr 2024 13:26:02 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 540B1140A54 for ; Tue, 16 Apr 2024 17:26:02 +0000 (UTC) X-FDA: 82016072964.05.FEB1E2D Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf18.hostedemail.com (Postfix) with ESMTP id A60601C0013 for ; Tue, 16 Apr 2024 17:26:00 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Qs9uoDsp; spf=pass (imf18.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1713288360; a=rsa-sha256; cv=none; b=1XB/HwMhlfcDbFwbgFmbJCB1spCpaXKs8UDPbtuq9AGCP+ddOrgWVNqAeS90EXf6k7/wxn gsyGOGXmikxip222RiVVQY9rjvEKyiFgi3WNhhMETjQpKnNt98ukZCYE4/ptQj3prJl4on xXwIjscJ3X6FbQl6KQjAG14GrvzwVWw= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Qs9uoDsp; spf=pass (imf18.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1713288360; 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:references:dkim-signature; bh=1onEK+mjIGOBc7x6Ypb0bDNESgiVTW/1kwHRGIFFvkM=; b=jDWCdqv5D5d4fl9Mf3qh28c6XB3V4NCdu5p95OarDZNy3CSI6e11ElOCVq7qnM0MQm6jyv kqQ0PqnsFC/entXeoV9nZE4sZhjCSsRVIDt3J18NqaRBmCM5oe2Shb2t3C5VB8oCBhiKjR AsPyv4g2YOK55MoPDKTpy0GzMaS0e9Y= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1713288360; 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; bh=1onEK+mjIGOBc7x6Ypb0bDNESgiVTW/1kwHRGIFFvkM=; b=Qs9uoDsp9tEIyBIOTxvgnD3ivcUEeLnkhHw5IJPm8S8RVMdIK/BknkAi3Xu6+eNDiFWMDf Bd6AWhUPnGP2+MoCZ1GiIbBwRpc/Uqz7sKb8rPSsqk2zcPypgp85Rw/biK8H+IfqHdXPDn 3H6CeTEZxeBfFU90yoa8/F0dPEToZow= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-46-CG4LeTsyOi2kOFzEDet5uA-1; Tue, 16 Apr 2024 13:25:56 -0400 X-MC-Unique: CG4LeTsyOi2kOFzEDet5uA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (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 2837A811001; Tue, 16 Apr 2024 17:25:56 +0000 (UTC) Received: from t14s.redhat.com (unknown [10.39.192.80]) by smtp.corp.redhat.com (Postfix) with ESMTP id DB2A949106; Tue, 16 Apr 2024 17:25:53 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Alex Shi Subject: [PATCH v1] mm/ksm: remove page_mapcount() usage in stable_tree_search() Date: Tue, 16 Apr 2024 19:25:33 +0200 Message-ID: <20240416172533.663418-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.1 X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: A60601C0013 X-Stat-Signature: 6cox4honn4cs1zeryg5z5n5yat16wza3 X-Rspam-User: X-HE-Tag: 1713288360-969614 X-HE-Meta: U2FsdGVkX1+iZmgTwGof8WMuZ3SoEyNWgmCIIMRxbdNAxdu04NrjS1Pj1Ays/aGFT4rQpiJYfb5luxif+3dMEU3S1W+fJk559Z91X//u7TFUC/oz/JQGe2wWf10220Rv/wuwnsXFOX5w7sR67JoIuBJyo3UIy62LppZDzWNYcGb8IlNYgUNsWihGMSPQQDGWE8T/UfgPs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0/HFdU1BXO+3s9wxQ5dsu4I7i9dVjWqyDZQVQ4KmpJceNRBqKfIqauFCrSU2QQ== 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 want to limit the use of page_mapcount() to the places where it is absolutely necessary. If our folio has a stable node, it is a (small) KSM folio -- see folio_stable_node(). Let's use folio_mapcount() in stable_tree_search() instead, which results in no functional change. The mapcount > 1 check is a bit confusing, because that's usually a check for page sharing. Looks like the reason is that we are guaranteed to not exceed ksm_max_page_sharing for the tree KSM folio when merging with that. Let's update the documentation to make that clearer. Cc: Andrew Morton Cc: Alex Shi (tencent) Signed-off-by: David Hildenbrand Reviewed-by: Alex Shi --- mm/ksm.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index 486c9974f8e20..159604ad47799 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -1909,12 +1909,15 @@ static struct page *stable_tree_search(struct page *page) if (page_node) { VM_BUG_ON(page_node->head != &migrate_nodes); /* - * Test if the migrated page should be merged - * into a stable node dup. If the mapcount is - * 1 we can migrate it with another KSM page - * without adding it to the chain. + * If the mapcount of our migrated KSM folio is + * at most 1, we can merge it with another + * KSM folio where we know that we have space + * for one more mapping without exceeding the + * ksm_max_page_sharing limit: see + * chain_prune(). This way, we can avoid adding + * this stable node to the chain. */ - if (page_mapcount(page) > 1) + if (folio_mapcount(folio) > 1) goto chain_append; }