From: Laurent Vivier <laurent@vivier.eu>
To: qemu-devel@nongnu.org
Cc: Laurent Vivier <laurent@vivier.eu>, Helge Deller <deller@gmx.de>
Subject: [PULL 07/28] linux-user: Fix brk() to release pages
Date: Fri, 10 Mar 2023 23:09:06 +0100 [thread overview]
Message-ID: <20230310220927.326606-8-laurent@vivier.eu> (raw)
In-Reply-To: <20230310220927.326606-1-laurent@vivier.eu>
From: Helge Deller <deller@gmx.de>
The current brk() implementation does not de-allocate pages if a lower
address is given compared to earlier brk() calls.
But according to the manpage, brk() shall deallocate memory in this case
and currently it breaks a real-world application, specifically building
the debian gcl package in qemu-user.
Fix this issue by reworking the qemu brk() implementation.
Tested with the C-code testcase included in qemu commit 4d1de87c750, and
by building debian package of gcl in a hppa-linux guest on a x86-64
host.
Signed-off-by: Helge Deller <deller@gmx.de>
Message-Id: <Y6gId80ek49TK1xB@p100>
Signed-off-by: Laurent Vivier <laurent@vivier.eu>
---
linux-user/syscall.c | 69 ++++++++++++++++++++++----------------------
1 file changed, 35 insertions(+), 34 deletions(-)
diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 49a4fee89918..931f9db47552 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -795,49 +795,52 @@ static inline int host_to_target_sock_type(int host_type)
}
static abi_ulong target_brk;
-static abi_ulong target_original_brk;
static abi_ulong brk_page;
void target_set_brk(abi_ulong new_brk)
{
- target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
+ target_brk = new_brk;
brk_page = HOST_PAGE_ALIGN(target_brk);
}
-//#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
-#define DEBUGF_BRK(message, args...)
-
/* do_brk() must return target values and target errnos. */
-abi_long do_brk(abi_ulong new_brk)
+abi_long do_brk(abi_ulong brk_val)
{
abi_long mapped_addr;
abi_ulong new_alloc_size;
+ abi_ulong new_brk, new_host_brk_page;
/* brk pointers are always untagged */
- DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk);
-
- if (!new_brk) {
- DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk);
+ /* return old brk value if brk_val unchanged or zero */
+ if (!brk_val || brk_val == target_brk) {
return target_brk;
}
- if (new_brk < target_original_brk) {
- DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n",
- target_brk);
+
+ new_brk = TARGET_PAGE_ALIGN(brk_val);
+ new_host_brk_page = HOST_PAGE_ALIGN(brk_val);
+
+ /* brk_val and old target_brk might be on the same page */
+ if (new_brk == TARGET_PAGE_ALIGN(target_brk)) {
+ if (brk_val > target_brk) {
+ /* empty remaining bytes in (possibly larger) host page */
+ memset(g2h_untagged(target_brk), 0, new_host_brk_page - target_brk);
+ }
+ target_brk = brk_val;
return target_brk;
}
- /* If the new brk is less than the highest page reserved to the
- * target heap allocation, set it and we're almost done... */
- if (new_brk <= brk_page) {
- /* Heap contents are initialized to zero, as for anonymous
- * mapped pages. */
- if (new_brk > target_brk) {
- memset(g2h_untagged(target_brk), 0, new_brk - target_brk);
- }
- target_brk = new_brk;
- DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n", target_brk);
- return target_brk;
+ /* Release heap if necesary */
+ if (new_brk < target_brk) {
+ /* empty remaining bytes in (possibly larger) host page */
+ memset(g2h_untagged(brk_val), 0, new_host_brk_page - brk_val);
+
+ /* free unused host pages and set new brk_page */
+ target_munmap(new_host_brk_page, brk_page - new_host_brk_page);
+ brk_page = new_host_brk_page;
+
+ target_brk = brk_val;
+ return target_brk;
}
/* We need to allocate more memory after the brk... Note that
@@ -846,10 +849,14 @@ abi_long do_brk(abi_ulong new_brk)
* itself); instead we treat "mapped but at wrong address" as
* a failure and unmap again.
*/
- new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page);
- mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
+ new_alloc_size = new_host_brk_page - brk_page;
+ if (new_alloc_size) {
+ mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
PROT_READ|PROT_WRITE,
MAP_ANON|MAP_PRIVATE, 0, 0));
+ } else {
+ mapped_addr = brk_page;
+ }
if (mapped_addr == brk_page) {
/* Heap contents are initialized to zero, as for anonymous
@@ -861,10 +868,8 @@ abi_long do_brk(abi_ulong new_brk)
* then shrunken). */
memset(g2h_untagged(target_brk), 0, brk_page - target_brk);
- target_brk = new_brk;
- brk_page = HOST_PAGE_ALIGN(target_brk);
- DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n",
- target_brk);
+ target_brk = brk_val;
+ brk_page = new_host_brk_page;
return target_brk;
} else if (mapped_addr != -1) {
/* Mapped but at wrong address, meaning there wasn't actually
@@ -872,10 +877,6 @@ abi_long do_brk(abi_ulong new_brk)
*/
target_munmap(mapped_addr, new_alloc_size);
mapped_addr = -1;
- DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk);
- }
- else {
- DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", target_brk);
}
#if defined(TARGET_ALPHA)
--
2.39.2
next prev parent reply other threads:[~2023-03-10 22:12 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-10 22:08 [PULL 00/28] Linux user for 8.0 patches Laurent Vivier
2023-03-10 22:09 ` [PULL 01/28] linux-user: Fix access to /proc/self/exe Laurent Vivier
2023-03-10 22:09 ` [PULL 02/28] linux-user: fix timerfd read endianness conversion Laurent Vivier
2023-03-10 22:09 ` [PULL 03/28] linux-user: add target to host netlink conversions Laurent Vivier
2023-03-10 22:09 ` [PULL 04/28] linux-user: Fix unaligned memory access in prlimit64 syscall Laurent Vivier
2023-03-10 22:09 ` [PULL 05/28] linux-user: add support for xtensa FDPIC Laurent Vivier
2023-03-10 22:09 ` [PULL 06/28] linux-user: fill out task state in /proc/self/stat Laurent Vivier
2023-03-10 22:09 ` Laurent Vivier [this message]
2023-03-10 22:09 ` [PULL 08/28] linux-user: Provide print_raw_param64() for 64-bit values Laurent Vivier
2023-03-10 22:09 ` [PULL 09/28] linux-user: Add strace for prlimit64() syscall Laurent Vivier
2023-03-10 22:09 ` [PULL 10/28] linux-user: fix sockaddr_in6 endianness Laurent Vivier
2023-03-10 22:09 ` [PULL 11/28] linux-user: handle netlink flag NLA_F_NESTED Laurent Vivier
2023-03-10 22:09 ` [PULL 12/28] linux-user: Add translation for argument of msync() Laurent Vivier
2023-03-10 22:09 ` [PULL 13/28] linux-user: Emulate CLONE_PIDFD flag in clone() Laurent Vivier
2023-03-10 22:09 ` [PULL 14/28] linux-user/sparc: Tidy syscall trap Laurent Vivier
2023-03-10 22:09 ` [PULL 15/28] linux-user/sparc: Tidy syscall error return Laurent Vivier
2023-03-10 22:09 ` [PULL 16/28] linux-user/sparc: Use TT_TRAP for flush windows Laurent Vivier
2023-03-10 22:09 ` [PULL 17/28] linux-user/sparc: Tidy window spill/fill traps Laurent Vivier
2023-03-10 22:09 ` [PULL 18/28] linux-user/sparc: Fix sparc64_{get, set}_context traps Laurent Vivier
2023-03-10 22:09 ` [PULL 19/28] linux-user/sparc: Handle software breakpoint trap Laurent Vivier
2023-03-10 22:09 ` [PULL 20/28] linux-user/sparc: Handle division by zero traps Laurent Vivier
2023-03-10 22:09 ` [PULL 21/28] linux-user/sparc: Handle getcc, setcc, getpsr traps Laurent Vivier
2023-03-10 22:09 ` [PULL 22/28] linux-user/sparc: Handle priviledged opcode trap Laurent Vivier
2023-03-10 22:09 ` [PULL 23/28] linux-user/sparc: Handle privilidged action trap Laurent Vivier
2023-03-10 22:09 ` [PULL 24/28] linux-user/sparc: Handle coprocessor disabled trap Laurent Vivier
2023-03-10 22:09 ` [PULL 25/28] linux-user/sparc: Handle unimplemented flush trap Laurent Vivier
2023-03-10 22:09 ` [PULL 26/28] linux-user/sparc: Handle floating-point exceptions Laurent Vivier
2023-03-10 22:09 ` [PULL 27/28] linux-user/sparc: Handle tag overflow traps Laurent Vivier
2023-03-10 22:09 ` [PULL 28/28] linux-user: fix bug about incorrect base addresss of gdt on i386 and x86_64 Laurent Vivier
2023-03-12 17:42 ` [PULL 00/28] Linux user for 8.0 patches Peter Maydell
-- strict thread matches above, loose matches on Subject: below --
2023-03-08 13:28 Laurent Vivier
2023-03-08 13:28 ` [PULL 07/28] linux-user: Fix brk() to release pages Laurent Vivier
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=20230310220927.326606-8-laurent@vivier.eu \
--to=laurent@vivier.eu \
--cc=deller@gmx.de \
--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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).