From nobody Thu Dec 18 15:27:33 2025 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (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 DD8B84B1E65 for ; Fri, 16 May 2025 00:22:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747354948; cv=none; b=e8xJKel+C2uDmn4OufFS6J7vCt+jQmOW4ZmGf6GwYnLEkkzgiMbifD0riGj3Z1utV8o22lx9BFw7EixfrEe4uLUEcyQ25TpxE/O4Z9AiSh6s7KpMxxQSHMzBBoskfxJosgAeaTQFhtLyfixHUxS4fyCfUudVqZimRpi2hIMBMsg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747354948; c=relaxed/simple; bh=FLr14or9G6ctdhc4NjO2TMqj5hlhyJOyOKBdrjBX+q0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=roDviWKC/gt5ETpQfcMoN/kupdjqYqbkJuBfXTdCUAT1H6xL3DS3C142QFBocAVCggLwtICIDEU7KimmWn9LxP0pFYFcTczpidrV6SVAMQMLMntEyJBX1jK6zSXVQ1Qz5Oyn0sZJTLl/DLGP+uafzi3tmhwNEGgu9J8LuAoomI4= 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=CUXqAjcz; arc=none smtp.client-ip=209.85.210.201 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="CUXqAjcz" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-741a845cab2so2140143b3a.0 for ; Thu, 15 May 2025 17:22:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747354945; x=1747959745; darn=vger.kernel.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=U2sAkeu7MGicRsmk1eQ6Ys1LarlK/EQzO2qqYVap+q8=; b=CUXqAjczPMuTHriZl6tvaL4123gmRh9zTXElchUlG6gYQCikDJRMz1sCutUHfzgoJf G9sVqQV3rr9ma/GFOyUz8xe0EmS8n2lyWgz3kUK4PfEgNgkm0q9RsJu4xrt7VAlqEksN pPPxv92ER/JvLVvvcpoayrDxJ+hpKqc2oVPgE0fnfwhrKKNDwTtXFbrzBsUgJbMWOFx8 En4szy+an3umatgmNrQ+G/cUAbUxOs/cNqgIsjezLCwixE0mU0BLsFO/m7M9C6LT6Oow /dixVeiszZhi/YLzvg/K/CPiFbEsSrQHIOVKmrOrkXlgwMhrhwVuG+r03Qz6rHAug8AW 4/KA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747354945; x=1747959745; 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=U2sAkeu7MGicRsmk1eQ6Ys1LarlK/EQzO2qqYVap+q8=; b=Hpzia0zCTx8tab8AGNuFI+Hk3SQqt/rya/wkBGL5PSFn2tvopFgXqR5lPJ5gVkz5Ic dlHyFPEuYOOhiGuOLiy5vSeiymNOijZdCtOdoyNmkY/nGa77K9CXnzCyanCZAcsCopQi YIv2P0DKcgg/5EXT+BR3ioBOWBDuLeE/iEjCZtFacDWKFEMRdzY+XcqNBkmeui9GAu0p jnN3y3hqIWSyib3xf5xQNBYx0uSEpnaqIKTsjzMiO/gYIBx6iRKb8RellNa+ZCE7DBrA n+25DYdEzaqTRmgf5GRHCf+YvNThe0CE5+GfPxodKZ5yM1JMA9+KSaRZam9r4BpG53FM 80Mw== X-Forwarded-Encrypted: i=1; AJvYcCWDnfmsgWCQnn6ubnZefSdGgTXxM7xlvZDPEKvoYAypXoP0Ksr1zW4EWNeODyAym906QPdiDgI+rQCB8ZQ=@vger.kernel.org X-Gm-Message-State: AOJu0YwPFQtyqk5nCyXbHK+riFX+A243gg/rmWB+N3S1EQu6bsawhcmj OR7uqG8ue3HepxtUIB62pJuxFgcMzBaJOVn/0rJ/1THLWmzP55vwXj7oViGBsqa/zVkIXRvgiFR Fii/CyEsSWB3iuivrq3pNfuTvwA== X-Google-Smtp-Source: AGHT+IE6+aZ6pB0+4YWi/bOE+1/FrllyYKfVOjrPP4jF0YyDdqPgCbM1S1tbN8ddfEjmhOfVoPvdJc2p2g7nYFoIDA== X-Received: from pfbmb27.prod.google.com ([2002:a05:6a00:761b:b0:736:b063:5038]) (user=ackerleytng job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:4fc2:b0:73e:598:7e5b with SMTP id d2e1a72fcca58-742acc906ccmr622342b3a.1.1747354944946; Thu, 15 May 2025 17:22:24 -0700 (PDT) Date: Thu, 15 May 2025 17:22:21 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.49.0.1101.gccaa498523-goog Message-ID: Subject: [RFC PATCH v2 51/51] KVM: selftests: Test guest_memfd for accuracy of st_blocks From: Ackerley Tng To: kvm@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, x86@kernel.org, linux-fsdevel@vger.kernel.org Cc: ackerleytng@google.com, aik@amd.com, ajones@ventanamicro.com, akpm@linux-foundation.org, amoorthy@google.com, anthony.yznaga@oracle.com, anup@brainfault.org, aou@eecs.berkeley.edu, bfoster@redhat.com, binbin.wu@linux.intel.com, brauner@kernel.org, catalin.marinas@arm.com, chao.p.peng@intel.com, chenhuacai@kernel.org, dave.hansen@intel.com, david@redhat.com, dmatlack@google.com, dwmw@amazon.co.uk, erdemaktas@google.com, fan.du@intel.com, fvdl@google.com, graf@amazon.com, haibo1.xu@intel.com, hch@infradead.org, hughd@google.com, ira.weiny@intel.com, isaku.yamahata@intel.com, jack@suse.cz, james.morse@arm.com, jarkko@kernel.org, jgg@ziepe.ca, jgowans@amazon.com, jhubbard@nvidia.com, jroedel@suse.de, jthoughton@google.com, jun.miao@intel.com, kai.huang@intel.com, keirf@google.com, kent.overstreet@linux.dev, kirill.shutemov@intel.com, liam.merwick@oracle.com, maciej.wieczor-retman@intel.com, mail@maciej.szmigiero.name, maz@kernel.org, mic@digikod.net, michael.roth@amd.com, mpe@ellerman.id.au, muchun.song@linux.dev, nikunj@amd.com, nsaenz@amazon.es, oliver.upton@linux.dev, palmer@dabbelt.com, pankaj.gupta@amd.com, paul.walmsley@sifive.com, pbonzini@redhat.com, pdurrant@amazon.co.uk, peterx@redhat.com, pgonda@google.com, pvorel@suse.cz, qperret@google.com, quic_cvanscha@quicinc.com, quic_eberman@quicinc.com, quic_mnalajal@quicinc.com, quic_pderrin@quicinc.com, quic_pheragu@quicinc.com, quic_svaddagi@quicinc.com, quic_tsoni@quicinc.com, richard.weiyang@gmail.com, rick.p.edgecombe@intel.com, rientjes@google.com, roypat@amazon.co.uk, rppt@kernel.org, seanjc@google.com, shuah@kernel.org, steven.price@arm.com, steven.sistare@oracle.com, suzuki.poulose@arm.com, tabba@google.com, thomas.lendacky@amd.com, vannapurve@google.com, vbabka@suse.cz, viro@zeniv.linux.org.uk, vkuznets@redhat.com, wei.w.wang@intel.com, will@kernel.org, willy@infradead.org, xiaoyao.li@intel.com, yan.y.zhao@intel.com, yilun.xu@intel.com, yuzenghui@huawei.com, zhiquan1.li@intel.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Test that st_blocks in struct stat (inode->i_blocks) is updated. Change-Id: I67d814f130671b6b64b575e6a25fd17b1994c640 Signed-off-by: Ackerley Tng --- .../testing/selftests/kvm/guest_memfd_test.c | 55 ++++++++++++++++--- 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c b/tools/testing= /selftests/kvm/guest_memfd_test.c index c8acccaa9e1d..f51cd876d7dc 100644 --- a/tools/testing/selftests/kvm/guest_memfd_test.c +++ b/tools/testing/selftests/kvm/guest_memfd_test.c @@ -142,41 +142,78 @@ static void test_file_size(int fd, size_t page_size, = size_t total_size) TEST_ASSERT_EQ(sb.st_blksize, page_size); } =20 -static void test_fallocate(int fd, size_t page_size, size_t total_size) +static void assert_st_blocks_equals_size(int fd, size_t page_size, size_t = expected_size) { + struct stat sb; + int ret; + + /* TODO: st_blocks is not updated for 4K-page guest_memfd. */ + if (page_size =3D=3D getpagesize()) + return; + + ret =3D fstat(fd, &sb); + TEST_ASSERT(!ret, "fstat should succeed"); + TEST_ASSERT_EQ(sb.st_blocks, expected_size / 512); +} + +static void test_fallocate(int fd, size_t test_page_size, size_t total_siz= e) +{ + size_t page_size; int ret; =20 ret =3D fallocate(fd, FALLOC_FL_KEEP_SIZE, 0, total_size); TEST_ASSERT(!ret, "fallocate with aligned offset and size should succeed"= ); + assert_st_blocks_equals_size(fd, test_page_size, total_size); =20 ret =3D fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, - page_size - 1, page_size); + test_page_size - 1, test_page_size); TEST_ASSERT(ret, "fallocate with unaligned offset should fail"); + assert_st_blocks_equals_size(fd, test_page_size, total_size); =20 - ret =3D fallocate(fd, FALLOC_FL_KEEP_SIZE, total_size, page_size); + ret =3D fallocate(fd, FALLOC_FL_KEEP_SIZE, total_size, test_page_size); TEST_ASSERT(ret, "fallocate beginning at total_size should fail"); + assert_st_blocks_equals_size(fd, test_page_size, total_size); =20 - ret =3D fallocate(fd, FALLOC_FL_KEEP_SIZE, total_size + page_size, page_s= ize); + ret =3D fallocate(fd, FALLOC_FL_KEEP_SIZE, total_size + test_page_size, t= est_page_size); TEST_ASSERT(ret, "fallocate beginning after total_size should fail"); + assert_st_blocks_equals_size(fd, test_page_size, total_size); =20 ret =3D fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, - total_size, page_size); + total_size, test_page_size); TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) at total_size should succeed"); + assert_st_blocks_equals_size(fd, test_page_size, total_size); =20 ret =3D fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, - total_size + page_size, page_size); + total_size + test_page_size, test_page_size); TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) after total_size should succeed"= ); + assert_st_blocks_equals_size(fd, test_page_size, total_size); =20 ret =3D fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, - page_size, page_size - 1); + test_page_size, test_page_size - 1); TEST_ASSERT(ret, "fallocate with unaligned size should fail"); + assert_st_blocks_equals_size(fd, test_page_size, total_size); =20 ret =3D fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, - page_size, page_size); + test_page_size, test_page_size); TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) with aligned offset and size sho= uld succeed"); + assert_st_blocks_equals_size(fd, test_page_size, total_size - test_page_s= ize); =20 - ret =3D fallocate(fd, FALLOC_FL_KEEP_SIZE, page_size, page_size); + ret =3D fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, + test_page_size, test_page_size); + TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) in a hole should succeed"); + assert_st_blocks_equals_size(fd, test_page_size, total_size - test_page_s= ize); + + ret =3D fallocate(fd, FALLOC_FL_KEEP_SIZE, test_page_size, test_page_size= ); TEST_ASSERT(!ret, "fallocate to restore punched hole should succeed"); + assert_st_blocks_equals_size(fd, test_page_size, total_size); + + page_size =3D getpagesize(); + if (test_page_size =3D=3D page_size) { + ret =3D fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, + test_page_size + page_size, page_size); + TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) of a subfolio should succeed"); + assert_st_blocks_equals_size(fd, test_page_size, total_size); + } } =20 static void test_invalid_punch_hole(int fd, size_t page_size, size_t total= _size) --=20 2.49.0.1045.g170613ef41-goog