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 D4FF7C52D71 for ; Fri, 9 Aug 2024 07:16:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 53AB86B0089; Fri, 9 Aug 2024 03:16:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4EA3E6B008A; Fri, 9 Aug 2024 03:16:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 38B106B008C; Fri, 9 Aug 2024 03:16:54 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 1B2F96B0089 for ; Fri, 9 Aug 2024 03:16:54 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 8B875A09CC for ; Fri, 9 Aug 2024 07:16:53 +0000 (UTC) X-FDA: 82431849906.09.84306F9 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf27.hostedemail.com (Postfix) with ESMTP id 31A584000E for ; Fri, 9 Aug 2024 07:16:51 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="OGa/C3vu"; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf27.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723187762; a=rsa-sha256; cv=none; b=ebHCyrp7+Z9TGCI4ZBEPpQwcvGQcAv8kA/ZtpYp8i/b78IBjcm/jBDkvlGhs/dqM5X/Mik c00VTjc1mdd1OnxTu4HNxCT6RiN4JPkqb+EYxbpXh8EEUxk8LvWOqXRE5JiD09X2Vb2lB6 7eSUbHhB1Td2OsXJ8zdSwizLTMW4gNI= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="OGa/C3vu"; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf27.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723187762; 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=WjJJO1LczwWOlW0BQ8SK5TpK9dJIW736kg5IdhWodDY=; b=VH3hVaBOOIETBU+eU54LdmIECI0nXOzrHl7TMad2qn9ltBbfZoGDsv29mW6TVCAMbjUt57 AVGSuLQfUBlQD08l5pVKXvrHmbo/+3Abw7YkV1puM202P6hVanVGr+WCVdXLom5ZrI4hS9 DM0zermO4fBTEIe/jweK2T8mkLzUQvw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1723187810; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:autocrypt:autocrypt; bh=WjJJO1LczwWOlW0BQ8SK5TpK9dJIW736kg5IdhWodDY=; b=OGa/C3vuO7+L7EZV/q+uU11cKdWV90NZ0YYGu7NozoquHj2nB6M4m6eSJJFXcXxaU/3fQV wcbCAbqAFhqghJ69PeqOuU6Tr71ShcsAIJnSVREcmImOiFPYeM40IRYzIOQbeKiaT7jzlP JPkXcuKIoL+2Ezc8gi0HdC6lOA0u3RA= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-49-Fur3C5EnPt2UxL5oPV1O8w-1; Fri, 09 Aug 2024 03:16:49 -0400 X-MC-Unique: Fur3C5EnPt2UxL5oPV1O8w-1 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-42816aacabcso12515415e9.1 for ; Fri, 09 Aug 2024 00:16:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723187808; x=1723792608; h=content-transfer-encoding:in-reply-to:organization:autocrypt :content-language:from:references:cc:to:subject:user-agent :mime-version:date:message-id:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=WjJJO1LczwWOlW0BQ8SK5TpK9dJIW736kg5IdhWodDY=; b=Ps0PCQY7W8v9G1WBfFFf8jD+aseMRCFkVmsZvjIqoVcM+TZ8et31crum8KiL0Nza+L XHG4e7LaOo7IUkj2nxT9TWznXgvHVHuPPTJk9YzA9Gs/Cu2QArdo31+Hz1FcDW8WX015 OHytfSrb2g2ZkJx6gwBHk7mbfL7xW+PF2x/JGYV5xGo8ujvM5raLvslKxQnOJhsOdIc9 B00WIixxBkTMBwJu97KWbgsUmoo6kuH3WCv2OkqK/ltdR3sp/twwy1P1846R+R7OPdbr SfA6m+k0fZAjwpugYu927VJyvN9xV9uknWpeMS3fYphPd0375vFZzzdTlxW/zEsdW9E0 kBTg== X-Forwarded-Encrypted: i=1; AJvYcCWfeBl1ppgcRV5a5a/HuCxxLa8sX1gVQV5iukMo7itw/vAgOl7/WWAoHEdpgwgR1Ojgk6SNx5P/gLxbvGGeCZ2ohlY= X-Gm-Message-State: AOJu0YzuWtn1UbA4eEbV6SkIx3yNEBHXY6iX4vvslyfmnYZui8qIUmuL 8cTxkJsfuGnDUM0TbtBx6wktFJTqD6XdyDNP09C2JzMyTaJzck3MEUbLNqDvRq+AVhDAtjcgpg6 32wnmFg3L38Ehp/O33Kfg+AqUh10HaaxkLo5kKwGDPgHG/4MA X-Received: by 2002:a05:6000:e4b:b0:367:998a:87b3 with SMTP id ffacd0b85a97d-36d5eb03f01mr486167f8f.28.1723187807899; Fri, 09 Aug 2024 00:16:47 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHYvY502q8JsmqHlkzADp4PV0Gc7OtZMHC9oUZX9AXJME4HJH4mLRZPYW58VetBNsRszDx9zw== X-Received: by 2002:a05:6000:e4b:b0:367:998a:87b3 with SMTP id ffacd0b85a97d-36d5eb03f01mr486144f8f.28.1723187807309; Fri, 09 Aug 2024 00:16:47 -0700 (PDT) Received: from ?IPV6:2003:cb:c71c:4e00:b097:7075:f6ba:300a? (p200300cbc71c4e00b0977075f6ba300a.dip0.t-ipconnect.de. [2003:cb:c71c:4e00:b097:7075:f6ba:300a]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36d2718030bsm4371518f8f.50.2024.08.09.00.16.46 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 09 Aug 2024 00:16:46 -0700 (PDT) Message-ID: Date: Fri, 9 Aug 2024 09:16:45 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH RFC 4/4] mm: guest_memfd: Add ability for mmap'ing pages To: Elliot Berman Cc: Andrew Morton , Paolo Bonzini , Sean Christopherson , Fuad Tabba , Patrick Roy , qperret@google.com, Ackerley Tng , linux-coco@lists.linux.dev, linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, kvm@vger.kernel.org References: <20240805-guest-memfd-lib-v1-0-e5a29a4ff5d7@quicinc.com> <20240805-guest-memfd-lib-v1-4-e5a29a4ff5d7@quicinc.com> <4cdd93ba-9019-4c12-a0e6-07b430980278@redhat.com> <20240806093625007-0700.eberman@hu-eberman-lv.qualcomm.com> <20240808101944778-0700.eberman@hu-eberman-lv.qualcomm.com> <6f3b5c38-fc33-43cd-8ab7-5b0f49169d5c@redhat.com> <20240808151910630-0700.eberman@hu-eberman-lv.qualcomm.com> From: David Hildenbrand Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoUQFV4R7y1KMEKoSyy8hQaK1umALTdL QZLQMzNE74ap+GDK0wnacPQFpcG1AE9RMq3aeErY5tujekBS32jfC/7AnH7I0v1v1TbbK3Gp XNeiN4QroO+5qaSr0ID2sz5jtBLRb15RMre27E1ImpaIv2Jw8NJgW0k/D1RyKCwaTsgRdwuK Kx/Y91XuSBdz0uOyU/S8kM1+ag0wvsGlpBVxRR/xw/E8M7TEwuCZQArqqTCmkG6HGcXFT0V9 PXFNNgV5jXMQRwU0O/ztJIQqsE5LsUomE//bLwzj9IVsaQpKDqW6TAPjcdBDPLHvriq7kGjt WhVhdl0qEYB8lkBEU7V2Yb+SYhmhpDrti9Fq1EsmhiHSkxJcGREoMK/63r9WLZYI3+4W2rAc UucZa4OT27U5ZISjNg3Ev0rxU5UH2/pT4wJCfxwocmqaRr6UYmrtZmND89X0KigoFD/XSeVv jwBRNjPAubK9/k5NoRrYqztM9W6sJqrH8+UWZ1Idd/DdmogJh0gNC0+N42Za9yBRURfIdKSb B3JfpUqcWwE7vUaYrHG1nw54pLUoPG6sAA7Mehl3nd4pZUALHwARAQABzSREYXZpZCBIaWxk ZW5icmFuZCA8ZGF2aWRAcmVkaGF0LmNvbT7CwZgEEwEIAEICGwMGCwkIBwMCBhUIAgkKCwQW AgMBAh4BAheAAhkBFiEEG9nKrXNcTDpGDfzKTd4Q9wD/g1oFAl8Ox4kFCRKpKXgACgkQTd4Q 9wD/g1oHcA//a6Tj7SBNjFNM1iNhWUo1lxAja0lpSodSnB2g4FCZ4R61SBR4l/psBL73xktp rDH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630-0700.eberman@hu-eberman-lv.qualcomm.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 31A584000E X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: y58md7a5h4y89iw9wnk6h7niwmbzb8ru X-HE-Tag: 1723187811-533399 X-HE-Meta: U2FsdGVkX1+B4R+hea3yy2tJmjXfGm1yY2lvTa1s5JKulnuB6jcWSiXmsBt3Uje3aAW+3As3/v14EkqJK1jDmuczIG1Eej9vdcOT59Jd89Jjl5OsJII26pREd64boJqxTWb1pjtkRwA2kepmYnAv7w+G6Mo6RDpsns0RUQuwSY4fcMQ4XKRC9bv3u38SO2XNDazgHf/Qnn8prYxISG3rvdSG5QUieC8SNzUv5pxF0Si/5KcKfydmqxJ8ARIE4fpLofLFs+cQUSVLXYF5q2tl9vLJuVLPWpRaT+QlxmKp3l29TiYbV9mMn7focekl6aRuHqUOY/RdqMLI6SIA2UvN3tl0Gra73CXa2aEHY4JKDyppV1UrNdn1+K2XckNT80n4m/7rOuEgBEMvqPRvJrzoSmkIPJEbZI6dSXt5fbk0aCDzpfuW2k+FD9dBp+nQ8ZYkgVajvaDVv8TRotcVa26AvrZguviqG+Zd9TkWg6xRj77xbLpuJ1Wq8gQozfEpRqgrT9kCR1nbnWrE6ui5gNzDojA37Vs+4Kl7JZgsdxIDF8Oigz+QIscM2aGdGeq5PU3p1esdSnO7SlQpxgca9b5vJaSu6tEEeOhliWo2wZmyV9ck1uddNSrtMdHwPR9ZGN9g/mx2wOJT6+iX48i1cVBqEcelk5ULsHmJmgzyKI+3Px+l4AjX+GK5TLaubBFQbRi+5qAHB7f7MV7BNmeSdDfLpzWSV1xscpOqXTAe9A54MCmVGgyj74JKtEQiixlzrD2GBxKhg2LV6mLF/TQf7McSNBLl+zfyxN2FXhkQjzFMc7b6X5HIDyZV1L84ZwWysR3CW7WgA+nBOWUVV73ocG1ppTmqIk9GaIMLDevNR5sBQuD5FyTykirzV3YMo6QTly5kvOTRtgCgcQqUd6vDlyRsVPGAPKNdQYC2M+gfs3+YGoVLGtyp7a36jw4/pECeZz/iPoYZqfrGA1Jq/olvh7O dRCz6WWU WmFNFeWx8cO2Ovl/ijH1KaAIIGCDa+8Qx9dK/X2fSXAIu7WofV9qG1WdFGqi6dSvkbvUUqAsSVA4b6cWfFetZ9+mOhpwCyqPeC0Kfh6JEfiFH5qQ9tyC3vN6c0K9Qj5VP2PQIQd/PtEcd32V9iUwWBFP/edSxOqpd04KUX6NxXDc1BuPgbZupRuMULLOibzz9LMHKw97HwT5+M1nKomrkjBSV9l6uyunEdef7a7Ua+vAJgIqETzt2doKWqth4tl+ts0WrKEs0oO9+V+S7g5YDDFwcOK8T6RC7VSE+V6gfWju09+PKAXF6eis9j3J0h0RLoUpjLkS14bIijMnhR4EpoD07VA1ns+vbmq5B8S4I8H2gETt4EMYTcvgh//f64DUspp3WZAmfRYFMbYrylbxvNbq3u6HnM5irRAnhWB9i/aHCVI4= 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 09.08.24 00:26, Elliot Berman wrote: > On Thu, Aug 08, 2024 at 11:55:15PM +0200, David Hildenbrand wrote: >> On 08.08.24 23:41, Elliot Berman wrote: >>> On Wed, Aug 07, 2024 at 06:12:00PM +0200, David Hildenbrand wrote: >>>> On 06.08.24 19:14, Elliot Berman wrote: >>>>> On Tue, Aug 06, 2024 at 03:51:22PM +0200, David Hildenbrand wrote: >>>>>>> - if (gmem_flags & GUEST_MEMFD_FLAG_NO_DIRECT_MAP) { >>>>>>> + if (!ops->accessible && (gmem_flags & GUEST_MEMFD_FLAG_NO_DIRECT_MAP)) { >>>>>>> r = guest_memfd_folio_private(folio); >>>>>>> if (r) >>>>>>> goto out_err; >>>>>>> @@ -107,6 +109,82 @@ struct folio *guest_memfd_grab_folio(struct file *file, pgoff_t index, u32 flags >>>>>>> } >>>>>>> EXPORT_SYMBOL_GPL(guest_memfd_grab_folio); >>>>>>> +int guest_memfd_make_inaccessible(struct file *file, struct folio *folio) >>>>>>> +{ >>>>>>> + unsigned long gmem_flags = (unsigned long)file->private_data; >>>>>>> + unsigned long i; >>>>>>> + int r; >>>>>>> + >>>>>>> + unmap_mapping_folio(folio); >>>>>>> + >>>>>>> + /** >>>>>>> + * We can't use the refcount. It might be elevated due to >>>>>>> + * guest/vcpu trying to access same folio as another vcpu >>>>>>> + * or because userspace is trying to access folio for same reason >>>>>> >>>>>> As discussed, that's insufficient. We really have to drive the refcount to 1 >>>>>> -- the single reference we expect. >>>>>> >>>>>> What is the exact problem you are running into here? Who can just grab a >>>>>> reference and maybe do nasty things with it? >>>>>> >>>>> >>>>> Right, I remember we had discussed it. The problem I faced was if 2 >>>>> vcpus fault on same page, they would race to look up the folio in >>>>> filemap, increment refcount, then try to lock the folio. One of the >>>>> vcpus wins the lock, while the other waits. The vcpu that gets the >>>>> lock vcpu will see the elevated refcount. >>>>> >>>>> I was in middle of writing an explanation why I think this is best >>>>> approach and realized I think it should be possible to do >>>>> shared->private conversion and actually have single reference. There >>>>> would be some cost to walk through the allocated folios and convert them >>>>> to private before any vcpu runs. The approach I had gone with was to >>>>> do conversions as late as possible. >>>> >>>> We certainly have to support conversion while the VCPUs are running. >>>> >>>> The VCPUs might be able to avoid grabbing a folio reference for the >>>> conversion and only do the folio_lock(): as long as we have a guarantee that >>>> we will disallow freeing the folio in gmem, for example, by syncing against >>>> FALLOC_FL_PUNCH_HOLE. >>>> >>>> So if we can rely on the "gmem" reference to the folio that cannot go away >>>> while we do what we do, we should be fine. >>>> >>>> >>>> >>>> Meanwhile, I was thinking if we would want to track the references we >>>> hand out to "safe" users differently. >>>> >>>> Safe references would only be references that would survive a >>>> private<->shared conversion, like KVM MMU mappings maybe? >>>> >>>> KVM would then have to be thought to return these gmem references >>>> differently. >>>> >>>> The idea would be to track these "safe" references differently >>>> (page->private?) and only allow dropping *our* guest_memfd reference if all >>>> these "safe" references are gone. That is, FALLOC_FL_PUNCH_HOLE would also >>>> fail if there are any "safe" reference remaining. >>>> >>>> <\random though> >>>> >>> >>> I didn't find a path in filemap where we can grab folio without >>> increasing its refcount. I liked the idea of keeping track of a "safe" >>> refcount, but I believe there is a small window to race comparing the >>> main folio refcount and the "safe" refcount. >> >> There are various possible models. To detect unexpected references, we could >> either use >> >> folio_ref_count(folio) == gmem_folio_safe_ref_count(folio) + 1 >> >> [we increment both ref counter] >> >> or >> >> folio_ref_count(folio) == 1 >> >> [we only increment the safe refcount and let other magic handle it as >> described] >> >> A vcpu could have >>> incremented the main folio refcount and on the way to increment the safe >>> refcount. Before that happens, another thread does the comparison and >>> sees a mismatch. >> >> Likely there won't be a way around coming up with code that is able to deal >> with such temporary, "speculative" folio references. >> >> In the simplest case, these references will be obtained from our gmem code >> only, and we'll have to detect that it happened and retry (a seqcount would >> be a naive solution). >> >> In the complex case, these references are temporarily obtained from other >> core-mm code -- using folio_try_get(). We can minimize some of them >> (speculative references from GUP or the pagecache), and try optimizing >> others (PFN walkers like page migration). >> >> But likely we'll need some retry magic, at least initially. >> > > I thought retry magic would not fly. I'll try this out. Any details why? At least the "other gmem code is currently taking a speculative reference" should be handable, these speculative references all happen from gmem code and it should be under our control. We can protect against some core-mm speculative references (GUP, page-cache): after we allocated pages for gmem, and a RCU grace period passed, these can no longer happen from old context that previously had these pages allocated before gmem allocated them. Other folio_try_get() users like memory offlining or page migration are more problematic. In general, the assumption is that they will give up quickly, for example when realizing that a folio is not LRU or non "non-lru-movable" -- which is the case for gmem-allocated pages. Yes, no retry magic would be very much preferred, but as soon as we want to map these folios to user space and have GUP work on them (IOW, we have to make the folio refcount usable), we cannot easily block all speculative references from core-mm, for example, by freezing the refcount at 0. Long term, we might find ways to just block these speculative references more efficiently / differently. -- Cheers, David / dhildenb