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 C039BC369D9 for ; Fri, 25 Apr 2025 20:23:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 819DB6B0005; Fri, 25 Apr 2025 16:23:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 79EF16B000A; Fri, 25 Apr 2025 16:23:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5F2DE6B000C; Fri, 25 Apr 2025 16:23:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 3789F6B0005 for ; Fri, 25 Apr 2025 16:23:40 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 859B71C7260 for ; Fri, 25 Apr 2025 20:23:41 +0000 (UTC) X-FDA: 83373691842.15.80D96FE Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf28.hostedemail.com (Postfix) with ESMTP id 2EC4BC0005 for ; Fri, 25 Apr 2025 20:23:39 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=SqwGWUdH; dmarc=pass (policy=quarantine) header.from=redhat.com; spf=pass (imf28.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1745612619; a=rsa-sha256; cv=none; b=tzKrKSSgph6jJUzp81n4By3WXt9VLMPfaP6UUpAnO8aoT8EEyx7T+TT8F/GCfGwtwVuspl HAtBUILgoFvLbvBfRisCPtIQC09WOPUbuFe0jhKlciPWh6ADdeOYn7iN7XhYQdhXXlVC0X a4ew7HI12quyMX41XjOUu2KEn1wTJU0= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=SqwGWUdH; dmarc=pass (policy=quarantine) header.from=redhat.com; spf=pass (imf28.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1745612619; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Lnuw5o7CPzgYgSg30nFZeV0xqdVhh4HEfg3UbcumuG4=; b=43jOIXO91smBtLAupwpXR07nQqb8HZCjmC18pxniWJLBq0poBZn9Lg3OlapW9KFvAvFtXX PpWu2ju2+GbyT10047LeUUt4yF15p56HKyyNG1UmajcWMNzrML4ffWYtOkCc/+PBiGidfW ePBM7PYN5D1MxoVtle5t3ev0zn3Vx7s= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1745612618; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Lnuw5o7CPzgYgSg30nFZeV0xqdVhh4HEfg3UbcumuG4=; b=SqwGWUdH6LPEIKz4sShX6NCXAxf5PW2M/kD1rTkUTpzf3/L5h9336WexrL0t4ZPSxPtqF/ 6mkpQ+snZHKP649zeU6GECVv4ArReXF7z9uFQdeYUkMWN+oGTb5+k4AlSc1zb5/KQ+Dkyk b2CUeYPBYR6bp8A9YkvLJS+v04JJgvc= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-311-CNTPHdCZPB-ojtxKqFtGJA-1; Fri, 25 Apr 2025 16:23:37 -0400 X-MC-Unique: CNTPHdCZPB-ojtxKqFtGJA-1 X-Mimecast-MFC-AGG-ID: CNTPHdCZPB-ojtxKqFtGJA_1745612617 Received: by mail-qv1-f72.google.com with SMTP id 6a1803df08f44-6e8f9450b19so55975786d6.1 for ; Fri, 25 Apr 2025 13:23:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745612617; x=1746217417; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=Lnuw5o7CPzgYgSg30nFZeV0xqdVhh4HEfg3UbcumuG4=; b=gzqeLBOaPh1783ULHaRrpg/6ts6RuQhJoHlquUj60day9Z6xFUQ1D7LC+TKTfDa6Qk dL/MGHtaBlKFfUr6UcgFnNR4Bz/TeMcw0GzYPQQZ5kmk/qnQrPA+Hw9qFI3vkGGFPWGJ Pw/LNxSomWvQby2HM9Y6xRPLNSTdye/pMcqBLtj6E5jH1C+eDy9q3Bs9CHq6561jXahZ Fol/KBAAYzYzSnMsBNtC0AP1fifjkwMHGVMnD7UhMRyPCDHedCPltfKoTwV8MjblCW1D WopJ2985I8iH89bWFJNWPz6JXeLdsNCttFU2Qnl9BIZZ3GIpcCmgscMtiIREzgSeZ9Yx HKRg== X-Forwarded-Encrypted: i=1; AJvYcCUWAl1VJ8mfkKVFqsFlqZyGhdRlax23iEJWzJNH1M6woVr0ws9+OoSsTFsZidpoEo3ELzL0gsCvNw==@kvack.org X-Gm-Message-State: AOJu0YyjKVVUAZV5rb/TF/mo2gRaKCkMU3ch2LF5tlHjj4OpuQEm5fc/ 2j14zZD3lJqAh4I+EZnEqxZpRFrHsFb9yaYX9wM9cZuc4JraKkoyHMt818tPfpJH2Dl82hMSyJv 22g43wqLeB03ePOgUMN2FTvKSXyYi9GPZ8tBW0sHcr86Sx1Zb X-Gm-Gg: ASbGnctiiJKaloZOvsK1askIN9EPKwIzxoPoobNcjBuoVrbWezN+N5kPUBVnd54fvD/ l/dTU4iH9njxwAFC8e7l3OKMGCJdSUY/ASKO+yng84b1NSjbh6L+NxpIDxJWxFWxy9tB61WPcnZ iDSidiwVthlcKNDWD4rzcAq/Wb+BHDgNNnZ1++/udOP3nMjC68a/s1wDpzO10+vLMTfTY3K70Bw QiKd1BbDKjQ8oUzwauY6Gbq5S8Za3Itz6OTMCM2a0X++eQPIMSahkTMfsFNriDfDyRRLbLG0QUV 4qs= X-Received: by 2002:a05:6214:ca2:b0:6f4:c939:a3f5 with SMTP id 6a1803df08f44-6f4cc38cae8mr50224116d6.17.1745612616913; Fri, 25 Apr 2025 13:23:36 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHAbB9VBhNfVFkn16GQ1jhi8yEPGvnD0kLIi/1k7vzQQf+ePUU4ZzjJOht8FgMwVJBxtCN+iQ== X-Received: by 2002:a05:6214:ca2:b0:6f4:c939:a3f5 with SMTP id 6a1803df08f44-6f4cc38cae8mr50223586d6.17.1745612616494; Fri, 25 Apr 2025 13:23:36 -0700 (PDT) Received: from x1.local ([85.131.185.92]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6f4c0aaf9a3sm25917506d6.98.2025.04.25.13.23.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Apr 2025 13:23:35 -0700 (PDT) Date: Fri, 25 Apr 2025 16:23:31 -0400 From: Peter Xu To: David Hildenbrand Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, x86@kernel.org, intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, linux-trace-kernel@vger.kernel.org, Dave Hansen , Andy Lutomirski , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , Jani Nikula , Joonas Lahtinen , Rodrigo Vivi , Tvrtko Ursulin , David Airlie , Simona Vetter , Andrew Morton , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , "Liam R. Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn , Pedro Falcato Subject: Re: [PATCH v1 05/11] mm: convert VM_PFNMAP tracking to pfnmap_track() + pfnmap_untrack() Message-ID: References: <20250425081715.1341199-1-david@redhat.com> <20250425081715.1341199-6-david@redhat.com> MIME-Version: 1.0 In-Reply-To: <20250425081715.1341199-6-david@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: JgSSyTrxc3wTuDT-8n85LaHfg9yePsK6K3L1fewGDi0_1745612617 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline X-Rspam-User: X-Rspamd-Queue-Id: 2EC4BC0005 X-Rspamd-Server: rspam04 X-Stat-Signature: arwg9sfxc65793xufshhrbymb5nwsiq7 X-HE-Tag: 1745612619-108302 X-HE-Meta: U2FsdGVkX1+YDS9ichBuEL4U18/cm+sTWg2Ge2L+X+pQdH9cMNXbrsb5HqH0zR3aMSS+Y1W22Wo4qR/XpVX8Sx9trz7RzI5bmfzGe7pN1yI97deCzyUKnz2QgK9zdV7ExaM1T2yNfZxaWOwTP2YSEy+pEhi1vJrHOqPzbRPVbZT+wTC4eltN5bY+/8QvA/1WXy+uROnZBJZ4tn7K0h77ajSQc2pfukzk0o86tF61LIx9Ya8q2yTWS+KNNMbgBPlESo34117+Q/i5ZAohioy9MbQS0SvicYn3BXLufoxRYwOD8cp3E0k0ql5iJ/oAs4oafoTEOpGmipIu/Nxmwz3St1qqmXWKW9Q2iBl6lpt4fWFv0BJJX5DWtbn6EyY5GmWFPmiPqBbgc06u99abIJCXXcOfNX7qnAP0WkBNC2/fzoRvNApmojDAb/lwQbX7NsmmOFzL5/XynsH+p3rWGyMp60lWPSALs7LUlSVTeqro5+NqLJjYYrp0WpAUjC/7ZISYYKgNfKsnYpRDtquQUol29IfW2n0a/YziLHvaaZrmeVlU78gBjtfbOd33eA43zZsVpfibSSLegYK5liIqz9grfB8hqxaLPWsJaBM8oh9wFZYX+sNwqYuh5S2B5wHvBzqZPfOUOaPv6E5XZLRHuTxIWnBJWkQnNRbP937t9GJ3UwHD8o1SHJZVUuhyZlgS+SiUmzRKeJFc5vX29LV7SVVqWI5aN7HM8FPXKg1ZrkSUhrf9S34vDo91njHqqRuAn1R6pk/MUzjb6kqteEeUJLlND5jPzLlhA5EfXq5WFKglUUfisYJ3NrDt9Be/qctLLY6BR0ROzApt34HRj9NGImtj13talkcLup8QUDrZLxVVMPpJhj/3pLnMhS+kJKWjjhvNZ1P6TLlGDDq1kr+H8Uo/XdgVJQYZ6QDbjfvdTGsJvyVsXGQ24812yz8zHaUZZESJUbbIRNVDoHXtfAEselp MkdpmG1/ upQOkjpgv/xrSCBWm8BOkZOAc2KomshC5URAXCUe5x985lBxCFWDWkz5UK5eclSy3M9q7xeLkRwYb5xCc43U9B31Cd7LXOqvCk9GZELTJX/MEq05IsvhSB1CG2jaytvhfRTepWaX8E5lTWZZn7sdxtLLEQCLeBdrAhJ9dzKdQ4mIR2dXd7s6duOUdTHNeZ9p2AiaUBCcQ475Mz9SmW3BYsCS1fPAU91gdvk0I9pD7un9qGsYTvSINTlF3omKll9ELKl57+sc6djInjHUYza4XX9NIHdeGQuWEipNZBzIvmUEPPGyPYASik8xlWR65i6dYXbwKwGzXFObJrgBXD7VHrGWBhAHZQZuh+4ZERVjpH08G2cJ7QNR30EPtNgozv8HMowxEPR2KrFNrIiYhecV7Gnrf87i9PaU+T4RY 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, Apr 25, 2025 at 10:17:09AM +0200, David Hildenbrand wrote: > Let's use our new interface. In remap_pfn_range(), we'll now decide > whether we have to track (full VMA covered) or only sanitize the pgprot > (partial VMA covered). > > Remember what we have to untrack by linking it from the VMA. When > duplicating VMAs (e.g., splitting, mremap, fork), we'll handle it similar > to anon VMA names, and use a kref to share the tracking. > > Once the last VMA un-refs our tracking data, we'll do the untracking, > which simplifies things a lot and should sort our various issues we saw > recently, for example, when partially unmapping/zapping a tracked VMA. > > This change implies that we'll keep tracking the original PFN range even > after splitting + partially unmapping it: not too bad, because it was > not working reliably before. The only thing that kind-of worked before > was shrinking such a mapping using mremap(): we managed to adjust the > reservation in a hacky way, now we won't adjust the reservation but > leave it around until all involved VMAs are gone. > > Signed-off-by: David Hildenbrand > --- > include/linux/mm_inline.h | 2 + > include/linux/mm_types.h | 11 ++++++ > kernel/fork.c | 54 ++++++++++++++++++++++++-- > mm/memory.c | 81 +++++++++++++++++++++++++++++++-------- > mm/mremap.c | 4 -- > 5 files changed, 128 insertions(+), 24 deletions(-) > > diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h > index f9157a0c42a5c..89b518ff097e6 100644 > --- a/include/linux/mm_inline.h > +++ b/include/linux/mm_inline.h > @@ -447,6 +447,8 @@ static inline bool anon_vma_name_eq(struct anon_vma_name *anon_name1, > > #endif /* CONFIG_ANON_VMA_NAME */ > > +void pfnmap_track_ctx_release(struct kref *ref); > + > static inline void init_tlb_flush_pending(struct mm_struct *mm) > { > atomic_set(&mm->tlb_flush_pending, 0); > diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h > index 56d07edd01f91..91124761cfda8 100644 > --- a/include/linux/mm_types.h > +++ b/include/linux/mm_types.h > @@ -764,6 +764,14 @@ struct vma_numab_state { > int prev_scan_seq; > }; > > +#ifdef __HAVE_PFNMAP_TRACKING > +struct pfnmap_track_ctx { > + struct kref kref; > + unsigned long pfn; > + unsigned long size; > +}; > +#endif > + > /* > * This struct describes a virtual memory area. There is one of these > * per VM-area/task. A VM area is any part of the process virtual memory > @@ -877,6 +885,9 @@ struct vm_area_struct { > struct anon_vma_name *anon_name; > #endif > struct vm_userfaultfd_ctx vm_userfaultfd_ctx; > +#ifdef __HAVE_PFNMAP_TRACKING > + struct pfnmap_track_ctx *pfnmap_track_ctx; > +#endif So this was originally the small concern (or is it small?) that this will grow every vma on x86, am I right? After all pfnmap vmas are the minority, I was thinking whether we could work it out without extending vma struct. I had a quick thought quite a while ago, but never tried out (it was almost off-track since vfio switched away from remap_pfn_range..), which is to have x86 maintain its own mapping of vma <-> pfn tracking using a global stucture. After all, the memtype code did it already with the memtype_rbroot, so I was thinking if vma info can be memorized as well, so as to get rid of get_pat_info() too. Maybe it also needs the 2nd layer like what you did with the track ctx, but the tree maintains the mapping instead of adding the ctx pointer into vma. Maybe it could work with squashing the two layers (or say, extending memtype rbtree), but maybe not.. It could make it slightly slower than vma->pfnmap_track_ctx ref when looking up pfn when holding a vma ref, but I assume it's ok considering that track/untrack should be slow path for pfnmaps, and pfnmaps shouldn't be a huge lot. I didn't think further, but if that'll work it'll definitely avoids the additional fields on x86 vmas. I'm curious whether you explored that direction, or maybe it's a known decision that the 8 bytes isn't a concern. Thanks, -- Peter Xu