Skip to contentSkip to conversation

linux-user: Pass through nanosecond timestamp components for stat syscalls

PATCH Superseded Chen-Yu Tsai 3 participants 3 messages May 22, 2019

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 linux-user: Pass through nanosecond timestamp components for stat syscalls

From: Chen-Yu Tsai <[email protected]>

Since Linux 2.6 the stat syscalls have mostly supported nanosecond components for each of the file-related timestamps.

QEMU user mode emulation currently does not pass through the nanosecond portion of the timestamp, even when the host system fills in the value. This results in a mismatch when run on subsecond resolution filesystems such as ext4 or XFS.

An example of this leading to inconsistency is cross-debootstraping a full desktop root filesystem of Debian Buster. Recent versions of fontconfig store the full timestamp (instead of just the second portion) of the directory in its per-directory cache file, and checks this against the directory to see if the cache is up-to-date. With QEMU user mode emulation, the timestamp stored is incorrect, and upon booting the rootfs natively, fontconfig discovers the mismatch, and proceeds to rebuild the cache on the comparatively slow machine (low-power ARM vs x86). This stalls the first attempt to open whatever application that incorporates fontconfig.

This patch renames the "unused" padding trailing each timestamp element to its nanosecond counterpart name if such an element exists in the kernel sources for the given platform. Not all do. Then have the syscall wrapper fill in the nanosecond portion if the host supports it, as specified by the _POSIX_C_SOURCE and _XOPEN_SOURCE feature macros.

Recent versions of glibc only use stat64 and newfstatat syscalls on 32-bit and 64-bit platforms respectively. The changes in this patch were tested by directly calling the stat, stat64 and newfstatat syscalls directly, in addition to the glibc wrapper, on arm and aarch64 little endian targets.

---

Changes since v1:

- Define TARGET_STAT_HAS_NSEC for targets that have the *_nsec fields in their struct stat

- Only copy the *_nsec fields in the stat (not stat64) code path if TARGET_STAT_HAS_NSEC is defined

- Picked up Reviewed-by from Laurent

As Laurent mentioned, linux-user/syscall_defs.h is indented with tabs, so the checkpatch.pl errors can be ignored.

Original cover letter:

This issue was found while integrating some software that uses newer versions of fontconfig into Raspbian images. We found that the first launch of said software always stalls with fontconfig regenerating its font cache files. Upon closer examination I found the timestamps were not matching. The rest is explained above. Currently we're just working around the problem by patching the correct timestamps into the cache files after the fact.

Please consider this a drive-by scratch-my-own-itch contribution, but I will stick around to deal with any comments raised during review. I'm not on the mailing lists either, so please keep me in CC.

checkpatch returns "ERROR: code indent should never use tabs" for linux-user/syscall_defs.h, however as far as I can tell the whole file is indented with tabs. I'm not sure what to make of this.

Finally, I think this could be worth backporting to older versions.

---
 linux-user/syscall.c      | 19 +++++++++++++++
 linux-user/syscall_defs.h | 49 +++++++++++++++++++++++++--------------
 2 files changed, 50 insertions(+), 18 deletions(-)
linux-user/syscall.c +19 -0
index efa3ec2837..6d0f13c155 100644
@@ -6408,6 +6408,11 @@ static inline abi_long host_to_target_stat64(void *cpu_env,
         __put_user(host_st->st_atime, &target_st->target_st_atime);
         __put_user(host_st->st_mtime, &target_st->target_st_mtime);
         __put_user(host_st->st_ctime, &target_st->target_st_ctime);
+#if _POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700
+        __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec);
+        __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec);
+        __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec);
+#endif
         unlock_user_struct(target_st, target_addr, 1);
     } else
 #endif
@@ -6438,6 +6443,11 @@ static inline abi_long host_to_target_stat64(void *cpu_env,
         __put_user(host_st->st_atime, &target_st->target_st_atime);
         __put_user(host_st->st_mtime, &target_st->target_st_mtime);
         __put_user(host_st->st_ctime, &target_st->target_st_ctime);
+#if _POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700
+        __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec);
+        __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec);
+        __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec);
+#endif
         unlock_user_struct(target_st, target_addr, 1);
     }
 
@@ -8866,6 +8876,15 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
                 __put_user(st.st_atime, &target_st->target_st_atime);
                 __put_user(st.st_mtime, &target_st->target_st_mtime);
                 __put_user(st.st_ctime, &target_st->target_st_ctime);
+#if (_POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700) && \
+    defined(TARGET_STAT_HAS_NSEC)
+                __put_user(st.st_atim.tv_nsec,
+                           &target_st->target_st_atime_nsec);
+                __put_user(st.st_mtim.tv_nsec,
+                           &target_st->target_st_mtime_nsec);
+                __put_user(st.st_ctim.tv_nsec,
+                           &target_st->target_st_ctime_nsec);
+#endif
                 unlock_user_struct(target_st, arg2, 1);
             }
         }
linux-user/syscall_defs.h +31 -18

174 lines not on this page · raw message

Signature · 1 line
2.20.1
#1 lore raw
Quoted 38 lines · Chen-Yu Tsai

From: Chen-Yu Tsai <[email protected]>

Since Linux 2.6 the stat syscalls have mostly supported nanosecond components for each of the file-related timestamps.

QEMU user mode emulation currently does not pass through the nanosecond portion of the timestamp, even when the host system fills in the value. This results in a mismatch when run on subsecond resolution filesystems such as ext4 or XFS.

An example of this leading to inconsistency is cross-debootstraping a full desktop root filesystem of Debian Buster. Recent versions of fontconfig store the full timestamp (instead of just the second portion) of the directory in its per-directory cache file, and checks this against the directory to see if the cache is up-to-date. With QEMU user mode emulation, the timestamp stored is incorrect, and upon booting the rootfs natively, fontconfig discovers the mismatch, and proceeds to rebuild the cache on the comparatively slow machine (low-power ARM vs x86). This stalls the first attempt to open whatever application that incorporates fontconfig.

This patch renames the "unused" padding trailing each timestamp element to its nanosecond counterpart name if such an element exists in the kernel sources for the given platform. Not all do. Then have the syscall wrapper fill in the nanosecond portion if the host supports it, as specified by the _POSIX_C_SOURCE and _XOPEN_SOURCE feature macros.

Recent versions of glibc only use stat64 and newfstatat syscalls on 32-bit and 64-bit platforms respectively. The changes in this patch were tested by directly calling the stat, stat64 and newfstatat syscalls directly, in addition to the glibc wrapper, on arm and aarch64 little endian targets.

---

Quoted 7 lines

Changes since v1:

- Define TARGET_STAT_HAS_NSEC for targets that have the *_nsec fields in their struct stat

- Only copy the *_nsec fields in the stat (not stat64) code path if TARGET_STAT_HAS_NSEC is defined

Please use the same syntax as the kernel does, it helps for code review:

TARGET_STAT_HAVE_NSEC

Thanks,
Laurent

#2 lore raw
In reply to Chen-Yu Tsai
Patchew URL: https://patchew.org/QEMU/[email protected]/

Patchew URL: https://patchew.org/QEMU/[email protected]/

Hi,

This series seems to have some coding style problems. See output below for more information:

Message-id: [email protected]
Type: series
Subject: [Qemu-devel] [PATCH v2] linux-user: Pass through nanosecond timestamp components for stat syscalls

=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram
./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===

From https://github.com/patchew-project/qemu
* [new tag] patchew/[email protected] -> patchew/[email protected]
Switched to a new branch 'test'
0ae03af3f9 linux-user: Pass through nanosecond timestamp components for stat syscalls

=== OUTPUT BEGIN ===
ERROR: code indent should never use tabs
#104: FILE: linux-user/syscall_defs.h:1197:
+^Iabi_ulong target_st_atime_nsec;$

ERROR: code indent should never use tabs
#107: FILE: linux-user/syscall_defs.h:1199:
+^Iabi_ulong target_st_mtime_nsec;$

ERROR: code indent should never use tabs
#110: FILE: linux-user/syscall_defs.h:1201:
+^Iabi_ulong target_st_ctime_nsec;$

ERROR: code indent should never use tabs
#119: FILE: linux-user/syscall_defs.h:1233:
+^Iabi_ulong^Itarget_st_atime_nsec;$

ERROR: code indent should never use tabs
#123: FILE: linux-user/syscall_defs.h:1236:
+^Iabi_ulong^Itarget_st_mtime_nsec;$

ERROR: code indent should never use tabs
#127: FILE: linux-user/syscall_defs.h:1239:
+^Iabi_ulong^Itarget_st_ctime_nsec;$

ERROR: code indent should never use tabs
#136: FILE: linux-user/syscall_defs.h:1318:
+^Iabi_ulong^Itarget_st_atime_nsec;$

ERROR: code indent should never use tabs
#140: FILE: linux-user/syscall_defs.h:1321:
+^Iabi_ulong^Itarget_st_mtime_nsec;$

ERROR: code indent should never use tabs
#144: FILE: linux-user/syscall_defs.h:1324:
+^Iabi_ulong^Itarget_st_ctime_nsec;$

ERROR: code indent should never use tabs
#160: FILE: linux-user/syscall_defs.h:1342:
+^Iabi_ulong^Itarget_st_atime_nsec;$

ERROR: code indent should never use tabs
#163: FILE: linux-user/syscall_defs.h:1344:
+^Iabi_ulong^Itarget_st_mtime_nsec;$

ERROR: code indent should never use tabs
#166: FILE: linux-user/syscall_defs.h:1346:
+^Iabi_ulong^Itarget_st_ctime_nsec;$

ERROR: code indent should never use tabs
#170: FILE: linux-user/syscall_defs.h:1349:
+^Iabi_ulong^I__unused1[2];$

ERROR: code indent should never use tabs
#179: FILE: linux-user/syscall_defs.h:1377:
+^Iunsigned int^Itarget_st_atime_nsec;$

ERROR: code indent should never use tabs
#183: FILE: linux-user/syscall_defs.h:1380:
+^Iunsigned int^Itarget_st_mtime_nsec;$

ERROR: code indent should never use tabs
#187: FILE: linux-user/syscall_defs.h:1383:
+^Iunsigned int^Itarget_st_ctime_nsec;$

ERROR: code indent should never use tabs
#191: FILE: linux-user/syscall_defs.h:1385:
+^Iunsigned int^I__unused1;$

ERROR: code indent should never use tabs
#192: FILE: linux-user/syscall_defs.h:1386:
+^Iunsigned int^I__unused2;$

total: 18 errors, 0 warnings, 211 lines checked

Commit 0ae03af3f97f (linux-user: Pass through nanosecond timestamp components for stat syscalls) has style problems, please review. If any of these errors are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
=== OUTPUT END ===

Test command exited with code: 1

The full log is available at
http://patchew.org/logs/[email protected]/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]