Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 C144638946C for ; Mon, 9 Mar 2026 09:54:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773050063; cv=none; b=ESWy3+OAil7GjQDtBGeA3n7DxXrXKqNFvFjFV9GJMxLK0DRE2G1B9zwG/mNa4Hl4isYiDCCZRA4O8y8hu5WgOYAlzxTAcyBIE3yEG1YckpJdXlsazHVnAk3hZBv0CbowsYhLNqEkJOR1qy/fs/zD1BkYPXOtz+/Bu/A9y43Yd+4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773050063; c=relaxed/simple; bh=j//w3WNdHhv+K+KJtK+49w6iacgmz4dTxY4RPng9cDM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=nalKBKh4iBBKq4PKRPCiJigwM+HyGd7d+97eZtYqvT8HOnyVYMZeNukKQFjUEaZpL58pWLyg5rke6plVEHtvCN+0uvqqNJ9LEcKcpeI8AHVYR/p8tvGknwNP+GxyJOI3SjkaMfIwPJDCMPE9x9A3QIzed67K276/E8DFz8F94zs= 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=jBoeGbob; arc=none smtp.client-ip=209.85.216.74 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="jBoeGbob" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-3597f559e70so5835083a91.3 for ; Mon, 09 Mar 2026 02:54:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1773050059; x=1773654859; 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=/MEyZb/LBxvw6GMpGFDX0oKNKuzzaTQJv09alTwZUHg=; b=jBoeGbobj/yi+M2LQwgl1qx7sArCfTFGvxF5XbkL3jWyqS21RVKROuu050PtaWf7O6 shDPTH3/eDYmksy5EQL/BRMSW66450RgItibmOJHKs2DCVG4xqHii4LUXqdmqMiwjjIi e4W7n3h1f5ZDOBqtkEyMFJgpi893oGhCfImQrwqX1whU92yLIfVK5bLyIqMxrHKI8hQP /sNZ4PSKd9vpzQ1LoQR2C9yzHeAOQSk62x6QcGd6uP1F19LE6c0V1wVJ//FZRipHqJMF P/tj4CJ0OqvB4Ozeb8cQEDp8bGVrgJKsbhWt68X+yTEw/5loh5W90exHu7vue4nRaqy6 Y2RA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1773050059; x=1773654859; 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=/MEyZb/LBxvw6GMpGFDX0oKNKuzzaTQJv09alTwZUHg=; b=Umjo+2OCdGA0YwVdEs8roHecqnsKDPyUbdE9LRJvh5u0tcgKXb6+3t0CSE212czMeW DB3AokvS3RGIpZQv5sErlTb8fSKCikGrQl3YoRqCuEaLTRxR+hCyidwk/x7qOWaYQFaE hYP9MvXHdIeQ6B+MoRh5GJO80LbbeYtWCXVC1SiPeiFyQnJIwa1bzXlB64uh/xVKUH+2 3JVms0SdXwW28IVjKyZkp0LtNb5DfuZLuPzpGE0m4+Z9dHS2I2inbfa3WgBGRtw4hU7T xHZYTDhqLAVA8yTvfzPwrk1efXdO/Ftdmg8huyoMvIPgIljfJaIi1CoyP0uXN7XJEigI H0qQ== X-Gm-Message-State: AOJu0YwDrujACskyJIuUfFydsCvXB35CGjQ+es1Z57fGEXZSH3fX1fXD lLlWt4nCkM+aimKqTFu89//06CkkLbAGTOanItm43q5Dtthbs0V0XYzn7+8AZEXDizLyuWuCKhZ rRki1MdqaSdkVSC6J55jEkhGznA== X-Received: from pjbcq1.prod.google.com ([2002:a17:90a:f981:b0:359:8ddd:8042]) (user=ackerleytng job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2b4e:b0:359:8e5e:43ee with SMTP id 98e67ed59e1d1-359be2ee7f0mr9058209a91.19.1773050058833; Mon, 09 Mar 2026 02:54:18 -0700 (PDT) Date: Mon, 09 Mar 2026 09:53:55 +0000 In-Reply-To: <20260309-gmem-st-blocks-v3-0-815f03d9653e@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260309-gmem-st-blocks-v3-0-815f03d9653e@google.com> X-Developer-Key: i=ackerleytng@google.com; a=ed25519; pk=sAZDYXdm6Iz8FHitpHeFlCMXwabodTm7p8/3/8xUxuU= X-Developer-Signature: v=1; a=ed25519-sha256; t=1773050050; l=3384; i=ackerleytng@google.com; s=20260225; h=from:subject:message-id; bh=j//w3WNdHhv+K+KJtK+49w6iacgmz4dTxY4RPng9cDM=; b=47SP9RDMCEYTpZusRbsJtAhE6uNyDbx53VwlcxfkhVofBND7pZ84qMDahkOmQWfyUOEuUMi+w 8KtzTKEfucPCalS3OewldUeVaBmw6TDArODKk48Vg/ruFqX9znVvd2L X-Mailer: b4 0.14.3 Message-ID: <20260309-gmem-st-blocks-v3-4-815f03d9653e@google.com> Subject: [PATCH RFC v3 4/4] KVM: selftests: Test that st_blocks is updated on allocation From: Ackerley Tng To: Paolo Bonzini , Andrew Morton , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , "Matthew Wilcox (Oracle)" , Shuah Khan , Jonathan Corbet , Alexander Viro , Christian Brauner , Jan Kara , seanjc@google.com, rientjes@google.com, rick.p.edgecombe@intel.com, yan.y.zhao@intel.com, fvdl@google.com, jthoughton@google.com, vannapurve@google.com, shivankg@amd.com, michael.roth@amd.com, pratyush@kernel.org, pasha.tatashin@soleen.com, kalyazin@amazon.com, tabba@google.com, Vlastimil Babka Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org, Ackerley Tng Content-Type: text/plain; charset="utf-8" The st_blocks field reported by fstat should reflect the number of allocated 512-byte blocks for the guest memfd file. Extend the fallocate test to verify that st_blocks is correctly updated when memory is allocated or deallocated via fallocate(FALLOC_FL_PUNCH_HOLE). Add checks after each fallocate call to ensure that st_blocks increases on allocation, decreases when a hole is punched, and is restored when the hole is re-allocated. Also verify that st_blocks remains unchanged for failing fallocate calls. Signed-off-by: Ackerley Tng --- tools/testing/selftests/kvm/guest_memfd_test.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c b/tools/testing/selftests/kvm/guest_memfd_test.c index 638906298ed73..3381a556f397d 100644 --- a/tools/testing/selftests/kvm/guest_memfd_test.c +++ b/tools/testing/selftests/kvm/guest_memfd_test.c @@ -276,41 +276,58 @@ static void test_file_size(int fd, size_t total_size) TEST_ASSERT_EQ(sb.st_blksize, page_size); } +static void assert_st_blocks_matches_size(int fd, size_t expected_size) +{ + struct stat sb; + + kvm_fstat(fd, &sb); + TEST_ASSERT_EQ(sb.st_blocks, expected_size / 512); +} + static void test_fallocate(int fd, size_t total_size) { int ret; ret = fallocate(fd, FALLOC_FL_KEEP_SIZE, 0, total_size); TEST_ASSERT(!ret, "fallocate with aligned offset and size should succeed"); + assert_st_blocks_matches_size(fd, total_size); ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, page_size - 1, page_size); TEST_ASSERT(ret, "fallocate with unaligned offset should fail"); + assert_st_blocks_matches_size(fd, total_size); ret = fallocate(fd, FALLOC_FL_KEEP_SIZE, total_size, page_size); TEST_ASSERT(ret, "fallocate beginning at total_size should fail"); + assert_st_blocks_matches_size(fd, total_size); ret = fallocate(fd, FALLOC_FL_KEEP_SIZE, total_size + page_size, page_size); TEST_ASSERT(ret, "fallocate beginning after total_size should fail"); + assert_st_blocks_matches_size(fd, total_size); ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, total_size, page_size); TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) at total_size should succeed"); + assert_st_blocks_matches_size(fd, total_size); ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, total_size + page_size, page_size); TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) after total_size should succeed"); + assert_st_blocks_matches_size(fd, total_size); ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, page_size, page_size - 1); TEST_ASSERT(ret, "fallocate with unaligned size should fail"); + assert_st_blocks_matches_size(fd, total_size); ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, page_size, page_size); TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) with aligned offset and size should succeed"); + assert_st_blocks_matches_size(fd, total_size - page_size); ret = fallocate(fd, FALLOC_FL_KEEP_SIZE, page_size, page_size); TEST_ASSERT(!ret, "fallocate to restore punched hole should succeed"); + assert_st_blocks_matches_size(fd, total_size); } static void test_invalid_punch_hole(int fd, size_t total_size) -- 2.53.0.473.g4a7958ca14-goog