Skip to contentSkip to conversation

kheaders: prevent `find` from seeing perl temp files

PATCH Superseded HONG Yifan 2 participants 8 messages Nov 7, 2024 – Dec 21, 2024

lore
Share & tools
lore

HONG Yifan

author
#0 lore raw

PATCH 1/1 kheaders: prevent `find` from seeing perl temp files

Symptom:

The command

    find ... | xargs ... perl -i

occasionally triggers error messages like the following, with the build still succeeding:

Can't open <redacted>/kernel/.tmp_cpio_dir/include/dt-bindings/clock/XXNX4nW9: No such file or directory.

Analysis:

With strace, the root cause has been identified to be perl -i creating temporary files inside $cpio_dir, which causes find to see the temporary files and emit the names. find is likely implemented with readdir. POSIX readdir says:

If a file is removed from or added to the directory after the most recent call to opendir() or rewinddir(), whether a subsequent call to readdir() returns an entry for that file is unspecified.

So if the libc that find links against choose to return that entry in readdir(), a possible sequence of events is the following:

1. find emits foo.h
2. xargs executes perl -i foo.h
3. perl (pid=100) creates temporary file XXXXXXXX
4. find sees file XXXXXXXX and emit it
5. PID 100 exits, cleaning up the temporary file XXXXXXXX
6. xargs executes perl -i XXXXXXXX
7. perl (pid=200) tries to read the file, but it doesn't exist any more.

... triggering the error message.

One can reproduce the bug with the following command (assuming PWD contains the list of headers in kheaders.tar.xz)

    for i in $(seq 100); do
        find -type f -print0 |
            xargs -0 -P8 -n1 perl -pi -e 'BEGIN {undef $/;}; s/\/\*((?!SPDX).)*?\*\///smg;';
    done

With a find linking against musl libc, the error message is emitted 6/100 times.

The fix:

This change modifies the command so find only emits .h names, thereby skipping the temporary files. Another possible fix would be to store the results of find before feeding them into xargs; I didn't take this approach because the current approach appears marginally more optimized and involves a smaller change.

---
 kernel/gen_kheaders.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/gen_kheaders.sh +1 -1
index 383fd43ac612..271e0145f406 100755
@@ -84,7 +84,7 @@ for f in $dir_list;
 done | cpio --quiet -pdu $cpio_dir >/dev/null 2>&1
 
 # Remove comments except SDPX lines
-find $cpio_dir -type f -print0 |
+find $cpio_dir -type f -name '*.h' -print0 |
 	xargs -0 -P8 -n1 perl -pi -e 'BEGIN {undef $/;}; s/\/\*((?!SPDX).)*?\*\///smg;'
 
 # Create archive and try to normalize metadata for reproducibility.
Signature · 1 line
2.47.0.199.ga7371fff76-goog
#1 lore raw
Quoted 11 lines · HONG Yifan

Symptom:

The command

    find ... | xargs ... perl -i

occasionally triggers error messages like the following, with the build still succeeding:

Can't open <redacted>/kernel/.tmp_cpio_dir/include/dt-bindings/clock/XXNX4nW9: No such file or directory.

I tested this patch on Alpine Linux
with "for i in $(seq 100); do" loop.

[Without this patch]

  CHK     kernel/kheaders_data.tar.xz
  GEN     kernel/kheaders_data.tar.xz
  AR      kernel/module/built-in.a

find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXKOEPLf:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXFEJdaa:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXdcljBm:
No such file or directory
Can't open /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXlLaJJF:
No such file or directory.
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXELBDIO:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXoHheAk:
No such file or directory
Can't open /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXeMGhpj:
No such file or directory.
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXbJmIOD:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXiHiAAd:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXjmiien:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXHDjNNM:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXlOHclm:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXbOGokp:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXfiMojG:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXhjfhgd:
No such file or directory
Can't open /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXiHbNEg:
No such file or directory.
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXKiEgne:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXLNlene:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXmKpBbk:
No such file or directory
Can't open /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXdgokkN:
No such file or directory.
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXDpfMGd:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXhAMmbc:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXpaKiKk:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXJNPgAH:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXlkCjbB:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXmkfeAA:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXFFbADB:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXEJlBBd:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXPHMiNP:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXeiMaiG:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXpfopfE:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXLOHHKc:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXiaBiIc:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXJainoG:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXJFhLEI:
No such file or directory
Can't open /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXhPACEf:
No such file or directory.
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXpMhaAc:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXNdoMNL:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXgKMLkg:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXaKkEKh:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXmdOFEo:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXmjKioE:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXPeEhNj:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXJCnpLp:
No such file or directory
Can't open /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXHNOiKd:
No such file or directory.
CC kernel/kheaders.o

[With this patch]

  CHK     kernel/kheaders_data.tar.xz
  GEN     kernel/kheaders_data.tar.xz
  AR      kernel/module/built-in.a

find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXeDFldH:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXLJMCah:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXckkiOH:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXNggIFH:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXnjOibB:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXkHOnGO:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXMogCin:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXogNJeJ:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXBAgJfo:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXfgFjJH:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXKiiEfj:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXkOEknj:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXjbddCE:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXDHGOlH:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXMGAdak:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXhfAChH:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXmGoEnP:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXGJEpaH:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXkKODpJ:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXDEfBDk:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXmgjJkj:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXBjhcbB:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXHoLKEE:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXOgcfAB:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXdjoEEc:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXFdEDFO:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXiNpHAN:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXopbKfc:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXlJnGkN:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXKfPIJB:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXDfbPkb:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXEiOgDb:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXdCdCIK:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXdoCePE:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXnDjKlh:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXEEcAMM:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXjlDBLj:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXEFPFLj:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXpoFkCA:
No such file or directory
CC kernel/kheaders.o

Indeed, the error messages from perl were fixed, but I still see a bunch of error messages,
which were presumably emitted by 'find'.

Do you know how to fix them?

Quoted 64 lines

Analysis:

With strace, the root cause has been identified to be perl -i creating temporary files inside $cpio_dir, which causes find to see the temporary files and emit the names. find is likely implemented with readdir. POSIX readdir says:

If a file is removed from or added to the directory after the most recent call to opendir() or rewinddir(), whether a subsequent call to readdir() returns an entry for that file is unspecified.

So if the libc that find links against choose to return that entry in readdir(), a possible sequence of events is the following:

1. find emits foo.h
2. xargs executes perl -i foo.h
3. perl (pid=100) creates temporary file XXXXXXXX
4. find sees file XXXXXXXX and emit it
5. PID 100 exits, cleaning up the temporary file XXXXXXXX
6. xargs executes perl -i XXXXXXXX
7. perl (pid=200) tries to read the file, but it doesn't exist any more.

... triggering the error message.

One can reproduce the bug with the following command (assuming PWD contains the list of headers in kheaders.tar.xz)

    for i in $(seq 100); do
        find -type f -print0 |
            xargs -0 -P8 -n1 perl -pi -e 'BEGIN {undef $/;}; s/\/\*((?!SPDX).)*?\*\///smg;';
    done

With a find linking against musl libc, the error message is emitted 6/100 times.

The fix:

This change modifies the command so find only emits .h names, thereby skipping the temporary files. Another possible fix would be to store the results of find before feeding them into xargs; I didn't take this approach because the current approach appears marginally more optimized and involves a smaller change.

---
 kernel/gen_kheaders.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/gen_kheaders.sh +1 -2
index 383fd43ac612..271e0145f406 100755
@@ -84,7 +84,7 @@ for f in $dir_list;
 done | cpio --quiet -pdu $cpio_dir >/dev/null 2>&1
 
 # Remove comments except SDPX lines
-find $cpio_dir -type f -print0 |
+find $cpio_dir -type f -name '*.h' -print0 |
        xargs -0 -P8 -n1 perl -pi -e 'BEGIN {undef $/;}; s/\/\*((?!SPDX).)*?\*\///smg;'
 
 # Create archive and try to normalize metadata for reproducibility.
--

2.47.0.199.ga7371fff76-goog

Signature · 2 lines
Best Regards
Masahiro Yamada
#2 lore raw
Quoted 9 lines · Masahiro Yamada
Quoted 7 lines · HONG Yifan

Symptom:

The command

    find ... | xargs ... perl -i
Quoted 8 lines
Quoted 4 lines

occasionally triggers error messages like the following, with the build still succeeding:

Can't open <redacted>/kernel/.tmp_cpio_dir/include/dt-bindings/clock/XXNX4nW9: No such file or directory.

I tested this patch on Alpine Linux
with "for i in $(seq 100); do" loop.

FWIW, I used Docker for testing.

This is the Dockerfile for my setup.

---------->8------------
ARG FROM=alpine

FROM ${FROM}

RUN \
apk add \
bash \
bison \
diffutils \
elfutils-dev \
emacs \
flex \
gcc \
git \
linux-headers \
make \

# c library
musl-dev \
nano \
openssl-dev \
perl \
python3 \
# useradd
shadow \
sudo \
tar
---------->8------------

I used linux-next-20241108 for the test kernel.

Signature · 2 lines
Best Regards
Masahiro Yamada
#3 lore raw
In reply to Masahiro Yamada
Quoted 208 lines · Masahiro Yamada
Quoted 11 lines · HONG Yifan

Symptom:

The command

    find ... | xargs ... perl -i

occasionally triggers error messages like the following, with the build still succeeding:

Can't open <redacted>/kernel/.tmp_cpio_dir/include/dt-bindings/clock/XXNX4nW9: No such file or directory.

I tested this patch on Alpine Linux
with "for i in $(seq 100); do" loop.

[Without this patch]

  CHK     kernel/kheaders_data.tar.xz
  GEN     kernel/kheaders_data.tar.xz
  AR      kernel/module/built-in.a

find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXKOEPLf:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXFEJdaa:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXdcljBm:
No such file or directory
Can't open /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXlLaJJF:
No such file or directory.
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXELBDIO:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXoHheAk:
No such file or directory
Can't open /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXeMGhpj:
No such file or directory.
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXbJmIOD:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXiHiAAd:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXjmiien:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXHDjNNM:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXlOHclm:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXbOGokp:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXfiMojG:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXhjfhgd:
No such file or directory
Can't open /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXiHbNEg:
No such file or directory.
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXKiEgne:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXLNlene:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXmKpBbk:
No such file or directory
Can't open /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXdgokkN:
No such file or directory.
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXDpfMGd:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXhAMmbc:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXpaKiKk:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXJNPgAH:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXlkCjbB:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXmkfeAA:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXFFbADB:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXEJlBBd:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXPHMiNP:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXeiMaiG:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXpfopfE:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXLOHHKc:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXiaBiIc:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXJainoG:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXJFhLEI:
No such file or directory
Can't open /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXhPACEf:
No such file or directory.
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXpMhaAc:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXNdoMNL:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXgKMLkg:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXaKkEKh:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXmdOFEo:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXmjKioE:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXPeEhNj:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXJCnpLp:
No such file or directory
Can't open /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXHNOiKd:
No such file or directory.
CC kernel/kheaders.o

[With this patch]

  CHK     kernel/kheaders_data.tar.xz
  GEN     kernel/kheaders_data.tar.xz
  AR      kernel/module/built-in.a

find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXeDFldH:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXLJMCah:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXckkiOH:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXNggIFH:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXnjOibB:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXkHOnGO:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXMogCin:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXogNJeJ:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXBAgJfo:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXfgFjJH:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXKiiEfj:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXkOEknj:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXjbddCE:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXDHGOlH:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXMGAdak:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXhfAChH:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXmGoEnP:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXGJEpaH:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXkKODpJ:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXDEfBDk:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXmgjJkj:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXBjhcbB:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXHoLKEE:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXOgcfAB:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXdjoEEc:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXFdEDFO:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXiNpHAN:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXopbKfc:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXlJnGkN:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXKfPIJB:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXDfbPkb:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXEiOgDb:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXdCdCIK:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXdoCePE:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXnDjKlh:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXEEcAMM:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/uapi/linux/XXjlDBLj:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/include/linux/XXEFPFLj:
No such file or directory
find: /home/masahiro/ref/linux-next/kernel/.tmp_cpio_dir/arch/x86/include/asm/XXpoFkCA:
No such file or directory
CC kernel/kheaders.o

Quoted 6 lines

Indeed, the error messages from perl were fixed, but I still see a bunch of error messages,
which were presumably emitted by 'find'.

Do you know how to fix them?

Sorry for the late reply; I just got back from my vacation.

Thanks for taking a look at the patch. The error would suggest that the patch is not robust enough against different find implementations. I'll upload a separate version of the patch that properly streams find into a temporary file before feeding it into xargs perl. That way, find shouldn't see the temporary files created by perl.

Quoted 78 lines
Quoted 71 lines

Analysis:

With strace, the root cause has been identified to be perl -i creating temporary files inside $cpio_dir, which causes find to see the temporary files and emit the names. find is likely implemented with readdir. POSIX readdir says:

If a file is removed from or added to the directory after the most recent call to opendir() or rewinddir(), whether a subsequent call to readdir() returns an entry for that file is unspecified.

So if the libc that find links against choose to return that entry in readdir(), a possible sequence of events is the following:

1. find emits foo.h
2. xargs executes perl -i foo.h
3. perl (pid=100) creates temporary file XXXXXXXX
4. find sees file XXXXXXXX and emit it
5. PID 100 exits, cleaning up the temporary file XXXXXXXX
6. xargs executes perl -i XXXXXXXX
7. perl (pid=200) tries to read the file, but it doesn't exist any more.

... triggering the error message.

One can reproduce the bug with the following command (assuming PWD contains the list of headers in kheaders.tar.xz)

    for i in $(seq 100); do
        find -type f -print0 |
            xargs -0 -P8 -n1 perl -pi -e 'BEGIN {undef $/;}; s/\/\*((?!SPDX).)*?\*\///smg;';
    done

With a find linking against musl libc, the error message is emitted 6/100 times.

The fix:

This change modifies the command so find only emits .h names, thereby skipping the temporary files. Another possible fix would be to store the results of find before feeding them into xargs; I didn't take this approach because the current approach appears marginally more optimized and involves a smaller change.

---
 kernel/gen_kheaders.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/gen_kheaders.sh +1 -2
index 383fd43ac612..271e0145f406 100755
@@ -84,7 +84,7 @@ for f in $dir_list;
 done | cpio --quiet -pdu $cpio_dir >/dev/null 2>&1
 
 # Remove comments except SDPX lines
-find $cpio_dir -type f -print0 |
+find $cpio_dir -type f -name '*.h' -print0 |
        xargs -0 -P8 -n1 perl -pi -e 'BEGIN {undef $/;}; s/\/\*((?!SPDX).)*?\*\///smg;'
 
 # Create archive and try to normalize metadata for reproducibility.
--
Quoted 2 lines

2.47.0.199.ga7371fff76-goog

--
Best Regards
Masahiro Yamada

author
#4 lore raw

PATCH v2 1/1 kheaders: prevent `find` from seeing perl temp files

Symptom:

The command

    find ... | xargs ... perl -i

occasionally triggers error messages like the following, with the build still succeeding:

Can't open <redacted>/kernel/.tmp_cpio_dir/include/dt-bindings/clock/XXNX4nW9: No such file or directory.

Analysis:

With strace, the root cause has been identified to be perl -i creating temporary files inside $cpio_dir, which causes find to see the temporary files and emit the names. find is likely implemented with readdir. POSIX readdir says:

If a file is removed from or added to the directory after the most recent call to opendir() or rewinddir(), whether a subsequent call to readdir() returns an entry for that file is unspecified.

So if the libc that find links against choose to return that entry in readdir(), a possible sequence of events is the following:

1. find emits foo.h
2. xargs executes perl -i foo.h
3. perl (pid=100) creates temporary file XXXXXXXX
4. find sees file XXXXXXXX and emit it
5. PID 100 exits, cleaning up the temporary file XXXXXXXX
6. xargs executes perl -i XXXXXXXX
7. perl (pid=200) tries to read the file, but it doesn't exist any more.

... triggering the error message.

One can reproduce the bug with the following command (assuming PWD contains the list of headers in kheaders.tar.xz)

    for i in $(seq 100); do
        find -type f -print0 |
            xargs -0 -P8 -n1 perl -pi -e 'BEGIN {undef $/;}; s/\/\*((?!SPDX).)*?\*\///smg;';
    done

With a find linking against musl libc, the error message is emitted 6/100 times.

The fix:

This change store the results of find before feeding them into xargs. find and xargs will no longer be able to see temporary files that perl creates after this change.

---
v2 <- v1: change from find *.h | xargs perl to
find > file; cat file | xargs perl because Masahiro discovered that the approach in v1 still causes find to see temporary files. The new approach is more robust.
v1: https://lore.kernel.org/all/[email protected]/

 kernel/gen_kheaders.sh | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
kernel/gen_kheaders.sh +5 -1
index 7e1340da5aca..4d70e6377da5 100755
@@ -84,8 +84,12 @@ for f in $dir_list;
 done | cpio --quiet -pdu $cpio_dir >/dev/null 2>&1
 
 # Remove comments except SDPX lines
-find $cpio_dir -type f -print0 |
+# Use a temporary file to store directory contents to prevent find/xargs from
+# seeing temporary files created by perl.
+find $cpio_dir -type f -print0 > "${cpio_dir}.contents.txt"
+cat "${cpio_dir}.contents.txt" | \
 	xargs -0 -P8 -n1 perl -pi -e 'BEGIN {undef $/;}; s/\/\*((?!SPDX).)*?\*\///smg;'
+rm -f "${cpio_dir}.contents.txt"
 
 # Create archive and try to normalize metadata for reproducibility.
 tar "${KBUILD_BUILD_TIMESTAMP:+--mtime=$KBUILD_BUILD_TIMESTAMP}" \

--
2.47.0.338.g60cca15819-goog

#5 lore raw
Quoted 44 lines · HONG Yifan

Symptom:

The command

    find ... | xargs ... perl -i

occasionally triggers error messages like the following, with the build still succeeding:

Can't open <redacted>/kernel/.tmp_cpio_dir/include/dt-bindings/clock/XXNX4nW9: No such file or directory.

Analysis:

With strace, the root cause has been identified to be perl -i creating temporary files inside $cpio_dir, which causes find to see the temporary files and emit the names. find is likely implemented with readdir. POSIX readdir says:

If a file is removed from or added to the directory after the most recent call to opendir() or rewinddir(), whether a subsequent call to readdir() returns an entry for that file is unspecified.

So if the libc that find links against choose to return that entry in readdir(), a possible sequence of events is the following:

1. find emits foo.h
2. xargs executes perl -i foo.h
3. perl (pid=100) creates temporary file XXXXXXXX
4. find sees file XXXXXXXX and emit it
5. PID 100 exits, cleaning up the temporary file XXXXXXXX
6. xargs executes perl -i XXXXXXXX
7. perl (pid=200) tries to read the file, but it doesn't exist any more.

... triggering the error message.

One can reproduce the bug with the following command (assuming PWD contains the list of headers in kheaders.tar.xz)

    for i in $(seq 100); do
        find -type f -print0 |
            xargs -0 -P8 -n1 perl -pi -e 'BEGIN {undef $/;}; s/\/\*((?!SPDX).)*?\*\///smg;';
    done
Quoted 6 lines

With a find linking against musl libc, the error message is emitted 6/100 times.

The fix:

This change store the results of find before feeding them into xargs.

store -> stores

Quoted 6 lines
kernel/gen_kheaders.sh +0 -0
index 7e1340da5aca..4d70e6377da5 100755

kernel/gen_kheaders.sh for f in $dir_list; · line 91

Quoted 9 lines
kernel/gen_kheaders.sh +4 -1
@@ -84,8 +84,12 @@ for f in $dir_list;
 done | cpio --quiet -pdu $cpio_dir >/dev/null 2>&1
 
 # Remove comments except SDPX lines
-find $cpio_dir -type f -print0 |
+# Use a temporary file to store directory contents to prevent find/xargs from
+# seeing temporary files created by perl.
+find $cpio_dir -type f -print0 > "${cpio_dir}.contents.txt"
+cat "${cpio_dir}.contents.txt" | \
        xargs -0 -P8 -n1 perl -pi -e 'BEGIN {undef $/;}; s/\/\*((?!SPDX).)*?\*\///smg;'

shellcheck pointed out the useless 'cat'.

In kernel/gen_kheaders.sh line 90:

cat "${cpio_dir}.contents.txt" | \
    ^------------------------^ SC2002 (style): Useless cat. Consider

'cmd < file | ..' or 'cmd file | ..' instead.

xargs ... < "${cpio_dir}.contents.txt"

Maybe

xargs -a "${cpio_dir}.contents.txt" ...

can be possible, but the -a option is not described in OpenGroup.

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/xargs.html

Quoted 7 lines
kernel/gen_kheaders.sh +1 -1
+rm -f "${cpio_dir}.contents.txt"
 
 # Create archive and try to normalize metadata for reproducibility.
 tar "${KBUILD_BUILD_TIMESTAMP:+--mtime=$KBUILD_BUILD_TIMESTAMP}" \
--

2.47.0.338.g60cca15819-goog

--
Best Regards

Masahiro Yamada

author
#6 lore raw

PATCH v3 1/1 kheaders: prevent `find` from seeing perl temp files

Symptom:

The command

    find ... | xargs ... perl -i

occasionally triggers error messages like the following, with the build still succeeding:

Can't open <redacted>/kernel/.tmp_cpio_dir/include/dt-bindings/clock/XXNX4nW9: No such file or directory.

Analysis:

With strace, the root cause has been identified to be perl -i creating temporary files inside $cpio_dir, which causes find to see the temporary files and emit the names. find is likely implemented with readdir. POSIX readdir says:

If a file is removed from or added to the directory after the most recent call to opendir() or rewinddir(), whether a subsequent call to readdir() returns an entry for that file is unspecified.

So if the libc that find links against choose to return that entry in readdir(), a possible sequence of events is the following:

1. find emits foo.h
2. xargs executes perl -i foo.h
3. perl (pid=100) creates temporary file XXXXXXXX
4. find sees file XXXXXXXX and emit it
5. PID 100 exits, cleaning up the temporary file XXXXXXXX
6. xargs executes perl -i XXXXXXXX
7. perl (pid=200) tries to read the file, but it doesn't exist any more.

... triggering the error message.

One can reproduce the bug with the following command (assuming PWD contains the list of headers in kheaders.tar.xz)

    for i in $(seq 100); do
        find -type f -print0 |
            xargs -0 -P8 -n1 perl -pi -e 'BEGIN {undef $/;}; s/\/\*((?!SPDX).)*?\*\///smg;';
    done

With a find linking against musl libc, the error message is emitted 6/100 times.

The fix:

This change stores the results of find before feeding them into xargs. find and xargs will no longer be able to see temporary files that perl creates after this change.

---
v3: (this patch)
Change from cat contents.txt | xargs to xargs < contents.txt to pass shellcheck. Fix typo in commit message.
v2: https://lore.kernel.org/all/[email protected]/
change from find *.h | xargs perl to
find > file; cat file | xargs perl because Masahiro discovered that the approach in v1 still causes find to see temporary files. The new approach is more robust.
v1: https://lore.kernel.org/all/[email protected]/

 kernel/gen_kheaders.sh | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
kernel/gen_kheaders.sh +7 -2
index 7e1340da5aca..3b58761b4690 100755
@@ -84,8 +84,13 @@ for f in $dir_list;
 done | cpio --quiet -pdu $cpio_dir >/dev/null 2>&1
 
 # Remove comments except SDPX lines
-find $cpio_dir -type f -print0 |
-	xargs -0 -P8 -n1 perl -pi -e 'BEGIN {undef $/;}; s/\/\*((?!SPDX).)*?\*\///smg;'
+# Use a temporary file to store directory contents to prevent find/xargs from
+# seeing temporary files created by perl.
+find $cpio_dir -type f -print0 > "${cpio_dir}.contents.txt"
+xargs -0 -P8 -n1 \
+	perl -pi -e 'BEGIN {undef $/;}; s/\/\*((?!SPDX).)*?\*\///smg;' \
+	< "${cpio_dir}.contents.txt"
+rm -f "${cpio_dir}.contents.txt"
 
 # Create archive and try to normalize metadata for reproducibility.
 tar "${KBUILD_BUILD_TIMESTAMP:+--mtime=$KBUILD_BUILD_TIMESTAMP}" \

--
2.47.1.613.gc27f4b7a9f-goog

#7 lore raw
Quoted 53 lines · HONG Yifan

Symptom:

The command

    find ... | xargs ... perl -i

occasionally triggers error messages like the following, with the build still succeeding:

Can't open <redacted>/kernel/.tmp_cpio_dir/include/dt-bindings/clock/XXNX4nW9: No such file or directory.

Analysis:

With strace, the root cause has been identified to be perl -i creating temporary files inside $cpio_dir, which causes find to see the temporary files and emit the names. find is likely implemented with readdir. POSIX readdir says:

If a file is removed from or added to the directory after the most recent call to opendir() or rewinddir(), whether a subsequent call to readdir() returns an entry for that file is unspecified.

So if the libc that find links against choose to return that entry in readdir(), a possible sequence of events is the following:

1. find emits foo.h
2. xargs executes perl -i foo.h
3. perl (pid=100) creates temporary file XXXXXXXX
4. find sees file XXXXXXXX and emit it
5. PID 100 exits, cleaning up the temporary file XXXXXXXX
6. xargs executes perl -i XXXXXXXX
7. perl (pid=200) tries to read the file, but it doesn't exist any more.

... triggering the error message.

One can reproduce the bug with the following command (assuming PWD contains the list of headers in kheaders.tar.xz)

    for i in $(seq 100); do
        find -type f -print0 |
            xargs -0 -P8 -n1 perl -pi -e 'BEGIN {undef $/;}; s/\/\*((?!SPDX).)*?\*\///smg;';
    done

With a find linking against musl libc, the error message is emitted 6/100 times.

The fix:

This change stores the results of find before feeding them into xargs. find and xargs will no longer be able to see temporary files that perl creates after this change.

Quoted 5 lines

---
v3: (this patch)
Change from cat contents.txt | xargs to xargs < contents.txt to pass shellcheck. Fix typo in commit message.

Applied to linux-kbuild (after resolving conflicts) Thanks.

Signature · 2 lines
Best Regards
Masahiro Yamada