From mboxrd@z Thu Jan 1 00:00:00 1970 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 33FFBC3ABAA for ; Fri, 2 May 2025 12:50:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1AECC6B008A; Fri, 2 May 2025 08:50:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 15F766B008C; Fri, 2 May 2025 08:50:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 025A46B0092; Fri, 2 May 2025 08:50:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id D88D26B008A for ; Fri, 2 May 2025 08:50:44 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 08F471A18DD for ; Fri, 2 May 2025 12:50:45 +0000 (UTC) X-FDA: 83397952050.07.3F5038E Received: from mail-ed1-f50.google.com (mail-ed1-f50.google.com [209.85.208.50]) by imf21.hostedemail.com (Postfix) with ESMTP id 1ECCE1C0007 for ; Fri, 2 May 2025 12:50:42 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=c+Xm5amr; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf21.hostedemail.com: domain of jannh@google.com designates 209.85.208.50 as permitted sender) smtp.mailfrom=jannh@google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1746190243; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Ld8hRinh9GbkBEMDrzL6i5e2SIxAQjM/p85QdAMHIYE=; b=Ip5Sg8I+Jp7SzJ6m/8q+cmOeDTKnXv5LIFUlGufE9F7X9VEDIcKfNXC1yGsKzBh+gikJPG NBkuWLEjHTw6ghDM/mLn8sGJoK1+s4i94eepxMVS12xjp8yH0Nse3TboBuNz2IRo6rIvI3 jM8Jul2d/sN8o/ypes6YrgSwbQpMStM= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=c+Xm5amr; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf21.hostedemail.com: domain of jannh@google.com designates 209.85.208.50 as permitted sender) smtp.mailfrom=jannh@google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1746190243; a=rsa-sha256; cv=none; b=WTMyNM9ciZHW/5u+tF23/dBt/s8ScUXfzj7OSWq0g8Fq71GgppM8i3JA4w9YIGVWiyjqme XMwNhzksF68NEg9F5USdDKCeOd9pKCxq7Z9Vl5jP0AIWt54uPOBaP6IdadLM0XFOV0/8YS lYT+gBwlKIRAlyXu9jn474SLhIs7PXw= Received: by mail-ed1-f50.google.com with SMTP id 4fb4d7f45d1cf-5dbfc122b82so9950a12.0 for ; Fri, 02 May 2025 05:50:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1746190241; x=1746795041; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=Ld8hRinh9GbkBEMDrzL6i5e2SIxAQjM/p85QdAMHIYE=; b=c+Xm5amrLiEkpmraiUbtDmFwHYXmDhM1zh7w2kyJqrl8bX4XgpHP9WKlXbhdshhzgC uhKcCtg+Cuv6Nq6KIjn9g5LD7xLTDLFzz1zRCyNRECEnzGpXfOOCEp3YpcztTA4Gq50C OJ44c2A1jrXLVEvhPnOEHCT/QMJczYo5LjWMsjFrZ9O6bbD0uvopC+Yt4EVgFZdlM4sw axzrOJe2J9Vp/0mh85QRKiw1YNOiP9OXZ1pDqCdVdFBVZDD/iLqB9gMyu93aTV/pzk3P BTMdFsnmOaq7+JXvktfRCoMT5RbWQ/uwNKvHDUv8KZkbFRXQ+8BDO+eqOHJvA5spGizt 3QpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746190241; x=1746795041; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Ld8hRinh9GbkBEMDrzL6i5e2SIxAQjM/p85QdAMHIYE=; b=fLd0BoqjASxGOug5AxyiQrZzIF6M16eftAVtLjEu/UwgDkxdkp8lWYBpNT0Kf7JJj0 65s6rUbse4lv2xlm6P19CBBtvd8Yq76TcZFb3JXxGLtdasOqSlV6aLIRhQcTl1VemlHX BJopsfHROy7KaDRpNHKJV2iOABcvVEzmpwJcPOz0KB5V55ybLLeycejc2lAJYV2cfF/Y ZGhOGnDHVTxvpctDEHyIjlX8uHhLFBhUypx0U/Xmsi5nP8oFcu6aP13qLISp6o8uI/iu 1BwnYJXyuLPI46Fg3Ror/w4hJvoT0a8DD8tvDV2KBrcvK8SpGu/fQSVLeb7SKszJjPxj QLKg== X-Gm-Message-State: AOJu0YwcIKbtfnzRIL0wM1kdCfAjWw6Y6rvbYq8KrbqziF9AxFU0Z9S5 SHxgd82Iob9mj8k1g7eQZ6HocqAV7tN7PHbPLbsP9gSTdMs6jtpmDJNCqWJSqZljt3wVjJw4rbl AFrA5U9x7LCUOn0hrHclj24QMzUbjXe1gi69J X-Gm-Gg: ASbGncvqUjjiZ4zoyKQtDo77mZpkvOhE5blLiWRpTCrOcnHeEMogvRx8u4Rw8JGkFxT I7sXmox+kMfSRn+HddWCfOC1v8nt5umkqEfpFKjBdpNSCT3rrWyl6i2ushp/pBRIwHD4FQw23ke 9h6V7SnW34xjLdjH37afuVzuIZYftFMiQ4FjDOz6K94BXAB4GI+w== X-Google-Smtp-Source: AGHT+IG3MxGSNZIU/DLDRhXYOvvIy6yvhn86oGlso7zA/bv5//6dzg5601r9+ubOpvul9MK0F9INy9LM5qMNZeJUT5g= X-Received: by 2002:aa7:c599:0:b0:5de:bcd9:4aa with SMTP id 4fb4d7f45d1cf-5f918c09629mr179021a12.3.1746190241144; Fri, 02 May 2025 05:50:41 -0700 (PDT) MIME-Version: 1.0 References: <20250428181218.85925-1-npache@redhat.com> <20250428181218.85925-8-npache@redhat.com> In-Reply-To: From: Jann Horn Date: Fri, 2 May 2025 14:50:05 +0200 X-Gm-Features: ATxdqUHoBHrob4F884q8RfFXZYe_rdAyjR_yWnJbdQTKUjzn_Qy_PjvRzckdGXk Message-ID: Subject: Re: [PATCH v5 07/12] khugepaged: add mTHP support To: David Hildenbrand , Nico Pache Cc: linux-mm@kvack.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, akpm@linux-foundation.org, corbet@lwn.net, rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, baohua@kernel.org, baolin.wang@linux.alibaba.com, ryan.roberts@arm.com, willy@infradead.org, peterx@redhat.com, ziy@nvidia.com, wangkefeng.wang@huawei.com, usamaarif642@gmail.com, sunnanyong@huawei.com, vishal.moola@gmail.com, thomas.hellstrom@linux.intel.com, yang@os.amperecomputing.com, kirill.shutemov@linux.intel.com, aarcange@redhat.com, raquini@redhat.com, dev.jain@arm.com, anshuman.khandual@arm.com, catalin.marinas@arm.com, tiwai@suse.de, will@kernel.org, dave.hansen@linux.intel.com, jack@suse.cz, cl@gentwo.org, jglisse@google.com, surenb@google.com, zokeefe@google.com, hannes@cmpxchg.org, rientjes@google.com, mhocko@suse.com, rdunlap@infradead.org, lorenzo.stoakes@oracle.com, Liam.Howlett@oracle.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Stat-Signature: nrmw7k1tcd3cobjnxkrxcztbpyu6fiqs X-Rspamd-Queue-Id: 1ECCE1C0007 X-Rspam-User: X-Rspamd-Server: rspam05 X-HE-Tag: 1746190242-543597 X-HE-Meta: U2FsdGVkX19i+KRZErNm/r0Kshy6+UDi0JsgcZ6F6hk6dU/I8aweNcSKco1jFguDdWtLiRfSlLX/ZOrhSa6q9yyl7JMX+DoyHWpWnFDg5a52OytG9V/0XB6h72V82/EV5BNaSYm5pzZfm4ERg65RbOX2Kgic7iMKPwRXDUAiGo9lioVwKuWKNuD6hXTzXoV7yDxRb/1V5i1M3JwqKAOrR4NN4XK6h29FlF5DX+D1fLinTZzIfGw9fMwr3F5JQSMfK2trz2KsGmLVOiQCuTwvI4+ZWJdkggPNk/h487hM0/2/9wBiRakDkXJi/2oVC6gWb1xBXvuWa0nyIl7174k+HZehTo7kHsabipzIQ2JxxvTB2iqYMrHQUY1G3jzrX827/h8oKgQ7QIfxWsH6BzNH4cBS20XMPJzgZLsDoqj968m9VuuPk4qD58k8px3INUhJRiAy+ZLnSWtkVT0tQ2RJXdA+Ab647S3t6j15ncGN4eJ7jJqBBczUf6L9Rkacjt0/yXLni0ksZIuzoLyFXcDMpOsekV97/srjkILIsCG96vFhOJWAPvvc3INmuGae7BBq0QpNXD7RhvQqLCr2R7QPQhXqRJXoT1xVzGR44kL45h8LF1UvR2sDMWsrXzol422psS0Gv4Z6VDJzJlsX6gxAmQ2u0XRTlAsO5SD2w81612ylhPPVI3PKw29mEp58cGMI5GPPWRCmWgfDA8gDieqq/lb34CS1pMXYPbeJO875/0IwlMP2iw+gudG+Ykb15Bo9dvh70GGZBtnPDwxuI63h1lUqkOH3Vm6BkhoT5slYTZSXlCWXalWbG5i0KlrdGQTmkvR1VBMg5bAT/wnHhVoEKogYuGpx+poy3pPSuSTbeO5yNVolrXa7j2NwwSIjf+CTyjw3KUKEfPJtPCy6YhziXu9QvHFaCSTdASIOG9SzdzkL1cFUDWDYYZsCAgqAeFuqTWR+epzHyKVZkpiYOp7 i37S7/8+ IyVRprduVolgCye+TJn2ca16/vyoqxv+7OSXgG5x2TCcQtCOfT09ulS8GpMv5uswgnznlHnbiRJMhWevONcL89w/syTte304oSMdnQjaUKt1I0jz911MU4UWlAHczFykmFCSHM2wPSkNu5wIUXKx50iMuPdFhcwkK10xjXdjLF9u8t3ohNcy0+w+YyG4q7AXDTTZoiIwQB1XZDraM7pXxC8zniu6fup0DpOjb8V7dPMnDQ/gEqcV5psJHjDh6BoPIPEzNueGoyhG3L2YzMfVKSx7kKvvC2R7vaP3N+VrQ4UdnU34dHTWpj/hW7Q== 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: On Fri, May 2, 2025 at 8:29=E2=80=AFAM David Hildenbrand = wrote: > On 02.05.25 00:29, Nico Pache wrote: > > On Wed, Apr 30, 2025 at 2:53=E2=80=AFPM Jann Horn wr= ote: > >> > >> On Mon, Apr 28, 2025 at 8:12=E2=80=AFPM Nico Pache = wrote: > >>> Introduce the ability for khugepaged to collapse to different mTHP si= zes. > >>> While scanning PMD ranges for potential collapse candidates, keep tra= ck > >>> of pages in KHUGEPAGED_MIN_MTHP_ORDER chunks via a bitmap. Each bit > >>> represents a utilized region of order KHUGEPAGED_MIN_MTHP_ORDER ptes.= If > >>> mTHPs are enabled we remove the restriction of max_ptes_none during t= he > >>> scan phase so we dont bailout early and miss potential mTHP candidate= s. > >>> > >>> After the scan is complete we will perform binary recursion on the > >>> bitmap to determine which mTHP size would be most efficient to collap= se > >>> to. max_ptes_none will be scaled by the attempted collapse order to > >>> determine how full a THP must be to be eligible. > >>> > >>> If a mTHP collapse is attempted, but contains swapped out, or shared > >>> pages, we dont perform the collapse. > >> [...] > >>> @@ -1208,11 +1211,12 @@ static int collapse_huge_page(struct mm_struc= t *mm, unsigned long address, > >>> vma_start_write(vma); > >>> anon_vma_lock_write(vma->anon_vma); > >>> > >>> - mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, addr= ess, > >>> - address + HPAGE_PMD_SIZE); > >>> + mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, _add= ress, > >>> + _address + (PAGE_SIZE << order)); > >>> mmu_notifier_invalidate_range_start(&range); > >>> > >>> pmd_ptl =3D pmd_lock(mm, pmd); /* probably unnecessary */ > >>> + > >>> /* > >>> * This removes any huge TLB entry from the CPU so we won't = allow > >>> * huge and small TLB entries for the same virtual address t= o > >> > >> It's not visible in this diff, but we're about to do a > >> pmdp_collapse_flush() here. pmdp_collapse_flush() tears down the > >> entire page table, meaning it tears down 2MiB of address space; and it > >> assumes that the entire page table exclusively corresponds to the > >> current VMA. > >> > >> I think you'll need to ensure that the pmdp_collapse_flush() only > >> happens for full-size THP, and that mTHP only tears down individual > >> PTEs in the relevant range. (That code might get a bit messy, since > >> the existing THP code tears down PTEs in a detached page table, while > >> mTHP would have to do it in a still-attached page table.) > > Hi Jann! > > > > I was under the impression that this is needed to prevent GUP-fast > > races (and potentially others). Why would you need to touch the PMD entry to prevent GUP-fast races for mTH= P? > > As you state here, conceptually the PMD case is, detach the PMD, do > > the collapse, then reinstall the PMD (similarly to how the system > > recovers from a failed PMD collapse). I tried to keep the current > > locking behavior as it seemed the easiest way to get it right (and not > > break anything). So I keep the PMD detaching and reinstalling for the > > mTHP case too. As Hugh points out I am releasing the anon lock too > > early. I will comment further on his response. As I see it, you're not "keeping" the current locking behavior; you're making a big implicit locking change by reusing a codepath designed for PMD THP for mTHP, where the page table may not be exclusively owned by one VMA. > > As I familiarize myself with the code more, I do see potential code > > improvements/cleanups and locking improvements, but I was going to > > leave those to a later series. > > Right, the simplest approach on top of the current PMD collapse is to do > exactly what we do in the PMD case, including the locking: which > apparently is no completely the same yet :). > > Instead of installing a PMD THP, we modify the page table and remap that. > > Moving from the PMD lock to the PTE lock will not make a big change in > practice for most cases: we already must disable essentially all page > table walkers (vma lock, mmap lock in write, rmap lock in write). > > The PMDP clear+flush is primarily to disable the last possible set of > page table walkers: (1) HW modifications and (2) GUP-fast. > > So after the PMDP clear+flush we know that (A) HW can not modify the > pages concurrently and (B) GUP-fast cannot succeed anymore. > > The issue with PTEP clear+flush is that we will have to remember all PTE > values, to reset them if anything goes wrong. Using a single PMD value > is arguably simpler. And then, the benefit vs. complexity is unclear. > > Certainly something to look into later, but not a requirement for the > first support, As I understand, one rule we currently have in MM is that an operation that logically operates on one VMA (VMA 1) does not touch the page tables of other VMAs (VMA 2) in any way, except that it may walk page tables that cover address space that intersects with both VMA 1 and VMA 2, and create such page tables if they are missing. This proposed patch changes that, without explicitly discussing this locking change. Just as one example: I think this patch retracts a page table without VMA-locking the relevant address space (we hold a VMA lock on VMA 1, but not on VMA 2), and we then drop the PMD lock after (temporarily) retracting the page table. At that point, I think a racing fault that uses the VMA-locked fastpath can observe the empty PMD, and can install a new page table? Then when collapse_huge_page() tries to re-add the retracted page table, I think we'll get a BUG_ON(). Similar thing with concurrent ftruncate() or such trying to zap PTEs, we can probably end up not zapping PTEs that should have been zapped? > The real challenge/benefit will be looking into avoiding taking all the > heavy weight locks. Dev has already been thinking about that. For mTHP > it might be easier than for THPs. Probably it will involve setting PTE > migration entries whenever we drop the PTL, and dealing with the > possibility of concurrent zapping of these migration entries.