From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DEF72C433EF for ; Sun, 26 Sep 2021 18:39:55 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 5350F6108E for ; Sun, 26 Sep 2021 18:39:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 5350F6108E Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=bsdimp.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=nongnu.org Received: from localhost ([::1]:37248 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mUZ46-0006XI-Jn for qemu-devel@archiver.kernel.org; Sun, 26 Sep 2021 14:39:54 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:59054) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mUZ2c-00056L-Ls for qemu-devel@nongnu.org; Sun, 26 Sep 2021 14:38:22 -0400 Received: from mail-vs1-xe2f.google.com ([2607:f8b0:4864:20::e2f]:46667) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mUZ2a-0008MK-Th for qemu-devel@nongnu.org; Sun, 26 Sep 2021 14:38:22 -0400 Received: by mail-vs1-xe2f.google.com with SMTP id x74so15685314vsx.13 for ; Sun, 26 Sep 2021 11:38:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bsdimp-com.20210112.gappssmtp.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=uAiD7RAJueWlbRUtpvTiNLaNfkn8WgzU69242ryt9RI=; b=Rc6JpiLsaleC34ROotmUdiilDDo5K5odMQjom+3K26Ajn6YD4bmNV9VaKFLLpE5rDg F3NT9T+wb6nmGFmbc5WNF1DupzuQTBCCr6hpMFxIQ4/GATwwaUgpuBu325oD5RKQLe01 APBEkxNg3DSzx/52fYJtyjM6qefqJ0o+KGPkVpToll3TifR7FRH6o3WWQ+QnkkzAQZzG d9Y+lFR4T/GEmcBZGhjCHaDoxDtQPs6pKOFU9ySos4pAmqwtjVOwOjj0KjeSQ4Ahs75O tjAK3FrrtIBNWXSKYFQzGRKOU/jFpLAhfFWbwhsODp7e0yL2N02Y9AVzITI4ID52qKd4 0ECw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=uAiD7RAJueWlbRUtpvTiNLaNfkn8WgzU69242ryt9RI=; b=XuUg6zWgpCqh7esGH5BIZDR++xuJa/uD/dy18a5J7pbo55y2YvVcIRjXVwsFzHh+v7 9PwVqqBPlu/E2Ab2qVzry/D/OYTOLCCzszbAvdvJ9O+gvULVEp+wdaGbu8VCa7C3S/yW D71+E1oA3Iuf7Ppm+86d7WbEN9y5bSgVZUyvzQNykH6rahluyGDnLW8FKbrolFe4AA6o 7pGrPhnXOpKb0dyYInWnKBcK0lSxSjiCRbSKhNlkESwV7/fl4ZMtf0uk2ebvjiTG6Z2M UfBdTcY/qmiJJZunwTS/xjHWpRX5vX5/P9AUMqkqmkdVsqQai2+oQtJMTWdhsRe6ib1G hyeQ== X-Gm-Message-State: AOAM5316KH/Q9vYguWGBwQpmHVcOD8bpRiJM1aV3p7KVf7P119cQefeA WNWRqQQwNmXi1whFViSbRjzCkJ7GJKHxfaI+dLwpoA== X-Google-Smtp-Source: ABdhPJwAoRedCRnw2rNsZxc6jz127G8OJfR98SIRGa6XUxGX5TBU3SDS4jzbjTFqPlShBWgjaShBidEBXL72Z7h2W+U= X-Received: by 2002:a05:6102:3648:: with SMTP id s8mr59387vsu.42.1632681499036; Sun, 26 Sep 2021 11:38:19 -0700 (PDT) MIME-Version: 1.0 References: <20210922045636.25206-1-imp@bsdimp.com> <20210922045636.25206-9-imp@bsdimp.com> <4d7f5ce6-22c4-e9de-39c6-fb92d7096385@linaro.org> In-Reply-To: <4d7f5ce6-22c4-e9de-39c6-fb92d7096385@linaro.org> From: Warner Losh Date: Sun, 26 Sep 2021 12:38:08 -0600 Message-ID: Subject: Re: [PATCH v2 8/9] bsd-user/mmap.c: Implement MAP_EXCL, required by jemalloc in head To: Richard Henderson Content-Type: multipart/alternative; boundary="000000000000c820ce05ccea4819" Received-SPF: none client-ip=2607:f8b0:4864:20::e2f; envelope-from=wlosh@bsdimp.com; helo=mail-vs1-xe2f.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, HTML_MESSAGE=0.001, 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 , QEMU Developers , =?UTF-8?Q?Philippe_Mathieu=2DDaud=C3=A9?= Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" --000000000000c820ce05ccea4819 Content-Type: text/plain; charset="UTF-8" On Fri, Sep 24, 2021 at 6:00 AM Richard Henderson < richard.henderson@linaro.org> wrote: > On 9/21/21 9:56 PM, Warner Losh wrote: > > + /* Reject the mapping if any page within the range is mapped */ > > + if (flags & MAP_EXCL) { > > + for (addr = start; addr < end; addr++) { > > + if (page_get_flags(addr) != 0) > > + goto fail; > > + } > > + } > > How about > > if ((flags & MAP_EXCL) && > page_check_range(start, len, 0) < 0) { > goto fail; > } > > Hmm. This (and your page_get_flags check) could assert due to > out-of-range guest address. > You're currently attempting that, > > /* > * Test if requested memory area fits target address space > * It can fail only on 64-bit host with 32-bit target. > * On any other target/host host mmap() handles this error > correctly. > */ > #if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64 > if ((unsigned long)start + len - 1 > (abi_ulong) -1) { > errno = EINVAL; > goto fail; > } > #endif > > but the test isn't correct. Note that reserved_va may be applied to > 64-bit guests, and > certainly may be smaller than (abi_ulong)-1. > > You want guest_range_valid_untagged here. > Great! Thanks for the tip! Warner --000000000000c820ce05ccea4819 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=
On Fri, Sep 24, 2021 at 6:00 AM Richa= rd Henderson <richard.he= nderson@linaro.org> wrote:
On 9/21/21 9:56 PM, Warner Losh wrote:
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 /* Reject the mapping if any page within = the range is mapped */
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (flags & MAP_EXCL) {
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 for (addr =3D start; addr &= lt; end; addr++) {
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if (page_get_= flags(addr) !=3D 0)
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= goto fail;
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 }
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 }

How about

=C2=A0 =C2=A0 =C2=A0if ((flags & MAP_EXCL) &&
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0page_check_range(start, len, 0) < 0) {=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 goto fail;
=C2=A0 =C2=A0 =C2=A0}

Hmm.=C2=A0 This (and your page_get_flags check) could assert due to out-of-= range guest address.
=C2=A0 You're currently attempting that,

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0/*
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 * Test if requested memory area fits tar= get address space
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 * It can fail only on 64-bit host with 3= 2-bit target.
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 * On any other target/host host mmap() h= andles this error correctly.
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 */
#if TARGET_ABI_BITS =3D=3D 32 && HOST_LONG_BITS =3D=3D 64
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if ((unsigned long)start + len - 1 > (= abi_ulong) -1) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0errno =3D EINVAL;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0goto fail;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0}
#endif

but the test isn't correct.=C2=A0 Note that reserved_va may be applied = to 64-bit guests, and
certainly may be smaller than (abi_ulong)-1.

You want guest_range_valid_untagged here.

Great! Thanks for the tip!

Warner=C2=A0
--000000000000c820ce05ccea4819--