git: 3a1bf59d195c - main - shmfd: consistently return size in 512 byte blocks for fstat(2) st_blocks

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Wed, 19 Aug 2026 14:47:19 UTC
The branch main has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=3a1bf59d195ced99c0f69774969d7090d21f6097

commit 3a1bf59d195ced99c0f69774969d7090d21f6097
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-08-19 02:27:46 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-08-19 14:46:34 +0000

    shmfd: consistently return size in 512 byte blocks for fstat(2) st_blocks
    
    This is ABI-breaking change that could be considered as the bug fix.
    
    Requested by:   David Timber <dxdt@dev.snart.me>
    Reviewed by:    emaste, markj
    Sponsored by:   The FreeBSD Foundation
    Relnotes:       yes
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D58942
---
 lib/libsys/stat.2   | 8 ++++++--
 sys/kern/uipc_shm.c | 8 ++------
 2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/lib/libsys/stat.2 b/lib/libsys/stat.2
index c16af32a340d..bbec876da1fd 100644
--- a/lib/libsys/stat.2
+++ b/lib/libsys/stat.2
@@ -271,10 +271,14 @@ be zero.
 .Pp
 For file descriptors referencing specific non-file objects,
 the field is type-specific.
+.Pp
 For POSIX shared memory segments
 .Pq see Xr shm_open 2 ,
-the block size is equal to
-the platform page size
+the block size is 512 bytes.
+For older versions of
+.Fx
+.Pq before 15.2 and 16.0
+the block size was equal to the platform page size
 .Pq Xr getpagesize 2 .
 .El
 .Pp
diff --git a/sys/kern/uipc_shm.c b/sys/kern/uipc_shm.c
index 8341364f4d16..04b6f4f6e07a 100644
--- a/sys/kern/uipc_shm.c
+++ b/sys/kern/uipc_shm.c
@@ -658,12 +658,8 @@ shm_stat(struct file *fp, struct stat *sb, struct ucred *active_cred)
 	sb->st_dev = shm_dev_ino;
 	sb->st_ino = shmfd->shm_ino;
 	sb->st_nlink = shmfd->shm_object->ref_count;
-	if (shm_largepage(shmfd)) {
-		sb->st_blocks = shmfd->shm_object->size /
-		    (pagesizes[shmfd->shm_lp_psind] >> PAGE_SHIFT);
-	} else {
-		sb->st_blocks = shmfd->shm_pages;
-	}
+	sb->st_blocks = ptoa(shm_largepage(shmfd) ? shmfd->shm_object->size :
+	    shmfd->shm_pages) / DEV_BSIZE;
 
 	return (0);
 }