From patchwork Wed Jan 8 23:31:17 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nico Pache X-Patchwork-Id: 13931710 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 6622BE77199 for ; Wed, 8 Jan 2025 23:33:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C77656B007B; Wed, 8 Jan 2025 18:33:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C265F6B0083; Wed, 8 Jan 2025 18:33:10 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AC7D36B0088; Wed, 8 Jan 2025 18:33:10 -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 8C8426B0083 for ; Wed, 8 Jan 2025 18:33:10 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 026D71C5F33 for ; Wed, 8 Jan 2025 23:33:09 +0000 (UTC) X-FDA: 82985887740.23.A52E3A0 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf04.hostedemail.com (Postfix) with ESMTP id 3DBEE40010 for ; Wed, 8 Jan 2025 23:33:08 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=i61sb2MC; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf04.hostedemail.com: domain of npache@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=npache@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736379188; a=rsa-sha256; cv=none; b=M4iNWZwajaa6/O9GezZpw5oVRRxIJX0vgBfS4rnqq9MfVuZ40MGI4g9q/4mGb8ONA3mIZs 5wv9IjR693l23EzRtTuKXDf3Ld0A86uN+hRsY3cxSVW4fvqSSLKDMkFFJxff3Wnm+rP/vI Ur+d0frlLUXGRrsTlbgVI9uYZYX0E0g= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=i61sb2MC; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf04.hostedemail.com: domain of npache@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=npache@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736379188; 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=x+jqVU9ZBjbAss1pYtlcKhjJs4UO6QLqbD/SlLsG+XQ=; b=T93fGRsBOHs9VpFH9VBV4gQMl9w14JQMiFBuMfNAwIybtCusUih2XnfzIicpvAUZ1M31oU hWV6bLMZe9adEhKLnt0/hGpNOiy9oxuZ3AndmHV7NjzR+nmJPJYnlwMi6PqEjGqvClZ+yx 1nPkxchsIEM733UWSWTSaDY0dOX/m4I= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1736379187; 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=x+jqVU9ZBjbAss1pYtlcKhjJs4UO6QLqbD/SlLsG+XQ=; b=i61sb2MCVRoIZHQlkGc3wo4dVLt74u7fK/JIKdvld1dM8+r8fuTwLZRliJ/WU6DlNI6WI5 ayvk/KvPGI49PfN/umvY4uYIzJ5VZ7ImvYZq3jmdRLYfEQcmkRctkVHeOkpHcsCfhdv+Fc Bw5aqlAoYRV/Byae1qKP+dg3RMFioFI= Received: from mx-prod-mc-01.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-653-xPwzlNi_O7akFagKf4tyXA-1; Wed, 08 Jan 2025 18:33:05 -0500 X-MC-Unique: xPwzlNi_O7akFagKf4tyXA-1 X-Mimecast-MFC-AGG-ID: xPwzlNi_O7akFagKf4tyXA Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (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-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 1CFC519560B8; Wed, 8 Jan 2025 23:33:01 +0000 (UTC) Received: from h1.redhat.com (unknown [10.22.80.41]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 85E1419560B7; Wed, 8 Jan 2025 23:32:52 +0000 (UTC) From: Nico Pache To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: ryan.roberts@arm.com, anshuman.khandual@arm.com, catalin.marinas@arm.com, cl@gentwo.org, vbabka@suse.cz, mhocko@suse.com, apopple@nvidia.com, dave.hansen@linux.intel.com, will@kernel.org, baohua@kernel.org, jack@suse.cz, srivatsa@csail.mit.edu, haowenchao22@gmail.com, hughd@google.com, aneesh.kumar@kernel.org, yang@os.amperecomputing.com, peterx@redhat.com, ioworker0@gmail.com, wangkefeng.wang@huawei.com, ziy@nvidia.com, jglisse@google.com, surenb@google.com, vishal.moola@gmail.com, zokeefe@google.com, zhengqi.arch@bytedance.com, jhubbard@nvidia.com, 21cnbao@gmail.com, willy@infradead.org, kirill.shutemov@linux.intel.com, david@redhat.com, aarcange@redhat.com, raquini@redhat.com, dev.jain@arm.com, sunnanyong@huawei.com, usamaarif642@gmail.com, audra@redhat.com, akpm@linux-foundation.org Subject: [RFC 01/11] introduce khugepaged_collapse_single_pmd to collapse a single pmd Date: Wed, 8 Jan 2025 16:31:17 -0700 Message-ID: <20250108233128.14484-2-npache@redhat.com> In-Reply-To: <20250108233128.14484-1-npache@redhat.com> References: <20250108233128.14484-1-npache@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 3DBEE40010 X-Stat-Signature: oic1f7wpme4u3ygrrdh7jgtgrfcqqipr X-Rspam-User: X-HE-Tag: 1736379188-758451 X-HE-Meta: U2FsdGVkX1+rOvqGx2NdG03EY8YDbI/FJ4EFTx7kiocMDdO8gPUuJ6rfTM8CKfEbGhbRcll6RhtWywqmJDRTMQJdYJ9uvcxZ4bsZa/WsXnpu7VSDQhlM5/y3vdUO8hPw/COHpjR3WDdNlmBBZh5GmmaxPqF/2QWHrMHoA9ViJNnfTbkkLOU2A0Ec7GQuLW5WkTeY4ytNkIloE2i9QSWxQp3Izi3gKNvsxVQhq1TSXl05OFyZw3BqokByjwEHd6Ti6fcy6rq2FuV5HhU+yjeT1nG2YCro/QeEiZsrqQp4xM8sr8KdbkNEUcGEPdME1D01ueR5Kwqa+UCe6wX38sqCsnHo4zV+cV2mu1oZyxMUWpqRFxmzgKUksR1XK+KeGOs28/STCsNvxveeOVy3cUmVEVmRA5j/7y+4C1v3OuEHvg/QSx8gt0RXCU45AMP/YNKHZNncT09Ef/XZ+vza0SVj2dvn2XJWGPe6bbagNQWwYEKZIMq/UdAu703oKbkn7fAKVELyWh4gMUD+ohJZxIuO5aZNVVJ2CrJEa6bXsUlFxEhWy9657YfP183PUNA8ZZ5ImopTPnI84Zgkv1cLmOAw/H47YqUyw482EOF1iqD9lZfUwmv0P2UUcOC36+/QYutr14jK1563JT3fIr/4k+G4o7EhfaFsMFJNNJmLmFmtLvKqk/AtfH+s+LsQ620td+12+wOU3wAuHwTQah+cPtHq4KL9l3yazR9dMpmxhcEpDRmT/Oy0FcJ2G66iCx+Yes9BBCB8VTjr8djiizAKdesor8r8qq/DV82cHf7FdK49CsPosKeJf8c97JI4GcAg/cI7ENPwlP+kuaXyiCAgyxreyQBOrRmvqPMDOBypKV+iHQQ6H7oohs+7ZpJCKtbNGaDFBs0SxbwCOviskg4loW0r56lWbvPUFmyY2bWGYjy/yeCC0Cs3DNQ9UgHSN3E5wtntW7mEZDs5b6hOG8mJ2VG sdc1w8+t vtWxOfNu2CqShSdcBi5onThBmR2VdyCl2y97VdLt0vXqZlTRbFUq51wEN77gPzsFtbnj81JDzkUjtz6pTwoGdzDjjQkluQXJX2zuoXmt0BKqS8V0pwdkPWn28exwrY5QErWwWWzk+KiCK1X1nQdqXUlNSmqUP9oDs2YVstYBIaIDpTwQpaiQ9zJZSrltUgHe0E1lh4l82HlsORcU7iFBTPRvjcPY8b7PPaPqIDkme9GfOTU6na8d2I19jLSCxQMFEFAAjrPeX0xVTROlwlCMK/4B0kzxC//qAWgj5ZGP1CqYW7VEyvii0jfliba26qE8Qwk9M4R+GKU+SDXuKIuivkngVQa9f56nTZerRnKs3JAaezyeea5Q+OZyeqCLnvH7GhkRrbHOeyUAI2Aaa5BxJl8V2F+oLsrROnO7OrabssrmNl/qV4MvGRmpBj0IAhBYXYPIAy+qHNCO5oP5DMYWhERxpD6aMKO1TQYddm7JmcIUJor4= 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: The khugepaged daemon and madvise_collapse have two different implementations that do almost the thing. Create khugepaged_collapse_single_pmd to increase code reuse and create a entry point for future khugepaged changes. Signed-off-by: Nico Pache --- mm/khugepaged.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 653dbb1ff05c..4d932839ff1d 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -2348,6 +2348,52 @@ static int hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr, } #endif +/* + * Try to collapse a single PMD starting at a PMD aligned addr, and return + * the results. + */ +static int khugepaged_collapse_single_pmd(unsigned long addr, struct mm_struct *mm, + struct vm_area_struct *vma, bool *mmap_locked, + struct collapse_control *cc) +{ + int result = SCAN_FAIL; + unsigned long tva_flags = cc->is_khugepaged ? TVA_ENFORCE_SYSFS : 0; + + if (!*mmap_locked) { + mmap_read_lock(mm); + *mmap_locked = true; + } + + if (thp_vma_allowable_order(vma, vma->vm_flags, + tva_flags, PMD_ORDER)) { + if (IS_ENABLED(CONFIG_SHMEM) && vma->vm_file) { + struct file *file = get_file(vma->vm_file); + pgoff_t pgoff = linear_page_index(vma, addr); + + mmap_read_unlock(mm); + *mmap_locked = false; + result = hpage_collapse_scan_file(mm, addr, file, pgoff, + cc); + fput(file); + if (result == SCAN_PTE_MAPPED_HUGEPAGE) { + mmap_read_lock(mm); + if (hpage_collapse_test_exit_or_disable(mm)) + goto end; + result = collapse_pte_mapped_thp(mm, addr, + !cc->is_khugepaged); + mmap_read_unlock(mm); + } + } else { + result = hpage_collapse_scan_pmd(mm, vma, addr, + mmap_locked, cc); + } + if (result == SCAN_SUCCEED || result == SCAN_PMD_MAPPED) + ++khugepaged_pages_collapsed; + } +end: + return result; +} + static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result, struct collapse_control *cc) __releases(&khugepaged_mm_lock)