From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (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 185BB32AADC for ; Wed, 25 Feb 2026 07:20:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772004056; cv=none; b=Q0kzZ/c9S4I2d5otxZHoxlzJY9bJD22zuT0lfgVDKZogm1Ai7GM5A06kfi1ZgUDe4Bn1z/j3xT9QWzRPr3IjW0LP8IAaHHg3TWPxrzX9gqcrLt68Fp5cBRqe6ezgXTmfdLXcEnSOW746bpH/cCQxNu4XhMCz+j6URQB9Mf3BdtE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772004056; c=relaxed/simple; bh=dOYyBdQXA1usfepnnNuDszu6x3QfaYGIbNqVbOtmh7o=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=islUydcBT/5j0Zl2v1WgAzqLHps5gFHpFUpgQWKzqlwS3CYePhS2r84rJItTzQ7laO496Tzu4WxnTHDnxBqx7QI7QIDW31m1xr3PFlLC1twbi/IuFCV7vvjN5fA45+StcCD7YOw8EPWZAwabJIeThjcsO/xCS+488ipTlL3W6kI= 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=I3tpnjvQ; arc=none smtp.client-ip=209.85.214.202 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="I3tpnjvQ" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2ad7e454f38so283655265ad.0 for ; Tue, 24 Feb 2026 23:20:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1772004054; x=1772608854; 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=uCRWKaRAQP2gBMw/IwAKqq2A0WtVFQ88sM/7soXGb8M=; b=I3tpnjvQuA+B/47Xi93nsUnNz4ABiLiO81bZWolQQPkRP1MvkYIHXHPcOHtDzHOify PgkdDgVxvzLGDnW4K8ka2w2JP7gAWU/wkpwAidUfBD69+O43IxV94mlbrPqfLgAdHPcX OjRh81UOBI/d6q5DtwbrYBvaSMeukq6Dd7aU4gwXzzBpAizPc8bmGZM0Mh9YCMD0gdYd TGdtNxefz49ybrs2EHiWpLKfZiSrI454tp9hIaJQyJ7zubi2ke7h0SM5+ngCPTHdfan4 q0noHo7uCAamVcCngr2oNwYcdDTHvf6jzPQJQ3AA/JXPJp/fldP4KQ33wlrj5hM34Ab4 vBhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772004054; x=1772608854; 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=uCRWKaRAQP2gBMw/IwAKqq2A0WtVFQ88sM/7soXGb8M=; b=Y08g8W7E36hLPEAzAzj3d0GJhzzkxu87wODyzkS+g+Ci0NPsn/YhLBINsc7FZzCych N8GrcfUnAcaEvVVkLMTdE/Ju1pV4Z1mLU72u7PXePdAmNh3vZX8FJXa/xNeBHYnr5w6s WAv715zl4xXOyqNNjQDvPbXcPXWzNZfQnILPjmx4dzTxyM2No5JXKzk6iuvlxhTgEwca YdPF23uc0w0DfnXzMFbf4F/5wqsxDYmFgVEvdgIQUmyTieOiT9LpeOhnCF0eVFCVVw8T V+2gHPjhIu5AmHXzx32No1YrLUhn8v49srLxOXiGAMkw8kTPgvtWIzv8h77U+t32Z5SX p0IA== X-Forwarded-Encrypted: i=1; AJvYcCWUQfYTUklw15zOwGsjoFxbgOfLUYMXDRc7Idj5iexI6y26sVHrDADuV/v5pcAWXGanwBOJHUcd429vjYA=@vger.kernel.org X-Gm-Message-State: AOJu0YxSLBGPmXZErwEl6XKHhFcKoWzrTAYBOQQ4vSl901OUg4M6RiTS RG+NeUWKrHXGgkgCTDgOgERJhsxLreVIMg7xbBoacTbz/1Q41Cj+vv7NPS0EE/3u4gIB+TAs8HR GjZ6h/k1Agill4h1gAh/Vs7XA/Q== X-Received: from plsk6.prod.google.com ([2002:a17:902:ba86:b0:29f:1738:99f3]) (user=ackerleytng job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:d511:b0:2a7:c188:bd1f with SMTP id d9443c01a7336-2add14146b1mr15540455ad.40.1772004054358; Tue, 24 Feb 2026 23:20:54 -0800 (PST) Date: Wed, 25 Feb 2026 07:20:41 +0000 In-Reply-To: <20260225-gmem-st-blocks-v2-0-87d7098119a9@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260225-gmem-st-blocks-v2-0-87d7098119a9@google.com> X-Developer-Key: i=ackerleytng@google.com; a=ed25519; pk=sAZDYXdm6Iz8FHitpHeFlCMXwabodTm7p8/3/8xUxuU= X-Developer-Signature: v=1; a=ed25519-sha256; t=1772004043; l=3384; i=ackerleytng@google.com; s=20260225; h=from:subject:message-id; bh=dOYyBdQXA1usfepnnNuDszu6x3QfaYGIbNqVbOtmh7o=; b=zZrSG7T8hLIyNxblKZAGfB+9IN1V9IOqYlhpVMvYXXGbthbgU9tmocvsvN/c7FJF4snDqAhSr TzWnTmDXwh2DG87+Tp1yHs6y/+kQ/SVsCjVtOclQbRZ0IyhxW13o7OV X-Mailer: b4 0.14.3 Message-ID: <20260225-gmem-st-blocks-v2-6-87d7098119a9@google.com> Subject: [PATCH RFC v2 6/6] 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" , Vlastimil Babka , 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 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 81387f06e770a..89228d73fa736 100644 --- a/tools/testing/selftests/kvm/guest_memfd_test.c +++ b/tools/testing/selftests/kvm/guest_memfd_test.c @@ -218,41 +218,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.414.gf7e9f6c205-goog