From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f198.google.com (mail-pg1-f198.google.com [209.85.215.198]) (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 BA13849E5C1 for ; Tue, 1 Sep 2026 18:35:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.198 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788287719; cv=none; b=cRq0nozNqJ9nWQnP0brrsMk2q2IlZNjlriahncOp1enyVmoNiEXBfD/0Xe6AAz0V8Dr+yOka4abQpOwXV2XkL24EqwPVbMzEjiOp5DkMR8DnKWzilChfVcJxEerjpBOMJnO2bcJ4Ziyx9VtgjFg/zCLn4LRbsON8U4SpgQYp8gk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788287719; c=relaxed/simple; bh=d6mWEdAbh3pA6h0kvJxTmQz3sCAdRZUVlyIUylbQoi4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jBve0MtMjUxA5MfMFkUVXaFKWMOSCCXa7dX0QH1A9oIDU+jO3f1WWgGIreEnKcPRIZtY5tWHw3JkK3OUn8QSu1f8SdJVuOrV97sghNJhkYZwPerbPg9qeEz2FiFOPBE9usOSwiZdh/CdRnZ0kPSnNQ2udlMf9KbzkuZSSZdFUzQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=aJ/i8hKe; arc=none smtp.client-ip=209.85.215.198 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--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="aJ/i8hKe" Received: by mail-pg1-f198.google.com with SMTP id 41be03b00d2f7-c89704da8c7so259061a12.0 for ; Tue, 01 Sep 2026 11:35:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1788287717; x=1788892517; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=LA0MtyhL9+n6pP8AlY+vz7qS2JveEW6zRalGa05+rBs=; b=aJ/i8hKeU2jlhpAWK9sJzuUmkRVeSqRdCS/Lig6QNmblH4MuDa9c+UgAFgPjRbPKDN OPo+yFpDnfV0UDQnzFnUceEivvRvHRFJTqcHPzYHSmQ5POGpEc9kv0OgE/WsYJ6VMccc JwFMDVTiz9gFkUBsnorJqYL9LOlRF0/z9kvG4rylrasCO3J+5I/rlRR5F3ASsMjYSr8U Oi9MO4w1oQIqF7d6NitC9j/W64i55ErnuLaKHk2VYXSL9I1D/EYIYAzDAChBsxWjL9TN Npo8jE1cxufv7fZS2WwJJd9J9wUbrWMhjNxEsI1vRJbFHz3i5W8smZNMS52hIOm0MqqQ AP1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788287717; x=1788892517; h=content-type: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:content-type; bh=LA0MtyhL9+n6pP8AlY+vz7qS2JveEW6zRalGa05+rBs=; b=VUoe7kJe/dbrPP18w4rt1cobPjWDqtCIQ6p9sFAt3TyDszB7nF2AudFf0+NgtC0zZ/ wMdhQOHcuwAZGM5l095qk0kAZqLFkd0lT5G1clrlFK8QWEUp8vmVX4+TGip3UhlvgA9J mSezuBFInKCsKsP8KECRKKoJRTWgD7Pf/g7z56kYGOq9kJsY6i+Y4KmXMU6+ttqqug6j q7ntukvRoPP2VtEsFiAnbG8wW7u+LwlU8o9Rbj/HgnqQLKRq43zH1NspwhT/UIDnnHWo 1Mw/r8C0QTukZuwfdR/dXsPVsyGtvnpsDDtbaGCI7+LfnKevWzjVX24B0LIBWKXxVhzZ 3Tcw== X-Forwarded-Encrypted: i=1; AHgh+RrrvlCENBvU1Q9Xx+QYb5/RMWx2LWxDWa0+c/zDN3QilxGNWg4k88rDXo9EN2rGapEi2o64LMR2fv4=@vger.kernel.org X-Gm-Message-State: AFuF++kEtTJBqeMt0fLRp4OKfUczIiFyXR9huuxQinHf3vLgEf888C6G TqUnkY0tou6hTEYK5JGoNLlNDvfnj52/iGfO/bxOeHkENwhOU0HOhiZfwz/6shTVfaMafOQr8RE Kzed6sw== X-Received: from pgcv12.prod.google.com ([2002:a05:6a02:530c:b0:c96:acc6:54d6]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:3944:b0:3d3:aec2:4dcb with SMTP id adf61e73a8af0-3d7b075b26bmr19293744637.23.1788287716571; Tue, 01 Sep 2026 11:35:16 -0700 (PDT) Date: Tue, 1 Sep 2026 11:35:15 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260830-gmem-inplace-conversion-v12-0-85e5fd25252a@google.com> <20260830-gmem-inplace-conversion-v12-15-85e5fd25252a@google.com> Message-ID: Subject: Re: [PATCH v12 15/45] KVM: guest_memfd: Ensure pages are not in use before conversion From: Sean Christopherson To: Fuad Tabba Cc: ackerleytng@google.com, aik@amd.com, andrew.jones@linux.dev, binbin.wu@linux.intel.com, brauner@kernel.org, chao.p.peng@linux.intel.com, david@kernel.org, jmattson@google.com, jthoughton@google.com, michael.roth@amd.com, oupton@kernel.org, pankaj.gupta@amd.com, qperret@google.com, rick.p.edgecombe@intel.com, rientjes@google.com, shivankg@amd.com, steven.price@arm.com, willy@infradead.org, wyihan@google.com, yan.y.zhao@intel.com, forkloop@google.com, pratyush@kernel.org, suzuki.poulose@arm.com, aneesh.kumar@kernel.org, liam@infradead.org, Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Jonathan Corbet , Shuah Khan , Shuah Khan , Vishal Annapurve , Andrew Morton , Chris Li , Kairui Song , Kemeng Shi , Nhat Pham , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , Youngjun Park , Qi Zheng , Shakeel Butt , Kiryl Shutsemau , Baoquan He , Jason Gunthorpe , John Hubbard , Peter Xu , tarunsahu@google.com, Randy Dunlap , Lorenzo Stoakes , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-coco@lists.linux.dev Content-Type: text/plain; charset="us-ascii" On Tue, Sep 01, 2026, Fuad Tabba wrote: > > diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c > > index 803c7cdbbe0f6..fe02c47c85fb5 100644 > > --- a/virt/kvm/guest_memfd.c > > +++ b/virt/kvm/guest_memfd.c > > @@ -538,8 +538,46 @@ static int kvm_gmem_mas_preallocate(struct ma_state *mas, u64 attributes, > > return mas_preallocate(mas, xa_mk_value(attributes), GFP_KERNEL); > > } > > > > +static bool kvm_gmem_has_outstanding_references(struct inode *inode, > > + pgoff_t start, size_t nr_pages, > > + pgoff_t *err_index) > > +{ > > + struct address_space *mapping = inode->i_mapping; > > + pgoff_t last = start + nr_pages - 1; > > + bool has_outstanding = false; > > + struct folio_batch fbatch; > > + pgoff_t next; > > + int i; > > + > > + folio_batch_init(&fbatch); > > + > > + next = start; > > + while (has_outstanding && filemap_get_folios(mapping, &next, last, &fbatch)) { > > has_outstanding starts as false, so the loop never runs and the function > always returns false. The outstanding-reference check is dead at this > patch, so a to-private conversion would not be rejected even when a page > still has an outstanding reference. > > It's fixed later in "KVM: guest_memfd: Handle lru_add fbatch refcounts > during conversion safety check", which changes the condition to > !has_outstanding. I think that fix belongs in this patch, so the check > works when it is introduced and the series bisects cleanly. Why even bother with has_outstanding? Avoiding it requires copy+pasting folio_batch_release(), but it's less code and IMO the end result is a lot easier to follow: struct address_space *mapping = inode->i_mapping; pgoff_t last = start + nr_pages - 1; struct folio_batch fbatch; pgoff_t next; int i; folio_batch_init(&fbatch); next = start; while (filemap_get_folios(mapping, &next, last, &fbatch)) { for (i = 0; i < folio_batch_count(&fbatch); ++i) { struct folio *folio = fbatch.folios[i]; /* * Outstanding references are anything other than those * from the page cache, plus 1 temporary reference held * by filemap_get_folios() in the folio batch. */ if (folio_ref_count(folio) != folio_nr_pages(folio) + 1) { *err_index = max(start, folio->index); folio_batch_release(&fbatch); return true; } } folio_batch_release(&fbatch); cond_resched(); } return false; and then we end up with: enum lru_cache_drained drained = LRU_CACHE_NOT_DRAINED; struct address_space *mapping = inode->i_mapping; pgoff_t last = start + nr_pages - 1; struct folio_batch fbatch; pgoff_t next; int i; folio_batch_init(&fbatch); next = start; while (filemap_get_folios(mapping, &next, last, &fbatch)) { for (i = 0; i < folio_batch_count(&fbatch); ++i) { struct folio *folio = fbatch.folios[i]; if (__folio_has_outstanding_references(folio, &drained)) { *err_index = max(start, folio->index); folio_batch_release(&fbatch); return true; } } folio_batch_release(&fbatch); cond_resched(); } return false;