From nobody Tue Feb 10 12:59:07 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.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 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1558697428; cv=none; d=zoho.com; s=zohoarc; b=gwYXehfOAy2lPLP/WXeEL3fBEb0MFD4WBIVQXIHxSbCxv75XPoZtJZ1j6Xd4DNUOIw8U+1cwl6ChP8vK/8A6Q40upL1pKlLy4QEhfCkjkdPQDRC7gF3p9vBqQhgrBH5x/8yqz8W54ARtY4GnJdwv4aStGooFo2px3TWS8ufOs4A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1558697428; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=4yo0AW3V9fGXCyJqmwmD22646W0vXW1B124NO2es8yk=; b=O4SZKsNl/yE+Es7IcK8m5QTcmeQZQ8GIOHwk9PcAvb9z/ePNOe7wLshXBKEAJ50OR5pgFpN+wiYcynX7Bh2K/+SHGudBAzDjRbG+gWcFXNbwlCCtNICWD3s3/VENGtU/BrbN0gbwMYunjNK1SBezxmUzaod2k7Gu+qoxiP6RMI8= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1558697428311705.6058753776113; Fri, 24 May 2019 04:30:28 -0700 (PDT) Received: from localhost ([127.0.0.1]:52738 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hU8P2-0000ab-9A for importer@patchew.org; Fri, 24 May 2019 07:30:24 -0400 Received: from eggs.gnu.org ([209.51.188.92]:38929) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hU8Kn-0005Z1-0R for qemu-devel@nongnu.org; Fri, 24 May 2019 07:26:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hU8Kl-00017U-Fw for qemu-devel@nongnu.org; Fri, 24 May 2019 07:26:00 -0400 Received: from mout.kundenserver.de ([217.72.192.75]:53869) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hU8Kl-00016z-66 for qemu-devel@nongnu.org; Fri, 24 May 2019 07:25:59 -0400 Received: from localhost.localdomain ([78.238.229.36]) by mrelayeu.kundenserver.de (mreue109 [212.227.15.183]) with ESMTPSA (Nemesis) id 1MdeSt-1guWWj09gn-00Zh5C; Fri, 24 May 2019 13:25:32 +0200 From: Laurent Vivier To: qemu-devel@nongnu.org Date: Fri, 24 May 2019 13:25:18 +0200 Message-Id: <20190524112520.17684-8-laurent@vivier.eu> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190524112520.17684-1-laurent@vivier.eu> References: <20190524112520.17684-1-laurent@vivier.eu> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:2ha9l4+kdw1Ivy1dipZ4Op7wBho/hxzHyL8IatCh663ngDSDH+E TgRqE3x4Mrc4VHEUx1wYLG5Ezzn4rRHJpGKCDVIMvDI+WG2pF/ZYsg+dDKs1Opt+ewHRoWY p/1a9hMO9z1wmRWQ8a1oVp/Jtpgw+JvN6o8H02q7vc8qXS14uc0k3yXjCSFPx2ucclNZS4f nKG9iZTqTu+8qOxPGzDWA== X-UI-Out-Filterresults: notjunk:1;V03:K0:gvayknHzqJI=:b3bHAxTf/HKNaXQIrjZZF/ rGkFn8KLB21a4ApsEBjLK0qdGoN2n4gmY61AJjA3m7zL6uTUvNLOgklyfRKYrEV0BloSjwEss et1GNf2FSayI3yafe5KLLbYRn1r0YVQ4j3KO25P+8NNkz7o7yek14Nt6YvvWdMDRdJxiA2pGY oGISBKKXPfePObZJkbWNTUQ5HzUu3ZAspgz/oO00bGvoX7Sy2wLdm2RL9RMdnSwfqndfkWXtX +lik8c69Uq2RsxC8pRtKcXddEDoWy39Jn44VjfiZ0JYQIwR1ow+N/CRga8ONbsxZI3pqDZYqs 7M1bV5L0wKFWhvTtWdppuPySkawuNGCAEMB4TswXojLZZ794MJP1LcUzkIz2AhR7zKwHmibH4 c9gw1dPGvko4Qj4iDnikzRVAWTyaA07FI2YiSn82oFKZAc6xOmFop4bpyDOVbdQjSnbQZG00A Ae9LHW+RuNZYC21b0ip/tSW8KPzqIs7Xwsfa24xjboBTOUKRUZXypLh3yTDpmmH3Gti/S7yDG 3nitT9Q9W/JgiiwAfElDJ3pDZTWV3hQY/qotlteRM+rqKHM4ZMU6pujBYHcSKL6c+K8rahTnM SFxIPuGrRElJOOGycFApN1g8p2/3NpOltlqxYQ1ic8A6V1hltDx2rc6PRA0VQrvJwRIUPEzxm srrr/Zk5n9QjSttKaZnjG8SxEyVMuV10Z5poAGzL0lXKXW7fchyKYmquRcPs8MM0ji5j6AdYI L5Dw6i5mPU4CgDn8jZDhQxSGil2kj6Dt8MHJ4g== X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 217.72.192.75 Subject: [Qemu-devel] [PULL v2 7/9] linux-user: Fix shmat emulation by honoring host SHMLBA 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: Aleksandar Rikalo , Riku Voipio , Richard Henderson , Laurent Vivier , Aleksandar Markovic , Aurelien Jarno Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" From: Richard Henderson For those hosts with SHMLBA > getpagesize, we don't automatically select a guest address that is compatible with the host. We can achieve this by boosting the alignment of guest_base and by adding an extra alignment argument to mmap_find_vma. Signed-off-by: Richard Henderson Message-Id: <20190519201953.20161-13-richard.henderson@linaro.org> Signed-off-by: Laurent Vivier --- linux-user/elfload.c | 17 ++++++----- linux-user/mmap.c | 70 +++++++++++++++++++++++--------------------- linux-user/qemu.h | 2 +- linux-user/syscall.c | 3 +- 4 files changed, 50 insertions(+), 42 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 02832adfbc0c..a23aa4493e72 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -3,6 +3,7 @@ #include =20 #include +#include =20 #include "qemu.h" #include "disas/disas.h" @@ -2012,6 +2013,8 @@ unsigned long init_guest_space(unsigned long host_sta= rt, unsigned long guest_start, bool fixed) { + /* In order to use host shmat, we must be able to honor SHMLBA. */ + unsigned long align =3D MAX(SHMLBA, qemu_host_page_size); unsigned long current_start, aligned_start; int flags; =20 @@ -2029,7 +2032,7 @@ unsigned long init_guest_space(unsigned long host_sta= rt, } =20 /* Setup the initial flags and start address. */ - current_start =3D host_start & qemu_host_page_mask; + current_start =3D host_start & -align; flags =3D MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE; if (fixed) { flags |=3D MAP_FIXED; @@ -2065,8 +2068,8 @@ unsigned long init_guest_space(unsigned long host_sta= rt, return (unsigned long)-1; } munmap((void *)real_start, host_full_size); - if (real_start & ~qemu_host_page_mask) { - /* The same thing again, but with an extra qemu_host_page_size + if (real_start & (align - 1)) { + /* The same thing again, but with extra * so that we can shift around alignment. */ unsigned long real_size =3D host_full_size + qemu_host_page_si= ze; @@ -2079,7 +2082,7 @@ unsigned long init_guest_space(unsigned long host_sta= rt, return (unsigned long)-1; } munmap((void *)real_start, real_size); - real_start =3D HOST_PAGE_ALIGN(real_start); + real_start =3D ROUND_UP(real_start, align); } current_start =3D real_start; } @@ -2106,7 +2109,7 @@ unsigned long init_guest_space(unsigned long host_sta= rt, } =20 /* Ensure the address is properly aligned. */ - if (real_start & ~qemu_host_page_mask) { + if (real_start & (align - 1)) { /* Ideally, we adjust like * * pages: [ ][ ][ ][ ][ ] @@ -2134,7 +2137,7 @@ unsigned long init_guest_space(unsigned long host_sta= rt, if (real_start =3D=3D (unsigned long)-1) { return (unsigned long)-1; } - aligned_start =3D HOST_PAGE_ALIGN(real_start); + aligned_start =3D ROUND_UP(real_start, align); } else { aligned_start =3D real_start; } @@ -2171,7 +2174,7 @@ unsigned long init_guest_space(unsigned long host_sta= rt, * because of trouble with ARM commpage setup. */ munmap((void *)real_start, real_size); - current_start +=3D qemu_host_page_size; + current_start +=3D align; if (host_start =3D=3D current_start) { /* Theoretically possible if host doesn't have any suitably * aligned areas. Normally the first mmap will fail. diff --git a/linux-user/mmap.c b/linux-user/mmap.c index e0249efe4fed..10796b37ac7b 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -202,49 +202,52 @@ unsigned long last_brk; =20 /* Subroutine of mmap_find_vma, used when we have pre-allocated a chunk of guest address space. */ -static abi_ulong mmap_find_vma_reserved(abi_ulong start, abi_ulong size) +static abi_ulong mmap_find_vma_reserved(abi_ulong start, abi_ulong size, + abi_ulong align) { - abi_ulong addr; - abi_ulong end_addr; + abi_ulong addr, end_addr, incr =3D qemu_host_page_size; int prot; - int looped =3D 0; + bool looped =3D false; =20 if (size > reserved_va) { return (abi_ulong)-1; } =20 - size =3D HOST_PAGE_ALIGN(size); + /* Note that start and size have already been aligned by mmap_find_vma= . */ + end_addr =3D start + size; - if (end_addr > reserved_va) { - end_addr =3D reserved_va; + if (start > reserved_va - size) { + /* Start at the top of the address space. */ + end_addr =3D ((reserved_va - size) & -align) + size; + looped =3D true; } - addr =3D end_addr - qemu_host_page_size; =20 + /* Search downward from END_ADDR, checking to see if a page is in use.= */ + addr =3D end_addr; while (1) { + addr -=3D incr; if (addr > end_addr) { if (looped) { + /* Failure. The entire address space has been searched. = */ return (abi_ulong)-1; } - end_addr =3D reserved_va; - addr =3D end_addr - qemu_host_page_size; - looped =3D 1; - continue; - } - prot =3D page_get_flags(addr); - if (prot) { - end_addr =3D addr; - } - if (addr && addr + size =3D=3D end_addr) { - break; + /* Re-start at the top of the address space. */ + addr =3D end_addr =3D ((reserved_va - size) & -align) + size; + looped =3D true; + } else { + prot =3D page_get_flags(addr); + if (prot) { + /* Page in use. Restart below this page. */ + addr =3D end_addr =3D ((addr - size) & -align) + size; + } else if (addr && addr + size =3D=3D end_addr) { + /* Success! All pages between ADDR and END_ADDR are free.= */ + if (start =3D=3D mmap_next_start) { + mmap_next_start =3D addr; + } + return addr; + } } - addr -=3D qemu_host_page_size; } - - if (start =3D=3D mmap_next_start) { - mmap_next_start =3D addr; - } - - return addr; } =20 /* @@ -253,7 +256,7 @@ static abi_ulong mmap_find_vma_reserved(abi_ulong start= , abi_ulong size) * It must be called with mmap_lock() held. * Return -1 if error. */ -abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size) +abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size, abi_ulong align) { void *ptr, *prev; abi_ulong addr; @@ -265,11 +268,12 @@ abi_ulong mmap_find_vma(abi_ulong start, abi_ulong si= ze) } else { start &=3D qemu_host_page_mask; } + start =3D ROUND_UP(start, align); =20 size =3D HOST_PAGE_ALIGN(size); =20 if (reserved_va) { - return mmap_find_vma_reserved(start, size); + return mmap_find_vma_reserved(start, size, align); } =20 addr =3D start; @@ -299,7 +303,7 @@ abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size) if (h2g_valid(ptr + size - 1)) { addr =3D h2g(ptr); =20 - if ((addr & ~TARGET_PAGE_MASK) =3D=3D 0) { + if ((addr & (align - 1)) =3D=3D 0) { /* Success. */ if (start =3D=3D mmap_next_start && addr >=3D TASK_UNMAPPE= D_BASE) { mmap_next_start =3D addr + size; @@ -313,12 +317,12 @@ abi_ulong mmap_find_vma(abi_ulong start, abi_ulong si= ze) /* Assume the result that the kernel gave us is the first with enough free space, so start again at the next higher target page. */ - addr =3D TARGET_PAGE_ALIGN(addr); + addr =3D ROUND_UP(addr, align); break; case 1: /* Sometimes the kernel decides to perform the allocation at the top end of memory instead. */ - addr &=3D TARGET_PAGE_MASK; + addr &=3D -align; break; case 2: /* Start over at low memory. */ @@ -416,7 +420,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, in= t prot, if (!(flags & MAP_FIXED)) { host_len =3D len + offset - host_offset; host_len =3D HOST_PAGE_ALIGN(host_len); - start =3D mmap_find_vma(real_start, host_len); + start =3D mmap_find_vma(real_start, host_len, TARGET_PAGE_SIZE); if (start =3D=3D (abi_ulong)-1) { errno =3D ENOMEM; goto fail; @@ -710,7 +714,7 @@ abi_long target_mremap(abi_ulong old_addr, abi_ulong ol= d_size, } else if (flags & MREMAP_MAYMOVE) { abi_ulong mmap_start; =20 - mmap_start =3D mmap_find_vma(0, new_size); + mmap_start =3D mmap_find_vma(0, new_size, TARGET_PAGE_SIZE); =20 if (mmap_start =3D=3D -1) { errno =3D ENOMEM; diff --git a/linux-user/qemu.h b/linux-user/qemu.h index ef400cb78ac6..82d33d7e93d5 100644 --- a/linux-user/qemu.h +++ b/linux-user/qemu.h @@ -443,7 +443,7 @@ abi_long target_mremap(abi_ulong old_addr, abi_ulong ol= d_size, abi_ulong new_addr); extern unsigned long last_brk; extern abi_ulong mmap_next_start; -abi_ulong mmap_find_vma(abi_ulong, abi_ulong); +abi_ulong mmap_find_vma(abi_ulong, abi_ulong, abi_ulong); void mmap_fork_start(void); void mmap_fork_end(int child); =20 diff --git a/linux-user/syscall.c b/linux-user/syscall.c index e5545cbafac7..51cc049e064f 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -3912,7 +3912,8 @@ static inline abi_ulong do_shmat(CPUArchState *cpu_en= v, else { abi_ulong mmap_start; =20 - mmap_start =3D mmap_find_vma(0, shm_info.shm_segsz); + /* In order to use the host shmat, we need to honor host SHMLBA. = */ + mmap_start =3D mmap_find_vma(0, shm_info.shm_segsz, MAX(SHMLBA, sh= mlba)); =20 if (mmap_start =3D=3D -1) { errno =3D ENOMEM; --=20 2.20.1