From nobody Mon Feb 9 03:34:53 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 35AC3204C36 for ; Wed, 8 Jan 2025 23:33:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736379191; cv=none; b=R1MxeILscLg/T9vIW1BI8Y6zMvjvdOQg13SZj/63UhmXlCRHl/f5+uJ0H0Z6EfFDX3TqmLe2Trkc6SkpZBlnHZfJuu8xkd4gNa6i0gq9v9o16zkrFO8ClPtm+JWsq+nWQvaqVy4mUD1CPqzXfbhzvni80ONhGLj9LmI95kJPhUw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736379191; c=relaxed/simple; bh=XTzAqLJrSXCTRCf8A/BA86mwOeFhzb57OfrE2FQmSYQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=d2PIYLkgWen8fqMvZXQ8H+J/H3Z1ECxJw0TYMgAzmGjNNfFbFM40qewAyr0nv43wLHtEG2GC+2wRvbUz/cdsnVbjJHneJ/Al01Sf8K08THodwWElNXOfNvYshCb5BRN65iOeybUSH4SJFUnf5M3L1/dJSx4QesD0q5tpWWBJvcM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=K86bdoEB; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="K86bdoEB" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1736379189; 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=K86bdoEBJj0HidciY4VdVuADdqLNVu4iziM5sGEaE7kOFT6DCFzY9/SnmRjhxhmmOiLrI+ UrAx2AgredOHejiok/TYEEdnSvhWAum6+OQ9L3FOnNpsFnFQ2to8RU+Ul5bpdffOe7cIax FeDPZFq0c2Idgi3g9HCAHDAHSfVuQN4= 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 Content-Type: text/plain; charset="utf-8" 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 =20 +/* + * 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_st= ruct *mm, + struct vm_area_struct *vma, bool *mmap_locked, + struct collapse_control *cc) +{ + int result =3D SCAN_FAIL; + unsigned long tva_flags =3D cc->is_khugepaged ? TVA_ENFORCE_SYSFS : 0; + + if (!*mmap_locked) { + mmap_read_lock(mm); + *mmap_locked =3D 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 =3D get_file(vma->vm_file); + pgoff_t pgoff =3D linear_page_index(vma, addr); + + mmap_read_unlock(mm); + *mmap_locked =3D false; + result =3D hpage_collapse_scan_file(mm, addr, file, pgoff, + cc); + fput(file); + if (result =3D=3D SCAN_PTE_MAPPED_HUGEPAGE) { + mmap_read_lock(mm); + if (hpage_collapse_test_exit_or_disable(mm)) + goto end; + result =3D collapse_pte_mapped_thp(mm, addr, + !cc->is_khugepaged); + mmap_read_unlock(mm); + } + } else { + result =3D hpage_collapse_scan_pmd(mm, vma, addr, + mmap_locked, cc); + } + if (result =3D=3D SCAN_SUCCEED || result =3D=3D SCAN_PMD_MAPPED) + ++khugepaged_pages_collapsed; + } +end: + return result; +} + static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *resul= t, struct collapse_control *cc) __releases(&khugepaged_mm_lock) --=20 2.47.1