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 2A90F6A8D9 for ; Fri, 1 Mar 2024 11:17:02 +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=1709291824; cv=none; b=KLkqaDdbLhMmg4syjFgw9NvH6cl8v9ZjHhdPW9amaTU8f2yFhmngv125L6Go6Xai/uzvyswqLNsEQpXsZGOMU3Skg1nOyIzygtnN6erjNkcEL40+b91JjOVxQDIVNQHVMAFNTuYL6M5Ih5uc2eNDTSaUvc11nWMnJISGAChwCzE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709291824; c=relaxed/simple; bh=2F5INcybYSBlsTuC5lQQe/pcELvA6BCsUmGtOzzVYcM=; h=Message-ID:Date:MIME-Version:Subject:To:References:From: In-Reply-To:Content-Type; b=MdPGD1AiAZ/OMo2UCpc08FVBFLHczImg+mwSoQlupdE1o7Ev3CDHsx3cWmWsSvjMV7AWVi17IYoGs5yGIqj75buI9mIZI7Hb6fQKeKvt64RaGsajyBPexXzcT7hgBtd1t3WqgqPR/5PnEbZ5fD+8xrlgAtypbxv4cRM6YxI5EZA= 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=e10SIWXy; 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="e10SIWXy" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709291822; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to: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=l7h8L36t6ZHKdWAqV7lhkUriHsYrS8uCVVI7qYoH0Mk=; b=e10SIWXy1AVAx65atQ7iSTPL2xu7uUfjby4+MS56i70kyJdJ8Qx3JAy16mSk7/Aq3mKtBJ SX3s6R7xkjC22BZh4WtFg6GgBmXOtwu1JXQ0tVOjG8eWHSc0DSMUeZoRhQmtup80vG3VbF sbwrWl0Lp/zR33+XqrPXQ0TRHIarJXc= 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-663-s_C5jGUIM_ec48M6F8PqnA-1; Fri, 01 Mar 2024 06:16:58 -0500 X-MC-Unique: s_C5jGUIM_ec48M6F8PqnA-1 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-33d11878726so904124f8f.0 for ; Fri, 01 Mar 2024 03:16:58 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709291817; x=1709896617; h=content-transfer-encoding:in-reply-to:organization:autocrypt:from :references:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=l7h8L36t6ZHKdWAqV7lhkUriHsYrS8uCVVI7qYoH0Mk=; b=LSgTPRleSnE0wx2otoohQ0oLr6ql43miC4qQ7nOWW55hH6p8kBzanGYQvPufcYsP3A 48duDzinpxnLR7YVFl8rICVlVdojxaIO5xelhQHfF0muyE3xx+C605C1vYCh9rRxauJv moFl7k44IRkUU1+89vK0+qkvWJ2bQoeSeY4l3XKUxl1YnUWNow6H0SAj9LWsy5L2PgbD QIezr6yY5pnF4S46q7ib0MKqrPwgqajCuYwW021NQhhJ3AxBajxNUMKhggz2sbsBpVg6 FzZ/ZePb/jL3CSzb9bUS2AeBCF9zeR/bUZ0ty69LRLMZE9+U4l3oWaHuz9ZbcNG8TOun CeZQ== X-Forwarded-Encrypted: i=1; AJvYcCVJJ+ig3QeAo40tv9waCioNlcvz/b4ju8MqJvzU7tKVjVrLDYcoIglFcFS06rSsTYCeExvr2ZCZlAFkvrl/G+7qWi+Bc0eD X-Gm-Message-State: AOJu0YyVirYcNbNTFB4n/dpdax7g671ozDYcllcD1BNhKGUXajTMjas5 Ce+TsJBUTfHb96wWfkyOA+/wFTHs+zPneE1JBtZJAMHnii7rdlM7EKgPG6OwSX7+24wtBVIkFSe xDG75SUdyubH+Rh7kn0b2rcOQHELsF3Dwe0TbQl+4MIH/TzLu0WQ2Fw== X-Received: by 2002:a5d:4e81:0:b0:33d:a190:f0c with SMTP id e1-20020a5d4e81000000b0033da1900f0cmr1064033wru.16.1709291817524; Fri, 01 Mar 2024 03:16:57 -0800 (PST) X-Google-Smtp-Source: AGHT+IFp1isGyPm2fiySEuFOLcKwywN+DApFjRV+WU0CQMIn4Sv8KZcSZoBBQS3Ta/UefMvGZlkiSA== X-Received: by 2002:a5d:4e81:0:b0:33d:a190:f0c with SMTP id e1-20020a5d4e81000000b0033da1900f0cmr1063994wru.16.1709291817058; Fri, 01 Mar 2024 03:16:57 -0800 (PST) Received: from ?IPV6:2003:cb:c713:3200:77d:8652:169f:b5f7? (p200300cbc7133200077d8652169fb5f7.dip0.t-ipconnect.de. [2003:cb:c713:3200:77d:8652:169f:b5f7]) by smtp.gmail.com with ESMTPSA id cc6-20020a5d5c06000000b0033dd4673a4asm4408594wrb.71.2024.03.01.03.16.55 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 01 Mar 2024 03:16:56 -0800 (PST) Message-ID: Date: Fri, 1 Mar 2024 12:16:54 +0100 Precedence: bulk X-Mailing-List: kvmarm@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: folio_mmapped To: Fuad Tabba , Quentin Perret , Matthew Wilcox , kvm@vger.kernel.org, kvmarm@lists.linux.dev, pbonzini@redhat.com, chenhuacai@kernel.org, mpe@ellerman.id.au, anup@brainfault.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, seanjc@google.com, brauner@kernel.org, akpm@linux-foundation.org, xiaoyao.li@intel.com, yilun.xu@intel.com, chao.p.peng@linux.intel.com, jarkko@kernel.org, amoorthy@google.com, dmatlack@google.com, yu.c.zhang@linux.intel.com, isaku.yamahata@intel.com, mic@digikod.net, vbabka@suse.cz, vannapurve@google.com, ackerleytng@google.com, mail@maciej.szmigiero.name, michael.roth@amd.com, wei.w.wang@intel.com, liam.merwick@oracle.com, isaku.yamahata@gmail.com, kirill.shutemov@linux.intel.com, suzuki.poulose@arm.com, steven.price@arm.com, quic_mnalajal@quicinc.com, quic_tsoni@quicinc.com, quic_svaddagi@quicinc.com, quic_cvanscha@quicinc.com, quic_pderrin@quicinc.com, quic_pheragu@quicinc.com, catalin.marinas@arm.com, james.morse@arm.com, yuzenghui@huawei.com, oliver.upton@linux.dev, maz@kernel.org, will@kernel.org, keirf@google.com, linux-mm@kvack.org References: <40a8fb34-868f-4e19-9f98-7516948fc740@redhat.com> <20240226105258596-0800.eberman@hu-eberman-lv.qualcomm.com> <925f8f5d-c356-4c20-a6a5-dd7efde5ee86@redhat.com> <755911e5-8d4a-4e24-89c7-a087a26ec5f6@redhat.com> <99a94a42-2781-4d48-8b8c-004e95db6bb5@redhat.com> <20240229114526893-0800.eberman@hu-eberman-lv.qualcomm.com> From: David Hildenbrand Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1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kv AxBQXWeXhJI9XQssfrf4Gusdqx8nPEpfOqCtbbwJMATbHyqLt7/oz/5deGuwxgb65pWIzufa N7eop7uh+6bezi+rugUI+w6DABEBAAHCwXwEGAEIACYCGwwWIQQb2cqtc1xMOkYN/MpN3hD3 AP+DWgUCXw7HsgUJEqkpoQAKCRBN3hD3AP+DWrrpD/4qS3dyVRxDcDHIlmguXjC1Q5tZTwNB boaBTPHSy/Nksu0eY7x6HfQJ3xajVH32Ms6t1trDQmPx2iP5+7iDsb7OKAb5eOS8h+BEBDeq 3ecsQDv0fFJOA9ag5O3LLNk+3x3q7e0uo06XMaY7UHS341ozXUUI7wC7iKfoUTv03iO9El5f XpNMx/YrIMduZ2+nd9Di7o5+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: <20240229114526893-0800.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 >> I don't think that we can assume that only a single VMA covers a page. >> >>> But of course, no rmap walk is always better. >> >> We've been thinking some more about how to handle the case where the >> host userspace has a mapping of a page that later becomes private. >> >> One idea is to refuse to run the guest (i.e., exit vcpu_run() to back >> to the host with a meaningful exit reason) until the host unmaps that >> page, and check for the refcount to the page as you mentioned earlier. >> This is essentially what the RFC I sent does (minus the bugs :) ) . >> >> The other idea is to use the rmap walk as you suggested to zap that >> page. If the host tries to access that page again, it would get a >> SIGBUS on the fault. This has the advantage that, as you'd mentioned, >> the host doesn't need to constantly mmap() and munmap() pages. It >> could potentially be optimised further as suggested if we have a >> cooperating VMM that would issue a MADV_DONTNEED or something like >> that, but that's just an optimisation and we would still need to have >> the option of the rmap walk. However, I was wondering how practical >> this idea would be if more than a single VMA covers a page? >> > > Agree with all your points here. I changed Gunyah's implementation to do > the unmap instead of erroring out. I didn't observe a significant > performance difference. However, doing unmap might be a little faster > because we can check folio_mapped() before doing the rmap walk. When > erroring out at mmap() level, we always have to do the walk. Right. On the mmap() level you won't really have to walk page tables, as the the munmap() already zapped the page and removed the "problematic" VMA. Likely, you really want to avoid repeatedly calling mmap()+munmap() just to access shared memory; but that's just my best guess about your user space app :) > >> Also, there's the question of what to do if the page is gupped? In >> this case I think the only thing we can do is refuse to run the guest >> until the gup (and all references) are released, which also brings us >> back to the way things (kind of) are... >> > > If there are gup users who don't do FOLL_PIN, I think we either need to > fix them or live with possibility here? We don't have a reliable > refcount for a folio to be safe to unmap: it might be that another vCPU > is trying to get the same page, has incremented the refcount, and > waiting for the folio_lock. Likely there could be a way to detect that when only the vCPUs are your concern? But yes, it's nasty. (has to be handled in either case :() Disallowing any FOLL_GET|FOLL_PIN could work. Not sure how some core-kernel FOLL_GET users would react to that, though. See vma_is_secretmem() and folio_is_secretmem() in mm/gup.c, where we disallow any FOLL_GET|FOLL_PIN of secretmem pages. We'd need a way to teach core-mm similarly about guest_memfd, which might end up rather tricky, but not impossible :) > This problem exists whether we block the > mmap() or do SIGBUS. There is work on doing more conversion to FOLL_PIN, but some cases are harder to convert. Most of O_DIRECT should be using it nowadays, but some other known use cases don't. The simplest and readily-available example is still vmsplice(). I don't think it was fixed yet to use FOLL_PIN. Use vmsplice() to pin the page in the pipe (read-only). Unmap the VMA. You can read the page any time later by reading from the pipe. So I wouldn't bet on all relevant cases being gone in the near future. -- Cheers, David / dhildenb