All of lore.kernel.org
 help / color / mirror / Atom feed
From: Roi Klevansky <roiklevansky@gmail.com>
To: qemu-devel@nongnu.org
Cc: "Alex Bennée" <alex.bennee@linaro.org>,
	"Laurent Vivier" <laurent@vivier.eu>,
	"Helge Deller" <deller@gmx.de>,
	"Pierrick Bouvier" <pierrick.bouvier@oss.qualcomm.com>,
	"Roi Klevansky" <roiklevansky@gmail.com>
Subject: [PATCH 1/2] linux-user: return ENOMEM from madvise on unmapped ranges
Date: Tue,  1 Sep 2026 00:20:39 +0300	[thread overview]
Message-ID: <20260831212040.1833650-2-roiklevansky@gmail.com> (raw)
In-Reply-To: <20260831212040.1833650-1-roiklevansky@gmail.com>

The kernel fails with ENOMEM when madvise() is called with a partially
or fully unmapped address range. This fix allows target_madvise() to
emulate this behavior by calling page_check_range() upfront while still
allowing advice specific errors to take precedence.

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4382
Signed-off-by: Roi Klevansky <roiklevansky@gmail.com>
---
 linux-user/mmap.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/linux-user/mmap.c b/linux-user/mmap.c
index cc0c2ee6c2..e5195cf501 100644
--- a/linux-user/mmap.c
+++ b/linux-user/mmap.c
@@ -1305,8 +1305,14 @@ abi_long target_madvise(abi_ulong start, abi_ulong len_in, int advice)
      * success, which is broken but some userspace programs fail to work
      * otherwise. Completely implementing such emulation is quite complicated
      * though.
+     *
+     * When some (or all) of the pages in the range are not mapped, madvise
+     * should fail with -ENOMEM. If another error occurs, its value is
+     * returned instead.
      */
     mmap_lock();
+    ret = page_check_range(start, len, PAGE_VALID) ? 0 : -TARGET_ENOMEM;
+
     switch (advice) {
     case MADV_NORMAL:
     case MADV_RANDOM:
@@ -1316,7 +1322,6 @@ abi_long target_madvise(abi_ulong start, abi_ulong len_in, int advice)
     case MADV_FREE:
     case MADV_COLD:
     case MADV_PAGEOUT:
-        ret = 0; /* OK */
         break;
     case MADV_REMOVE:
         ret = -EOPNOTSUPP;
-- 
2.55.0



  reply	other threads:[~2026-08-31 23:54 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-31 21:20 [PATCH 0/2] linux-user: return ENOMEM from madvise on unmapped ranges Roi Klevansky
2026-08-31 21:20 ` Roi Klevansky [this message]
2026-08-31 21:20 ` [PATCH 2/2] tests/tcg: add linux-user test for madvise returning ENOMEM Roi Klevansky

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260831212040.1833650-2-roiklevansky@gmail.com \
    --to=roiklevansky@gmail.com \
    --cc=alex.bennee@linaro.org \
    --cc=deller@gmx.de \
    --cc=laurent@vivier.eu \
    --cc=pierrick.bouvier@oss.qualcomm.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.