Skip to contentSkip to conversation

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

PATCH Superseded Richard Henderson 5 participants 7 messages Dec 5, 2017 – Dec 7, 2017

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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

From: Richard Henderson <[email protected]>

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.

---

Changes since v1 (Nov 2016):
* Require interp_dirfd set before trying the *at path.

r~

---
 linux-user/qemu.h    |   1 +
 linux-user/elfload.c |  12 ++-
 linux-user/main.c    |   3 +-
 linux-user/syscall.c | 236 ++++++++++++++++++++++++++++++++++++++++++---------
 4 files changed, 208 insertions(+), 44 deletions(-)
linux-user/qemu.h +1 -0
index 4edd7d0c08..d04924fd2e 100644
@@ -437,6 +437,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/elfload.c +11 -1
index 20f3d8c2c3..5d3f7aa11d 100644
@@ -2203,7 +2203,17 @@ static void load_elf_interp(const char *filename, struct image_info *info,
 {
     int fd, retval;
 
-    fd = open(path(filename), O_RDONLY);
+    switch (interp_dirfd > 0 && filename[0] == '/') {
+    case true:
+        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 6286661bd3..06edc2c6bc 100644
@@ -99,6 +99,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
@@ -4297,7 +4298,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/syscall.c +194 -42

379 lines not on this page · raw message

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

Hi,

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

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

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

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

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

 t [tag update]            patchew/[email protected] -> patchew/[email protected]
 t [tag update]            patchew/[email protected] -> patchew/[email protected]
 * [new tag]               patchew/[email protected] -> patchew/[email protected]

Switched to a new branch 'test'
88b8681d57 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:8287:
+ if (!(fn = lock_user_string(arg2)))

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

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

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

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

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

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

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

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

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

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

total: 11 errors, 0 warnings, 419 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 38 lines · Richard Henderson

From: Richard Henderson <[email protected]>

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.

---

Changes since v1 (Nov 2016):
* Require interp_dirfd set before trying the *at path.

r~

---
 linux-user/qemu.h    |   1 +
 linux-user/elfload.c |  12 ++-
 linux-user/main.c    |   3 +-
 linux-user/syscall.c | 236 ++++++++++++++++++++++++++++++++++++++++++---------
 4 files changed, 208 insertions(+), 44 deletions(-)
linux-user/qemu.h +1 -0
index 4edd7d0c08..d04924fd2e 100644
@@ -437,6 +437,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/elfload.c +0 -0
index 20f3d8c2c3..5d3f7aa11d 100644

linux-user/elfload.c static void load_elf_interp(const char *filename, struct image_info *info, · line 2207

Quoted 6 lines
linux-user/elfload.c +2 -1
@@ -2203,7 +2203,17 @@ static void load_elf_interp(const char *filename, struct image_info *info,
 {
     int fd, retval;
 
-    fd = open(path(filename), O_RDONLY);
+    switch (interp_dirfd > 0 && filename[0] == '/') {
+    case true:

gcc doesn't like this pattern:

/home/petmay01/linaro/qemu-from-laptop/qemu/linux-user/syscall.c: In function ‘do_name_to_handle_at’:
/home/petmay01/linaro/qemu-from-laptop/qemu/linux-user/syscall.c:7231:13:
error: switch condition has boolean value [-Werror=switch-bool]

     switch (interp_dirfd > 0 && name[0] == '/') {
             ^

It seems like an odd way to write an if().

thanks
-- PMM

#3 lore raw
Quoted 49 lines · Peter Maydell
Quoted 38 lines · Richard Henderson

From: Richard Henderson <[email protected]>

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.

---

Changes since v1 (Nov 2016):
* Require interp_dirfd set before trying the *at path.

r~

---
 linux-user/qemu.h    |   1 +
 linux-user/elfload.c |  12 ++-
 linux-user/main.c    |   3 +-
 linux-user/syscall.c | 236 ++++++++++++++++++++++++++++++++++++++++++---------
 4 files changed, 208 insertions(+), 44 deletions(-)
linux-user/qemu.h +1 -0
index 4edd7d0c08..d04924fd2e 100644
@@ -437,6 +437,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/elfload.c +0 -0
index 20f3d8c2c3..5d3f7aa11d 100644

linux-user/elfload.c static void load_elf_interp(const char *filename, struct image_info *info, · line 2207

Quoted 6 lines
linux-user/elfload.c +2 -1
@@ -2203,7 +2203,17 @@ static void load_elf_interp(const char *filename, struct image_info *info,
 {
     int fd, retval;
 
-    fd = open(path(filename), O_RDONLY);
+    switch (interp_dirfd > 0 && filename[0] == '/') {
+    case true:

gcc doesn't like this pattern:

Quoted 8 lines

/home/petmay01/linaro/qemu-from-laptop/qemu/linux-user/syscall.c: In function ‘do_name_to_handle_at’:
/home/petmay01/linaro/qemu-from-laptop/qemu/linux-user/syscall.c:7231:13:
error: switch condition has boolean value [-Werror=switch-bool]

     switch (interp_dirfd > 0 && name[0] == '/') {
             ^

It seems like an odd way to write an if().

Huh. My gcc likes it just fine. What version are you using?

It's an IF with a /* fallthru */, so there are very few less ugly ways to write it...

r~

#4 lore raw
Quoted 10 lines · Richard Henderson
Quoted 9 lines · Peter Maydell

gcc doesn't like this pattern:

/home/petmay01/linaro/qemu-from-laptop/qemu/linux-user/syscall.c: In function ‘do_name_to_handle_at’:
/home/petmay01/linaro/qemu-from-laptop/qemu/linux-user/syscall.c:7231:13:
error: switch condition has boolean value [-Werror=switch-bool]

     switch (interp_dirfd > 0 && name[0] == '/') {
             ^
Quoted 6 lines
Quoted 1 line

It seems like an odd way to write an if().

Huh. My gcc likes it just fine. What version are you using?

It's an IF with a /* fallthru */, so there are very few less ugly ways to write it...

gcc (Ubuntu 5.4.0-6ubuntu1~16.04.5) 5.4.0 20160609 (for x86-64).

thanks
-- PMM

#5 lore raw
In reply to Richard Henderson
Quoted 13 lines · Richard Henderson
Quoted 13 lines

linux-user/elfload.c static void load_elf_interp(const char *filename, struct image_info *info, · line 2207

Quoted 3 lines
linux-user/elfload.c +2 -1
-    fd = open(path(filename), O_RDONLY);
+    switch (interp_dirfd > 0 && filename[0] == '/') {
+    case true:

gcc doesn't like this pattern:

/home/petmay01/linaro/qemu-from-laptop/qemu/linux-user/syscall.c: In function ‘do_name_to_handle_at’:
/home/petmay01/linaro/qemu-from-laptop/qemu/linux-user/syscall.c:7231:13:
error: switch condition has boolean value [-Werror=switch-bool]

     switch (interp_dirfd > 0 && name[0] == '/') {
             ^
Quoted 6 lines
Quoted 1 line

It seems like an odd way to write an if().

Huh. My gcc likes it just fine. What version are you using?

It's an IF with a /* fallthru */, so there are very few less ugly ways to write it...

Maybe:

while (1) {
    if (interp_dirfd > 0 && filename[0] == '/') {
        fd = openat(interp_dirfd, filename + 1, O_RDONLY);
        if (fd >= 0 || errno != ENOENT) {
            break;
        }
    }
    fd = open(filename, O_RDONLY);
    break;
}
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#6 lore raw
Quoted 5 lines · Eric Blake
Quoted 2 lines

It's an IF with a /* fallthru */, so there are very few less ugly ways to write it...

Maybe:

Quoted 10 lines
while (1) {
    if (interp_dirfd > 0 && filename[0] == '/') {
        fd = openat(interp_dirfd, filename + 1, O_RDONLY);
        if (fd >= 0 || errno != ENOENT) {
            break;
        }
    }
    fd = open(filename, O_RDONLY);
    break;
}

Nice. I'd been pondering

    if (interp_dirfd < 0
        || filename[0] != '/'
        || (fd = openat(interp_dirfd, filename + 1, O_RDONLY),
            fd < 0 && errno == ENOENT)) {
        fd = open(filename, O_RDONLY);
    }

r~