From mboxrd@z Thu Jan 1 00:00:00 1970 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 A37E616C878 for ; Fri, 9 Aug 2024 07:16:52 +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=1723187814; cv=none; b=ShKl3FI/0Ym3EFxiHQ/r3aOnC7iglI5LtiDcXy7dxQ6sO1pNCFQlqUwNPcObgCH8Zwq7jzfxe/K2N22BTsX19TAofEOmi8ALC1B2TWPTFPE6djfGg2L08jE3aK6jR/FHSkfqGrYIZXIObqt7edHSNv+h0azHDBeUj9KIJl2ruzI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723187814; c=relaxed/simple; bh=/IODmwfYieU5IO6p13ZgFbK/dzcp4i/Wgb9xnFF8XhM=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=Ed9tpq0NBsrk3Z2yOVeJc7I02TiSgi7rgTmm3ts6Deq5XGeyP6sSoywWzozGtHZNkh9Rg1SgvKUQWFN1buppjzn+c1WSI143GqeM8xlcLdgHw0EyYcJWscTXDdst756yoA1s9XqroeiFPg5RmAJ6cmm/bLWIXi1WTqXvZ9p62GA= 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=V4HV+24A; 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="V4HV+24A" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1723187811; 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=V4HV+24AzvH1Nw5gdd+tpuew5yyTgrJAXkJhEJQR7+PAZlPSmfDRS+QjRXgjaBMV5b4x2w PZkOPS6QQf7pHj/489v15Lqxp1fgjnriH5pf6vL1gI3h+FC/7urMbtxDwr40lUuUn+D6Di Cq3EiuIQ69tsRo+Xa5kslyRerYo0zaM= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-527-36y5_2-hPjuL-VwRaKDLkw-1; Fri, 09 Aug 2024 03:16:49 -0400 X-MC-Unique: 36y5_2-hPjuL-VwRaKDLkw-1 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-3686d709f6dso1006787f8f.0 for ; Fri, 09 Aug 2024 00:16:49 -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=CXqWw2psrRuo+1VSCSRqL/h+HzG/wZKSqX6c5i/FOp2vSzRFdK5sN0XV0UTJZTLZ8o qlhJ/bqaZGuRiXkz0SisGXmptcr1oky1DqV3aHydm9tLbpY5bVt411jwz8jS3sJrl89s nzkbu/FNpjFi1IlyRGnzHVtwBIxrZ4oV3wa3jKwR6kdviY7+d7Rmax7jWLRxgMMuzW2I IQZ4011Bo1U7c6E/aSu05tbPS5PkZv+IyNb7JWIa1ucNopGeE/hh71l3kKYZBFdFZN5B 7VYe34Y3Aabqb+dfcJ82shsCmpOZpVqp0VLEoH/3J2AUmaytEP3eArdss6Qswu4wuLPE 13kA== X-Forwarded-Encrypted: i=1; AJvYcCVd9QJuigfwdkKcSPJF+1hPTSQivAvAk5qCrNPf3Ag3BEYLMqll8TMAT76LrbsuR7X64WYUcLB2AA3v/O/Jh/0Jz6uouF1QRdPvIg== X-Gm-Message-State: AOJu0YwwLG2lVYTmdK89HayPgNdCuOVRJnG/xvpVuuL86Mc70w3/V9SN FjZMvYwmBUXhfcwt5WMBPdpTzj5F3vo6NX2lS++nogtQFI2q3XAfRznzjEPGoVRMl7J2EgxwMMk AiGTbXOrBf0XuukCN5wI7ndp5DWxFzYJnSxDdl15Tfla791HTxXPBnSCvVGo= X-Received: by 2002:a05:6000:e4b:b0:367:998a:87b3 with SMTP id ffacd0b85a97d-36d5eb03f01mr486166f8f.28.1723187807898; 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 Precedence: bulk X-Mailing-List: linux-coco@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: 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//bLwzj9IVsaQpKDqW6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di7o5+KIwlb2mAB9sTNHdMrXesX8eBL6T9b+MZJk+mZuPxKNVfEQMQ a5SxUEADIPQTPNvBewdeI80yeOCrN+Zzwy/Mrx9EPeu59Y5vSJOx/z6OUImD/GhX7Xvkt3kq Er5KTrJz3++B6SH9pum9PuoE/k+nntJkNMmQpR4MCBaV/J9gIOPGodDKnjdng+mXliF3Ptu6 3oxc2RCyGzTlxyMwuc2U5Q7KtUNTdDe8T0uE+9b8BLMVQDDfJjqY0VVqSUwImzTDLX9S4g/8 kC4HRcclk8hpyhY2jKGluZO0awwTIMgVEzmTyBphDg/Gx7dZU1Xf8HFuE+UZ5UDHDTnwgv7E th6RC9+WrhDNspZ9fJjKWRbveQgUFCpe1sa77LAw+XFrKmBHXp9ZVIe90RMe2tRL06BGiRZr jPrnvUsUUsjRoRNJjKKA/REq+sAnhkNPPZ/NNMjaZ5b8Tovi8C0tmxiCHaQYqj7G2rgnT0kt WNyWQQ== Organization: Red Hat In-Reply-To: <20240808151910630-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 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