From nobody Fri Apr 26 20:48:28 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1590157546; cv=none; d=zohomail.com; s=zohoarc; b=grGQrE0rZc5/qJzrJg0003O0/YxqEoXBx5fCb6s3Lkok9IWA151GGs01Hi24Y8HrIHKz8gC7Y54Re129BpmiCYByBm1fXheFnrgeBe45Vq6JXVsLG7CD2SRbc6j87SQ5uR1IhysSips5z4XJoI3KRCLtkYZ8qbm6LLTBYabk128= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1590157546; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=Q9g2hw1e4m0meDSSDGhsHzYZBM3E+e3OyOVzFlX36Xg=; b=i430bwgt7sJkztNigD85XEno1AzdlLgdmbswtUDF7GL2wQAcVkmctlKHNBuN/jzE9rEoqQJEOS/8YZchYDeklePaLu5yTrZ1XiMtif85ytTFjnYPQyO3RNvJIY/GOffg+4guroHaJ1Fsdc8mb0LHUW2zxDZMYcIYLT7FWWvdkGM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1590157546391395.2247529251416; Fri, 22 May 2020 07:25:46 -0700 (PDT) Received: from localhost ([::1]:55082 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jc8cK-0005IS-SC for importer@patchew.org; Fri, 22 May 2020 10:25:44 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:55870) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jc8bY-0004R8-El for qemu-devel@nongnu.org; Fri, 22 May 2020 10:24:56 -0400 Received: from mail-ed1-x544.google.com ([2a00:1450:4864:20::544]:41984) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jc8bX-0000BH-Jg for qemu-devel@nongnu.org; Fri, 22 May 2020 10:24:56 -0400 Received: by mail-ed1-x544.google.com with SMTP id k19so9393761edv.9 for ; Fri, 22 May 2020 07:24:55 -0700 (PDT) Received: from zen.linaroharston ([51.148.130.216]) by smtp.gmail.com with ESMTPSA id y16sm8122483ejq.82.2020.05.22.07.24.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 May 2020 07:24:52 -0700 (PDT) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 784871FF7E; Fri, 22 May 2020 15:24:51 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Q9g2hw1e4m0meDSSDGhsHzYZBM3E+e3OyOVzFlX36Xg=; b=YmNCOsFnlyZ96ruU4ia2lZvzznbLXilZL0nz0yQPGRqlT3IaNUPwTpKnt/plQlymdX GabGsPX+amXe6sec5Fafz/CT840eWCXTW8U01F10gyKJnhIsD8PhZ7NT1p5MJMhhVaHx 7R6pLns8d0j1wiO7AIvS37F8T4LY2OkZ37JrrEcG0yJw8B+bXJrrXZYuYSYKVHoNSqO5 snbkcdDqz+FJ0tclrCgcVpwK6coFIoX6ThhYBlzGQEFKJK/eqReStNGFHKkxrNd52BTT O+NMWMzCrq2wKTOTfK/8PgBkSo15vpeF7i0laSynnabii90YrVtrcxuvow8qBmWTFzGO jmGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Q9g2hw1e4m0meDSSDGhsHzYZBM3E+e3OyOVzFlX36Xg=; b=DWolpLww0GCyF7WUEC5ma3fOvbwAI1cxgXgbAOD8LkQG+USJXV568uJAln6cvmUSVv c1Kl6Yucy5MAbZGux9r2K7cz3ry0t08cz/dHU1LyxNRhu47F8tDxz3rLr6enCHjyjQur IHUygej7z1L7Q9r1BeEPRZatSp8tKNgKl3b4wpGDAN8b9HsvlhEoA/SijuKydx/HHANj +L6VJR3uWbdRo92zhTP55kfLOLSWgdBFyoAL7V/tMsuJ7vDucIP8Uy5tAk6idh9v+EB+ 8/12JBdhM2XH8HFAO6AlsQmgAg/i0sbGmfzlt5nBCBDnGgZ38V7WT1hm+TSby9xjPuQY /fig== X-Gm-Message-State: AOAM531+QV6kkNtsUDeER5qwgGPBBdyaC8AoGogBVsPBZTmUMvb7dIC4 dyo56uBpCKgUiH6rF239u+NO5Q== X-Google-Smtp-Source: ABdhPJxKXtxJ4ZUZ6fNusImQyNZiQ5K4NKDJ71sGzOh+d0ls/B3uR9t3mPc7aa7Ij9URjxG3npc41w== X-Received: by 2002:a05:6402:155:: with SMTP id s21mr3092059edu.144.1590157493672; Fri, 22 May 2020 07:24:53 -0700 (PDT) From: =?UTF-8?q?Alex=20Benn=C3=A9e?= To: qemu-devel@nongnu.org Subject: [PATCH] linux-user: provide fallback pgd_find_hole for bare chroots Date: Fri, 22 May 2020 15:24:46 +0100 Message-Id: <20200522142446.25581-1-alex.bennee@linaro.org> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.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; Received-SPF: pass client-ip=2a00:1450:4864:20::544; envelope-from=alex.bennee@linaro.org; helo=mail-ed1-x544.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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, RCVD_IN_DNSWL_NONE=-0.0001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, Riku Voipio , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Laurent Vivier Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) When running QEMU out of a chroot environment we may not have access to /proc/self/maps. As there is no other "official" way to introspect our memory map we need to fall back to the original technique of repeatedly trying to mmap an address range until we find one that works. Fortunately it's not quite as ugly as the original code given we already re-factored the complications of dealing with the ARM_COMMPAGE. We do make an attempt to skip over brk() which is about the only concrete piece of information we have about the address map at this moment. Fixes: ee9474303 Reported-by: Peter Maydell Signed-off-by: Alex Benn=C3=A9e --- linux-user/elfload.c | 48 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 01a9323a637..d6027867a1a 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -2099,6 +2099,50 @@ static void pgb_have_guest_base(const char *image_na= me, abi_ulong guest_loaddr, } } =20 +/** + * pgd_find_hole_fallback: potential mmap address + * @guest_size: size of available space + * @brk: location of break + * @align: memory alignment + * + * This is a fallback method for finding a hole in the host address + * space if we don't have the benefit of being able to access + * /proc/self/map. It can potentially take a very long time as we can + * only dumbly iterate up the host address space seeing if the + * allocation would work. + */ +static uintptr_t pgd_find_hole_fallback(uintptr_t guest_size, uintptr_t br= k, long align) +{ + uintptr_t base; + + /* Start at the bottom and work our way up */ + base =3D mmap_min_addr; + + while (true) { + uintptr_t align_start, end; + align_start =3D ROUND_UP(base, align); + end =3D align_start + guest_size; + + /* if brk is anywhere in the range give ourselves some room to gro= w. */ + if (align_start <=3D brk && brk < end) { + base +=3D 16 * MiB; + continue; + } else if (align_start + guest_size < align_start) { + /* we have run out of space */ + return -1; + } else { + int flags =3D MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE | MA= P_FIXED; + void * mmap_start =3D mmap((void *) align_start, guest_size, + PROT_NONE, flags, -1, 0); + if (mmap_start !=3D MAP_FAILED) { + munmap((void *) align_start, guest_size); + return (uintptr_t) mmap_start; + } + base +=3D qemu_host_page_size; + } + } +} + /* Return value for guest_base, or -1 if no hole found. */ static uintptr_t pgb_find_hole(uintptr_t guest_loaddr, uintptr_t guest_siz= e, long align) @@ -2114,6 +2158,10 @@ static uintptr_t pgb_find_hole(uintptr_t guest_loadd= r, uintptr_t guest_size, /* Read brk after we've read the maps, which will malloc. */ brk =3D (uintptr_t)sbrk(0); =20 + if (!maps) { + return pgd_find_hole_fallback(guest_size, brk, align); + } + /* The first hole is before the first map entry. */ this_start =3D mmap_min_addr; =20 --=20 2.20.1