From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C9DA3CAC5B8 for ; Fri, 26 Sep 2025 19:36:34 +0000 (UTC) Received: from boromir.ozlabs.org (localhost [127.0.0.1]) by lists.ozlabs.org (Postfix) with ESMTP id 4cYLS13ZkRz2xjv; Sat, 27 Sep 2025 05:36:33 +1000 (AEST) Authentication-Results: lists.ozlabs.org; arc=none smtp.remote-ip="2607:f8b0:4864:20::104a" ARC-Seal: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1758915393; cv=none; b=AOFFEl81tbQiB6fOwP1zi5ID6t0cgvNCJ3SxO3ubPDe5iyEPQiUSMJyADNnbLFpzm/8wEWX+k9NQ84tBnZM8AoL9Ba+5FmdpSlyvaXxyUW5Ikv8zP4oul5GJSyV69wK8LfAze4pMKAVFRerURRM4YDeox47TrWtBTz1M4QWvurOlKigKYokSJFWWYXX8cXCtLImFNcblBFZrjxUbHmqxy94dskC24Ml7cUNYTR7yDf1hvChWoX6iZEQavGGAnXNDeMfd/ik5a/4wE6OCNdhnJZDkx0JLtC0/VcTAjfbzEpW7Jz/PBeO8bFQ1g6iqiTs/q08xxYwdYE6vE4Q1BKSQaw== ARC-Message-Signature: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1758915393; c=relaxed/relaxed; bh=Ob/iOTJHVtIxulD10+qfYKaLq05XmILMIQZZtCe3LNU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Cw3mrOgDiT3iD0flwhCZqXBk968xfp9qxhJ37jHd5x11IC+5NGg0XObMXio9DaZOc/NwCbhUsyNbKKpopkNaYf/ycGUNpwSEut8NHp42MuqoZDJ76o4c6CojmEiGI71XUpNKop6yPuO0XEOxqcxNEfjhg1CVTLAIMtM3X/Lut4qPhglCR+n3vG5V2RSbN/pbpV5CvIhJVGTx6xvvvReSBmSksdZlU3QR7rFBcUqq3AQCuZ7r4rrrWvYshdQTtSrhqbZcVYBVm/eA6J7u7PmzPThLxOAVLa6DyxmLQgv5FbSS9gZsXos6ciqyrigYYkl+tfeb3rxtvUL490sSB4Du3Q== ARC-Authentication-Results: i=1; lists.ozlabs.org; dmarc=pass (p=reject dis=none) header.from=google.com; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20230601 header.b=cETO3gsD; dkim-atps=neutral; spf=pass (client-ip=2607:f8b0:4864:20::104a; helo=mail-pj1-x104a.google.com; envelope-from=3povwaaykc7stfbokdhpphmf.dpnmjovy-fspgtmjtut.p0mbct.psh@flex--seanjc.bounces.google.com; receiver=lists.ozlabs.org) smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: lists.ozlabs.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20230601 header.b=cETO3gsD; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=flex--seanjc.bounces.google.com (client-ip=2607:f8b0:4864:20::104a; helo=mail-pj1-x104a.google.com; envelope-from=3povwaaykc7stfbokdhpphmf.dpnmjovy-fspgtmjtut.p0mbct.psh@flex--seanjc.bounces.google.com; receiver=lists.ozlabs.org) Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4cYLS02gtXz2xK5 for ; Sat, 27 Sep 2025 05:36:31 +1000 (AEST) Received: by mail-pj1-x104a.google.com with SMTP id 98e67ed59e1d1-33428befc3aso2809331a91.2 for ; Fri, 26 Sep 2025 12:36:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1758915389; x=1759520189; darn=lists.ozlabs.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Ob/iOTJHVtIxulD10+qfYKaLq05XmILMIQZZtCe3LNU=; b=cETO3gsDcHAu5fY5fDZQWapc1oJGhrYEQQBsZtdbtjatU28hZRJf1EN6fzg6HvyTZp LAEmKAzBChNSUuxi5pboC3ip44Mg7FJ/aGf/+xQshI6eS3StQUc0PiGVl/P0ZqyPskEZ tvrPLGEQR7+Fy09cXlbCXkobas09WUgv1GahJoGEnMFLvT3v0Wq4VM1Uv3fWEeLWhx6X ELdVkzENo+8Z0B1lWfbsO8fasPkdQ4Uc24FFNCW2oqTFb7Go/hk/w3a6sdyp/cdRbO6m vqeti0tmaBY47T3kKHwn10Q32ow8gRyEIY1+p0dTjpIghoFy4lbkl4K2pUJsC06aCAtc 6QpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758915389; x=1759520189; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Ob/iOTJHVtIxulD10+qfYKaLq05XmILMIQZZtCe3LNU=; b=pnO7MLMEaOfG4L5DTVRa8nB7LBqnsyYQxCZBjRTG/1TrT0MnnL9WsHnjgM8YN9Txq6 ZbB001+kqlHJV/VG0/Sh96gIqIPNBAorlYshcb1CY704TVly+sh642RLXY8wv3Yp0rva u+d1bkBdrRkfgRA7jwiFPmsdA9Z16wghaJU78PA0nq8Zz1PJrE+J7Ye867wQ38vjz1GH oH3CQcfYogoqrieE0DqHvthmEd1XG+oJOIOWe43MBex5yCU/p7emSuBLl8Zy4d/SE4a1 aTDgZq9k9/5MTMZ9OvikYj0Iu92DFlCjY7/lwwZ9izsGZZCKnT8rRmKcqWcvR+BBX5re TkCQ== X-Forwarded-Encrypted: i=1; AJvYcCVSyJt3Cios5ciO0Ov43qWFgnA9P91KqDFKHtgWQBAjNP7rWFAp4QzYnVBwxdMZ25c/aK33dx7U/E2NZw==@lists.ozlabs.org X-Gm-Message-State: AOJu0YwH/e7DN59hkvQoLuZQ4bX8U/GzfM7O1ggPEqsW5hNk9DI0QntA W9+aBU6XP3w96hY58liPscMfnYW2sXF2ZuHaQGr43nom868F49F4BKKJee635bP9GGorGoHjkGi hFy3pug== X-Google-Smtp-Source: AGHT+IFDksLZfiXKr/BILK4eWMYTpySqi06A6DOxX2zeNKzHO6pmJyAGRZ48y/vvPgWC6WRjoIw3qUZ+1+0= X-Received: from pjbaz14.prod.google.com ([2002:a17:90b:28e:b0:32d:e264:a78e]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1808:b0:32b:d8bf:c785 with SMTP id 98e67ed59e1d1-3342a2c3979mr8949944a91.20.1758915388771; Fri, 26 Sep 2025 12:36:28 -0700 (PDT) Date: Fri, 26 Sep 2025 12:36:27 -0700 In-Reply-To: X-Mailing-List: linux-erofs@lists.ozlabs.org List-Id: List-Help: List-Owner: List-Post: List-Subscribe: , , List-Unsubscribe: Precedence: list Mime-Version: 1.0 References: <20250827175247.83322-2-shivankg@amd.com> <20250827175247.83322-9-shivankg@amd.com> Message-ID: Subject: Re: [PATCH kvm-next V11 6/7] KVM: guest_memfd: Enforce NUMA mempolicy using shared policy From: Sean Christopherson To: Shivank Garg Cc: willy@infradead.org, akpm@linux-foundation.org, david@redhat.com, pbonzini@redhat.com, shuah@kernel.org, vbabka@suse.cz, brauner@kernel.org, viro@zeniv.linux.org.uk, dsterba@suse.com, xiang@kernel.org, chao@kernel.org, jaegeuk@kernel.org, clm@fb.com, josef@toxicpanda.com, kent.overstreet@linux.dev, zbestahu@gmail.com, jefflexu@linux.alibaba.com, dhavale@google.com, lihongbo22@huawei.com, lorenzo.stoakes@oracle.com, Liam.Howlett@oracle.com, rppt@kernel.org, surenb@google.com, mhocko@suse.com, ziy@nvidia.com, matthew.brost@intel.com, joshua.hahnjy@gmail.com, rakie.kim@sk.com, byungchul@sk.com, gourry@gourry.net, ying.huang@linux.alibaba.com, apopple@nvidia.com, tabba@google.com, ackerleytng@google.com, paul@paul-moore.com, jmorris@namei.org, serge@hallyn.com, pvorel@suse.cz, bfoster@redhat.com, vannapurve@google.com, chao.gao@intel.com, bharata@amd.com, nikunj@amd.com, michael.day@amd.com, shdhiman@amd.com, yan.y.zhao@intel.com, Neeraj.Upadhyay@amd.com, thomas.lendacky@amd.com, michael.roth@amd.com, aik@amd.com, jgg@nvidia.com, kalyazin@amazon.com, peterx@redhat.com, jack@suse.cz, hch@infradead.org, cgzones@googlemail.com, ira.weiny@intel.com, rientjes@google.com, roypat@amazon.co.uk, chao.p.peng@intel.com, amit@infradead.org, ddutile@redhat.com, dan.j.williams@intel.com, ashish.kalra@amd.com, gshan@redhat.com, jgowans@amazon.com, pankaj.gupta@amd.com, papaluri@amd.com, yuzhao@google.com, suzuki.poulose@arm.com, quic_eberman@quicinc.com, linux-bcachefs@vger.kernel.org, linux-btrfs@vger.kernel.org, linux-erofs@lists.ozlabs.org, linux-f2fs-devel@lists.sourceforge.net, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-coco@lists.linux.dev Content-Type: text/plain; charset="us-ascii" On Thu, Sep 25, 2025, Sean Christopherson wrote: > On Wed, Aug 27, 2025, Shivank Garg wrote: > > @@ -26,6 +28,9 @@ static inline struct kvm_gmem_inode_info *KVM_GMEM_I(struct inode *inode) > > return container_of(inode, struct kvm_gmem_inode_info, vfs_inode); > > } > > > > +static struct mempolicy *kvm_gmem_get_pgoff_policy(struct kvm_gmem_inode_info *info, > > + pgoff_t index); > > + > > /** > > * folio_file_pfn - like folio_file_page, but return a pfn. > > * @folio: The folio which contains this index. > > @@ -112,7 +117,25 @@ static int kvm_gmem_prepare_folio(struct kvm *kvm, struct kvm_memory_slot *slot, > > static struct folio *kvm_gmem_get_folio(struct inode *inode, pgoff_t index) > > { > > /* TODO: Support huge pages. */ > > - return filemap_grab_folio(inode->i_mapping, index); > > + struct mempolicy *policy; > > + struct folio *folio; > > + > > + /* > > + * Fast-path: See if folio is already present in mapping to avoid > > + * policy_lookup. > > + */ > > + folio = __filemap_get_folio(inode->i_mapping, index, > > + FGP_LOCK | FGP_ACCESSED, 0); > > + if (!IS_ERR(folio)) > > + return folio; > > + > > + policy = kvm_gmem_get_pgoff_policy(KVM_GMEM_I(inode), index); > > + folio = __filemap_get_folio_mpol(inode->i_mapping, index, > > + FGP_LOCK | FGP_ACCESSED | FGP_CREAT, > > + mapping_gfp_mask(inode->i_mapping), policy); > > + mpol_cond_put(policy); > > + > > + return folio; > > } > > > > static void kvm_gmem_invalidate_begin(struct kvm_gmem *gmem, pgoff_t start, > > @@ -372,8 +395,45 @@ static vm_fault_t kvm_gmem_fault_user_mapping(struct vm_fault *vmf) > > return ret; > > } > > > > +#ifdef CONFIG_NUMA > > +static int kvm_gmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol) > > +{ > > + struct inode *inode = file_inode(vma->vm_file); > > + > > + return mpol_set_shared_policy(&KVM_GMEM_I(inode)->policy, vma, mpol); > > +} > > + > > +static struct mempolicy *kvm_gmem_get_policy(struct vm_area_struct *vma, > > + unsigned long addr, pgoff_t *pgoff) > > +{ > > + struct inode *inode = file_inode(vma->vm_file); > > + > > + *pgoff = vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT); > > + return mpol_shared_policy_lookup(&KVM_GMEM_I(inode)->policy, *pgoff); > > +} > > + > > +static struct mempolicy *kvm_gmem_get_pgoff_policy(struct kvm_gmem_inode_info *info, > > + pgoff_t index) > > I keep reading this is "page offset policy", as opposed to "policy given a page > offset". Another oddity that is confusing is that this helper explicitly does > get_task_policy(current), while kvm_gmem_get_policy() lets the caller do that. > The end result is the same, but I think it would be helpful for gmem to be > internally consistent. > > If we have kvm_gmem_get_policy() use this helper, then we can kill two birds with > one stone: > > static struct mempolicy *__kvm_gmem_get_policy(struct gmem_inode *gi, > pgoff_t index) > { > struct mempolicy *mpol; > > mpol = mpol_shared_policy_lookup(&gi->policy, index); > return mpol ? mpol : get_task_policy(current); > } > > static struct mempolicy *kvm_gmem_get_policy(struct vm_area_struct *vma, > unsigned long addr, pgoff_t *pgoff) > { > *pgoff = vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT); > > return __kvm_gmem_get_policy(GMEM_I(file_inode(vma->vm_file)), *pgoff); Argh!!!!! This breaks the selftest because do_get_mempolicy() very specifically falls back to the default_policy, NOT to the current task's policy. That is *exactly* the type of subtle detail that needs to be commented, because there's no way some random KVM developer is going to know that returning NULL here is important with respect to get_mempolicy() ABI. On a happier note, I'm very glad you wrote a testcase :-) I've got this as fixup-to-the-fixup: diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index e796cc552a96..61130a52553f 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -114,8 +114,8 @@ static int kvm_gmem_prepare_folio(struct kvm *kvm, struct kvm_memory_slot *slot, return r; } -static struct mempolicy *__kvm_gmem_get_policy(struct gmem_inode *gi, - pgoff_t index) +static struct mempolicy *kvm_gmem_get_folio_policy(struct gmem_inode *gi, + pgoff_t index) { #ifdef CONFIG_NUMA struct mempolicy *mpol; @@ -151,7 +151,7 @@ static struct folio *kvm_gmem_get_folio(struct inode *inode, pgoff_t index) if (!IS_ERR(folio)) return folio; - policy = __kvm_gmem_get_policy(GMEM_I(inode), index); + policy = kvm_gmem_get_folio_policy(GMEM_I(inode), index); folio = __filemap_get_folio_mpol(inode->i_mapping, index, FGP_LOCK | FGP_ACCESSED | FGP_CREAT, mapping_gfp_mask(inode->i_mapping), policy); @@ -431,9 +431,18 @@ static int kvm_gmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpo static struct mempolicy *kvm_gmem_get_policy(struct vm_area_struct *vma, unsigned long addr, pgoff_t *pgoff) { + struct inode *inode = file_inode(vma->vm_file); + *pgoff = vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT); - return __kvm_gmem_get_policy(GMEM_I(file_inode(vma->vm_file)), *pgoff); + /* + * Note! Directly return whatever the lookup returns, do NOT return + * the current task's policy as is done when looking up the policy for + * a specific folio. Kernel ABI for get_mempolicy() is to return + * MPOL_DEFAULT when there is no defined policy, not whatever the + * default policy resolves to. + */ + return mpol_shared_policy_lookup(&GMEM_I(inode)->policy, *pgoff); } #endif /* CONFIG_NUMA */