Skip to contentSkip to conversation

linux-user: Add emulation for MADV_WIPEONFORK and MADV_KEEPONFORK in madvise()

PATCH Superseded Helge Deller 2 participants 4 messages Dec 12, 2022

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user: Add emulation for MADV_WIPEONFORK and MADV_KEEPONFORK in madvise()

Both parameters have a different value on the parisc platform, so first translate the target value into a host value for usage in the native madvise() syscall.

Those parameters are often used by security sensitive applications (e.g. tor browser, boringssl, ...) which expect the call to return a proper return code on failure, so return -EINVAL if qemu fails to forward the syscall to the host OS.

Tested with testcase of tor browser when running hppa-linux guest on x86-64 host.

linux-user/mmap.c +26 -6
index 10f5079331..c75342108c 100644
@@ -901,11 +901,25 @@ abi_long target_madvise(abi_ulong start, abi_ulong len_in, int advice)
         return -TARGET_EINVAL;
     }
 
+    /* Translate for some architectures which have different MADV_xxx values */
+    switch (advice) {
+    case TARGET_MADV_DONTNEED:      /* alpha */
+        advice = MADV_DONTNEED;
+        break;
+    case TARGET_MADV_WIPEONFORK:    /* parisc */
+        advice = MADV_WIPEONFORK;
+        break;
+    case TARGET_MADV_KEEPONFORK:    /* parisc */
+        advice = MADV_KEEPONFORK;
+        break;
+    /* we do not care about the other MADV_xxx values yet */
+    }
+
     /*
      * A straight passthrough may not be safe because qemu sometimes turns
      * private file-backed mappings into anonymous mappings.
      *
-     * This is a hint, so ignoring and returning success is ok.
+     * For MADV_DONTNEED, which is a hint, ignoring and returning success is ok.
      *
      * This breaks MADV_DONTNEED, completely implementing which is quite
      * complicated. However, there is one low-hanging fruit: mappings that are
@@ -913,11 +927,17 @@ abi_long target_madvise(abi_ulong start, abi_ulong len_in, int advice)
      * passthrough is safe, so do it.
      */
     mmap_lock();
-    if (advice == TARGET_MADV_DONTNEED &&
-        can_passthrough_madv_dontneed(start, end)) {
-        ret = get_errno(madvise(g2h_untagged(start), len, MADV_DONTNEED));
-        if (ret == 0) {
-            page_reset_target_data(start, start + len);
+    switch (advice) {
+    case MADV_WIPEONFORK:
+    case MADV_KEEPONFORK:
+        ret = -EINVAL;
+        /* fall through */
+    case MADV_DONTNEED:
+        if (can_passthrough_madv_dontneed(start, end)) {
+            ret = get_errno(madvise(g2h_untagged(start), len, advice));
+            if ((advice == MADV_DONTNEED) && (ret == 0)) {
+                page_reset_target_data(start, start + len);
+            }
         }
     }
     mmap_unlock();
Acked-by on v1 1/1
#1 lore raw
Quoted 32 lines · Helge Deller

Both parameters have a different value on the parisc platform, so first translate the target value into a host value for usage in the native madvise() syscall.

Those parameters are often used by security sensitive applications (e.g. tor browser, boringssl, ...) which expect the call to return a proper return code on failure, so return -EINVAL if qemu fails to forward the syscall to the host OS.

Tested with testcase of tor browser when running hppa-linux guest on x86-64 host.

linux-user/mmap.c +10 -0
index 10f5079331..c75342108c 100644
@@ -901,11 +901,25 @@ abi_long target_madvise(abi_ulong start, abi_ulong len_in, int advice)
         return -TARGET_EINVAL;
     }
 
+    /* Translate for some architectures which have different MADV_xxx values */
+    switch (advice) {
+    case TARGET_MADV_DONTNEED:      /* alpha */
+        advice = MADV_DONTNEED;
+        break;
+    case TARGET_MADV_WIPEONFORK:    /* parisc */
+        advice = MADV_WIPEONFORK;
+        break;
+    case TARGET_MADV_KEEPONFORK:    /* parisc */
+        advice = MADV_KEEPONFORK;

linux-user/mmap.c abi_long target_madvise(abi_ulong start, abi_ulong len_in, int advice) · line 922

Quoted 10 lines
linux-user/mmap.c +5 -1
+        break;
+    /* we do not care about the other MADV_xxx values yet */
+    }
+
     /*
      * A straight passthrough may not be safe because qemu sometimes turns
      * private file-backed mappings into anonymous mappings.
      *
-     * This is a hint, so ignoring and returning success is ok.
+     * For MADV_DONTNEED, which is a hint, ignoring and returning success is ok.

Actually, MADV_DONTNEED is one of the few values, which is not always a hint - it can be used to e.g. zero out pages.

As the next paragraph states, strictly speaking, MADV_DONTNEED is currently broken, because it can indeed be ignored without indication in some cases, but it's still arguably better than not honoring it at all.

Quoted 17 lines
linux-user/mmap.c +4 -5
      *
      * This breaks MADV_DONTNEED, completely implementing which is quite
      * complicated. However, there is one low-hanging fruit: mappings that are
@@ -913,11 +927,17 @@ abi_long target_madvise(abi_ulong start, abi_ulong len_in, int advice)
      * passthrough is safe, so do it.
      */
     mmap_lock();
-    if (advice == TARGET_MADV_DONTNEED &&
-        can_passthrough_madv_dontneed(start, end)) {
-        ret = get_errno(madvise(g2h_untagged(start), len, MADV_DONTNEED));
-        if (ret == 0) {
-            page_reset_target_data(start, start + len);
+    switch (advice) {
+    case MADV_WIPEONFORK:
+    case MADV_KEEPONFORK:
+        ret = -EINVAL;

linux-user/mmap.c abi_long target_madvise(abi_ulong start, abi_ulong len_in, int advice) · line 943

Quoted 10 lines
linux-user/mmap.c +7 -0
+        /* fall through */
+    case MADV_DONTNEED:
+        if (can_passthrough_madv_dontneed(start, end)) {
+            ret = get_errno(madvise(g2h_untagged(start), len, advice));
+            if ((advice == MADV_DONTNEED) && (ret == 0)) {
+                page_reset_target_data(start, start + len);
+            }
         }
     }
     mmap_unlock();

Nit: maybe rename can_passthrough_madv_dontneed() to can_passthrough(), since now it's used not only for MADV_DONTNEED?

With the MADV_DONTNEED comment change:

#2 lore raw
Quoted 38 lines · Ilya Leoshkevich
Quoted 37 lines · Helge Deller

Both parameters have a different value on the parisc platform, so first translate the target value into a host value for usage in the native madvise() syscall.

Those parameters are often used by security sensitive applications (e.g. tor browser, boringssl, ...) which expect the call to return a proper return code on failure, so return -EINVAL if qemu fails to forward the syscall to the host OS.

Tested with testcase of tor browser when running hppa-linux guest on x86-64 host.

linux-user/mmap.c +14 -0
index 10f5079331..c75342108c 100644
@@ -901,11 +901,25 @@ abi_long target_madvise(abi_ulong start, abi_ulong len_in, int advice)
          return -TARGET_EINVAL;
      }
 
+    /* Translate for some architectures which have different MADV_xxx values */
+    switch (advice) {
+    case TARGET_MADV_DONTNEED:      /* alpha */
+        advice = MADV_DONTNEED;
+        break;
+    case TARGET_MADV_WIPEONFORK:    /* parisc */
+        advice = MADV_WIPEONFORK;
+        break;
+    case TARGET_MADV_KEEPONFORK:    /* parisc */
+        advice = MADV_KEEPONFORK;
+        break;
+    /* we do not care about the other MADV_xxx values yet */
+    }
+
      /*
Quoted 8 lines

linux-user/mmap.c abi_long target_madvise(abi_ulong start, abi_ulong len_in, int advice) · line 922

Quoted 5 lines
linux-user/mmap.c +1 -1
       * A straight passthrough may not be safe because qemu sometimes turns
       * private file-backed mappings into anonymous mappings.
       *
-     * This is a hint, so ignoring and returning success is ok.
+     * For MADV_DONTNEED, which is a hint, ignoring and returning success is ok.

Actually, MADV_DONTNEED is one of the few values, which is not always a hint - it can be used to e.g. zero out pages.

Right, it _should_ zero out pages and return 0, or otherwise return failure. I think the problem is that some userspace apps will then sadly break if we change the current behaviour....

Anyway, in this patch I didn't wanted to touch MAD_DONTNEED.

Quoted 4 lines

As the next paragraph states, strictly speaking, MADV_DONTNEED is currently broken, because it can indeed be ignored without indication in some cases, but it's still arguably better than not honoring it at all.

Yep.

Quoted 22 lines
Quoted 22 lines
linux-user/mmap.c +10 -5
       *
       * This breaks MADV_DONTNEED, completely implementing which is quite
       * complicated. However, there is one low-hanging fruit: mappings that are
@@ -913,11 +927,17 @@ abi_long target_madvise(abi_ulong start, abi_ulong len_in, int advice)
       * passthrough is safe, so do it.
       */
      mmap_lock();
-    if (advice == TARGET_MADV_DONTNEED &&
-        can_passthrough_madv_dontneed(start, end)) {
-        ret = get_errno(madvise(g2h_untagged(start), len, MADV_DONTNEED));
-        if (ret == 0) {
-            page_reset_target_data(start, start + len);
+    switch (advice) {
+    case MADV_WIPEONFORK:
+    case MADV_KEEPONFORK:
+        ret = -EINVAL;
+        /* fall through */
+    case MADV_DONTNEED:
+        if (can_passthrough_madv_dontneed(start, end)) {
+            ret = get_errno(madvise(g2h_untagged(start), len, advice));
+            if ((advice == MADV_DONTNEED) && (ret == 0)) {
+                page_reset_target_data(start, start + len);
Quoted 8 lines

linux-user/mmap.c abi_long target_madvise(abi_ulong start, abi_ulong len_in, int advice) · line 943

Quoted 5 lines
linux-user/mmap.c +1 -0
+            }
          }
      }
      mmap_unlock();

Nit: maybe rename can_passthrough_madv_dontneed() to can_passthrough(), since now it's used not only for MADV_DONTNEED?

Maybe can_passthrough_madvise() is better?

Quoted 1 line

With the MADV_DONTNEED comment change:

Just for me to understand correctly:
You propose that I shouldn't touch that comment in my followup-patch, right? That's ok.

Quoted 1 line

Thanks!
Helge

#3 lore raw
Quoted 92 lines · Helge Deller
Quoted 38 lines · Ilya Leoshkevich
Quoted 37 lines · Helge Deller

Both parameters have a different value on the parisc platform, so first translate the target value into a host value for usage in the native madvise() syscall.

Those parameters are often used by security sensitive applications (e.g. tor browser, boringssl, ...) which expect the call to return a proper return code on failure, so return -EINVAL if qemu fails to forward the syscall to the host OS.

Tested with testcase of tor browser when running hppa-linux guest on x86-64 host.

linux-user/mmap.c +14 -0
index 10f5079331..c75342108c 100644
@@ -901,11 +901,25 @@ abi_long target_madvise(abi_ulong start, abi_ulong len_in, int advice)
          return -TARGET_EINVAL;
      }
 
+    /* Translate for some architectures which have different MADV_xxx values */
+    switch (advice) {
+    case TARGET_MADV_DONTNEED:      /* alpha */
+        advice = MADV_DONTNEED;
+        break;
+    case TARGET_MADV_WIPEONFORK:    /* parisc */
+        advice = MADV_WIPEONFORK;
+        break;
+    case TARGET_MADV_KEEPONFORK:    /* parisc */
+        advice = MADV_KEEPONFORK;
+        break;
+    /* we do not care about the other MADV_xxx values yet */
+    }
+
      /*
Quoted 8 lines

linux-user/mmap.c abi_long target_madvise(abi_ulong start, abi_ulong len_in, int advice) · line 922

Quoted 5 lines
linux-user/mmap.c +1 -1
       * A straight passthrough may not be safe because qemu sometimes turns
       * private file-backed mappings into anonymous mappings.
       *
-     * This is a hint, so ignoring and returning success is ok.
+     * For MADV_DONTNEED, which is a hint, ignoring and returning success is ok.

Actually, MADV_DONTNEED is one of the few values, which is not always a hint - it can be used to e.g. zero out pages.

Right, it _should_ zero out pages and return 0, or otherwise return failure. I think the problem is that some userspace apps will then sadly break if we change the current behaviour....

Anyway, in this patch I didn't wanted to touch MAD_DONTNEED.

Quoted 4 lines

As the next paragraph states, strictly speaking, MADV_DONTNEED is currently broken, because it can indeed be ignored without indication in some cases, but it's still arguably better than not honoring it at all.

Yep.

Quoted 22 lines
Quoted 22 lines
linux-user/mmap.c +10 -5
       *
       * This breaks MADV_DONTNEED, completely implementing which is quite
       * complicated. However, there is one low-hanging fruit: mappings that are
@@ -913,11 +927,17 @@ abi_long target_madvise(abi_ulong start, abi_ulong len_in, int advice)
       * passthrough is safe, so do it.
       */
      mmap_lock();
-    if (advice == TARGET_MADV_DONTNEED &&
-        can_passthrough_madv_dontneed(start, end)) {
-        ret = get_errno(madvise(g2h_untagged(start), len, MADV_DONTNEED));
-        if (ret == 0) {
-            page_reset_target_data(start, start + len);
+    switch (advice) {
+    case MADV_WIPEONFORK:
+    case MADV_KEEPONFORK:
+        ret = -EINVAL;
+        /* fall through */
+    case MADV_DONTNEED:
+        if (can_passthrough_madv_dontneed(start, end)) {
+            ret = get_errno(madvise(g2h_untagged(start), len, advice));
+            if ((advice == MADV_DONTNEED) && (ret == 0)) {
+                page_reset_target_data(start, start + len);
Quoted 8 lines

linux-user/mmap.c abi_long target_madvise(abi_ulong start, abi_ulong len_in, int advice) · line 943

Quoted 5 lines
linux-user/mmap.c +1 -0
+            }
          }
      }
      mmap_unlock();

Nit: maybe rename can_passthrough_madv_dontneed() to can_passthrough(), since now it's used not only for MADV_DONTNEED?

Quoted 1 line

Maybe can_passthrough_madvise() is better?

Sounds good to me as well. The idea with PAGE_PASSTHROUGH was that we should be able to passthrough anything; but with this patch we still use it only for madvise(), and indicating it in the name makes sense.

Quoted 5 lines
Quoted 1 line
Quoted 1 line

With the MADV_DONTNEED comment change:

Just for me to understand correctly:
You propose that I shouldn't touch that comment in my followup-patch, right? That's ok.

Either that, or maybe make it more precise - strictly speaking, it's not correct at the moment anyway. Maybe something like this?

Most advice values are hints, so ignoring and returning success is ok.

However, this would break MADV_DONTNEED, MADV_WIPEONFORK and MADV_KEEPONFORK ...

Quoted 4 lines
Quoted 1 line

Thanks!
Helge