From patchwork Tue Aug 1 23:27:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Helge Deller X-Patchwork-Id: 13337360 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E7C14EB64DD for ; Tue, 1 Aug 2023 23:29:33 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qQymT-0001Jj-Ji; Tue, 01 Aug 2023 19:27:57 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qQymS-0001Ii-54 for qemu-devel@nongnu.org; Tue, 01 Aug 2023 19:27:56 -0400 Received: from mout.gmx.net ([212.227.17.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qQymO-00077t-TX for qemu-devel@nongnu.org; Tue, 01 Aug 2023 19:27:55 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1690932467; x=1691537267; i=deller@gmx.de; bh=dNLgHG2agLIXgxeCp0C/7+fkMs1PEdZE6CSSHjmy6d0=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=RLojQbWby4qWMuYp/OpV5DD3LQu6GnNyHhqXusJNLnfKqToqE30nokbaTs9flKHWjRcnl05 LqLPYri6ktKWUgLhefHfPsQZf/cm60Kp5YL+ObxyDUvEnzJV7IBkI++33HXZQO6Vv6lI4eDxX 4IA4JiiG6vc8YuHBKu1Jm4JvWm1bup7d4MhMdg19LUKk6nJqH/iidZ9Buc2WEt7bILp1F4zun 4To+H94Tt9wPUY0yecbr5MFK1LvdWG3fKEYGIuxRngznQG7uzxHcSSUYArmuF3moAyCgC2zqY CQiWZMUytGJcxWAtsh5udVkI7a3trHtnLIIgYWhkIeVFCEeWOC+A== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from p100.fritz.box ([94.134.150.247]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MxDou-1pcIvr0Eq8-00xZJK; Wed, 02 Aug 2023 01:27:47 +0200 From: Helge Deller To: qemu-devel@nongnu.org Cc: Richard Henderson , Laurent Vivier , Paolo Bonzini , Joel Stanley , Akihiko Odaki , Helge Deller Subject: [PATCH v6 1/8] linux-user: Unset MAP_FIXED_NOREPLACE for host Date: Wed, 2 Aug 2023 01:27:38 +0200 Message-ID: <20230801232745.4125-2-deller@gmx.de> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230801232745.4125-1-deller@gmx.de> References: <20230801232745.4125-1-deller@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:B3tlJFMshtiM2Poswf1l5+h72Tn/39nTePXhwLZ1K7DWjHtB9t4 ss+UfiYeoRbyApOMTnkTi8/w5NMxJmK8e/MdWFUxGc6ipHpgsHF5EPXpbb+Wf9KHVw1RP+O oVL6bKCCvGwsQRzfXUaQZR5zcW6roqruY1hq0eVUY5mrJCZRJpD0XoFkjeshX0aJMQwVQQv tVRg7VW4gn92bicRHMPWA== UI-OutboundReport: notjunk:1;M01:P0:/36yxsE/tso=;mt90+x41U2D6EVVh0VO6Dna8tps eiI+Z3pn8nEnpTc4e9a2QG2kMR26VLCHY4/kDcqdvydUSuqSSyO7iSj2gLiAiex1eIayZm5xh cWAESoDe4QaisltZBYngzU/m3JSLRpYlZgJnGnYVSmL6YzjqBgactHB0x5rHzquRe9RpAKVas OJzj0pdcoianZkw6y8PqwXr3U8apOk9X6nhiCYwvnQXX4g7wrTdKmwpVvBDzYisaVDzaNuVoC pMFqlj0n+rjr8qePbqomGp5UfesIIn68/oXkY+aRzAlZmzZyh9nDHNt7G7dR//fcjPHzK1FQi VcmbXJ8nupIO5ohDPBqn/gMWSoSRPcqrCD06O9oIHdvY/mdJlDXnuNQWNvWK/d4XNdtIm72YZ Tc9vm87wzHcVgLpi5r1Wm/LKpPhSBQKdS6YY+WxDCk1wpWr4s5fgxF1LCU79/IqrPXQxRp7Qc 0qOCAtMUKO7d6OcH2lMxSg7hZGFEcUd2L9X1BPFd2usK5PJQncsZoNLHEXXL2+6G1VzGwFAd4 Kt9hfqiYrqPIDVCZV6Szh3CngRANgCy+Kow5I1fysg+acQjrlQ4iTvGFhLAQpLeGiWg5ersjr BAIoXLwLGvPmqnNoX/tLfjtay6pvxwwwJhqmuVy8Q3Py0SYOztkDHSlpCwfgCUYoch/b4Eyx6 R/ylUrckFvH8+i3q1rtscmFmsSkXxPQuUofw1hlQgTKnO4eBa2i/Ojq4ki5qU4sgT6Nwkb26k wCvDb4WvZBLxJTvLmozKLhitgTDmk2rNWYQdpXnULMnt9Fnt2yAbrlvN0maZJKkfVAajLFNPR YALzT7Q7lQphxwATX5yE7Lsub3pWKl9om7zpABqAr5r6RTsoEkFPfbuYBKrpcBQFPzOOI3rg+ 656EePfTi/EzNYoheXDy6TWrrySiKQJ+qYGqqisSga4478/VZOOaa3XG6ZSnIS7xs/1owPiiY oKpm1HMfUXFrX6/mGGr0BbE8/Xw= Received-SPF: pass client-ip=212.227.17.21; envelope-from=deller@gmx.de; helo=mout.gmx.net X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Akihiko Odaki Passing MAP_FIXED_NOREPLACE to host will fail if the virtual address space is reserved with mmap. Replace it with MAP_FIXED. Signed-off-by: Akihiko Odaki Reviewed-by: Helge Deller Signed-off-by: Helge Deller --- linux-user/mmap.c | 2 ++ 1 file changed, 2 insertions(+) -- 2.41.0 diff --git a/linux-user/mmap.c b/linux-user/mmap.c index a5dfb56545..2f26cbaf5d 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -610,6 +610,8 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot, goto fail; } + flags = (flags & ~MAP_FIXED_NOREPLACE) | MAP_FIXED; + /* * worst case: we cannot map the file because the offset is not * aligned, so we read it From patchwork Tue Aug 1 23:27:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Helge Deller X-Patchwork-Id: 13337358 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 12280C0015E for ; Tue, 1 Aug 2023 23:29:04 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qQymU-0001K6-7P; Tue, 01 Aug 2023 19:27:58 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qQymS-0001Im-8C for qemu-devel@nongnu.org; Tue, 01 Aug 2023 19:27:56 -0400 Received: from mout.gmx.net ([212.227.17.22]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qQymP-00077u-0T for qemu-devel@nongnu.org; Tue, 01 Aug 2023 19:27:55 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1690932467; x=1691537267; i=deller@gmx.de; bh=N0Dlhp9SfJjPZs7/6Px6gA2M4iFsr6qxZXp6AFGyc34=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=ae2GvHY+wc+d5DW1xyLJfYeC4bMWpFF4zpZCDuY1L6e57LY80RFSpvdA2uu3l1p9MxxO0cL Ke3p0a7DgI2u7nmQ27/O8ilv32Z9xoDRt6UodpDX4prWDficF2GKsEacJwzNxpCaWTrJFaZz7 4LSsa3fgua12L1lSphZqIeQWk/NJdUUr8GD+Mql9Geql6RCxMTt2WnwdwcRyxz63xIy7SXQr1 F7U/y5B+fcNgs2NWqQPC9BOMbOpXIi5spkXLxrwQT9FXyNjE65wSjtVXUZbRf7Mkof1KmUFtK RJlY/HmVLFeolsFXDDqsuKI22b18wOqi9XrzwR0uWb8+o3OnDQ4Q== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from p100.fritz.box ([94.134.150.247]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1N5mGB-1plPrs1Emh-0179wv; Wed, 02 Aug 2023 01:27:47 +0200 From: Helge Deller To: qemu-devel@nongnu.org Cc: Richard Henderson , Laurent Vivier , Paolo Bonzini , Joel Stanley , Akihiko Odaki , Helge Deller Subject: [PATCH v6 2/8] linux-user: Do not call get_errno() in do_brk() Date: Wed, 2 Aug 2023 01:27:39 +0200 Message-ID: <20230801232745.4125-3-deller@gmx.de> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230801232745.4125-1-deller@gmx.de> References: <20230801232745.4125-1-deller@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:TmUFxzVccsPk1UUJ2K+MD5sAUSAzgEXo8tLUBGR2FYcGWfFhPK9 giW/X8ohG8VXEZQzMSTwK/VQpbdX+uwZ9FBLBTGwfdGPgnWeWbJ+LIpM6zclNDCIB0X30rK fnPy8ywWu6U34YTTN0oulZoWtIiemJ8Yibw1Yb+RJ8C99tvbhWBFD9FPYZafkA4MsgVJOBa ZXtFRjbCCB1n7c9ULFuKA== UI-OutboundReport: notjunk:1;M01:P0:2CFNa0EdhMA=;rp4y4vDVstcfqiOUZoWJFcA9N/I gqBJzdtpRtk5cDCCJQxVlIx3kPgslCixFWPCx1E7OtjhKJTd3NLRiGnDRQCN6KlVU2YWeyMvK skyYMKLfdfnpdjQ/bT7IwNjY8Q5G6m5fIbOD2SflvD54k1F9dXUkwGPAxSQc4z4FmBSmeVXax Z4LRrlwXCLYQkMWxkfRS5rw/jMAoooyPW4uYnaebjrX8Syrz4B1+VPt1Q/FSWwIkLnwLkBMv8 aNq2unxHda9HPc5qv4mjythJ7gCIJkBzvNZod7I79yWoSMvPDz6hIXiy6ZM6mrwepi33ey56G fFP0An5Wzgeohdv1T0ukyg1eD0yIcz5bo642iBWDQwZBgqf4sAhpJLpuxINPqG0yeExS9Q65J rRooX3/k5SAuCcCGDEFm60hhXbeg70myo8QSVx4iP0ol1m75qhDxqcSHlZjQUGtFeJJANhNtA gkUO41cg+sQ48L1BJm+ypPzFJTFwGPY4lblnvWSQzELB7hXHrSxH7XKlAniOaQFUb46YivXsa BnuiDz7SUF8y0/0g2439JnYWVj8BRluVPZsfdl06i1+xpT1ty+DVGUfV0d+UnpmeZ+r+JC0qo JG1O/rHz/rGLKADfhcTbTiF1qLm3PJQOpUIXIUMVOpVMKjQ8utuIt+BUREJXFknKkBxKHyrc+ 47vTafTIDOGFGDdt8KVRBOA1qpled2bNZbVEeDpT2mVLeAq/JZ0oj592+kGQbJwWDmRrYxa+b AEk816+FttGxnq0rfHNkqX/yAEobFCROs+zE8hfzOljNPABcXqANk0uyPUpSoAR+z5MFvbG3P aLLjSkIbQpsG3dz8GS3s/Jx+ovYII+GIn0dt4Gz5VqhN3CPS9LjXA8fnGXDiP/lyAYqZJvzya IxAKjvYaEmm4GbUUVbZ6ijZi3sTYyB/XVuT/poTO3YwzchpKzl1fZMWeyqF7c+CEj4bnDeFLf gCGOnuGwVG0r7yaxncT5vdPLqOQ= Received-SPF: pass client-ip=212.227.17.22; envelope-from=deller@gmx.de; helo=mout.gmx.net X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Akihiko Odaki Later the returned value is compared with -1, and negated errno is not expected. Fixes: 00faf08c95 ("linux-user: Don't use MAP_FIXED in do_brk()") Signed-off-by: Akihiko Odaki Reviewed-by: Helge Deller Signed-off-by: Helge Deller --- linux-user/syscall.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) -- 2.41.0 diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 95727a816a..b9d2ec02f9 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -862,9 +862,9 @@ abi_long do_brk(abi_ulong brk_val) */ if (new_host_brk_page > brk_page) { new_alloc_size = new_host_brk_page - brk_page; - mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size, - PROT_READ|PROT_WRITE, - MAP_ANON|MAP_PRIVATE, 0, 0)); + mapped_addr = target_mmap(brk_page, new_alloc_size, + PROT_READ|PROT_WRITE, + MAP_ANON|MAP_PRIVATE, 0, 0); } else { new_alloc_size = 0; mapped_addr = brk_page; From patchwork Tue Aug 1 23:27:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Helge Deller X-Patchwork-Id: 13337359 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C4FF2EB64DD for ; Tue, 1 Aug 2023 23:29:09 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qQymW-0001XW-Jg; Tue, 01 Aug 2023 19:28:00 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qQymT-0001Jk-He for qemu-devel@nongnu.org; Tue, 01 Aug 2023 19:27:57 -0400 Received: from mout.gmx.net ([212.227.17.20]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qQymP-000788-Lo for qemu-devel@nongnu.org; Tue, 01 Aug 2023 19:27:57 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1690932467; x=1691537267; i=deller@gmx.de; bh=682nIaPBgnz2pLAOe9TKVmBAtWcENqYxpj26a83E5nY=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=rlnQ+OrnJGQDFHgAyrnhw/E/qhvTftkCnbKP5duD4NA6JGu5TfGyQCbmf6BNArRbxfMWeb4 I2YYVeqOkJQADD2r/cnYj+uKNi9blULwlTj5QKQ80knCKsQXy2xkoIGMhhXqXqCDpLNwnW/Cc z50ip8iHBCY4sR2eFgpAbVknhxFKYeJAXhfKcvW/fie5tXBGigShoXMl2FTrZOeQriGiRMO8z 08BZIqxzMDWM1zr7lUGXu0Ve7JlZ6zSvV80hd4KZYqVA2CRmEHapi9NvC1KKeLOTfRNrMN26/ gkIHzL+6+nmHnmoLjUXqCUYVnbX2ICQ9Q7Xq6REsB+YuoJ0KAtjA== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from p100.fritz.box ([94.134.150.247]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MtOKc-1pZZNd2RYs-00uqI8; Wed, 02 Aug 2023 01:27:47 +0200 From: Helge Deller To: qemu-devel@nongnu.org Cc: Richard Henderson , Laurent Vivier , Paolo Bonzini , Joel Stanley , Akihiko Odaki , Helge Deller Subject: [PATCH v6 3/8] linux-user: Use MAP_FIXED_NOREPLACE for do_brk() Date: Wed, 2 Aug 2023 01:27:40 +0200 Message-ID: <20230801232745.4125-4-deller@gmx.de> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230801232745.4125-1-deller@gmx.de> References: <20230801232745.4125-1-deller@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:2zbxf4Mlm/sfRfMCxhUDsMmDMmpgR4UM56FXN4/Xnj4ZbT4C38u 2ydGFZ2GmTlK3BEdAPI8FOUf70MnDWYhypt2bT7HoovpI9wT+4BeQTE5yharYHzcoVRcwXH Meq5RUSwuuSO6SPBSoy7kqORAkULNtBi1mlmSSLLNEFG3tSvoAv1jbYbYOGPTGj30h4VtH8 WUW81iQPndRcfbsY18JNw== UI-OutboundReport: notjunk:1;M01:P0:vUQg5kBlA4w=;alQkAbN6aqIx1M49faYAsK8oKVp QLf1y3o3V9aMUppmo/vd23t0mW7XlyzmU2cgxUXWrTQCp7ONqSThcYtSPGmYnoKFmhQ/JasDh cP7txei3w/CgK1Jz/SxTHsnvd8sTl3ZuFT+Wyi3uWDWfxOMn4mmIakjOTsNMr+ioiN/SfKMRX 7/p7E+3BAKZFDdHD8yGArTQ+Yf/4XwMHD8d9boLKVrGV3Qvv3aBn0AE33oO6FVgh6X3CT0EKg /JRZAFqowvNFrc9Z+FIFnfL9gkjjqVmgvQpsMrJeiSouDoMe0gQ9xAARZxncympNikXo8szq/ 0T6VTT6sa9vqpdtMeNV50CvojPTR+eLqmPqrbb8RXLLJjAiqXTCGuW4KUDAbVc804RF0JSNk2 reU+H34upvgr/yjfr0WN1YDNozINWscfjY1AcG/SEC8wB36MMbtuswaGidZCtrqwkqtBI53M9 bk3ge1D8Ci+WQvIzI+hlNYSQackZa9gx/hthZfLf+UKZ6rXEVm7Fi1CU8CLLAtwBQ/6qtiTTs eGEqh+LHUoPnSx67bDZnJ7+WAmgpwLfl2HzTU2H7gnBqscw9bI/XpemDdj91H1zH/3JYy3NAT Em4o1tWjkOvHDJg9Qu4NY8jqV7Ya5mLrBCIzJPMKVEbmuFnXXc2qo1FGDr5eKOP4T2J2p0BQl VBLsDM/QBsBTigyHeWtjkYGtAjOcAa72o1DwrwKSImJyH+mI9k5Xrs/HRAfu+buU/fyHb8/aC YT91PGw2DBd+tbC+XiszHbHvakfNraVJe71pMZ74lCrKdqNbTqsmikm1GdANQkyXGBy5yznMi eCPzA9vkUN7S081mlYmYtG3D292FQnYsPJ+B7buWOqmUVxsp720PLvPv3a1DyLB14JwE/oU27 imfvLW8iTqkgGdpgICURAwRKzmMGH7r631qHHxl4GHuFHutHsw67zf0Idzq4WhTyFqTqRAycb d1UoQT6h/N+Gx/WZ3reDXyqoT1Q= Received-SPF: pass client-ip=212.227.17.20; envelope-from=deller@gmx.de; helo=mout.gmx.net X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Akihiko Odaki MAP_FIXED_NOREPLACE can ensure the mapped address is fixed without concerning that the new mapping overwrites something else. Signed-off-by: Akihiko Odaki Reviewed-by: Helge Deller Signed-off-by: Helge Deller --- linux-user/syscall.c | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) -- 2.41.0 diff --git a/linux-user/syscall.c b/linux-user/syscall.c index b9d2ec02f9..ac429a185a 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -854,17 +854,12 @@ abi_long do_brk(abi_ulong brk_val) return target_brk; } - /* We need to allocate more memory after the brk... Note that - * we don't use MAP_FIXED because that will map over the top of - * any existing mapping (like the one with the host libc or qemu - * itself); instead we treat "mapped but at wrong address" as - * a failure and unmap again. - */ if (new_host_brk_page > brk_page) { new_alloc_size = new_host_brk_page - brk_page; mapped_addr = target_mmap(brk_page, new_alloc_size, - PROT_READ|PROT_WRITE, - MAP_ANON|MAP_PRIVATE, 0, 0); + PROT_READ | PROT_WRITE, + MAP_FIXED_NOREPLACE | MAP_ANON | MAP_PRIVATE, + 0, 0); } else { new_alloc_size = 0; mapped_addr = brk_page; @@ -883,12 +878,6 @@ abi_long do_brk(abi_ulong brk_val) 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 - * enough space for this brk. - */ - target_munmap(mapped_addr, new_alloc_size); - mapped_addr = -1; } #if defined(TARGET_ALPHA) From patchwork Tue Aug 1 23:27:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Helge Deller X-Patchwork-Id: 13337354 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 98884EB64DD for ; Tue, 1 Aug 2023 23:28:20 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qQymS-0001Jc-UC; Tue, 01 Aug 2023 19:27:56 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qQymQ-0001BK-TA for qemu-devel@nongnu.org; Tue, 01 Aug 2023 19:27:54 -0400 Received: from mout.gmx.net ([212.227.17.22]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qQymO-00077y-TX for qemu-devel@nongnu.org; Tue, 01 Aug 2023 19:27:54 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1690932468; x=1691537268; i=deller@gmx.de; bh=ZAGQPFsn2pZ8MmOMealg/D8/q1s7JqKrqHIFaQj3uic=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=Gh2/qwmqWyRJ24gY2CB42NH00tEY15toAeK70pW94ij+N7P1J3S5PU5E2fy8WtCiRjWCMCo kZNIBuvjwcu+FyLDjxyTruNyT5ckarhcRHBcCZV/VDn2mJ0E6cIq84BgpAErX7xviAAnOpMcp +RbDvm6TxxFA/aV1slt4Ya0M/RW7Px/ykilql+jR+D0La75v0NhiL/q64eRHmlK5GhZSIYqnD U40Condu0SAu4MqGnrDyGO5/1fPRO5RjILbHH0i6uVVCoAcBIjcYdcrHL8EhRqNFZHTK/cw5h UP/paZcC1JWKqQSbWrzqV7tkVB1/P6P+5iWR9v5CjFYN7lndcMYQ== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from p100.fritz.box ([94.134.150.247]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MbAgq-1ppS6V3RPc-00beiI; Wed, 02 Aug 2023 01:27:47 +0200 From: Helge Deller To: qemu-devel@nongnu.org Cc: Richard Henderson , Laurent Vivier , Paolo Bonzini , Joel Stanley , Akihiko Odaki , Helge Deller Subject: [PATCH v6 4/8] linux-user: Do nothing if too small brk is specified Date: Wed, 2 Aug 2023 01:27:41 +0200 Message-ID: <20230801232745.4125-5-deller@gmx.de> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230801232745.4125-1-deller@gmx.de> References: <20230801232745.4125-1-deller@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:LwLTCi1pUahACstVhvRkf5Bmkp+Qetgts09Fqen6Tsl9OjruMQ0 hwoaKFALyYanwLNqB3/kwoGZYFeS/ujTBSer2QxtiI+RiRwsLzNlanbn/nyrRTfpKPM8tlT EwRQQ0wjVIEMApj0IArvREWMXH4dIh4hXFK/qwuRxkcBhV0qdSIXOoTHRAlFb4z9O4ts7kS 5r9IFf69hbnjfTTvRjXiQ== UI-OutboundReport: notjunk:1;M01:P0:csyIOGYGJuc=;2AsaHcIZG9g17YPWrIL6Dtgo2p/ +LLqsolo5kGuwBuQG3xNK1n35srQAFcjMdn333D8JX8Qltks2LxFqr1ZzFLVWHLva2s22HUew VtvAvT0rkhsvtr0H2IiyWDzOT2li3MI/O/pPH8NYHbeV7EmTUSfuzS5yuL322Ml/c1Oyudgrg oSYYsnwhPWZbKcUtRdFAWwBtSx1iXszOmNTDCuxF6JeBc1PciEwgJFeEmG72EiIrLy2P7Z1Oi GpcP2jcCAcZc3Ijya2dS+Z6J6ky2G5hggikU8/3Y0+cnU2aHdsEZ83EVi2JKCTHCMMk6KIXWa wL2x6o6SvYS9ZCabY4NjVkiiRKzl+cxo50sEteboljF8jEmrN1NW+fiqiaEMNwludBYYBcU0A y2/Q4hrACso40hBBYVr0rXmG2CvHcMg5aDc2TQJKsGhYNs83OFMrHow7D4JkMWRcf2epyOr4H UvxCbvwvllZMA1q3U1SsKbrTj5e4K2tBEH9Ck3h63x4XpyFo8yjRxNjlNE9lfCSuo/R24wIQ+ aZDyfFoUuEvxPiMwhH/RzwQmzfTVJ+3YGIF9C7pmzuJuC35tRyWIVHtDOHO0PdxWii3EpAqwX qZNDauy6z1a7spomQQIPgGlKdV8R0q8ieOo58pEWXVS/UxIckCcGcqKJ3RFrRo5TXUZS03gmd BgfVmpzC/NG2vpJwUoFqnMz7WCLx2m3vEDUCcgOh2Owxxtutt4jCFr4zFxi45yD+PbTCz8uBi +S/+yVEEFxLfxEcWkEvnQ2ImsOWTrgEKU63ZGurNqfYZKfxNkiG4ltjU7Py+HMnE9hsbupU6C wg0E2cGpV2ZD5OPfytP0CIKGIXqCmJWF9vxfk5LOhb6o57+tt8rZOj/2tqDSdKENLb8yqwFvt cWXEi5ftC7xLRrSynsL2Ad4ZHvsak5YwZRcnqcfcLEAG4vu3QMbnBm9p0VzLDMGkj4uZ3XDK+ fKGs3Jqgi1hT0PO6FZjFqRdqZnE= Received-SPF: pass client-ip=212.227.17.22; envelope-from=deller@gmx.de; helo=mout.gmx.net X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Akihiko Odaki Linux 6.4.7 does nothing when a value smaller than the initial brk is specified. Fixes: 86f04735ac ("linux-user: Fix brk() to release pages") Signed-off-by: Akihiko Odaki Reviewed-by: Helge Deller Signed-off-by: Helge Deller --- linux-user/syscall.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) -- 2.41.0 diff --git a/linux-user/syscall.c b/linux-user/syscall.c index ac429a185a..ebdc8c144c 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -820,14 +820,14 @@ abi_long do_brk(abi_ulong brk_val) /* brk pointers are always untagged */ - /* return old brk value if brk_val unchanged or zero */ - if (!brk_val || brk_val == target_brk) { + /* return old brk value if brk_val unchanged */ + if (brk_val == target_brk) { return target_brk; } /* do not allow to shrink below initial brk value */ if (brk_val < initial_target_brk) { - brk_val = initial_target_brk; + return target_brk; } new_brk = TARGET_PAGE_ALIGN(brk_val); From patchwork Tue Aug 1 23:27:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Helge Deller X-Patchwork-Id: 13337361 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4B74BEB64DD for ; Tue, 1 Aug 2023 23:30:21 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qQymU-0001K7-6r; Tue, 01 Aug 2023 19:27:58 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qQymS-0001JE-Ga for qemu-devel@nongnu.org; Tue, 01 Aug 2023 19:27:56 -0400 Received: from mout.gmx.net ([212.227.17.22]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qQymO-00077x-Td for qemu-devel@nongnu.org; Tue, 01 Aug 2023 19:27:56 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1690932468; x=1691537268; i=deller@gmx.de; bh=u2YNxFmEdrBbo7ggn7oE7m69UMkwd/D2Njq/BE8FFh8=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=oLcqeXmuYGx8dosoZg5i1bXs9+BEOsGCCu/O36t+7LMdP22kivIiisA4q/IqldddCHyxXHO mRkB755dowGBdaBmZJaFOhA1g0kq5jBowZmvlXDg2gFptKBx3VF4bd06cB0lCFXJ63qh58jPw KzNIr3XnhfVhAgw+oQV1qfCeJ+hGPbAnO4CcRR1yozqZ7YYU2OiXv748dwEmHKFpcDlX7kROu 1AfYNu1PUnN9NEWk3ApF9U/XKAV/99xoMOhiwvpGFlbhVK+1XOublM4ROQBhjWxZTfvuUftAD i7LAKN1krOoC7ULhspNs7bF7fGzYWQabqVrpIZF3OQZczAPur2Cg== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from p100.fritz.box ([94.134.150.247]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MbivG-1pr6GQ0F9a-00dJPI; Wed, 02 Aug 2023 01:27:48 +0200 From: Helge Deller To: qemu-devel@nongnu.org Cc: Richard Henderson , Laurent Vivier , Paolo Bonzini , Joel Stanley , Akihiko Odaki , Helge Deller Subject: [PATCH v6 5/8] linux-user: Do not align brk with host page size Date: Wed, 2 Aug 2023 01:27:42 +0200 Message-ID: <20230801232745.4125-6-deller@gmx.de> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230801232745.4125-1-deller@gmx.de> References: <20230801232745.4125-1-deller@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:5C5yuTtJdLQAlkoS3iX42TV9t3riQMIWnqLao4B1qfUoMBgVihv Ab/hMXS62poXaX4L/Feq0zGkdNzrN02R/rgmQ//VPT+7JLLxHXr6YbVRypRUvbV8eqBfFGO YBtUiTXTV1Grz31LZG9cyhH3Dj1uaspMb8ETiWGfbLqLFCNIf/O5EeJNtvtSUEDZ5/QSm2G yKfbHtvaAvEukhfEsW0Gw== UI-OutboundReport: notjunk:1;M01:P0:GOqbO38/KBg=;XBZnqVcj47JsbdPMzBzkDzKWvok azzWz3loQLJGYREeQhegl8oSxkdwGyd0oeSCDmOHzmrFMtpDbGQOH/Slh3rp2LIlcTUX0iIYk EiNDutECDxq46ErZIQDGG/gziWh0JeqneR20n8fjEwMaqYC89CqzaBYQY4zER5wHEMlzcaGhA groOVjhkEVi1CrlVuZtxf5ar/u8Tr6OedVRgd6Hl5TqdBbo0xqHWzdfPV+Ivc7ib9Z+Ug93Oe jYUla2qDUKr1YiVuqGMCfrsKySbnvS89naY/SpC+76dT8fsbedHAG4K7i01xWFiyyCtkdesBu xcOaHFDlopODQWIPhukViHLgoOAFkERDJggWYowH4wkyMyRCAckf7ZVRTRA+Sh9wSdfLrRD9Q i/paP4pBcWGYavTm1DPZuUtsw6c8N5iPFF8OeJJAvvvKg6cea1wqvf5kgEThVHtFg/V9QwXJ2 NjrqZKY9+tYn7SuIuhnSyO9q1lGCZQXcBN17ea4bPPbvTfogQqygvSfhffwD28tdnrx/QDonf gKQQteNpLeWRQ+rGDoRu6J1i/UFwjSD+9OuMpOYLMW2KTaT7/hnx2h8f6q3AFV54u5Pi/92JQ JJsaCt0oB0h9YRT5COdCafs57PX3pVImJ+Eks3A32bK/3bwhPkINNt3u8u3uRPsJZ+PUDtABu nT24GSIz/1wcjIj8YT/h1pob20DHXxfDAlfZvJ/WrCafBRDQOeMWk4jSMvMS97puuaQyUQVCi 4f6Wj+LhdHqPHli6KCG7zUrZeOwRVkHIldCYT3SFtkmMwRQXBUHyFSYAEMix5D/im+oIAjCBX cxOv9FXMd7GuZPuAyaifOKY9mqS4o9zWnpBBBk3zKpD4eqqkwsKlYCYmZ0vmXokGvMJAFK52W B/OoD9isYiYKSE08AHOdoNVetC9qLoXsEdsEEVNQNIAkvhpEVQmvaTyg9LgRehk9sm5dpdeA0 YRCR/qmTqElPBdv8NNpbb6xHDh4= Received-SPF: pass client-ip=212.227.17.22; envelope-from=deller@gmx.de; helo=mout.gmx.net X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Akihiko Odaki do_brk() minimizes calls into target_mmap() by aligning the address with host page size, which is potentially larger than the target page size. However, the current implementation of this optimization has two bugs: - The start of brk is rounded up with the host page size while brk advertises an address aligned with the target page size as the beginning of brk. This makes the beginning of brk unmapped. - Content clearing after mapping is flawed. The size to clear is specified as HOST_PAGE_ALIGN(brk_page) - brk_page, but brk_page is aligned with the host page size so it is always zero. This optimization actually has no practical benefit. It makes difference when brk() is called multiple times with values in a range of the host page size. However, sophisticated memory allocators try to avoid to make such frequent brk() calls. For example, glibc 2.37 calls brk() to shrink the heap only when there is a room more than 128 KiB. It is rare to have a page size larger than 128 KiB if it happens. Let's remove the optimization to fix the bugs and make the code simpler. Fixes: 86f04735ac ("linux-user: Fix brk() to release pages") Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1616 Signed-off-by: Akihiko Odaki Reviewed-by: Helge Deller Signed-off-by: Helge Deller --- linux-user/elfload.c | 4 ++-- linux-user/syscall.c | 54 ++++++++++---------------------------------- 2 files changed, 14 insertions(+), 44 deletions(-) -- 2.41.0 diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 861ec07abc..2aee2298ec 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -3678,8 +3678,8 @@ int load_elf_binary(struct linux_binprm *bprm, struct image_info *info) * to mmap pages in this space. */ if (info->reserve_brk) { - abi_ulong start_brk = HOST_PAGE_ALIGN(info->brk); - abi_ulong end_brk = HOST_PAGE_ALIGN(info->brk + info->reserve_brk); + abi_ulong start_brk = TARGET_PAGE_ALIGN(info->brk); + abi_ulong end_brk = TARGET_PAGE_ALIGN(info->brk + info->reserve_brk); target_munmap(start_brk, end_brk - start_brk); } diff --git a/linux-user/syscall.c b/linux-user/syscall.c index ebdc8c144c..475260b7ce 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -802,81 +802,51 @@ static inline int host_to_target_sock_type(int host_type) } static abi_ulong target_brk, initial_target_brk; -static abi_ulong brk_page; void target_set_brk(abi_ulong new_brk) { target_brk = TARGET_PAGE_ALIGN(new_brk); initial_target_brk = target_brk; - brk_page = HOST_PAGE_ALIGN(target_brk); } /* do_brk() must return target values and target errnos. */ 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; + abi_ulong new_brk; + abi_ulong old_brk; /* brk pointers are always untagged */ - /* return old brk value if brk_val unchanged */ - if (brk_val == target_brk) { - return target_brk; - } - /* do not allow to shrink below initial brk value */ if (brk_val < initial_target_brk) { return target_brk; } new_brk = TARGET_PAGE_ALIGN(brk_val); - new_host_brk_page = HOST_PAGE_ALIGN(brk_val); + old_brk = TARGET_PAGE_ALIGN(target_brk); - /* brk_val and old target_brk might be on the same page */ - if (new_brk == TARGET_PAGE_ALIGN(target_brk)) { - /* empty remaining bytes in (possibly larger) host page */ - memset(g2h_untagged(new_brk), 0, new_host_brk_page - new_brk); + /* new and old target_brk might be on the same page */ + if (new_brk == old_brk) { target_brk = brk_val; return target_brk; } /* Release heap if necesary */ - if (new_brk < target_brk) { - /* empty remaining bytes in (possibly larger) host page */ - memset(g2h_untagged(new_brk), 0, new_host_brk_page - new_brk); - - /* 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; + if (new_brk < old_brk) { + target_munmap(new_brk, old_brk - new_brk); target_brk = brk_val; return target_brk; } - if (new_host_brk_page > brk_page) { - new_alloc_size = new_host_brk_page - brk_page; - mapped_addr = target_mmap(brk_page, new_alloc_size, - PROT_READ | PROT_WRITE, - MAP_FIXED_NOREPLACE | MAP_ANON | MAP_PRIVATE, - 0, 0); - } else { - new_alloc_size = 0; - mapped_addr = brk_page; - } - - if (mapped_addr == brk_page) { - /* Heap contents are initialized to zero, as for anonymous - * mapped pages. Technically the new pages are already - * initialized to zero since they *are* anonymous mapped - * pages, however we have to take care with the contents that - * come from the remaining part of the previous page: it may - * contains garbage data due to a previous heap usage (grown - * then shrunken). */ - memset(g2h_untagged(brk_page), 0, HOST_PAGE_ALIGN(brk_page) - brk_page); + mapped_addr = target_mmap(old_brk, new_brk - old_brk, + PROT_READ | PROT_WRITE, + MAP_FIXED_NOREPLACE | MAP_ANON | MAP_PRIVATE, + 0, 0); + if (mapped_addr == old_brk) { target_brk = brk_val; - brk_page = new_host_brk_page; return target_brk; } From patchwork Tue Aug 1 23:27:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Helge Deller X-Patchwork-Id: 13337357 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 79773EB64DD for ; Tue, 1 Aug 2023 23:28:48 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qQymT-0001Jn-Ni; Tue, 01 Aug 2023 19:27:57 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qQymS-0001J9-Du for qemu-devel@nongnu.org; Tue, 01 Aug 2023 19:27:56 -0400 Received: from mout.gmx.net ([212.227.17.20]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qQymP-00078A-Gj for qemu-devel@nongnu.org; Tue, 01 Aug 2023 19:27:56 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1690932468; x=1691537268; i=deller@gmx.de; bh=zaJutCebOOz0E3/UV6NEgL2gk9VANF+T02Ta9HvtqlI=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=pTrWyjIsd4aS+DPs7MbWYgAXPnfGwrAqwUvZu1sfsLPHvuF5UZJhVRrJhyrxTIwC/qF3ztc +d4wBurnZML3W6L2+O7imFoXnVbPFLno1ePd+rDGrMqDVpqpKgLIXUsQ7TXocUbMcI6dSKpQN ufXlviImeT9I2ZeixhbymZmI7UjIz+Xd9N3lFdhfGHGjC6AlSXo9xe9BDZU7P8+VdL0YOffAj wDtYo0UNsaBI14XrFYGBPn88srs4U4rJqnprblyuJsOqisB9zATtoWH0C5fAcp+p8kMezXc7O GmB3eAI22ouaeVY0vD9VD3bYeBXtjaTXenbJgVGqgjHKWm1dwO+Q== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from p100.fritz.box ([94.134.150.247]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MjS9C-1pyq1W1GMU-00kyMy; Wed, 02 Aug 2023 01:27:48 +0200 From: Helge Deller To: qemu-devel@nongnu.org Cc: Richard Henderson , Laurent Vivier , Paolo Bonzini , Joel Stanley , Akihiko Odaki , Helge Deller Subject: [PATCH v6 6/8] linux-user: Show heap address in /proc/pid/maps Date: Wed, 2 Aug 2023 01:27:43 +0200 Message-ID: <20230801232745.4125-7-deller@gmx.de> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230801232745.4125-1-deller@gmx.de> References: <20230801232745.4125-1-deller@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:FcSKaqRG+UQoM/e8bVKZMhJhM92ldW50ooy66A+1sZs6BFfQhI8 0Del+mw0qLTPjRlkm7EGfHAWPFZsNJE/HHbSC/J7uHwLyEUU8yHZU4IsJsDWknzCsn/mZya GVzdII3YfUqdlzKTJDq5F0qXr6jFGkb0WjeRnfokcJtc72g2DeivRqTRSMuB8APbpnBCmov emFK5YAUPRqYT99+Xb3Gg== UI-OutboundReport: notjunk:1;M01:P0:3abnKUOp52I=;c5LZGAegFhlRiUEfFCKHzxn8agj TvSfQNLp7lZ3ZDD7mTdAGFHpd3KTHmaMGmNBy3JTnwEhfKcsIRdHT/nuCwyyRcs6GHwCyv9Mg ii1fU/wTtwMQmrbmn4abO8jj7qLhhyUYSnO7EGy9H0fq6a4YCofBwOZ7uwiw4zhYYwx5csJXG YWz7nLKYDoIj0cwYgyj6mq8MIIeltNIHU8MWR5JtqwYcERwTNOi9zsQkjFbilWdWRZueaIz4P bV4tCbmb1/uK7XMLmp8wKzs9Bl9OzhFYAggHaLFmUJDJ3jvuyD//DhKVyGYx17p55vE63+Pp3 u8k437NG9WQ8EYz4TAU4ql6DG9q0RYZbQaYeIrnwK48/W5B8i4Y+WodzQmpw/6s49FMgupGr2 FPc5IwKIr3pYuMBtM1DgVJ1KbPBghCxtLBLi2yJjl+s8TsrZwiwIjS53kfjgT8b7esbzvtZAi z7+EWaPE2miKZav7ftMAmf6gSMfBlsCh2en2wHBW/vDgKefUVa+kJMamdXibf638ThYt/i7eX n1gZvjgY0FRY8CQGbw/A6vHmex0jTX17suDT+uGue/BwxjTlYD7eAog+VFgxW7NIByi3ohXUX bVlMdOszqk7dxZWRnm7rX+rAAhb7yEQxN03cIACCPzpNxQGZ7dTKzXGLxafd6kuiij8bvft6c YMy9zQlpZRWEeqflaGL1TEpRAQbG4pZMxZRNVMNofnpxp/o+82u7AxVTya34hEdXk77hLl1YC ut3HWdOAR17QdgFf4O7TkIL0B6r1S7kUVZQnFUNVncv1Epfo8V9ZoBVztB4F+qOG3vJNe8XuD 6StwmnDWBYzTdkCX+BpygaVBul8abKGQP14GYBIzytklT6cKTWxAHOOY0A3RLACxrfBOh8za6 xHJIxgYT8a+0CoHujet3Ng5PGnygv0mimStRg1blEnl+P1po8fBZPvzzxa0MLhVoK+5dea3Pd lkWKv7ZkxG058svtFkTc0y/FsMs= Received-SPF: pass client-ip=212.227.17.20; envelope-from=deller@gmx.de; helo=mout.gmx.net X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Show the memory location of the heap in the /proc/pid/maps file inside the guest. Store the heap address in ts->heap_base, which requires to make that variable accessible for all guest architectures, not just architectures for semihosted binaries (arm, m68k, riscv). Note that /proc/pid/maps in the guest needs to show target-aligned addresses. This is fixed in this patch, so now the heap and stack address for architectures like sparc64 and alpha now show up in that output as well. Show 32- and 64-bit pointers with 8 digits and leading zeros (%08x/%08lx). For 64-bit we could use %16lx, but we mimic the Linux kernel, which shows even 64-bit addresses with %08lx. Example: user@machine:/# uname -a Linux paq 5.15.88+ #47 SMP Sun Jan 15 12:53:11 CET 2023 aarch64 GNU/Linux user@machine:/# cat /proc/self/maps Linux p100 6.4.4-200.fc38.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Jul 19 16:32:49 UTC 2023 aarch64 GNU/Linux 5500000000-5500009000 r-xp 00000000 fd:00 570430 /usr/bin/cat 5500009000-550001f000 ---p 00000000 00:00 0 550001f000-5500020000 r--p 0000f000 fd:00 570430 /usr/bin/cat 5500020000-5500021000 rw-p 00010000 fd:00 570430 /usr/bin/cat 5500021000-5500042000 rw-p 00000000 00:00 0 [heap] 7000000000-7000001000 ---p 00000000 00:00 0 7000001000-7000801000 rw-p 00000000 00:00 0 [stack] 7000801000-7000827000 r-xp 00000000 fd:00 571555 /usr/lib/aarch64-linux-gnu/ld-linux-aarch64.so.1 7000827000-700083f000 ---p 00000000 00:00 0 700083f000-7000841000 r--p 0002e000 fd:00 571555 /usr/lib/aarch64-linux-gnu/ld-linux-aarch64.so.1 7000841000-7000843000 rw-p 00030000 fd:00 571555 /usr/lib/aarch64-linux-gnu/ld-linux-aarch64.so.1 7000843000-7000844000 r-xp 00000000 00:00 0 7000844000-7000846000 rw-p 00000000 00:00 0 7000850000-70009d7000 r-xp 00000000 fd:00 571558 /usr/lib/aarch64-linux-gnu/libc.so.6 70009d7000-70009ed000 ---p 00187000 fd:00 571558 /usr/lib/aarch64-linux-gnu/libc.so.6 70009ed000-70009f0000 r--p 0018d000 fd:00 571558 /usr/lib/aarch64-linux-gnu/libc.so.6 70009f0000-70009f2000 rw-p 00190000 fd:00 571558 /usr/lib/aarch64-linux-gnu/libc.so.6 Signed-off-by: Helge Deller --- include/exec/cpu_ldst.h | 4 ++-- linux-user/main.c | 2 ++ linux-user/qemu.h | 4 ++-- linux-user/syscall.c | 13 +++++++++---- 4 files changed, 15 insertions(+), 8 deletions(-) -- 2.41.0 diff --git a/include/exec/cpu_ldst.h b/include/exec/cpu_ldst.h index 645476f0e5..f1e6f31e88 100644 --- a/include/exec/cpu_ldst.h +++ b/include/exec/cpu_ldst.h @@ -72,10 +72,10 @@ */ #if TARGET_VIRT_ADDR_SPACE_BITS <= 32 typedef uint32_t abi_ptr; -#define TARGET_ABI_FMT_ptr "%x" +#define TARGET_ABI_FMT_ptr "%08x" #else typedef uint64_t abi_ptr; -#define TARGET_ABI_FMT_ptr "%"PRIx64 +#define TARGET_ABI_FMT_ptr "%08"PRIx64 #endif #ifndef TARGET_TAGGED_ADDRESSES diff --git a/linux-user/main.c b/linux-user/main.c index dba67ffa36..fa6e47510f 100644 --- a/linux-user/main.c +++ b/linux-user/main.c @@ -946,6 +946,7 @@ int main(int argc, char **argv, char **envp) } } + info->brk = TARGET_PAGE_ALIGN(info->brk); target_set_brk(info->brk); syscall_init(); signal_init(); @@ -955,6 +956,7 @@ int main(int argc, char **argv, char **envp) the real value of GUEST_BASE into account. */ tcg_prologue_init(tcg_ctx); + ts->heap_base = info->brk; target_cpu_copy_regs(env, regs); if (gdbstub) { diff --git a/linux-user/qemu.h b/linux-user/qemu.h index 802794db63..7a6adac637 100644 --- a/linux-user/qemu.h +++ b/linux-user/qemu.h @@ -121,11 +121,11 @@ typedef struct TaskState { #ifdef TARGET_M68K abi_ulong tp_value; #endif -#if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_RISCV) + /* Extra fields for semihosted binaries. */ abi_ulong heap_base; abi_ulong heap_limit; -#endif + abi_ulong stack_base; int used; /* non zero if used */ struct image_info *info; diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 475260b7ce..dc8266c073 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -8078,8 +8078,9 @@ static int open_self_maps_1(CPUArchState *cpu_env, int fd, bool smaps) MapInfo *e = (MapInfo *) s->data; if (h2g_valid(e->start)) { - unsigned long min = e->start; - unsigned long max = e->end; + /* show page granularity of guest in /proc/pid/maps */ + unsigned long min = TARGET_PAGE_ALIGN(e->start); + unsigned long max = TARGET_PAGE_ALIGN(e->end); int flags = page_get_flags(h2g(min)); const char *path; @@ -8090,14 +8091,18 @@ static int open_self_maps_1(CPUArchState *cpu_env, int fd, bool smaps) continue; } + path = e->path; + + if (ts->heap_base && h2g(min) == ts->heap_base) { + path = "[heap]"; + } + #ifdef TARGET_HPPA if (h2g(max) == ts->info->stack_limit) { #else if (h2g(min) == ts->info->stack_limit) { #endif path = "[stack]"; - } else { - path = e->path; } count = dprintf(fd, TARGET_ABI_FMT_ptr "-" TARGET_ABI_FMT_ptr From patchwork Tue Aug 1 23:27:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Helge Deller X-Patchwork-Id: 13337355 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id BFAACEB64DD for ; Tue, 1 Aug 2023 23:28:26 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qQymU-0001MM-Sv; Tue, 01 Aug 2023 19:27:58 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qQymS-0001JO-KZ for qemu-devel@nongnu.org; Tue, 01 Aug 2023 19:27:56 -0400 Received: from mout.gmx.net ([212.227.17.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qQymP-000786-7M for qemu-devel@nongnu.org; Tue, 01 Aug 2023 19:27:56 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1690932468; x=1691537268; i=deller@gmx.de; bh=lAN249IyTXdxU+qyf5qo5ihznd6eja0cEzaQkKzVRDg=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=N7CJn+/jqJi/7qHQRoViMk25d2ZE8WR/PNI3x2dR79Fwq/G1/baWMhZSYvbuceKbuXIWFKu 5qY+DgGBjN3mxRMYks1azhn99zhtMXUq4LBrSvmvNmbCWygAp8uOWUgi16G+xtfyGq8yiaSXD bAkffJ8wk/TYKQ1nBIs5EbdsDqwmgLe8GseAFzJkhjOBa3nW6AlKujUXeWh6W2HzruGqTpQev 0OaFvAgK+R1AwirksN5mRhbfmqswDEYE2W+FMjLU6CHMbsSo11HngVY+7FGj4eqGECFNxmIiX 5M/P5kNJBEIVcNDSKnYa+4q6QLAtQwp+1CF/x7qz/UzzRUlwTrEA== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from p100.fritz.box ([94.134.150.247]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MCKFu-1qaMhK2Gf4-009RlI; Wed, 02 Aug 2023 01:27:48 +0200 From: Helge Deller To: qemu-devel@nongnu.org Cc: Richard Henderson , Laurent Vivier , Paolo Bonzini , Joel Stanley , Akihiko Odaki , Helge Deller Subject: [PATCH v6 7/8] linux-user: Optimize memory layout for static and dynamic executables Date: Wed, 2 Aug 2023 01:27:44 +0200 Message-ID: <20230801232745.4125-8-deller@gmx.de> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230801232745.4125-1-deller@gmx.de> References: <20230801232745.4125-1-deller@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:wHB8943utChrnrZuINliwuivkUPx+xinjcGdb6oRRoDh4B10PkV H3nAVCjTXtm5nJ1MV0MNOr/GnP1euQFfxvw+bNEF0HzSuRQnJ6obLkC8YBOcojuOYUQinQD +kzpcyR46wvHi+p3FQC6PU6PPdFhK8UFLELWC9Br+BB0M6zyD4eisJSdNV3EShwRuZR0UEJ qDI/bro7//3WyKa/Pskdw== UI-OutboundReport: notjunk:1;M01:P0:kK2wHqQeajU=;x9uVhfPvK5WHWhvDx24BVvQgc2M DxSQlbp46+Bis6N4IfRm+D0EZaDUMMkP6XxPhHJs6ZeS8zqIOhZxHcRzMIGOQIWFp47h5Q6XO hmPmbqHgPLr+DIXXdB56anNptFZ/mqoMsdrMHHvj4sIrtf325vLdWz5G6whoZRcPGvJ3A7429 2FYm52CY1+M+GRlTY44Gtkl2tQr9DbgFnL5v6TyJ0y4I4swh5SjmHpDyiOaTmB5UbxNv0pNVm yrMW+szHBeeirXnhYCscD+bXSY/Orl7PY6SCpGOCrJmYlaIzJIxMADbRctxjbMv3obbmujkWn wWs6LjpgkE/DHQ0KzDSd6AysAkkgsJ3snAVwucoQpiOUgK962rTid57R3lFVTaW3cHFcebeS1 7oqgQpbAgJZ78gPwqi5141cXQssxSpYmn4ML/3ymPZ/JFs7l7rLaCFSgu3/IuNZLZal3gKzfq iKGXmEUZAOmgKWXH1BvOqb3uPEkSyBMSYMrciRsjCXV/ZITiQk8iDkZc1XQTBxGuiuoWEXdQu Xb0u5Asza/PdcwOuXUjMyTqCP4Uja06p6fLyuKolYtmssmoFt7k3rwuse3KnmbwLOmM11D5SU yimOxFTIJyuOKxHx05EeJnJdhZVPh3fdg+KQ/bSe/J/F9MDgD1ptjEtfSOUnswQgqNNYFUQ4+ CTziegnA+qdcrKYsUYpn4Z5QRgJBM7+huf4avv0vvcTJ6ZiZ7DjXmpxrxmN3P2K0yCPaYRjNG /YCZ4m+ohCDHxjywihpxJxQl3P9ckpwnnwwRDENZOKCX6DdxwrgmTirIiP5h5ZW6f21Gj004W 2r3Lw0EVbrCqR065prPVd/dueMHywk3yIysY+Co6MUw+nogZFDerHwYZerCxbuUYru2D1mytM m/7QNy5o1S+YNlbR9tqKgIS//5J1m+rWlLfDNaqoSnTuL3sOXh2EOBe+9cIXK/M1vNMYw2XQg 3Ce6FOcZZWL2aeR6d7tRosvxbNI= Received-SPF: pass client-ip=212.227.17.21; envelope-from=deller@gmx.de; helo=mout.gmx.net X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Reorganize the guest memory layout to get as much memory as possible for heap for the guest application. This patch optimizes the memory layout by loading pie executables into lower memory and shared libs into higher memory (at TASK_UNMAPPED_BASE). This leaves a bigger memory area usable for heap space which will be located directly after the executable. Up to now, pie executable and shared libs were loaded directly behind each other in the area at TASK_UNMAPPED_BASE, which leaves very little space for heap. I tested this patchset with chroots of alpha, arm, armel, arm64, hppa, m68k, mips64el, mipsel, powerpc, ppc64, ppc64el, s390x, sh4 and sparc64 on a x86-64 host, and with a static armhf binary (which fails to run without this patch). This patch temporarily breaks the Thread Sanitizer (TSan) application which expects specific boundary definitions for memory mappings on different platforms [1], see commit aab613fb9597 ("linux-user: Update TASK_UNMAPPED_BASE for aarch64") for aarch64. The follow-up patch fixes it again. [1] https://github.com/llvm/llvm-project/blob/master/compiler-rt/lib/tsan/rtl/tsan_platform.h Signed-off-by: Helge Deller --- linux-user/elfload.c | 55 +++++++++++++------------------------------- linux-user/mmap.c | 8 ++++--- 2 files changed, 21 insertions(+), 42 deletions(-) -- 2.41.0 diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 2aee2298ec..47a118e430 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -3023,6 +3023,7 @@ static void load_elf_image(const char *image_name, int image_fd, abi_ulong load_addr, load_bias, loaddr, hiaddr, error; int i, retval, prot_exec; Error *err = NULL; + bool is_main_executable; /* First of all, some simple consistency checks */ if (!elf_check_ident(ehdr)) { @@ -3106,28 +3107,8 @@ static void load_elf_image(const char *image_name, int image_fd, } } - if (pinterp_name != NULL) { - /* - * This is the main executable. - * - * Reserve extra space for brk. - * We hold on to this space while placing the interpreter - * and the stack, lest they be placed immediately after - * the data segment and block allocation from the brk. - * - * 16MB is chosen as "large enough" without being so large as - * to allow the result to not fit with a 32-bit guest on a - * 32-bit host. However some 64 bit guests (e.g. s390x) - * attempt to place their heap further ahead and currently - * nothing stops them smashing into QEMUs address space. - */ -#if TARGET_LONG_BITS == 64 - info->reserve_brk = 32 * MiB; -#else - info->reserve_brk = 16 * MiB; -#endif - hiaddr += info->reserve_brk; - + is_main_executable = (pinterp_name != NULL); + if (is_main_executable) { if (ehdr->e_type == ET_EXEC) { /* * Make sure that the low address does not conflict with @@ -3136,7 +3117,7 @@ static void load_elf_image(const char *image_name, int image_fd, probe_guest_base(image_name, loaddr, hiaddr); } else { /* - * The binary is dynamic, but we still need to + * The binary is dynamic (pie-executabe), but we still need to * select guest_base. In this case we pass a size. */ probe_guest_base(image_name, 0, hiaddr - loaddr); @@ -3159,7 +3140,7 @@ static void load_elf_image(const char *image_name, int image_fd, */ load_addr = target_mmap(loaddr, (size_t)hiaddr - loaddr + 1, PROT_NONE, MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | - (ehdr->e_type == ET_EXEC ? MAP_FIXED : 0), + (is_main_executable ? MAP_FIXED : 0), -1, 0); if (load_addr == -1) { goto exit_mmap; @@ -3194,7 +3175,8 @@ static void load_elf_image(const char *image_name, int image_fd, info->end_code = 0; info->start_data = -1; info->end_data = 0; - info->brk = 0; + /* possible start for brk is behind all sections of this ELF file. */ + info->brk = TARGET_PAGE_ALIGN(hiaddr); info->elf_flags = ehdr->e_flags; prot_exec = PROT_EXEC; @@ -3288,9 +3270,6 @@ static void load_elf_image(const char *image_name, int image_fd, info->end_data = vaddr_ef; } } - if (vaddr_em > info->brk) { - info->brk = vaddr_em; - } #ifdef TARGET_MIPS } else if (eppnt->p_type == PT_MIPS_ABIFLAGS) { Mips_elf_abiflags_v0 abiflags; @@ -3618,6 +3597,15 @@ int load_elf_binary(struct linux_binprm *bprm, struct image_info *info) if (elf_interpreter) { load_elf_interp(elf_interpreter, &interp_info, bprm->buf); + /* + * Use brk address of interpreter if it was loaded above the + * executable and leaves less than 16 MB for heap. + * This happens e.g. with static binaries on armhf. + */ + if (interp_info.brk > info->brk && + interp_info.load_bias - info->brk < 16 * MiB) { + info->brk = interp_info.brk; + } /* If the program interpreter is one of these two, then assume an iBCS2 image. Otherwise assume a native linux image. */ @@ -3672,17 +3660,6 @@ int load_elf_binary(struct linux_binprm *bprm, struct image_info *info) bprm->core_dump = &elf_core_dump; #endif - /* - * If we reserved extra space for brk, release it now. - * The implementation of do_brk in syscalls.c expects to be able - * to mmap pages in this space. - */ - if (info->reserve_brk) { - abi_ulong start_brk = TARGET_PAGE_ALIGN(info->brk); - abi_ulong end_brk = TARGET_PAGE_ALIGN(info->brk + info->reserve_brk); - target_munmap(start_brk, end_brk - start_brk); - } - return 0; } diff --git a/linux-user/mmap.c b/linux-user/mmap.c index 2f26cbaf5d..c624feead0 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -299,14 +299,16 @@ static bool mmap_frag(abi_ulong real_start, abi_ulong start, abi_ulong last, #ifdef TARGET_AARCH64 # define TASK_UNMAPPED_BASE 0x5500000000 #else -# define TASK_UNMAPPED_BASE (1ul << 38) +# define TASK_UNMAPPED_BASE 0x4000000000 #endif -#else +#elif HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 32 #ifdef TARGET_HPPA # define TASK_UNMAPPED_BASE 0xfa000000 #else -# define TASK_UNMAPPED_BASE 0x40000000 +# define TASK_UNMAPPED_BASE 0xe0000000 #endif +#else /* HOST_LONG_BITS == 32 && TARGET_ABI_BITS == 32 */ +# define TASK_UNMAPPED_BASE 0x40000000 #endif abi_ulong mmap_next_start = TASK_UNMAPPED_BASE; From patchwork Tue Aug 1 23:27:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Helge Deller X-Patchwork-Id: 13337362 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id BCEDFC41513 for ; Tue, 1 Aug 2023 23:30:21 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qQymU-0001KC-F6; Tue, 01 Aug 2023 19:27:58 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qQymT-0001Jl-Jx for qemu-devel@nongnu.org; Tue, 01 Aug 2023 19:27:57 -0400 Received: from mout.gmx.net ([212.227.17.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qQymP-000789-Hk for qemu-devel@nongnu.org; Tue, 01 Aug 2023 19:27:57 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1690932468; x=1691537268; i=deller@gmx.de; bh=1NFU1YviBFmyuIZFHJbwVvbCDXdoRmikoWBij4USGWU=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=j5UauReCePrF8CNZOgGaTc20I5qvp0stItKyuu5q3fzNBRzplgXrBHvq3YY3J8Ex/07kAkB G2GT9ifd/V0ZZcqLXrH7MtAr1yblEFzipB/Mjapr0qslauKo0rkATqcjtuTQNnzo2EGBssOvu 41vqzGki/eYOS3s+X2kz8YBwfy3QfY7N7KcCIwZ/oQbA+KyB4sJjqNS8e0yBggL5E+19xPF+L +46ETnoOx5m3ZP8PIaLV2Wh5IMqqB3rg7Y6t0fEGBV8IiVzIhLhYqx2sJLMdCKPYM4BR66b59 oYyRJDGcnI0fifcJocvUv2GvkhZagH582Oi2UNY2O54C1HT39GYw== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from p100.fritz.box ([94.134.150.247]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MlNp7-1pz6jp3IIX-00lnaJ; Wed, 02 Aug 2023 01:27:48 +0200 From: Helge Deller To: qemu-devel@nongnu.org Cc: Richard Henderson , Laurent Vivier , Paolo Bonzini , Joel Stanley , Akihiko Odaki , Helge Deller Subject: [PATCH v6 8/8] linux-user: Load pie executables at upper memory Date: Wed, 2 Aug 2023 01:27:45 +0200 Message-ID: <20230801232745.4125-9-deller@gmx.de> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230801232745.4125-1-deller@gmx.de> References: <20230801232745.4125-1-deller@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:oEQ5BeGs/zGeiLDZaQX8yqdaTWWxFnfr0OOnvq7UesuMOR4zFRG z7oIHmEImpS4XbJsknsP6i7If35YaqG/wOcUFfd6hmFNp6JSx1pIsPuXtMgMJfrvB5ClByi QgPgsAnJwpro6tVQlA9vw2u3N+dX3oq7RuX0dbvd6VcCatXGRlkCljPGTRV6fyLPf1QlDz5 nF/zI0mHVyC81e0qNDuVQ== UI-OutboundReport: notjunk:1;M01:P0:7lqx3j3ha7Y=;ckoRRyF7pI3lCAqntnczbHSBAFf kuOlInHr8aHWWo2Qu6XCJkgDTGJBUXNbm4jt8omLyY6vTJKZOEF8BCyJVRYp7/KFGclX+tvnU rYqvSKYbJBYwSP6G7gJ0K5ToYBIT2qg7MUltRvQGJFWSC+bhXdN98YsdfDhU+nhodgXvPXcC7 uwDywuKMr4svMO/TTmaUwZp6uB0/aC5wmk8PExKb1GiHTk1I2U/zkK0qsmOd9gQhOe5yM7xZT dS1F0SX/lxq3dimeFJ9COw0aJNMty+hIuOdFw5jpKmjkuvcQdW6QUgrLtqpyd8K5k9bryxiqV 0wNStTZPFFY8AUUbc8rGmvdTqiKe82eM3ckcyxHP4cNfXxJQklhZ9FosobtfOloGQXgaUys/K p3ce4wQHv34qUg78eTfcZ5/Knnz8zCoOGsYeRlygclU0Rs8gm1IBPf/x8f8FB3ryNbPgGzct9 d3GJVqldyLfD4lZPKa2yX+iUCItLHoXkgOeDH2i4NStn2krAL0QAIr0/dBrTMUjRSJbbLNOkh zH3Zk4V0G2NkiWzdYfK7VYap+LeVbfasSZjX0C+ABwX19sL4kYn5OCHDSgK8p61cFICl9xyb+ Iilth4BiwxLyNxb9lmzaEyJzXDYPmYsPvV7EMeuaGGxO17cQUh3CxojlnAO2ErPFiAPaXjN4t RHWqTHaF2/DJ3sydfOr8j+Phyx1a1QpjxWJyu9nJ0QIAcpvbyPYdfBWhtPaJKJPTolH1Amvsl +6ijIaoQ3xHAQ/XbO15mJ/b0DHOXU+7Ls2uKSY9sXmNxgWrtU1DqSE99rQb8TWAVgJsuvb3+p Op+87husWM0Rqc9HnIR4mpITJDw5xXoW2r67GtJDymRXkuOfqdwlasbea1qTJrGUlZ3awUGZC 5dj+kHIlJXdJo7yYQGG4IHEeQrrOKknDqBQLZHpX+peTX2wMPGm26yzOpLNbbxiUstTweGMF/ 9jf3cnoYvkAEqMP7sCLkiq/A17g= Received-SPF: pass client-ip=212.227.17.21; envelope-from=deller@gmx.de; helo=mout.gmx.net X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Fix the elf loader to calculate a valid TASK_UNMAPPED_BASE address for all 32-bit architectures, based on the GUEST_ADDR_MAX constant. Additionally modify the elf loader to load dynamic pie executables at around: ~ 0x5500000000 for 64-bit guest binaries on 64-bit host, - 0x00300000 for 32-bit guest binaries on 64-bit host, and - 0x00000000 for 32-bit guest binaries on 32-bit host. With this patch the Thread Sanitizer (TSan) application will work again, as in commit aab613fb9597 ("linux-user: Update TASK_UNMAPPED_BASE for aarch64"). Signed-off-by: Helge Deller --- linux-user/elfload.c | 6 ++++-- linux-user/loader.h | 12 ++++++++++++ linux-user/mmap.c | 35 ++++++++++++++++++----------------- 3 files changed, 34 insertions(+), 19 deletions(-) -- 2.41.0 diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 47a118e430..8f5a79b537 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -3021,6 +3021,7 @@ static void load_elf_image(const char *image_name, int image_fd, struct elfhdr *ehdr = (struct elfhdr *)bprm_buf; struct elf_phdr *phdr; abi_ulong load_addr, load_bias, loaddr, hiaddr, error; + unsigned long load_offset = 0; int i, retval, prot_exec; Error *err = NULL; bool is_main_executable; @@ -3121,6 +3122,7 @@ static void load_elf_image(const char *image_name, int image_fd, * select guest_base. In this case we pass a size. */ probe_guest_base(image_name, 0, hiaddr - loaddr); + load_offset = TASK_UNMAPPED_BASE_PIE; } } @@ -3138,7 +3140,7 @@ static void load_elf_image(const char *image_name, int image_fd, * In both cases, we will overwrite pages in this range with mappings * from the executable. */ - load_addr = target_mmap(loaddr, (size_t)hiaddr - loaddr + 1, PROT_NONE, + load_addr = target_mmap(loaddr + load_offset, (size_t)hiaddr - loaddr + 1, PROT_NONE, MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | (is_main_executable ? MAP_FIXED : 0), -1, 0); @@ -3176,7 +3178,7 @@ static void load_elf_image(const char *image_name, int image_fd, info->start_data = -1; info->end_data = 0; /* possible start for brk is behind all sections of this ELF file. */ - info->brk = TARGET_PAGE_ALIGN(hiaddr); + info->brk = TARGET_PAGE_ALIGN(load_offset + hiaddr); info->elf_flags = ehdr->e_flags; prot_exec = PROT_EXEC; diff --git a/linux-user/loader.h b/linux-user/loader.h index 59cbeacf24..3bbfc108eb 100644 --- a/linux-user/loader.h +++ b/linux-user/loader.h @@ -18,6 +18,18 @@ #ifndef LINUX_USER_LOADER_H #define LINUX_USER_LOADER_H +/* where to map binaries? */ +#if HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 64 +# define TASK_UNMAPPED_BASE_PIE 0x5500000000 +# define TASK_UNMAPPED_BASE 0x7000000000 +#elif HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 32 +# define TASK_UNMAPPED_BASE_PIE 0x00300000 +# define TASK_UNMAPPED_BASE (GUEST_ADDR_MAX - 0x20000000 + 1) +#else /* HOST_LONG_BITS == 32 && TARGET_ABI_BITS == 32 */ +# define TASK_UNMAPPED_BASE_PIE 0x00000000 +# define TASK_UNMAPPED_BASE 0x40000000 +#endif + /* * Read a good amount of data initially, to hopefully get all the * program headers loaded. diff --git a/linux-user/mmap.c b/linux-user/mmap.c index c624feead0..3441198e21 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -23,6 +23,7 @@ #include "user-internals.h" #include "user-mmap.h" #include "target_mman.h" +#include "loader.h" static pthread_mutex_t mmap_mutex = PTHREAD_MUTEX_INITIALIZER; static __thread int mmap_lock_count; @@ -295,23 +296,6 @@ static bool mmap_frag(abi_ulong real_start, abi_ulong start, abi_ulong last, return true; } -#if HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 64 -#ifdef TARGET_AARCH64 -# define TASK_UNMAPPED_BASE 0x5500000000 -#else -# define TASK_UNMAPPED_BASE 0x4000000000 -#endif -#elif HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 32 -#ifdef TARGET_HPPA -# define TASK_UNMAPPED_BASE 0xfa000000 -#else -# define TASK_UNMAPPED_BASE 0xe0000000 -#endif -#else /* HOST_LONG_BITS == 32 && TARGET_ABI_BITS == 32 */ -# define TASK_UNMAPPED_BASE 0x40000000 -#endif -abi_ulong mmap_next_start = TASK_UNMAPPED_BASE; - unsigned long last_brk; /* @@ -344,6 +328,23 @@ abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size, abi_ulong align) abi_ulong addr; int wrapped, repeat; + static abi_ulong mmap_next_start; + + /* initialize mmap_next_start if necessary */ + if (!mmap_next_start) { + mmap_next_start = TASK_UNMAPPED_BASE; + + /* do sanity checks on guest memory layout */ + if (mmap_next_start >= GUEST_ADDR_MAX) { + mmap_next_start = GUEST_ADDR_MAX - 0x1000000000 + 1; + } + + if (TASK_UNMAPPED_BASE_PIE >= mmap_next_start) { + fprintf(stderr, "Memory too small for PIE executables.\n"); + exit(EXIT_FAILURE); + } + } + align = MAX(align, qemu_host_page_size); /* If 'start' == 0, then a default start address is used. */