From patchwork Wed Oct 2 15:25:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Wilcox X-Patchwork-Id: 13819960 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 76B2ACF6D38 for ; Wed, 2 Oct 2024 15:25:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F07DC4401B2; Wed, 2 Oct 2024 11:25:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EB7E34401AF; Wed, 2 Oct 2024 11:25:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D7FDC4401B2; Wed, 2 Oct 2024 11:25:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id B060B4401AF for ; Wed, 2 Oct 2024 11:25:40 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 5D8761A0A07 for ; Wed, 2 Oct 2024 15:25:40 +0000 (UTC) X-FDA: 82629036840.21.919C7B9 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf24.hostedemail.com (Postfix) with ESMTP id C297A18000A for ; Wed, 2 Oct 2024 15:25:37 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=JfbP2o5P; dmarc=none; spf=none (imf24.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727882698; a=rsa-sha256; cv=none; b=n8DCUW4JQvivh2JplY04f691rqNLuo/1wXeujWXap37GP6J9AbRHtTAMIRma+UKB+Cn1jg 5niXaisDnGCuuvzeyUDd7b4HmK0VR/5J0Sw2DQpcwQlcTD6uvw34f39yZWhUsOrkXj5rC4 TV78bcYzRb8LCqryvhCv66YHjLTN+5c= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=JfbP2o5P; dmarc=none; spf=none (imf24.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1727882698; 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=Osoc8X/udePgkx9ybsKr2k/UQEoeeSG0T43hRiCLjBo=; b=yEI69tgoDw4RxyHrM9VzrLGZMtDsQj8Wgp5isnbR+BCy1QnNOBruMOFHGmqYMfx/lVxb5n 5tmdPZa56nGfp15tuTPbCBjZzDyr75K6BXrPvlRxwbAfswtHqqrbkikM5pmUad8Lyx8DfU Pcm1FjNcNR6aEet9Xkbb1KNYR9TDR/k= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=Osoc8X/udePgkx9ybsKr2k/UQEoeeSG0T43hRiCLjBo=; b=JfbP2o5PCBYGcsva64UkntHyF0 nVMS+CnwCmDYc8W0TuuuQjI9roDrAAKOqLYv3115eEyV2m2hDojCug75My3Xqh1myaQWniRwBWFeS M34VosKOb9v/lkpVaDjq4fLIBxwxBBF5+CYB5hSjiEF/gUQ79lNbb3hZYhGRniBcVaEi0jmRohjs8 zBMNMc+EJDHTygNUCjChSz72YPTEyS9jaRGACZAItchRGLyHmGHyREg6opruVOtb4b8BwRHXEIPkR V6tpsyIwToxjRQIC0dNG7OuAEKViBJjASMAh2v3E9syHh0YVwVvad3PQX1uUDqxqphDfCppaYPJJc AK4GDgRQ==; Received: from willy by casper.infradead.org with local (Exim 4.98 #2 (Red Hat Linux)) id 1sw1EN-00000005fMm-0dtF; Wed, 02 Oct 2024 15:25:35 +0000 From: "Matthew Wilcox (Oracle)" To: Andrew Morton Cc: "Matthew Wilcox (Oracle)" , linux-mm@kvack.org, Alex Shi Subject: [PATCH 1/5] ksm: Use a folio in try_to_merge_one_page() Date: Wed, 2 Oct 2024 16:25:27 +0100 Message-ID: <20241002152533.1350629-2-willy@infradead.org> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20241002152533.1350629-1-willy@infradead.org> References: <20241002152533.1350629-1-willy@infradead.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: C297A18000A X-Rspamd-Server: rspam01 X-Stat-Signature: 4mcju6nqjcqo3fmar4i49ykyeo6pw94a X-HE-Tag: 1727882737-692745 X-HE-Meta: U2FsdGVkX18FGmTCYEztBdeElJt7901AZY1k+CujqlBY9bwD2A8+QDfYsuopyg7oWpAl6nR/1s6np7Zux5x6bzoMR8GMcvgCznJdATSuEhr5o0nhJ/1piah7dAO19Tq304J4lxifzxPao0AguDWKbpKB4ok64n7bLG40vfjUIz2G5IW0bt2xskmyEOo56NayXaCY/T1+OKeU1ZVFGiFIXDyvtqWPymJGCBKsYp2q+diujT6bcwl9X7fQpmnX3JH11r2tY8sm2au6G01NtPMYwI6I87/XPsX3OKN7c3jJGh58p1sqV+QG30R4qjS5AOB3w2md/cnAiWR/Y/2vs8DodKCxoYwhJSF6h7Erigey236tKtKBeH0bJKmzP3zzE3eWEeVL99aiNBJBECtY5SFrYSO3/5nhzeydZ3oo8eoaE4OJPzHEONFY6BBFsPGVf+fzd/lv4ppd0fDtBoAcQk3bbJjadBx+VYUGO9UjNZqz+2NSySaGqZQilDPByw8VpeW3AKuW4lKRxyGc01SGA3HZgX7zkvjnLEbEIdT3GhmRNRM/hnRzGPVaS7+buvRuw/ZzolCYwV1HnTVvEeQEwHeWr1iOFSgsprKNZ6My8z4xg/A+W5si49vajHfd92uoaE8CC+wMWKuC9ECNWgMtPC7m8mmcyQ1XWKIa7Wu6+pnfi8hwTSsNoA5DEMYgmc2vt1i+3plX+xZ7gcg2nXUtcRMlF7Api2GDv0yvtV9OzJMtjZwRIWFH6Wz0g+gAsLNa3bcVDCtXj/xjYYZT5yYVTQZOQBk+i0CXEuguraXZwWstolT5JL+SNI5S8CHL+QIOz7d8SW/jOMwqlHN8c4bf+psF6ryWsIUDmzhB46i3fafx8wHR5yL/D3bVljrZX5VK41+Ha9a3ycXUUSCKbDUuY8MC685qZd4EZmEbain93a48ZW7IrKvFtAx2fU9UH1zhKex901tFQXzP6pKtBxLZJEt ptIGpg91 ancTIVxVkNU4sUmsHz48JlvHv6sMC0WLotzS/3sx3Ix74xlIZDx0jMwqEYPSRT76H/4fvge4hP6tayh6Co8Xu81Zohk0RnZ2MkUx54FYMZlKt+nKY9oHAVxhh7Uu5/u9th0BywF2/Ra4TiiowecdxZsRwRfkX8LDRaHRH7M0BlRHpBqQT/HVUiHd9KERZY6Kv8FT5Q1nikqSvMt3g3h0ydhkiFZ6vtgRqsGSx8pyva60GsOrgq0nJQDzBxNUbTZKyc3gMJApIweCs+QdNVJ8Nz5bKdqAgmnlmT1wl 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: It is safe to use a folio here because all callers took a refcount on this page. The one wrinkle is that we have to recalculate the value of folio after splitting the page, since it has probably changed. Replaces nine calls to compound_head() with one. Signed-off-by: Matthew Wilcox (Oracle) --- mm/ksm.c | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index a2e2a521df0a..57f998b172e6 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -1443,28 +1443,29 @@ static int replace_page(struct vm_area_struct *vma, struct page *page, static int try_to_merge_one_page(struct vm_area_struct *vma, struct page *page, struct page *kpage) { + struct folio *folio = page_folio(page); pte_t orig_pte = __pte(0); int err = -EFAULT; if (page == kpage) /* ksm page forked */ return 0; - if (!PageAnon(page)) + if (!folio_test_anon(folio)) goto out; /* * We need the folio lock to read a stable swapcache flag in - * write_protect_page(). We use trylock_page() instead of - * lock_page() because we don't want to wait here - we - * prefer to continue scanning and merging different pages, - * then come back to this page when it is unlocked. + * write_protect_page(). We trylock because we don't want to wait + * here - we prefer to continue scanning and merging different + * pages, then come back to this page when it is unlocked. */ - if (!trylock_page(page)) + if (!folio_trylock(folio)) goto out; - if (PageTransCompound(page)) { + if (folio_test_large(folio)) { if (split_huge_page(page)) goto out_unlock; + folio = page_folio(page); } /* @@ -1473,28 +1474,28 @@ static int try_to_merge_one_page(struct vm_area_struct *vma, * ptes are necessarily already write-protected. But in either * case, we need to lock and check page_count is not raised. */ - if (write_protect_page(vma, page_folio(page), &orig_pte) == 0) { + if (write_protect_page(vma, folio, &orig_pte) == 0) { if (!kpage) { /* - * While we hold page lock, upgrade page from - * PageAnon+anon_vma to PageKsm+NULL stable_node: + * While we hold folio lock, upgrade folio from + * anon to a NULL stable_node with the KSM flag set: * stable_tree_insert() will update stable_node. */ - folio_set_stable_node(page_folio(page), NULL); - mark_page_accessed(page); + folio_set_stable_node(folio, NULL); + folio_mark_accessed(folio); /* - * Page reclaim just frees a clean page with no dirty + * Page reclaim just frees a clean folio with no dirty * ptes: make sure that the ksm page would be swapped. */ - if (!PageDirty(page)) - SetPageDirty(page); + if (!folio_test_dirty(folio)) + folio_mark_dirty(folio); err = 0; } else if (pages_identical(page, kpage)) err = replace_page(vma, page, kpage, orig_pte); } out_unlock: - unlock_page(page); + folio_unlock(folio); out: return err; }