Skip to contentSkip to conversation

linux-user: Use *at functions instead of caching interp_prefix contents

PATCH In review Richard Henderson 3 participants 5 messages Jan 12, 2017 – Nov 28, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user: Use *at functions instead of caching interp_prefix contents

If the interp_prefix is a complete chroot, it may have a *lot* of files. Setting up the cache for this is quite expensive. Instead, use the *at versions of various syscalls to attempt the operation in the prefix.

---
 linux-user/elfload.c |  12 ++-
 linux-user/main.c    |   3 +-
 linux-user/qemu.h    |   1 +
 linux-user/syscall.c | 236 ++++++++++++++++++++++++++++++++++++++++++---------
 util/Makefile.objs   |   2 +-
 util/path.c          | 178 --------------------------------------
 6 files changed, 209 insertions(+), 223 deletions(-)
 delete mode 100644 util/path.c
linux-user/elfload.c +11 -1
index 547053c..8b947fd 100644
@@ -2026,7 +2026,17 @@ static void load_elf_interp(const char *filename, struct image_info *info,
 {
     int fd, retval;
 
-    fd = open(path(filename), O_RDONLY);
+    switch (filename[0]) {
+    case '/':
+        fd = openat(interp_dirfd, filename + 1, O_RDONLY);
+        if (fd >= 0 || errno != ENOENT) {
+            break;
+        }
+        /* fallthru */
+    default:
+        fd = open(filename, O_RDONLY);
+        break;
+    }
     if (fd < 0) {
         goto exit_perror;
     }
linux-user/main.c +2 -1
index c1d5eb4..dba988b 100644
@@ -81,6 +81,7 @@ unsigned long reserved_va;
 static void usage(int exitcode);
 
 static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
+int interp_dirfd;
 const char *qemu_uname_release;
 
 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
@@ -4013,7 +4014,7 @@ int main(int argc, char **argv, char **envp)
     memset(&bprm, 0, sizeof (bprm));
 
     /* Scan interp_prefix dir for replacement files. */
-    init_paths(interp_prefix);
+    interp_dirfd = open(interp_prefix, O_CLOEXEC | O_DIRECTORY | O_PATH);
 
     init_qemu_uname_release();
 
linux-user/qemu.h +1 -0
index da73a01..f91e2d5 100644
@@ -434,6 +434,7 @@ void mmap_fork_start(void);
 void mmap_fork_end(int child);
 
 /* main.c */
+extern int interp_dirfd;
 extern unsigned long guest_stack_size;
 
 /* user access */
linux-user/syscall.c +194 -42

379 lines not on this page · raw message

util/Makefile.objs +1 -1
index ad0f9c7..429d1b0 100644
@@ -10,7 +10,7 @@ util-obj-$(CONFIG_WIN32) += event_notifier-win32.o
 util-obj-$(CONFIG_POSIX) += memfd.o
 util-obj-$(CONFIG_WIN32) += oslib-win32.o
 util-obj-$(CONFIG_WIN32) += qemu-thread-win32.o
-util-obj-y += envlist.o path.o module.o
+util-obj-y += envlist.o module.o
 util-obj-$(call lnot,$(CONFIG_INT128)) += host-utils.o
 util-obj-y += bitmap.o bitops.o hbitmap.o
 util-obj-y += fifo8.o
util/path.c deleted +0 -178

178 lines not on this page · raw message

Signature · 1 line
2.9.3
#1 lore raw
Your series seems to have some coding style problems. See output below for more information:

Hi,

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

Type: series
Message-id: [email protected]
Subject: [Qemu-devel] [PATCH] linux-user: Use *at functions instead of caching interp_prefix contents

=== TEST SCRIPT BEGIN ===
#!/bin/bash

BASE=base
n=1
total=$(git log --oneline $BASE.. | wc -l)
failed=0

# Useful git options
git config --local diff.renamelimit 0
git config --local diff.renames True

commits="$(git log --format=%H --reverse $BASE..)" for c in $commits; do

    echo "Checking PATCH $n/$total: $(git log -n 1 --format=%s $c)..."
    if ! git show $c --format=email | ./scripts/checkpatch.pl --mailback -; then
        failed=1
        echo
    fi
    n=$((n+1))

done

exit $failed
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
From https://github.com/patchew-project/qemu
* [new tag] patchew/[email protected] -> patchew/[email protected]
Switched to a new branch 'test'
b5d6d07 linux-user: Use *at functions instead of caching interp_prefix contents

=== OUTPUT BEGIN ===
Checking PATCH 1/1: linux-user: Use *at functions instead of caching interp_prefix contents...
ERROR: do not use assignment in if condition
#135: FILE: linux-user/syscall.c:8085:
+ if (!(fn = lock_user_string(arg2)))

ERROR: braces {} are necessary for all arms of this statement #135: FILE: linux-user/syscall.c:8085:
+ if (!(fn = lock_user_string(arg2)))
[...]

ERROR: do not use assignment in if condition
#160: FILE: linux-user/syscall.c:8113:
+ if (!(fn = lock_user_string(arg1))) {

ERROR: do not use assignment in if condition
#183: FILE: linux-user/syscall.c:8133:
+ if (!(fn = lock_user_string(arg2))) {

ERROR: do not use assignment in if condition
#315: FILE: linux-user/syscall.c:9513:
+ if (!(fn = lock_user_string(arg1))) {

ERROR: do not use assignment in if condition
#338: FILE: linux-user/syscall.c:9533:
+ if (!(fn = lock_user_string(arg1))) {

ERROR: do not use assignment in if condition
#364: FILE: linux-user/syscall.c:10621:
+ if (!(fn = lock_user_string(arg1))) {

ERROR: do not use assignment in if condition
#389: FILE: linux-user/syscall.c:10643:
+ if (!(fn = lock_user_string(arg1))) {

ERROR: do not use assignment in if condition
#415: FILE: linux-user/syscall.c:10677:
+ if (!(fn = lock_user_string(arg2)))

ERROR: braces {} are necessary for all arms of this statement #415: FILE: linux-user/syscall.c:10677:
+ if (!(fn = lock_user_string(arg2)))
[...]

ERROR: do not use assignment in if condition
#439: FILE: linux-user/syscall.c:11673:
+ if (!(fn = lock_user_string(arg2))) {

total: 11 errors, 0 warnings, 427 lines checked

Your patch 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

---
Email generated automatically by Patchew [http://patchew.org/].
Please send your feedback to [email protected]

#2 lore raw
In reply to Richard Henderson
Quoted 4 lines · Richard Henderson

If the interp_prefix is a complete chroot, it may have a *lot* of files. Setting up the cache for this is quite expensive. Instead, use the *at versions of various syscalls to attempt the operation in the prefix.

Presumably this also fixes the completely broken handling of symlinks in the interp_prefix tree?

Unfortunately patch will break bsd-user as it stands, because bsd-user also calls init_paths. There's also a usage in tests/tcg/.

Awkward problem: I suspect you'll find this breaks a few guest programs which don't like the fact that there's now an open fd which doesn't belong to the guest. (For instance IIRC some LTP test programs do "check that the rlimit on number of open files hits after opening exactly N files".)

Incidentally it's a shame C doesn't make it easier to abstract out the repeated pattern

        switch (PATHNAME[0]) {
        case '/':
            ret = SOMEFN(interp_dirfd, PATHNAME + 1, STUFF);
            if (ret == 0 || errno != ENOENT) {
                break;
            }
            /* fallthru */
        default:
            ret = SOMEFN(FD, PATHNAME, STUFF);
            break;
        }

(and the variant which uses a 2nd function).

A macro would get awkwardly heavily parameterised, I think.

thanks
-- PMM

#3 lore raw
Quoted 8 lines · Peter Maydell
Quoted 4 lines · Richard Henderson

If the interp_prefix is a complete chroot, it may have a *lot* of files. Setting up the cache for this is quite expensive. Instead, use the *at versions of various syscalls to attempt the operation in the prefix.

Presumably this also fixes the completely broken handling of symlinks in the interp_prefix tree?

Presumably.

Quoted 3 lines

Unfortunately patch will break bsd-user as it stands, because bsd-user also calls init_paths. There's also a usage in tests/tcg/.

Oops, yes.

Quoted 5 lines

Awkward problem: I suspect you'll find this breaks a few guest programs which don't like the fact that there's now an open fd which doesn't belong to the guest. (For instance IIRC some LTP test programs do "check that the rlimit on number of open files hits after opening exactly N files".)

Hum. Probably. I don't see how I can avoid that.

Quoted 15 lines

Incidentally it's a shame C doesn't make it easier to abstract out the repeated pattern

        switch (PATHNAME[0]) {
        case '/':
            ret = SOMEFN(interp_dirfd, PATHNAME + 1, STUFF);
            if (ret == 0 || errno != ENOENT) {
                break;
            }
            /* fallthru */
        default:
            ret = SOMEFN(FD, PATHNAME, STUFF);
            break;
        }
Quoted 3 lines

(and the variant which uses a 2nd function).

A macro would get awkwardly heavily parameterised, I think.

I did briefly play with such a macro, and I found it to be uglier than just replicating the pattern.

r~

#4 lore raw
In reply to Richard Henderson
Quoted 51 lines · Richard Henderson

If the interp_prefix is a complete chroot, it may have a *lot* of files. Setting up the cache for this is quite expensive. Instead, use the *at versions of various syscalls to attempt the operation in the prefix.

---
 linux-user/elfload.c |  12 ++-
 linux-user/main.c    |   3 +-
 linux-user/qemu.h    |   1 +
 linux-user/syscall.c | 236 ++++++++++++++++++++++++++++++++++++++++++---------
 util/Makefile.objs   |   2 +-
 util/path.c          | 178 --------------------------------------
 6 files changed, 209 insertions(+), 223 deletions(-)
 delete mode 100644 util/path.c
linux-user/elfload.c +11 -1
index 547053c..8b947fd 100644
@@ -2026,7 +2026,17 @@ static void load_elf_interp(const char *filename, struct image_info *info,
 {
     int fd, retval;
 
-    fd = open(path(filename), O_RDONLY);
+    switch (filename[0]) {
+    case '/':
+        fd = openat(interp_dirfd, filename + 1, O_RDONLY);
+        if (fd >= 0 || errno != ENOENT) {
+            break;
+        }
+        /* fallthru */
+    default:
+        fd = open(filename, O_RDONLY);
+        break;
+    }
     if (fd < 0) {
         goto exit_perror;
     }
linux-user/main.c +1 -0
index c1d5eb4..dba988b 100644
@@ -81,6 +81,7 @@ unsigned long reserved_va;
 static void usage(int exitcode);
 
 static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
+int interp_dirfd;
 const char *qemu_uname_release;
 
 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so

linux-user/main.c int main(int argc, char **argv, char **envp) · line 4017

Quoted 5 lines
linux-user/main.c +1 -1
@@ -4013,7 +4014,7 @@ int main(int argc, char **argv, char **envp)
     memset(&bprm, 0, sizeof (bprm));
 
     /* Scan interp_prefix dir for replacement files. */
-    init_paths(interp_prefix);
+    interp_dirfd = open(interp_prefix, O_CLOEXEC | O_DIRECTORY | O_PATH);

I've been using this patch over the last week or so as a convenient way of being able to run guest binaries without having to actually use chroot, and I just noticed a bug here:
if the interp_prefix doesn't exist, this will set interp_dirfd to -1 and then every file access will fail with EBADF. We should treat "prefix doesn't exist" like "don't use a prefix", because by default we use /usr/gnemul/qemu-something, which probably doesn't exist for most people.

thanks
-- PMM