From patchwork Mon Dec 11 16:22:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Wilcox X-Patchwork-Id: 13487535 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 1E2C4C4167B for ; Mon, 11 Dec 2023 16:22:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 35A7C6B015C; Mon, 11 Dec 2023 11:22:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 309136B015E; Mon, 11 Dec 2023 11:22:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 183846B015F; Mon, 11 Dec 2023 11:22:35 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id EF6446B015C for ; Mon, 11 Dec 2023 11:22:34 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id C25AD1607B6 for ; Mon, 11 Dec 2023 16:22:34 +0000 (UTC) X-FDA: 81555055428.04.CDDDAF6 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf12.hostedemail.com (Postfix) with ESMTP id 0A2544001B for ; Mon, 11 Dec 2023 16:22:32 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=sqO4Rkl9; dmarc=none; spf=none (imf12.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=1702311753; 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=6/O8AxQhOc3DY3F0avx8jrFn8ZBbS1ihhWhpgyLNNSI=; b=HXhy22pw/aq/+gBxWf11upKyOrqivUF3gPX5w/m8XTu7syjF7MwBsPuylKrHT0wXwGrHnw M7lBsOeo19vzDQPsAcY94uO+ZUECGbWLHJnZNen4SuDIdRRh/JqqYSVjUDfg2GgjmGK0W7 ub+3Ox3Oyewl2NKF5DMryiWj9puDDhM= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=sqO4Rkl9; dmarc=none; spf=none (imf12.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=1702311753; a=rsa-sha256; cv=none; b=IYy/x7oSBz675HaAns2mY+4VKWlLNOzlGy3ds+MVoOfOO5kvusaaZg08CHNWoK+rTNuubW RjbHcPTFenmkeWJ6xY1pJzBTKfy8bxMva8Vavus98IiViv8pAV/bG0ZEUKR68Yw7N1FfTe iRQM0Rb3l/8iBd/ifH1jIDusmoS345k= 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=6/O8AxQhOc3DY3F0avx8jrFn8ZBbS1ihhWhpgyLNNSI=; b=sqO4Rkl9ug6swiCRwJCQ3KJwBD 2KOiGPD1cihCHpe6GLFNLQUMTmwYkZJ8a9U7sLFTRZXyrZhr+8n5xVUbeHtCYnt7T1eZKct4VgKXl uRFsghqrpn17YmJiCyhrZ7LeXdx2m+3Qt/HexoTORA0SPT/vAci6GnfjnQ0TDuEUaUtxAueIX5O1O 7X791Mvod35j0GJso6kh2BYZtzsgnlLiDAzTUcZit4mspNAQ+g8LhHNMnV1TPPAoD+yrf9hOVgHEP FsUskY2xNjV9T/IUqWm+kfJASUpkxuVmt+2G1blenqDXGieMPh5WUGBt5sDiY1LbrKOhAu/R2RpsV s/9uhn+A==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1rCj2u-0090JB-1X; Mon, 11 Dec 2023 16:22:16 +0000 From: "Matthew Wilcox (Oracle)" To: Andrew Morton Cc: "Matthew Wilcox (Oracle)" , linux-mm@kvack.org Subject: [PATCH 1/9] mm: Convert ksm_might_need_to_copy() to work on folios Date: Mon, 11 Dec 2023 16:22:06 +0000 Message-Id: <20231211162214.2146080-2-willy@infradead.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20231211162214.2146080-1-willy@infradead.org> References: <20231211162214.2146080-1-willy@infradead.org> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: kr834hje6xp5et9rjegzbqd69tm8b435 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 0A2544001B X-HE-Tag: 1702311752-160514 X-HE-Meta: U2FsdGVkX1+eyN2S7FkbW96xomdzuZfPWgWpl3xQawa+Z/UTU87zY42FxFMlI3VznjV8W83JAiscd9BSO1bqNRgkKxqvWYCknHel+hFsmqtZu1EO2U8R+oo0AG4eExY3u31R5HsDk9UO1HXFsJQlupofu5XVCNqz+Z37QKaHonGc00638tUG2fHNxna5qYVNirkR+B4Tca9FwkRD9aBcUvx6B/5iuHfsVweOAHMIaKzuRrh12U9QFBRjvY7nRFF7+soYLjBv/AtxivRv57DRVEkUvQxw4JatdtWdvy0NTnB9LqItHUnqna5QMmSJkNn49QkzfGzwxRsock2dFYikcFfS/chixbHHxMTQTVR6jtY6ExK4gPzAnVlDlejEHbn0sfDgl0XoKsFq3WzPLoNNdtBxT2Z2Kvun2bUv3zXhM+MkScLlHz9+k3m5Tqu8L17I/zvTzNaKvdiwnC68GT+CB9t4TlQRkUCbLJscaqtzqTPBkVzIrAPnbhDH4/kjQkcLeXew80p4uOXuBhlTzcDfNO/J/XGRCTCAGT5LqoC20PD9eePVtpSqtjNU1gaaXZLRCKa09ny4vZRdGOjua1f4+wEcBfA0tY1sTcH9mzn/jCwQfU3fflJDNmZWxRf2CCpuiTOpXzqJ6PhQctOwt1TsRgGekWcu0jKhgllQlswDLIWErq5DT0bCgAvtigzvZB64aVkN6kSOTkPs/BwMf1Ag9ANrM+i1tgwmRm0r4qGg7PGn3uoklBCaRcaVo9HqSb7xVEkaFj0yANBZsBBrFtluKF89eLgoeK93iUK2kIAOVqONFzgRMo2g4rkvhheCssbY7iUysLVIkpAbdf7F0Eh7Ry6JpXGiR7DNBxUzOvBLZCUCMJGPFvIiYvK/OtjzBde6VhDkkw1tVBxYeFezxA/9tCusaUSd5COKIU1KO2ieskP0AhhZdZ/kJYPykyejMBKw8sDXVfK6Ylf5UO9DLQx Bc0CvKrB 7rQlIt0olH4wB93FAF/kE5VQWoZdFIk/gTTUNvfx9qAhR41mGRxu2wAJOSkRFEMZ5AqAczem+n/DpHSaAAveliGiBgo52f8hlQWKtCaeWinYHnTAe6i+R+vRmCg7g7sysl0vTRFY9MQ3nsa4B2LnO7pINBmUIjTX9NovwmG+wghQz1AT7NVcRaQaQ1Q6kB8u2LmqskQguOYxUvcVCqsbR9kp1Sq3Psodem7A7 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: Accept a folio as an argument and return a folio result. Removes a call to compound_head() in do_swap_page(), and prevents folio & page from getting out of sync in unuse_pte(). Signed-off-by: Matthew Wilcox (Oracle) Reviewed-by: David Hildenbrand --- include/linux/ksm.h | 6 +++--- mm/ksm.c | 21 +++++++++++---------- mm/memory.c | 8 ++++---- mm/swapfile.c | 8 +++++--- 4 files changed, 23 insertions(+), 20 deletions(-) diff --git a/include/linux/ksm.h b/include/linux/ksm.h index 4643d5244e77..401348e9f92b 100644 --- a/include/linux/ksm.h +++ b/include/linux/ksm.h @@ -76,7 +76,7 @@ static inline void ksm_exit(struct mm_struct *mm) * We'd like to make this conditional on vma->vm_flags & VM_MERGEABLE, * but what if the vma was unmerged while the page was swapped out? */ -struct page *ksm_might_need_to_copy(struct page *page, +struct folio *ksm_might_need_to_copy(struct folio *folio, struct vm_area_struct *vma, unsigned long addr); void rmap_walk_ksm(struct folio *folio, struct rmap_walk_control *rwc); @@ -129,10 +129,10 @@ static inline int ksm_madvise(struct vm_area_struct *vma, unsigned long start, return 0; } -static inline struct page *ksm_might_need_to_copy(struct page *page, +static inline struct folio *ksm_might_need_to_copy(struct folio *folio, struct vm_area_struct *vma, unsigned long addr) { - return page; + return folio; } static inline void rmap_walk_ksm(struct folio *folio, diff --git a/mm/ksm.c b/mm/ksm.c index b93389a3780e..16532fa85a46 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -2875,30 +2875,30 @@ void __ksm_exit(struct mm_struct *mm) trace_ksm_exit(mm); } -struct page *ksm_might_need_to_copy(struct page *page, +struct folio *ksm_might_need_to_copy(struct folio *folio, struct vm_area_struct *vma, unsigned long addr) { - struct folio *folio = page_folio(page); + struct page *page = folio_page(folio, 0); struct anon_vma *anon_vma = folio_anon_vma(folio); struct folio *new_folio; if (folio_test_large(folio)) - return page; + return folio; if (folio_test_ksm(folio)) { if (folio_stable_node(folio) && !(ksm_run & KSM_RUN_UNMERGE)) - return page; /* no need to copy it */ + return folio; /* no need to copy it */ } else if (!anon_vma) { - return page; /* no need to copy it */ + return folio; /* no need to copy it */ } else if (folio->index == linear_page_index(vma, addr) && anon_vma->root == vma->anon_vma->root) { - return page; /* still no need to copy it */ + return folio; /* still no need to copy it */ } if (PageHWPoison(page)) return ERR_PTR(-EHWPOISON); if (!folio_test_uptodate(folio)) - return page; /* let do_swap_page report the error */ + return folio; /* let do_swap_page report the error */ new_folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, vma, addr, false); if (new_folio && @@ -2907,9 +2907,10 @@ struct page *ksm_might_need_to_copy(struct page *page, new_folio = NULL; } if (new_folio) { - if (copy_mc_user_highpage(&new_folio->page, page, addr, vma)) { + if (copy_mc_user_highpage(folio_page(new_folio, 0), page, + addr, vma)) { folio_put(new_folio); - memory_failure_queue(page_to_pfn(page), 0); + memory_failure_queue(folio_pfn(folio), 0); return ERR_PTR(-EHWPOISON); } folio_set_dirty(new_folio); @@ -2920,7 +2921,7 @@ struct page *ksm_might_need_to_copy(struct page *page, #endif } - return new_folio ? &new_folio->page : NULL; + return new_folio; } void rmap_walk_ksm(struct folio *folio, struct rmap_walk_control *rwc) diff --git a/mm/memory.c b/mm/memory.c index 055647120f01..318f923134e4 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3942,15 +3942,15 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) * page->index of !PageKSM() pages would be nonlinear inside the * anon VMA -- PageKSM() is lost on actual swapout. */ - page = ksm_might_need_to_copy(page, vma, vmf->address); - if (unlikely(!page)) { + folio = ksm_might_need_to_copy(folio, vma, vmf->address); + if (unlikely(!folio)) { ret = VM_FAULT_OOM; goto out_page; - } else if (unlikely(PTR_ERR(page) == -EHWPOISON)) { + } else if (unlikely(folio == ERR_PTR(-EHWPOISON))) { ret = VM_FAULT_HWPOISON; goto out_page; } - folio = page_folio(page); + page = folio_page(folio, 0); /* * If we want to map a page that's in the swapcache writable, we diff --git a/mm/swapfile.c b/mm/swapfile.c index 8be70912e298..0371b7b3cd27 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1749,11 +1749,13 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd, int ret = 1; swapcache = page; - page = ksm_might_need_to_copy(page, vma, addr); - if (unlikely(!page)) + folio = ksm_might_need_to_copy(folio, vma, addr); + if (unlikely(!folio)) return -ENOMEM; - else if (unlikely(PTR_ERR(page) == -EHWPOISON)) + else if (unlikely(folio == ERR_PTR(-EHWPOISON))) hwpoisoned = true; + else + page = folio_file_page(folio, swp_offset(entry)); pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); if (unlikely(!pte || !pte_same_as_swp(ptep_get(pte),