From nobody Fri Apr 19 17:12:44 2024 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 (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1526486505505480.3891807653173; Wed, 16 May 2018 09:01:45 -0700 (PDT) Received: from localhost ([::1]:53062 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fIyrx-0002C1-PK for importer@patchew.org; Wed, 16 May 2018 12:01:37 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42646) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fIyld-0005y3-1R for qemu-devel@nongnu.org; Wed, 16 May 2018 11:55:06 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fIylZ-0005g2-Rv for qemu-devel@nongnu.org; Wed, 16 May 2018 11:55:05 -0400 Received: from [69.196.150.84] (port=34131 helo=smcpolin-M01.vmware.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fIylZ-0005fl-Np for qemu-devel@nongnu.org; Wed, 16 May 2018 11:55:01 -0400 Received: by smcpolin-M01.vmware.com (Postfix, from userid 375304) id 9ECB1A5D5F2; Wed, 16 May 2018 11:27:08 -0400 (EDT) From: Steve Mcpolin Date: Wed, 16 May 2018 10:47:32 -0400 To: qemu-devel@nongnu.org Message-Id: <20180516152708.9ECB1A5D5F2@smcpolin-M01.vmware.com> X-detected-operating-system: by eggs.gnu.org: Mac OS X [generic] [fuzzy] X-Received-From: 69.196.150.84 Subject: [Qemu-devel] [PATCH] linux-user: Remove extra mapping 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: qemu-trivial@nongnu.org, riku.voipio@iki.fi, laurent@vivier.eu, smcpolin@vmware.com, qemu-devel@nongnu.org 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" When a guest mmap()'d a file, a transient MAP_ANONYMOUS mapping was created, which required the kernel to reserve this memory, then subsequently released by applying a mapping with just the requested flags and fd. This transient mapping causes spurious failures when the available memory is smaller than the mapping. This patch avoids this transient mapping. Signed-off-by: Steve Mcpolin --- linux-user/mmap.c | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/linux-user/mmap.c b/linux-user/mmap.c index 9168a20..f91841f 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -453,21 +453,28 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, = int prot, /* Note: we prefer to control the mapping address. It is especially important if qemu_host_page_size > qemu_real_host_page_size */ - p =3D mmap(g2h(start), host_len, prot, - flags | MAP_FIXED | MAP_ANONYMOUS, -1, 0); - if (p =3D=3D MAP_FAILED) - goto fail; - /* update start so that it points to the file position at 'offset'= */ - host_start =3D (unsigned long)p; - if (!(flags & MAP_ANONYMOUS)) { - p =3D mmap(g2h(start), len, prot, + if (flags & MAP_ANONYMOUS) { + offset =3D 0; + host_offset =3D 0; + fd =3D -1; + } + p =3D mmap(g2h(start), len, prot, flags | MAP_FIXED, fd, host_offset); - if (p =3D=3D MAP_FAILED) { - munmap(g2h(start), host_len); - goto fail; + host_start =3D (uintptr_t)p; + if (p !=3D MAP_FAILED && host_len > HOST_PAGE_ALIGN(len)) { + void *q; + q =3D mmap(g2h(start + len), host_len - HOST_PAGE_ALIGN(len), + prot, MAP_FIXED | MAP_ANONYMOUS, -1, 0); + if (q =3D=3D MAP_FAILED) { + p =3D MAP_FAILED; } - host_start +=3D offset - host_offset; } + if (p =3D=3D MAP_FAILED) { + munmap(g2h(start), host_len); + goto fail; + } + host_start +=3D offset - host_offset; + /* update start so that it points to the file position at 'offset'= */ start =3D h2g(host_start); } else { if (start & ~TARGET_PAGE_MASK) { --=20 2.7.4