From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D28FE3C17 for ; Tue, 4 Mar 2025 00:19:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741047545; cv=none; b=ImVXlrxe4f1BIAf6JtIMsW8AS4wo2rA73AkGwsp7ySsfxk50p5wDohFHLHHLa+ohw9wjQ9lb0Dd+loKcAttYO6maHXtIVl1ha0B9f1jDPemyCVSHkZCvF/MQs09UEJunGZtjQ5+AL5iELXxREfIKscj/vDNbbBOe/2aSi8SehUI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741047545; c=relaxed/simple; bh=om4PCQPoPa4VZTX5RXZmkisQn/L+HobOuJGn+TrX82k=; h=Date:In-Reply-To:Mime-Version:Message-ID:Subject:From:To:Cc: Content-Type; b=nEr/LbTci1TlzwC00x9E/hAYFXoOVmcry5XrlZ9zCd/MFzM5Tpiwjo8CzFtIwI+EQ6zfULap2cq43UdwKE1QoRk3JWXe+EwRZhvzXHpyvDPv9fTiMA4xwQ7gvnmZ1fZCx6XRAIfaBxv9d0MG8X5IQwfxKS1WxSZIURyLY90Vk8A= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ackerleytng.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=rEXHFwOB; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ackerleytng.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="rEXHFwOB" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2fea1685337so8952741a91.0 for ; Mon, 03 Mar 2025 16:19:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1741047543; x=1741652343; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:in-reply-to:date:from:to :cc:subject:date:message-id:reply-to; bh=TDyYj5B62h+Bnx/iegfnSRCOJrRTTkL1sRbV1d02P2o=; b=rEXHFwOBLn3iPcfx0HDFxrfJ9ESIe29A9k6SXpUfMIFkqxBIfdNkkq4q46GgboCI8j ouVfCyLxdSvYz8C1QeW8uqfQ2p4Coyc1cRR3CYBCegP9Qr+gi2+aNnoo97g/ahlu04qD irRnTLqku7FD+O+UT8Njr23By0s7GRwA8hbI18y3KWUoFxvWspgTR7CtQVXr+xDNkIhy w2MYdpJ+ODvQVxMqPbjAXcKFhB/mWR/gLzHMGtm53NHPGY1OYamQO0vZ2qUAtEoNlGyM xV2H02obUJwFf1HB8jDFEsWKA8sWDOsFtlEIdN3oQbbBGUiooczdh1/zy6l93kW/pECG FDIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741047543; x=1741652343; h=cc:to:from:subject:message-id:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=TDyYj5B62h+Bnx/iegfnSRCOJrRTTkL1sRbV1d02P2o=; b=pdiZb9g/uBnDKi1WsDvTmz33dTF/+rKRIircwT18io7a70ySQpDtq1DltcXVGPZW+K GJmcyUoDU0JK7tV2dJb1pBBUa3bp5vuAZ62pjT5tDuF0m1NddNPv12Cla0QRmRr7qDRe BNiFRnsZoORiGRrtRCALMTP1u1Iplbts/SP05Y+QJB72DXGR7omAEH92zSPCMW+PvH/M mt7FI1L/lzFppIVXurAYmeki1Kt2O0guXT8YOohfVsLBSOxuQWxW8Ha0NV0LFRvSPDsI SZykru9vsnhFpB2u1v4ICFah/rNJdHY1fo/2v1RFauxDNCkEtWlTNBIiMgQtwCj5nqBD opUw== X-Forwarded-Encrypted: i=1; AJvYcCVsJPPme/zOVRBfqbEejSVBaW7o2CJJyeWHtpeleCT50rHdtlWtYYF46A3+4nzhQ53jQHXaZDDRhOscCIw=@vger.kernel.org X-Gm-Message-State: AOJu0YzWXks6/We3EU72aa9od8XGGBwHxECb+0bRUQRf/Urh5NQHsMIT cpZ4Vzj//MN7f9EWCh1YdufFezR7F+MM3EOqUXSLmvcQCDpLJBqj+sxxWuY9qMWrM4UTjRXp/u+ kehsY7oY86hYmFaH9GSRC5A== X-Google-Smtp-Source: AGHT+IGjrHco3Rt+VZziwKrqBORZRwSaK6/c6j7Oy5v1wwCg0MU8Clnd3M8Xq66zBx5JDkHN1Q0qDWfyKXWIOSRuKw== X-Received: from pfbei32.prod.google.com ([2002:a05:6a00:80e0:b0:725:1ef3:c075]) (user=ackerleytng job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:72a1:b0:1e1:a449:ff71 with SMTP id adf61e73a8af0-1f3390f51f8mr2373495637.1.1741047543098; Mon, 03 Mar 2025 16:19:03 -0800 (PST) Date: Tue, 04 Mar 2025 00:19:01 +0000 In-Reply-To: (message from Vlastimil Babka on Mon, 3 Mar 2025 09:58:51 +0100) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Message-ID: Subject: Re: [PATCH v6 4/5] KVM: guest_memfd: Enforce NUMA mempolicy using shared policy From: Ackerley Tng To: Vlastimil Babka Cc: shivankg@amd.com, akpm@linux-foundation.org, willy@infradead.org, pbonzini@redhat.com, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, chao.gao@intel.com, seanjc@google.com, david@redhat.com, bharata@amd.com, nikunj@amd.com, michael.day@amd.com, Neeraj.Upadhyay@amd.com, thomas.lendacky@amd.com, michael.roth@amd.com, tabba@google.com Content-Type: text/plain; charset="UTF-8" Vlastimil Babka writes: > On 2/28/25 18:25, Ackerley Tng wrote: >> Shivank Garg writes: >> >>> Previously, guest-memfd allocations followed local NUMA node id in absence >>> of process mempolicy, resulting in arbitrary memory allocation. >>> Moreover, mbind() couldn't be used since memory wasn't mapped to userspace >>> in the VMM. >>> >>> Enable NUMA policy support by implementing vm_ops for guest-memfd mmap >>> operation. This allows the VMM to map the memory and use mbind() to set >>> the desired NUMA policy. The policy is then retrieved via >>> mpol_shared_policy_lookup() and passed to filemap_grab_folio_mpol() to >>> ensure that allocations follow the specified memory policy. >>> >>> This enables the VMM to control guest memory NUMA placement by calling >>> mbind() on the mapped memory regions, providing fine-grained control over >>> guest memory allocation across NUMA nodes. >>> >>> The policy change only affect future allocations and does not migrate >>> existing memory. This matches mbind(2)'s default behavior which affects >>> only new allocations unless overridden with MPOL_MF_MOVE/MPOL_MF_MOVE_ALL >>> flags, which are not supported for guest_memfd as it is unmovable. >>> >>> Suggested-by: David Hildenbrand >>> Signed-off-by: Shivank Garg >>> --- >>> virt/kvm/guest_memfd.c | 76 +++++++++++++++++++++++++++++++++++++++++- >>> 1 file changed, 75 insertions(+), 1 deletion(-) >>> >>> diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c >>> index f18176976ae3..b3a8819117a0 100644 >>> --- a/virt/kvm/guest_memfd.c >>> +++ b/virt/kvm/guest_memfd.c >>> @@ -2,6 +2,7 @@ >>> #include >>> #include >>> #include >>> +#include >>> #include >>> #include >>> >>> @@ -11,8 +12,12 @@ struct kvm_gmem { >>> struct kvm *kvm; >>> struct xarray bindings; >>> struct list_head entry; >>> + struct shared_policy policy; >>> }; >>> >> >> struct shared_policy should be stored on the inode rather than the file, >> since the memory policy is a property of the memory (struct inode), >> rather than a property of how the memory is used for a given VM (struct >> file). > > That makes sense. AFAICS shmem also uses inodes to store policy. > >> When the shared_policy is stored on the inode, intra-host migration [1] >> will work correctly, since the while the inode will be transferred from >> one VM (struct kvm) to another, the file (a VM's view/bindings of the >> memory) will be recreated for the new VM. >> >> I'm thinking of having a patch like this [2] to introduce inodes. > > shmem has it easier by already having inodes > >> With this, we shouldn't need to pass file pointers instead of inode >> pointers. > > Any downsides, besides more work needed? Or is it feasible to do it using > files now and convert to inodes later? > > Feels like something that must have been discussed already, but I don't > recall specifics. Here's where Sean described file vs inode: "The inode is effectively the raw underlying physical storage, while the file is the VM's view of that storage." [1]. I guess you're right that for now there is little distinction between file and inode and using file should be feasible, but I feel that this dilutes the original intent. Something like [2] doesn't seem like too big of a change and could perhaps be included earlier rather than later, since it will also contribute to support for restricted mapping [3]. [1] https://lore.kernel.org/all/ZLGiEfJZTyl7M8mS@google.com/ [2] https://lore.kernel.org/all/d1940d466fc69472c8b6dda95df2e0522b2d8744.1726009989.git.ackerleytng@google.com/ [3] https://lore.kernel.org/all/20250117163001.2326672-1-tabba@google.com/T/