From nobody Tue Oct 28 12:17:37 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1514484896958745.3110561624483; Thu, 28 Dec 2017 10:14:56 -0800 (PST) Received: from localhost ([::1]:60731 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eUchj-0004vc-Qc for importer@patchew.org; Thu, 28 Dec 2017 13:14:55 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54657) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eUcbR-00084e-Vw for qemu-devel@nongnu.org; Thu, 28 Dec 2017 13:08:27 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eUcbQ-00013W-2z for qemu-devel@nongnu.org; Thu, 28 Dec 2017 13:08:25 -0500 Received: from mav.lukeshu.com ([2001:19f0:5c00:8069:5400:ff:fe26:6a86]:41954) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eUcbP-00012o-UW for qemu-devel@nongnu.org; Thu, 28 Dec 2017 13:08:24 -0500 Received: from build64-par (unknown [IPv6:2601:803:202:9275:da50:e6ff:fe00:4a5b]) by mav.lukeshu.com (Postfix) with ESMTPSA id 7BB7D80507; Thu, 28 Dec 2017 13:08:18 -0500 (EST) From: Luke Shumaker To: qemu-devel@nongnu.org Date: Thu, 28 Dec 2017 13:08:08 -0500 Message-Id: <20171228180814.9749-6-lukeshu@lukeshu.com> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20171228180814.9749-1-lukeshu@lukeshu.com> References: <20171228180814.9749-1-lukeshu@lukeshu.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2001:19f0:5c00:8069:5400:ff:fe26:6a86 Subject: [Qemu-devel] [PATCH 05/10] linux-user: init_guest_space: Clarify page alignment logic X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Luke Shumaker , Riku Voipio , Laurent Vivier Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Luke Shumaker There are 3 parts to this change: - Add a comment showing the relative sizes and positions of the blocks of memory - introduce and use new aligned_{start,size} instead of adjusting real_{start_size} - When we clean up (on failure), munmap(real_start, real_size) instead of munmap(aligned_start, aligned_size). It *shouldn't* make any difference, but I will admit that this does mean we are making the syscall with different values, so this isn't quite a no-op patch. Signed-off-by: Luke Shumaker Reviewed-by: Peter Maydell --- linux-user/elfload.c | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index f41cecc3cb..22f2632dfa 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -1827,7 +1827,7 @@ unsigned long init_guest_space(unsigned long host_sta= rt, unsigned long guest_start, bool fixed) { - unsigned long current_start, real_start; + unsigned long current_start, aligned_start; int flags; =20 assert(host_start || host_size); @@ -1853,7 +1853,8 @@ unsigned long init_guest_space(unsigned long host_sta= rt, /* Otherwise, a non-zero size region of memory needs to be mapped * and validated. */ while (1) { - unsigned long real_size =3D host_size; + unsigned long real_start, real_size, aligned_size; + aligned_size =3D real_size =3D host_size; =20 /* Do not use mmap_find_vma here because that is limited to the * guest address space. We are going to make the @@ -1867,26 +1868,48 @@ unsigned long init_guest_space(unsigned long host_s= tart, =20 /* Ensure the address is properly aligned. */ if (real_start & ~qemu_host_page_mask) { + /* Ideally, we adjust like + * + * pages: [ ][ ][ ][ ][ ] + * old: [ real ] + * [ aligned ] + * new: [ real ] + * [ aligned ] + * + * But if there is something else mapped right after it, + * then obviously it won't have room to grow, and the + * kernel will put the new larger real someplace else with + * unknown alignment (if we made it to here, then + * fixed=3Dfalse). Which is why we grow real by a full page + * size, instead of by part of one; so that even if we get + * moved, we can still guarantee alignment. But this does + * mean that there is a padding of < 1 page both before + * and after the aligned range; the "after" could could + * cause problems for ARM emulation where it could butt in + * to where we need to put the commpage. + */ munmap((void *)real_start, host_size); - real_size =3D host_size + qemu_host_page_size; + real_size =3D aligned_size + qemu_host_page_size; real_start =3D (unsigned long) mmap((void *)real_start, real_size, PROT_NONE, flags, -1, = 0); if (real_start =3D=3D (unsigned long)-1) { return (unsigned long)-1; } - real_start =3D HOST_PAGE_ALIGN(real_start); + aligned_start =3D HOST_PAGE_ALIGN(real_start); + } else { + aligned_start =3D real_start; } =20 /* Check to see if the address is valid. */ - if (!host_start || real_start =3D=3D current_start) { + if (!host_start || aligned_start =3D=3D current_start) { #if defined(TARGET_ARM) && !defined(TARGET_AARCH64) /* On 32-bit ARM, we need to also be able to map the commpage.= */ - int valid =3D init_guest_commpage(real_start - guest_start, - real_size + guest_start); + int valid =3D init_guest_commpage(aligned_start - guest_start, + aligned_size + guest_start); if (valid =3D=3D 1) { break; } else if (valid =3D=3D -1) { - munmap((void *)real_start, host_size); + munmap((void *)real_start, real_size); return (unsigned long)-1; } /* valid =3D=3D 0, so try again. */ @@ -1905,7 +1928,7 @@ unsigned long init_guest_space(unsigned long host_sta= rt, * address space randomization put a shared library somewhere * inconvenient. */ - munmap((void *)real_start, host_size); + munmap((void *)real_start, real_size); current_start +=3D qemu_host_page_size; if (host_start =3D=3D current_start) { /* Theoretically possible if host doesn't have any suitably @@ -1917,7 +1940,7 @@ unsigned long init_guest_space(unsigned long host_sta= rt, =20 qemu_log_mask(CPU_LOG_PAGE, "Reserved 0x%lx bytes of guest address spa= ce\n", host_size); =20 - return real_start; + return aligned_start; } =20 static void probe_guest_base(const char *image_name, --=20 2.15.1