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 41BAB12BF1D for ; Wed, 28 Feb 2024 11:11:36 +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=1709118699; cv=none; b=PSCoX3MAxoR/TzcpkD7wqwX/+xYR7P/TfAgj8YclmlkRVkQ92rPd9gMfYD7cyiwgbh7yV4uDTBmMeXyhUJVXXuO12Eq3Ahf3P28RzS9t9et3Q9LBkUioiSxRSiL4Ga8hTANlNkB4A+XuvZzlE8+156Ck605u4JxPe7olznMTK7Q= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709118699; c=relaxed/simple; bh=N69/suQ4MaSCYmrP4WB8YtYlGmUS63cenW87j39hg6Y=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=sAC6o5dnrYNm7kd/juaq6ah9RXL5OCjbqj+nwJmXvbqUtJhlcAw0IdWGNror6E/I8PeUsrA00NkBv7FB5iZTfYe7g8t2ElMVskqWvBCszBugpmw+/nbKqudnr2CTNOXhesIYtN40Wwf99aC9EM4+jLCycVrLs6dGuTiNC48zMU4= 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=jLFxQ/rU; 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="jLFxQ/rU" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709118696; 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=aUQ9sWDupyMNgIyawFwqXCHJWRUg3SyzP5KUns9QT5Y=; b=jLFxQ/rUFQvKvTE5QCYabOXE6DgdcN8ZGePQGpjffVWHlrkvqowsc4SxpAitrAilvR/i4X sNlVWfHofb2FBT4+Jv2vP4jLnFsRKnu3PiqiskGPbkuhywqSj38ah4LVO+kjpzIcCJ8Y1a OHwFmc+jJn0XIDy/9LA6ETOzyM6eG0s= Received: from mail-lj1-f199.google.com (mail-lj1-f199.google.com [209.85.208.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-643-LbGLbqO4OyCz1KNkpUkOPQ-1; Wed, 28 Feb 2024 06:11:34 -0500 X-MC-Unique: LbGLbqO4OyCz1KNkpUkOPQ-1 Received: by mail-lj1-f199.google.com with SMTP id 38308e7fff4ca-2d2b9de4ce6so6369541fa.2 for ; Wed, 28 Feb 2024 03:11:34 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709118693; x=1709723493; h=content-transfer-encoding:in-reply-to:organization:autocrypt:from :references:cc: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=aUQ9sWDupyMNgIyawFwqXCHJWRUg3SyzP5KUns9QT5Y=; b=soB5ZNAWk5uTicOHDMTk412iRI+j9WjfJgte7C6iqtHoykzIwg8E9vMsronQ+6O92y 1qaWmKloCZkEg/w9TDtidj+zzwOeXXrzaDZgQi98C+AC9v7E6oXI7xxWO6bCQ9Sy2us/ Rc1SRjQizelWET2e4NDlmJ9x+4BuV59FHECjrvv7wgjxWn1yN7s7iAAFvQSQfajCGiZD FCQs7nAVURQljmblo9U04x1OePOiWcSMIv3YmuJwuUfzoydoHjZy99jRRpkxuGmX2GNN xxmpkK5igO4UUSJAjZpue7R1aiJBDP3bpstGfOq7iHm8mCTOTDTagLjgD5NDSvMstOXS iBIA== X-Forwarded-Encrypted: i=1; AJvYcCUfXUGJs0MLnhBIBq3lnOYFGg0UFyR0PJ2yp6NyEu+dMpI4EsgVR669IrWBTLbEWIGuRE/3+40INCwPZqJBtRBTLmWnXw+N X-Gm-Message-State: AOJu0Yzp8Ark9oIQw8BcW3cjlGNKuXpGl1eh6gU9WgpZddHfvfmHhN5s MnO15Uy/ed/IIAWCSkyKTO4m8b3nbYtUTWnyLSKfy667YzKncw8kcYFvDJ8MjiYzIa2+7xSd0Il 7wwCBbMqLzcZDPuK7RCqBhmQqSIlBTwRmb0ugfkESb3xWh+Pre11XMg== X-Received: by 2002:a2e:a7c6:0:b0:2d2:3b18:6ffd with SMTP id x6-20020a2ea7c6000000b002d23b186ffdmr9973363ljp.41.1709118693394; Wed, 28 Feb 2024 03:11:33 -0800 (PST) X-Google-Smtp-Source: AGHT+IHqOhbD2iOLY+ugTcvnejnosxm0uWRyxRkcMKFvjB5ZSJwMYQ9xMyT7/OZhG22VLJQSEK3wcA== X-Received: by 2002:a2e:a7c6:0:b0:2d2:3b18:6ffd with SMTP id x6-20020a2ea7c6000000b002d23b186ffdmr9973300ljp.41.1709118692897; Wed, 28 Feb 2024 03:11:32 -0800 (PST) Received: from [10.32.64.237] (nat-pool-muc-t.redhat.com. [149.14.88.26]) by smtp.gmail.com with ESMTPSA id q16-20020adffed0000000b0033ce06c303csm14185846wrs.40.2024.02.28.03.11.31 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 28 Feb 2024 03:11:32 -0800 (PST) Message-ID: <755911e5-8d4a-4e24-89c7-a087a26ec5f6@redhat.com> Date: Wed, 28 Feb 2024 12:11:30 +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: Quentin Perret Cc: Matthew Wilcox , Fuad Tabba , 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, viro@zeniv.linux.org.uk, 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: <20240222161047.402609-1-tabba@google.com> <20240222141602976-0800.eberman@hu-eberman-lv.qualcomm.com> <40a8fb34-868f-4e19-9f98-7516948fc740@redhat.com> <20240226105258596-0800.eberman@hu-eberman-lv.qualcomm.com> <925f8f5d-c356-4c20-a6a5-dd7efde5ee86@redhat.com> From: David Hildenbrand Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiH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usdqx8nPEpfOqCtbbwJMATbHyqLt7/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: 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 28.02.24 11:48, Quentin Perret wrote: > On Tuesday 27 Feb 2024 at 15:59:37 (+0100), David Hildenbrand wrote: >>> >>> Ah, this was something I hadn't thought about. I think both Fuad and I >>> need to update our series to check the refcount rather than mapcount >>> (kvm_is_gmem_mapped for Fuad, gunyah_folio_lend_safe for me). >> >> An alternative might be !folio_mapped() && !folio_maybe_dma_pinned(). But >> checking for any unexpected references might be better (there are still some >> GUP users that don't use FOLL_PIN). > > As a non-mm person I'm not sure to understand to consequences of holding > a GUP pin to a page that is not covered by any VMA. The absence of VMAs > imply that userspace cannot access the page right? Presumably the kernel > can't be coerced into accessing that page either? Is that correct? Simple example: register the page using an iouring fixed buffer, then unmap the VMA. iouring now has the page pinned and can read/write it using an address in the kernel vitual address space (direct map). Then, you can happily make the kernel read/write that page using iouring, even though no VMA still covers/maps that page. [...] >> Instead of >> >> 1) Converting a page to private only if there are no unexpected >> references (no mappings, GUP pins, ...) and no VMAs covering it where >> we could fault it in later >> 2) Disallowing mmap when the range would contain any private page >> 3) Handling races between mmap and page conversion > > The one thing that makes the second option cleaner from a userspace > perspective (IMO) is that the conversion to private is happening lazily > during guest faults. So whether or not an mmapped page can indeed be > accessed from userspace will be entirely undeterministic as it depends > on the guest faulting pattern which userspace is entirely unaware of. > Elliot's suggestion would prevent spurious crashes caused by that > somewhat odd behaviour, though arguably sane userspace software > shouldn't be doing that to start with. The last sentence is the important one. User space should not access that memory. If it does, it gets a slap on the hand. Because it should not access that memory. We might even be able to export to user space which pages are currently accessible and which ones not (e.g., pagemap), although it would be racy as long as the VM is running and can trigger a conversion. > > To add a layer of paint to the shed, the usage of SIGBUS for > something that is really a permission access problem doesn't feel SIGBUS stands for "BUS error (bad memory access)." Which makes sense, if you try accessing something that can no longer be accessed. It's now inaccessible. Even if it is temporarily. Just like a page with an MCE error. Swapin errors. Etc. You cannot access it. It might be a permission problem on the pKVM side, but it's not the traditional "permission problem" as in mprotect() and friends. You cannot resolve that permission problem yourself. It's a higher entity that turned that memory inaccessible. > appropriate. Allocating memory via guestmem and donating that to a > protected guest is a way for userspace to voluntarily relinquish access > permissions to the memory it allocated. So a userspace process violating > that could, IMO, reasonably expect a SEGV instead of SIGBUS. By the > point that signal would be sent, the page would have been accounted > against that userspace process, so not sure the paging examples that > were discussed earlier are exactly comparable. To illustrate that > differently, given that pKVM and Gunyah use MMU-based protection, there > is nothing architecturally that prevents a guest from sharing a page > back with Linux as RO. Sure, then allow page faults that allow for reads and give a signal on write faults. In the scenario, it even makes more sense to not constantly require new mmap's from user space just to access a now-shared page. > Note that we don't currently support this, so I > don't want to conflate this use case, but that hopefully makes it a > little more obvious that this is a "there is a page, but you don't > currently have the permission to access it" problem rather than "sorry > but we ran out of pages" problem. We could user other signals, at least as the semantics are clear and it's documented. Maybe SIGSEGV would be warranted. I consider that a minor detail, though. Requiring mmap()/munmap() dances just to access a page that is now shared from user space sounds a bit suboptimal. But I don't know all the details of the user space implementation. "mmap() the whole thing once and only access what you are supposed to access" sounds reasonable to me. If you don't play by the rules, you get a signal. But I'm happy to learn more details. -- Cheers, David / dhildenb