From nobody Mon Feb 9 01:30:54 2026 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 16345843005944.2602784879170486; Mon, 18 Oct 2021 12:11:40 -0700 (PDT) Received: from localhost ([::1]:42588 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mcY2t-0006hH-9f for importer@patchew.org; Mon, 18 Oct 2021 15:11:39 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43370) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mcXtL-0007z4-1k for qemu-devel@nongnu.org; Mon, 18 Oct 2021 15:01:48 -0400 Received: from mail-il1-x132.google.com ([2607:f8b0:4864:20::132]:34745) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mcXtI-0001Zy-BJ for qemu-devel@nongnu.org; Mon, 18 Oct 2021 15:01:46 -0400 Received: by mail-il1-x132.google.com with SMTP id g2so15997576ild.1 for ; Mon, 18 Oct 2021 12:01:43 -0700 (PDT) Received: from dune.bsdimp.com (50-253-99-174-static.hfc.comcastbusiness.net. [50.253.99.174]) by smtp.gmail.com with ESMTPSA id a8sm6945151ilh.5.2021.10.18.12.01.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Oct 2021 12:01:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bsdimp-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=64uP86lLO6D5epKoICdnCmOVA/U9XxsL32Tuch8fnPI=; b=0YOE2pezWZKQrglheK2JyPphX7Repp2NkjkzkF116maZ6Bwn3v231LVFGRZWK/YNrD BwfXKTAS5/nCBCR8anjvS9LE3zIZbIGiCkcLUnQ3WTJTzuUU/4MMM+VtLMEpYP3wC2ot 1bk/jtXc4ZCscU8omA9Wp4JO8UxprsvoSTCCqjnpFEDCYc51FRbFDVQ3+ES/g4r5JIHg u1lsghkS8xZK5Qelb1BKuQV5Wdfjs2WXxNkspg84PF3TUsiUBYsovLZdNvN98SCKC6GG lNb7jRVmKaHi03Z9Zr1Y3w8OIp87sC6PrPno8dU1/3BSia7nn268iAuveeiCjlyjQhkZ jrmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=64uP86lLO6D5epKoICdnCmOVA/U9XxsL32Tuch8fnPI=; b=bAncqqDjwGe7IhYXyWzs5D8g/YgtFLEf8K08Jli3H/g23Nx/vcrC2T+QGej9hC9A8d tMo8zt8buUYVSlMuI46EFIWwDiZglhoLIJ14q/xSBxFDQKrgu0zkLeE8gMKIlic/DOoP 4thKx3PYr81ecl1NtJfMWDBLgIxQB4BaC/Q3WDHspOS1F+pT9XVhCYpKgeOKIJUp9zqS wBBlV3nEZ3YYmbNUc67dq1+9P53sl7LEyweqmFW2Uzh2A46Mg1QVqZQJid7nODC8AVDH WqhYe1zH7s98tz9fr+b8WPP6IGaiVvJ3A4WM/kgbCJ/Cf876xzlPn4tXxENCYqtQuCQn cHkA== X-Gm-Message-State: AOAM5324Rq+PiFH0gpfzrD+m26wC2JuBTHV3JP8bgG0yDkjIJ7rp7ANr e9cGqB7nbfg1hozJz23EUb38BunZEkWnEw== X-Google-Smtp-Source: ABdhPJwjKJaplhZBsplQBVnfwaEDJbcgyLCJa7HaGuLCXRIhc8wFlkEzBXwGuyd8htb/6X4+H2yjzw== X-Received: by 2002:a05:6e02:120f:: with SMTP id a15mr7893991ilq.109.1634583702247; Mon, 18 Oct 2021 12:01:42 -0700 (PDT) From: Warner Losh To: qemu-devel@nongnu.org Subject: [PULL v2 07/23] bsd-user/mmap.c: Don't mmap fd == -1 independently from MAP_ANON flag Date: Mon, 18 Oct 2021 13:00:59 -0600 Message-Id: <20211018190115.5365-8-imp@bsdimp.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211018190115.5365-1-imp@bsdimp.com> References: <20211018190115.5365-1-imp@bsdimp.com> MIME-Version: 1.0 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: none client-ip=2607:f8b0:4864:20::132; envelope-from=imp@bsdimp.com; helo=mail-il1-x132.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no 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: Kyle Evans , Richard Henderson , Guy Yur , Laurent Vivier , Warner Losh Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1634584301343100001 Content-Type: text/plain; charset="utf-8" Switch checks for !(flags & MAP_ANONYMOUS) with checks for fd !=3D -1. MAP_STACK and MAP_GUARD both require fd =3D=3D -1 and don't require mapping the fd either. Add analysis from Guy Yur detailing the different cases for MAP_GUARD and MAP_STACK. Signed-off-by: Guy Yur [ partially merged before, finishing the job and documenting origin] Signed-off-by: Warner Losh Reviewed-by: Richard Henderson Reviewed-by: Kyle Evans --- bsd-user/mmap.c | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/bsd-user/mmap.c b/bsd-user/mmap.c index face98573f..4ecd949a10 100644 --- a/bsd-user/mmap.c +++ b/bsd-user/mmap.c @@ -127,7 +127,27 @@ error: return ret; } =20 -/* map an incomplete host page */ +/* + * map an incomplete host page + * + * mmap_frag can be called with a valid fd, if flags doesn't contain one of + * MAP_ANON, MAP_STACK, MAP_GUARD. If we need to map a page in those cases= , we + * pass fd =3D=3D -1. However, if flags contains MAP_GUARD then MAP_ANON c= annot be + * added. + * + * * If fd is valid (not -1) we want to map the pages with MAP_ANON. + * * If flags contains MAP_GUARD we don't want to add MAP_ANON because it + * will be rejected. See kern_mmap's enforcing of constraints for MAP_G= UARD + * in sys/vm/vm_mmap.c. + * * If flags contains MAP_ANON it doesn't matter if we add it or not. + * * If flags contains MAP_STACK, mmap adds MAP_ANON when called so doesn't + * matter if we add it or not either. See enforcing of constraints for + * MAP_STACK in kern_mmap. + * + * Don't add MAP_ANON for the flags that use fd =3D=3D -1 without specifyi= ng the + * flags directly, with the assumption that future flags that require fd = =3D=3D -1 + * will also not require MAP_ANON. + */ static int mmap_frag(abi_ulong real_start, abi_ulong start, abi_ulong end, int prot, int flags, int fd, abi_ulong offset) @@ -147,9 +167,9 @@ static int mmap_frag(abi_ulong real_start, } =20 if (prot1 =3D=3D 0) { - /* no page was there, so we allocate one */ + /* no page was there, so we allocate one. See also above. */ void *p =3D mmap(host_start, qemu_host_page_size, prot, - flags | MAP_ANON, -1, 0); + flags | ((fd !=3D -1) ? MAP_ANON : 0), -1, 0); if (p =3D=3D MAP_FAILED) return -1; prot1 =3D prot; @@ -157,7 +177,7 @@ static int mmap_frag(abi_ulong real_start, prot1 &=3D PAGE_BITS; =20 prot_new =3D prot | prot1; - if (!(flags & MAP_ANON)) { + if (fd !=3D -1) { /* msync() won't work here, so we return an error if write is possible while it is a shared mapping */ if ((flags & TARGET_BSD_MAP_FLAGMASK) =3D=3D MAP_SHARED && @@ -565,7 +585,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, in= t prot, * worst case: we cannot map the file because the offset is not * aligned, so we read it */ - if (!(flags & MAP_ANON) && + if (fd !=3D -1 && (offset & ~qemu_host_page_mask) !=3D (start & ~qemu_host_page_= mask)) { /* * msync() won't work here, so we return an error if write is --=20 2.32.0