From patchwork Fri Aug 2 15:55:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13751754 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 DDD77C3DA4A for ; Fri, 2 Aug 2024 15:56:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 74C916B00A3; Fri, 2 Aug 2024 11:56:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6D4ED6B00A4; Fri, 2 Aug 2024 11:56:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 577466B00A5; Fri, 2 Aug 2024 11:56:55 -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 35FB96B00A3 for ; Fri, 2 Aug 2024 11:56:55 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id D38E0C13EC for ; Fri, 2 Aug 2024 15:56:54 +0000 (UTC) X-FDA: 82407758748.04.4D963CE Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf17.hostedemail.com (Postfix) with ESMTP id 21BEA4001B for ; Fri, 2 Aug 2024 15:56:52 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=EgLAis2q; spf=pass (imf17.hostedemail.com: domain of david@redhat.com designates 170.10.129.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=1722614155; 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=XisBOloUEP3qStGU5kEnvOleMULjhsDMvLI1Qb38ywI=; b=UDYHIpGQ+jHkLeVrI2MfLRtS6+PWQ2vdZ5+jaiHxtSF95NRl0mZWuzLxx6f3pXECTtgnel wDY+ZGqpgh3zi/dTKlk1KHFbn3ZrvrF6WCD2fftHtPXXbcRuSrWrqUfY0CQpPyWndyIsvg 75n6VvX5wao0pzT90Eo9sCmd8mVNv2M= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722614155; a=rsa-sha256; cv=none; b=t35DtqMhT1X2ConkIn/I/B79JOx7Lahy3Ir6XxOeQW1JuLHuTXnkmv92C8ht6+4nIBPSjj 8oh5D7Dsn4g7EIRjQHlUxKU/8PqsCTaInE7k3MmnFDBCDaWW3bi6MXUZLXOgmb0clIEy54 EhA7hlSFU9gwDW740hVwtOhADLS663c= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=EgLAis2q; spf=pass (imf17.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722614212; 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=XisBOloUEP3qStGU5kEnvOleMULjhsDMvLI1Qb38ywI=; b=EgLAis2qodxejLBxwWpVNsuu2eVYq9OsqJ2MR6kOZeYcTX8p4Id7ksFmIkbxwvLezHeepu PJHL2ZeJiLyKb+X/iimAkzZlJ8PvkDLOY3APKRByvjdTAAUECRnSvcU17JnL++1n9EPtaC iSo8eAahcruafEqZ0yqMQ6th9IPMQdM= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-533-7fMx_I3IMvS20EQpewJYGQ-1; Fri, 02 Aug 2024 11:56:48 -0400 X-MC-Unique: 7fMx_I3IMvS20EQpewJYGQ-1 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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 mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 833C51955D4D; Fri, 2 Aug 2024 15:56:45 +0000 (UTC) Received: from t14s.redhat.com (unknown [10.39.192.113]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id B30F4300018D; Fri, 2 Aug 2024 15:56:39 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linux-doc@vger.kernel.org, kvm@vger.kernel.org, linux-s390@vger.kernel.org, linux-fsdevel@vger.kernel.org, David Hildenbrand , Andrew Morton , "Matthew Wilcox (Oracle)" , Jonathan Corbet , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Sven Schnelle , Gerald Schaefer Subject: [PATCH v1 11/11] mm/ksm: convert break_ksm() from walk_page_range_vma() to folio_walk Date: Fri, 2 Aug 2024 17:55:24 +0200 Message-ID: <20240802155524.517137-12-david@redhat.com> In-Reply-To: <20240802155524.517137-1-david@redhat.com> References: <20240802155524.517137-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Rspamd-Queue-Id: 21BEA4001B X-Stat-Signature: dbtryqfcmd1huckxfgyzhmdobyameuqj X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1722614212-889300 X-HE-Meta: U2FsdGVkX189M08/S5T4C5eZqjh33xa9TLO/vXfWpfml7tBktYGVQu/joLhvWfUV3UU3WbcS6B9vqOCbhe3PWHOr1b5hEzbFJxaxJHhb9AUtjFH/Cl/AUFfCJSMdWEWkmNauM43MoSPERu3ExwFZ9RY1/c9EiImDrqPq8S67el6nGjUnVRVp854qn4dHD19jbmvECRXNU4+MykLbA61IxkWxJap0+fDi0C7AcmCno8sfJpxc30Xa7y+a6gRVFqh3oeaNgt3h8CXHnxTeozX0G3BcQ0pDassuLERg+6M7AzZoA/pJDxET9Xdr0vI4/02oqa0/GR5sXtnPh/1SLrVvWv1Jwxg86vF1dhz7RqzBmLVCLreJzO/nGiRMjfEfxKyICBAVvbSpNfK4XczXI6kMfvt9lnO6NfxOLqc/WQOrEr1G5MOdDShIHZ7BCDBE98dDsKfNm+jrLrBQ3xSQdQJsVr/43bx5ucC60CrzFF25yU3UNS1oqGmLdPChp8AiJNr2VhEqAnZHhWnmbFs7tdhfxTCQaqEdFTGRnMv7DUatAZhdgmpA8w5uFZlk/WDLI9F4XsqihMtbi+jfMVtENnqrftqaYlSZEMg62TzV0//TOeJXeLCc0OtpfeHJZOLyHG2zIKf9hevkGw+QiE/x1ucYLmo+fpMj1Z0RelEuG6c1kdLLV4l2jOIy0hfFwtWZBMDeSsotjnkYXUJDvePfdS2j2u1YCbxZkUNHvBGqVidEem+OXg7lL2NKQmVeWWyCU6I7LoMZ5SLJtE8+h9WglMWzKTezJl/gDx2CGyHR4Gqsx63Fk3W7FIXZyx/3bWAwtPifp8jAAI5YdHm+mF6AvOPIoVXPB28+59bZ12aQcW/7sm+6+Fq4mA6FWc/hmhs6NljUNUYimKBblKMsjFAFyHqC4Y9EKqvr9OjvRGJ21EX/xcjg3yAF+zXdV1bzjhVI/T/jOBZoWd3GJzR3/ynoxAu KVBVd6Gm c2EIy1wILUQjm0otgC1hB2FoYbhKG0xVHq1vkk8fv1ZpQnZjpeM9IkqsnFll7+05lYacHMZQUoE5q/NyTM09E89Dr7GUQG1rxN4qzfeHeozr+Tl709nk7Iwq7Hjualpm9j01utwPmYDu2HDEivehbY1LzRzofolPhIsPhoJgay90RCiiHW0XS8G5VG7v15jBvFLriXEqvAIS9hh0qmiPFG3CHLfcuxJ99zEJWCBVHwi4oY2ADkY9zJAO3Me9MDKkoRON9XbJUw3xNf4vWhxyjy5/v6zfsxqXLWKaZzNjNWlUkX6pzMarLVNj4lJZGeFtm9P2riQmnXnitF4vCWrNbFLmL1A== 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: Let's simplify by reusing folio_walk. Keep the existing behavior by handling migration entries and zeropages. Signed-off-by: David Hildenbrand --- mm/ksm.c | 63 ++++++++++++++------------------------------------------ 1 file changed, 16 insertions(+), 47 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index 0f5b2bba4ef0..8e53666bc7b0 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -608,47 +608,6 @@ static inline bool ksm_test_exit(struct mm_struct *mm) return atomic_read(&mm->mm_users) == 0; } -static int break_ksm_pmd_entry(pmd_t *pmd, unsigned long addr, unsigned long next, - struct mm_walk *walk) -{ - struct page *page = NULL; - spinlock_t *ptl; - pte_t *pte; - pte_t ptent; - int ret; - - pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl); - if (!pte) - return 0; - ptent = ptep_get(pte); - if (pte_present(ptent)) { - page = vm_normal_page(walk->vma, addr, ptent); - } else if (!pte_none(ptent)) { - swp_entry_t entry = pte_to_swp_entry(ptent); - - /* - * As KSM pages remain KSM pages until freed, no need to wait - * here for migration to end. - */ - if (is_migration_entry(entry)) - page = pfn_swap_entry_to_page(entry); - } - /* return 1 if the page is an normal ksm page or KSM-placed zero page */ - ret = (page && PageKsm(page)) || is_ksm_zero_pte(ptent); - pte_unmap_unlock(pte, ptl); - return ret; -} - -static const struct mm_walk_ops break_ksm_ops = { - .pmd_entry = break_ksm_pmd_entry, - .walk_lock = PGWALK_RDLOCK, -}; - -static const struct mm_walk_ops break_ksm_lock_vma_ops = { - .pmd_entry = break_ksm_pmd_entry, - .walk_lock = PGWALK_WRLOCK, -}; - /* * We use break_ksm to break COW on a ksm page by triggering unsharing, * such that the ksm page will get replaced by an exclusive anonymous page. @@ -665,16 +624,26 @@ static const struct mm_walk_ops break_ksm_lock_vma_ops = { static int break_ksm(struct vm_area_struct *vma, unsigned long addr, bool lock_vma) { vm_fault_t ret = 0; - const struct mm_walk_ops *ops = lock_vma ? - &break_ksm_lock_vma_ops : &break_ksm_ops; + + if (lock_vma) + vma_start_write(vma); do { - int ksm_page; + bool ksm_page = false; + struct folio_walk fw; + struct folio *folio; cond_resched(); - ksm_page = walk_page_range_vma(vma, addr, addr + 1, ops, NULL); - if (WARN_ON_ONCE(ksm_page < 0)) - return ksm_page; + folio = folio_walk_start(&fw, vma, addr, + FW_MIGRATION | FW_ZEROPAGE); + if (folio) { + /* Small folio implies FW_LEVEL_PTE. */ + if (!folio_test_large(folio) && + (folio_test_ksm(folio) || is_ksm_zero_pte(fw.pte))) + ksm_page = true; + folio_walk_end(&fw, vma); + } + if (!ksm_page) return 0; ret = handle_mm_fault(vma, addr,