From nobody Sat May 18 09:01:21 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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 ARC-Seal: i=1; a=rsa-sha256; t=1691129350; cv=none; d=zohomail.com; s=zohoarc; b=JIQa6K5uhtPeV2KQ+WzYFRWJxwXyjqBA+QSrgJXt1tvpuKaw443FxDxyceAeBucAYXMHwYwNVjEXLEPiwUts8xtJKxYmfEbmSTek4vxx7TKhXg/Qav36Bg8Thy3ojrNpA+ta25WZAE2StoK9lzsLQJGRmdpb5ZK9QZhWvcb/yUI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1691129350; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject; bh=8Yzj+pV5jD5ZMwBzvzwTSptQ3FdtTNwwP/WIzcKIpLY=; b=AxcvXJfUaXwT+048t5HzYWhXG7/ARR0fF9sP7Id1iwqR0TwsiTj+C/JJB+Q6/UtCjt8J5kH5KBxJg5LyBeK0uNEnSYwYe8uzh5dDqcYPKiKhQ3y9xs/j3C9FNBmHL3rl6D5wYwNDW1KO+fjMRvBOpBSCv5aBfw8as9A9hZB09Bc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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 1691129350257313.2380482931642; Thu, 3 Aug 2023 23:09:10 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qRnzO-0008PM-S9; Fri, 04 Aug 2023 02:08:42 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qRnzN-0008PA-Jt for qemu-devel@nongnu.org; Fri, 04 Aug 2023 02:08:41 -0400 Received: from mail-pg1-x52f.google.com ([2607:f8b0:4864:20::52f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qRnzL-0008E0-RB for qemu-devel@nongnu.org; Fri, 04 Aug 2023 02:08:41 -0400 Received: by mail-pg1-x52f.google.com with SMTP id 41be03b00d2f7-56401f1da3dso959766a12.0 for ; Thu, 03 Aug 2023 23:08:39 -0700 (PDT) Received: from alarm.flets-east.jp ([2400:4050:a840:1e00:78d2:b862:10a7:d486]) by smtp.gmail.com with ESMTPSA id l12-20020a17090a660c00b002630c9d78aasm792843pjj.5.2023.08.03.23.08.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Aug 2023 23:08:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20221208.gappssmtp.com; s=20221208; t=1691129318; x=1691734118; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=8Yzj+pV5jD5ZMwBzvzwTSptQ3FdtTNwwP/WIzcKIpLY=; b=cqBH8u/0gx0RnJb1qDMKH+4exjody1rS3Re2wxVCJude5DSCtx+mfJ0fKEVbpYqPSA 6bVOIcGpIU6PBLStTHtFqwGF0IhZ6aiIvLiqrd+CUYrl6TA1GshyIFAH2Gke/7MAoO2p twOEU0TtUsbqJJTJAp4nsyayxjVz6VNP2McquiFukdd87TtwFA6OZhHvXxe6ksCCZeu9 q9/OFbKEtid/F6GicSK+uVeJyuhwtlSO9v21KplljGCACeLMkfdAdHe6VU4dMoWvJ6im Wp136QwdkNpOTh3pTsAY5qeYJqYX8skwLd5I0qFxl1aD/3Z+aRUaxLqZsDbvNAntymSK C/VA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691129318; x=1691734118; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=8Yzj+pV5jD5ZMwBzvzwTSptQ3FdtTNwwP/WIzcKIpLY=; b=EPd62oGd7/pnaiA4l+hY2w44j2h7TMP21hT8RnC7/NOkzsAyzH06ljqNKfcPzp4q2I cKtlQGps4vCeUUZnjxykvqzgaK611brfZ2stjWMVWaqXTL8vyjFhl0T+4086IoiTOuhs vtnvGdr5tTw8mo4J+iM6QoEutVsO9gcmsgSYvdcmFPCzZyPcG7hY4cKTuTxBNEMnDCV0 Et+cWjTORirMPR6FvZb8teZ4VyxjEPdorpohkWXaQiyyfETXVkCM/B+iEWhmAN+XUdZe 4Srshw3AucDIz08FVMvGTXfjJmdQpatBH9YsGudTypXG3UktwsBBZNyQUOaXclbjkgQ3 aXmA== X-Gm-Message-State: AOJu0Yz6J5KCfadX78svTPcgtU4BZ4p+B5rqohOJp1BrUiBljwqpdmx9 lezjhJBi756MGy3LBZFA84yukQ== X-Google-Smtp-Source: AGHT+IHDIwLjBSKRS3HFTk8B+dGHQ4/8AHn1AZHoHC4fQeRBBCImENtBA5p9mawgsVpbxDzocC29NA== X-Received: by 2002:a17:90b:3805:b0:268:d862:dda8 with SMTP id mq5-20020a17090b380500b00268d862dda8mr686780pjb.0.1691129318339; Thu, 03 Aug 2023 23:08:38 -0700 (PDT) From: Akihiko Odaki To: Cc: Richard Henderson , qemu-devel@nongnu.org, joel@jms.id.au, laurent@vivier.eu, deller@gmx.de, Akihiko Odaki Subject: [PATCH] linux-user: Undo incomplete mmap Date: Fri, 4 Aug 2023 15:08:27 +0900 Message-ID: <20230804060829.37043-1-akihiko.odaki@daynix.com> X-Mailer: git-send-email 2.41.0 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::52f; envelope-from=akihiko.odaki@daynix.com; helo=mail-pg1-x52f.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, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @daynix-com.20221208.gappssmtp.com) X-ZM-MESSAGEID: 1691129351390100001 Content-Type: text/plain; charset="utf-8" When the host page size is greater than the target page size and MAP_FIXED or MAP_FIXED_NOREPLACE is requested, mmap will be done for three parts: start, middle, and end. If a later part of mmap fail, mmap done in the earlier parts must be reverted. Fixes: 54936004fd ("mmap emulation") Signed-off-by: Akihiko Odaki --- Based-on: <20230804014517.6361-1-richard.henderson@linaro.org> ("[PATCH for-8.1 v8 00/17] linux-user: brk fixes") linux-user/mmap.c | 65 +++++++++++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 25 deletions(-) diff --git a/linux-user/mmap.c b/linux-user/mmap.c index 949c4090f3..0a4ce79ce5 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -224,13 +224,15 @@ int target_mprotect(abi_ulong start, abi_ulong len, i= nt target_prot) =20 /* map an incomplete host page */ static bool mmap_frag(abi_ulong real_start, abi_ulong start, abi_ulong las= t, - int prot, int flags, int fd, off_t offset) + int prot, int flags, int fd, off_t offset, bool *map= ped) { abi_ulong real_last; void *host_start; int prot_old, prot_new; int host_prot_old, host_prot_new; =20 + *mapped =3D false; + if (!(flags & MAP_ANONYMOUS) && (flags & MAP_TYPE) =3D=3D MAP_SHARED && (prot & PROT_WRITE)) { @@ -271,6 +273,7 @@ static bool mmap_frag(abi_ulong real_start, abi_ulong s= tart, abi_ulong last, return false; } prot_old =3D prot; + *mapped =3D true; } prot_new =3D prot | prot_old; =20 @@ -448,7 +451,7 @@ abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size= , abi_ulong align) abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot, int flags, int fd, off_t offset) { - abi_ulong ret, last, real_start, real_last, retaddr, host_len; + abi_ulong ret, last, real_start, retaddr, host_len; abi_ulong passthrough_start =3D -1, passthrough_last =3D 0; int page_flags; off_t host_offset; @@ -577,12 +580,16 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, = int target_prot, passthrough_start =3D start; passthrough_last =3D last; } else { + abi_ulong middle_start =3D HOST_PAGE_ALIGN(start); + abi_ulong middle_last =3D ((start + len) & qemu_host_page_mask) - = 1; + abi_ulong mapped_len =3D 0; + bool mapped; + if (start & ~TARGET_PAGE_MASK) { errno =3D EINVAL; goto fail; } last =3D start + len - 1; - real_last =3D HOST_PAGE_ALIGN(last) - 1; =20 /* * Test if requested memory area fits target address space @@ -649,35 +656,26 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, = int target_prot, } =20 /* handle the start of the mapping */ - if (start > real_start) { - if (real_last =3D=3D real_start + qemu_host_page_size - 1) { + if (start < middle_start) { + if (last < middle_start) { /* one single host page */ if (!mmap_frag(real_start, start, last, - target_prot, flags, fd, offset)) { + target_prot, flags, fd, offset, &mapped)) { goto fail; } goto the_end1; } - if (!mmap_frag(real_start, start, - real_start + qemu_host_page_size - 1, - target_prot, flags, fd, offset)) { + if (!mmap_frag(real_start, start, middle_start - 1, + target_prot, flags, fd, offset, &mapped)) { goto fail; } - real_start +=3D qemu_host_page_size; - } - /* handle the end of the mapping */ - if (last < real_last) { - abi_ulong real_page =3D real_last - qemu_host_page_size + 1; - if (!mmap_frag(real_page, real_page, last, - target_prot, flags, fd, - offset + real_page - start)) { - goto fail; + if (mapped) { + mapped_len =3D qemu_host_page_size; } - real_last -=3D qemu_host_page_size; } =20 /* map the middle (easier) */ - if (real_start < real_last) { + if (middle_start < middle_last) { void *p, *want_p; off_t offset1; size_t len1; @@ -685,10 +683,10 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, = int target_prot, if (flags & MAP_ANONYMOUS) { offset1 =3D 0; } else { - offset1 =3D offset + real_start - start; + offset1 =3D offset + middle_start - start; } - len1 =3D real_last - real_start + 1; - want_p =3D g2h_untagged(real_start); + len1 =3D middle_last - middle_start + 1; + want_p =3D g2h_untagged(middle_start); =20 p =3D mmap(want_p, len1, target_to_host_prot(target_prot), flags, fd, offset1); @@ -697,10 +695,27 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, = int target_prot, munmap(p, len1); errno =3D EEXIST; } + if (mapped_len) { + munmap(g2h_untagged(middle_start - mapped_len), mapped= _len); + } + goto fail; + } + mapped_len +=3D len1; + passthrough_start =3D middle_start; + passthrough_last =3D middle_last; + } + + /* handle the end of the mapping */ + if (last > middle_last) { + abi_ulong real_page =3D middle_last + 1; + if (!mmap_frag(real_page, real_page, last, + target_prot, flags, fd, + offset + real_page - start, &mapped)) { + if (mapped_len) { + munmap(g2h_untagged(real_page - mapped_len), mapped_le= n); + } goto fail; } - passthrough_start =3D real_start; - passthrough_last =3D real_last; } } the_end1: --=20 2.41.0