From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f198.google.com (mail-pl1-f198.google.com [209.85.214.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 695D03D9549 for ; Wed, 26 Aug 2026 09:18:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.198 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787735922; cv=none; b=ROLJTDy9R9H3BRX+7T+n+hIHULwTsV9x+JYbqiz4znUjGH45TtJGMq8J3rZNukD6RmsUqN9rvtBOJU4pKkxCHThqMXIqbOvD/PtdnbX9eVd9uHG8z/31ejJPN6wdlBo6Up0EZyFUaZl3tvzZTGFLTP/VXdVOhYN2apR99kpa4LY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787735922; c=relaxed/simple; bh=5Zy0wgRHoYXULlg01g6F2vvkEu14R8QvWTtrIAn8XBM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Xnhd5XxKXogJ9RhjiNEoE+UDtoZLbeQXE278pWiD51u1MS5iwCwZbr7QKDrshE2QfB9ggdgUahQhwgQvwpMD2uJrcFKuxIcZMz9dpTC+Y7yOz3MmDAoxbtWYY5g/33B9I22oWujdJXWnDbd2FcVQDb32i7wRqU1MYCNH8xJmtuE= 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=vD2s2keY; arc=none smtp.client-ip=209.85.214.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--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="vD2s2keY" Received: by mail-pl1-f198.google.com with SMTP id d9443c01a7336-2ce7dfd33ffso6572535ad.0 for ; Wed, 26 Aug 2026 02:18:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1787735915; x=1788340715; 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=D9viyxfvYsSlaAuG4kZ9V8QlGv88Bbdqdj5aKht9VSg=; b=vD2s2keYcKLUg9jv8wtF/cuyEqMfESMHDCmA3GIvTHX2qSwIe2H0S7roM2hANWMn74 xuSHBElwhOxn0GtDxY+d65hbstwOt5+EsHlKSfSJGz4fLrxDBFvtb7t25WB59JY2bZo7 pJ3WM5dFpWykYwpmHc64f0bBTGZyFQ+kQ2APZs6lOwIktzWV2IM4KMiAgGzf2udDlw8+ daY1s3E2jc+K8zbiwysBli0mAcknRMcg2aoenSYnWuLTtItrPTTspDewvbHYGNoyaUzW Yfb+H10gO8YBXzhAUesKWS8/iMt3FsbjrdRaQrjHPjEidRKlAT8LzjPJ4tL3YQKsWj97 Aphg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787735915; x=1788340715; 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=D9viyxfvYsSlaAuG4kZ9V8QlGv88Bbdqdj5aKht9VSg=; b=TRElOP0uKB03/M08ESBrde2v/9Kyx4go06vAqGLdnMrEYlb1BvLYtSCTgnA/nux7wP 1cYFbSUuazjTySRU6WT2wRGOfD6rIV3nIkXU18zm3nEjiMmc/pC8MlRRafPnD98bOxze /MctY1lsGFjn72wG0qIywJRFVuYW7SgiwKuGdp/4gJ/o2oRfDu8fMo7ADrKRNfRctvU8 2HTbf8c/DZWO/NrhmgIhf41VckAPUbW25zsrsXqcucbIpuXklQBmwKSMB6aV8jpUjbPr 9qM6YAObMKMivadoAF28BKbih9rFHJmaJOUL9hB+x3MwmnaC32JP2LTDV8Xuvk44xqk2 y60w== X-Gm-Message-State: AFuF++kcKUi3GaJ7Zpp/i1jKoStcg84+ifNH86tWUvON86/Iuc4mShu8 G+UTkGvKthSUBxelf5AKcJ0FmfXpq1LkQNVG1RAMRAGdcbKGldEVar+fSI4PunC/f8WJN73k8+b htSwrz4T9BFD/GlX8n9JQcBU7Qw== X-Received: from plmt11.prod.google.com ([2002:a17:903:3d4b:b0:2d0:34d:e973]) (user=ackerleytng job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:22ca:b0:2d6:fd55:3a9d with SMTP id d9443c01a7336-2d707a55a21mr87392135ad.3.1787735914764; Wed, 26 Aug 2026 02:18:34 -0700 (PDT) Date: Wed, 26 Aug 2026 09:18:14 +0000 In-Reply-To: <20260826-gmem-inplace-conversion-v11-0-0a15d8a799aa@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260826-gmem-inplace-conversion-v11-0-0a15d8a799aa@google.com> X-Developer-Key: i=ackerleytng@google.com; a=ed25519; pk=sAZDYXdm6Iz8FHitpHeFlCMXwabodTm7p8/3/8xUxuU= X-Developer-Signature: v=1; a=ed25519-sha256; t=1787735885; l=1963; i=ackerleytng@google.com; s=20260225; h=from:subject:message-id; bh=5Zy0wgRHoYXULlg01g6F2vvkEu14R8QvWTtrIAn8XBM=; b=hJwcYJYD3aQhvV9hZ30dJZ8oFevxPiomh64e9F++PWR61XHxRgbkXw3b9UE+Q/itDtGXtMPp4 zJhf3vH1eeDAA7nHPjekSt0TG32A8zYAxjhuYb571OlL26p5daTs2K6 X-Mailer: b4 0.16.0 Message-ID: <20260826-gmem-inplace-conversion-v11-16-0a15d8a799aa@google.com> Subject: [PATCH v11 16/46] KVM: guest_memfd: Return early if range already has requested attributes From: Ackerley Tng To: 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 , Sean Christopherson , 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, Fuad Tabba , Vlastimil Babka Cc: 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, Ackerley Tng , Xiaoyao Li Content-Type: text/plain; charset="utf-8" Provide a function to check that a range has given attributes. Optimize setting memory attributes by returning early if all pages in the requested range already have the requested attributes. Reviewed-by: Fuad Tabba Tested-by: Shivank Garg Reviewed-by: Binbin Wu Reviewed-by: Xiaoyao Li Signed-off-by: Ackerley Tng --- virt/kvm/guest_memfd.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index d1134b23576e4..6dc199be0eb87 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -98,6 +98,24 @@ static bool kvm_gmem_is_shared_mem(struct inode *inode, pgoff_t index) return !kvm_gmem_is_private_mem(inode, index); } +static bool kvm_gmem_range_has_attributes(struct inode *inode, + pgoff_t index, size_t nr_pages, + u64 attributes) +{ + struct maple_tree *mt = &GMEM_I(inode)->attributes; + pgoff_t end = index + nr_pages - 1; + void *entry; + + lockdep_assert(mt_lock_is_held(mt)); + + mt_for_each(mt, entry, index, end) { + if (kvm_gmem_get_attributes(inode, entry) != attributes) + return false; + } + + return true; +} + /* * Returns a locked folio on success. The caller is responsible for * setting the up-to-date flag before the memory is mapped into the guest. @@ -623,12 +641,15 @@ static int __kvm_gmem_set_attributes(struct inode *inode, pgoff_t start, pgoff_t end = start + nr_pages; struct maple_tree *mt; struct ma_state mas; - int r; + int r = 0; mt = &gi->attributes; filemap_invalidate_lock(mapping); + if (kvm_gmem_range_has_attributes(inode, start, nr_pages, attrs)) + goto out; + mas_init(&mas, mt, start); r = kvm_gmem_mas_preallocate(&mas, attrs, start, nr_pages); if (r) { -- 2.55.0.887.g758fc8c411-goog