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.133.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 3A5EC38F83 for ; Mon, 11 Nov 2024 22:14:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731363268; cv=none; b=ZLC2lMS7wvihXoBCVvPZnaIWPHyI7W1Hl5e0ISIQDIfT5ILHxwe6fQf2uWShZkyqMlJSwbN9JL13F/x/f2NnG3sYWS3JbkCpJrIs76UljO2r0mahkp8FDiZxUjx7gC0bK+ILWwksPkLncgVSB+6wja/KEjydGfq9uDpOLUONO4Q= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731363268; c=relaxed/simple; bh=4e2faVv4xnB+7UV+V5ti1fHu/d/YCpjKrYt+AP58qj0=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=H5Gt8E8gsAZw5GRw4DfAchTxxhG76dWsmFaj6/AMNfA6nuIa4yqaRHG4vSvx9uUwcBiG6UXMsUOCavtBAgkmwG98V+MOrPEV4VwFh98DI+COlbGASH2qW0bWUhkjqItB7p1oSXM+OtNOTODIDsZwVmcuJxHAamYQYjFnh8iyT/w= 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=eNNvI30K; arc=none smtp.client-ip=170.10.133.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="eNNvI30K" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1731363265; 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=i+vO5Chh4XyZzqgY6wUPooEn0FNBsk5Lf4RjLmwEopI=; b=eNNvI30KWvr8Rj8o5F6SvwT39uqMtzFnPKrutKDyjly3GjJ5NRL+pAI2yCNoZ0lgCShN+i X0EkEJrHSjei5n4X+CieQzeiwFWEjWSXpo9/o1PAxf41CwIciSw6i8d3OLf0wkDd/CTDJp eAg97s56UjbM9qfFxsuVJ1dBfu3q3eY= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-578-K66Z1T6GOoi15bFw4j5P0A-1; Mon, 11 Nov 2024 17:14:23 -0500 X-MC-Unique: K66Z1T6GOoi15bFw4j5P0A-1 X-Mimecast-MFC-AGG-ID: K66Z1T6GOoi15bFw4j5P0A Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-43163a40ee0so37838115e9.0 for ; Mon, 11 Nov 2024 14:14:23 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731363262; x=1731968062; 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=i+vO5Chh4XyZzqgY6wUPooEn0FNBsk5Lf4RjLmwEopI=; b=i65pHdpQOswzewpslCmIt+N29LioR/Xy7BvN6Zmw9u4qFsh28I76TGt260DPhPUq28 BpeZc7iJxKUz4bt0FyZgGwSVZ2iwbKCAB19H8qxhRvbSfwiC+nhYOD0j9tDmYD2nETfh Ijd2YIEP3iu9bCzHojSl2Y6U6qGmOUFHyqFDjWO7TfeHUarl6rK6cIZeHIcRce+F7XRt /pX6KoeEmNNKtQLl3JpOJIwmr94H5KlErQN/CDjRzvRaySPZ4ocUbIkO8TkdiI042fiC wXoAO3UhPkhUNFraS/7uHhZRLSTKI01QWzXhNWrskYOltWJL+h9lLt+5KpLEql84jjZj UQRA== X-Forwarded-Encrypted: i=1; AJvYcCVBUO92db4gK1PfQr3HzOzEaces8fYS2OMx4HIdlRuIi9fGGI1zzkCb4+rxtMvphiNAHluMkC496b8=@vger.kernel.org X-Gm-Message-State: AOJu0Yyj2oh9uN51joaRVueFBXv2d2YLkNPVaGakxJTXNHNJCKr1ZL6h gDF9cfgyWrj2bdi9vjtoxl0UppGwkad3PYNyEOOg7pUWCkBsO5LJD9VHzVgX++AO+v+43kPV3rK oN1Iw0BgXduMEEEY1qp+ZlQnrtj4pTRKRPoY9FxzbxQJbxlTWWNNhUDnqSQ== X-Received: by 2002:a05:600c:5493:b0:42e:75a6:bb60 with SMTP id 5b1f17b1804b1-432b7508b8amr127838195e9.19.1731363262550; Mon, 11 Nov 2024 14:14:22 -0800 (PST) X-Google-Smtp-Source: AGHT+IGVLiJ0g/s6yUPMim4b3RPUgDEhqfWOalae/b5Xgk//XG5JFwtKRIUcqOjRw8kzwpuRybK1EA== X-Received: by 2002:a05:600c:5493:b0:42e:75a6:bb60 with SMTP id 5b1f17b1804b1-432b7508b8amr127837945e9.19.1731363262115; Mon, 11 Nov 2024 14:14:22 -0800 (PST) Received: from ?IPV6:2003:cb:c730:4300:18eb:6c63:a196:d3a2? (p200300cbc730430018eb6c63a196d3a2.dip0.t-ipconnect.de. [2003:cb:c730:4300:18eb:6c63:a196:d3a2]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-432aa737721sm228937965e9.36.2024.11.11.14.14.19 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 11 Nov 2024 14:14:21 -0800 (PST) Message-ID: <6fbef654-36e2-4be5-906e-2a648a845278@redhat.com> Date: Mon, 11 Nov 2024 23:14:19 +0100 Precedence: bulk X-Mailing-List: linux-api@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [RFC PATCH 0/4] Add fbind() and NUMA mempolicy support for KVM guest_memfd To: Vlastimil Babka , Paolo Bonzini , Matthew Wilcox , Shivank Garg Cc: x86@kernel.org, viro@zeniv.linux.org.uk, brauner@kernel.org, jack@suse.cz, akpm@linux-foundation.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-api@vger.kernel.org, linux-arch@vger.kernel.org, kvm@vger.kernel.org, chao.gao@intel.com, pgonda@google.com, thomas.lendacky@amd.com, seanjc@google.com, luto@kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, arnd@arndb.de, kees@kernel.org, bharata@amd.com, nikunj@amd.com, michael.day@amd.com, Neeraj.Upadhyay@amd.com, linux-coco@lists.linux.dev References: <20241105164549.154700-1-shivankg@amd.com> <6004eaa4-934c-48f4-b502-cf7e436462fc@amd.com> <10ffac79-0dba-4c30-991e-f3ca2b5ff639@redhat.com> From: David Hildenbrand Content-Language: en-US Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoUQFV4R7y1KMEKoSyy8hQaK1umALTdL QZLQMzNE74ap+GDK0wnacPQFpcG1AE9RMq3aeErY5tujekBS32jfC/7AnH7I0v1v1TbbK3Gp XNeiN4QroO+5qaSr0ID2sz5jtBLRb15RMre27E1ImpaIv2Jw8NJgW0k/D1RyKCwaTsgRdwuK Kx/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mPx2iP5+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: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 11.11.24 12:02, Vlastimil Babka wrote: > On 11/8/24 18:31, Paolo Bonzini wrote: >> On 11/7/24 16:10, Matthew Wilcox wrote: >>> On Thu, Nov 07, 2024 at 02:24:20PM +0530, Shivank Garg wrote: >>>> The folio allocation path from guest_memfd typically looks like this... >>>> >>>> kvm_gmem_get_folio >>>> filemap_grab_folio >>>> __filemap_get_folio >>>> filemap_alloc_folio >>>> __folio_alloc_node_noprof >>>> -> goes to the buddy allocator >>>> >>>> Hence, I am trying to have a version of filemap_alloc_folio() that takes an mpol. >>> >>> It only takes that path if cpuset_do_page_mem_spread() is true. Is the >>> real problem that you're trying to solve that cpusets are being used >>> incorrectly? >> >> If it's false it's not very different, it goes to alloc_pages_noprof(). >> Then it respects the process's policy, but the policy is not >> customizable without mucking with state that is global to the process. >> >> Taking a step back: the problem is that a VM can be configured to have >> multiple guest-side NUMA nodes, each of which will pick memory from the >> right NUMA node in the host. Without a per-file operation it's not >> possible to do this on guest_memfd. The discussion was whether to use >> ioctl() or a new system call. The discussion ended with the idea of >> posting a *proposal* asking for *comments* as to whether the system call >> would be useful in general beyond KVM. >> >> Commenting on the system call itself I am not sure I like the >> file_operations entry, though I understand that it's the simplest way to >> implement this in an RFC series. It's a bit surprising that fbind() is >> a total no-op for everything except KVM's guest_memfd. >> >> Maybe whatever you pass to fbind() could be stored in the struct file *, >> and used as the default when creating VMAs; as if every mmap() was >> followed by an mbind(), except that it also does the right thing with >> MAP_POPULATE for example. Or maybe that's a horrible idea? > > mbind() manpage has this: > > The specified policy will be ignored for any MAP_SHARED > mappings in the specified memory range. Rather the pages will be allocated > according to the memory policy of the thread that caused the page to be > allocated. Again, this may not be the thread that called mbind(). I recall discussing that a couple of times in the context of QEMU. I have some faint recollection that the manpage is a bit imprecise: IIRC, hugetlb also ends up using the VMA policy for MAP_SHARED mappings during faults (huge_node()->get_vma_policy()) -- but in contrast to shmem, it doesn't end up becoming the "shared" policy for the file, used when accessed through other VMAs. > > So that seems like we're not very keen on having one user of a file set a > policy that would affect other users of the file? For VMs in QEMU we really want to configure the policy once in the main process and have all other processes (e.g., vhost-user) not worry about that when they mmap() guest memory. With shmem this works by "shared policy" design (below). For hugetlb, we rely on the fact that mbind()+MADV_POPULATE_WRITE allows us to preallocate NUMA-aware. So with hugetlb we really preallocate all guest RAM to guarantee the NUMA placement. It would not be the worst idea to have a clean interface to configure file-range policies instead of having this weird shmem mbind() behavior and the hugetlb hack. Having that said, other filesystem are rarely used for backing VMs, at least in combination with NUMA. So nobody really cared that much for now. Maybe fbind() would primarily only be useful for in-memory filesystems (shmem/hugetlb/guest_memfd). > > Now the next paragraph of the manpage says that shmem is different, and > guest_memfd is more like shmem than a regular file. > > My conclusion from that is that fbind() might be too broad and we don't want > this for actual filesystem-backed files? And if it's limited to guest_memfd, > it shouldn't be an fbind()? I was just once again diving into how mbind() on shmem is handled. And in fact, mbind() will call vma->set_policy() to update the per file-range policy. I wonder why we didn't do the same for hugetlb ... but of course, hugetlb must be special in any possible way. Not saying it's the best idea, but as we are talking about mmap support of guest_memfd (only allowing to fault in shared/faultable pages), one *could* look into implementing mbind()+vma->set_policy() for guest_memfd similar to how shmem handles it. It would require a (temporary) dummy VMA in the worst case (all private memory). It sounds a bit weird, though, to require a VMA to configure this, though. But at least it's similar to what shmem does ... -- Cheers, David / dhildenb