From nobody Sun May 19 03:12:45 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=1690960774; cv=none; d=zohomail.com; s=zohoarc; b=kxXzxyehpaWd9yB9ES0qEyjP/Eu3oGOlKdZQaS2tRimdABcCXXExa9AUTEBmwFzNg7l8NIqFT8xQ8X/zYaWY4Gw7C9LDh5KRCzQvfWVxBKjSLjykZ1QaHY+5HmDYVBxTqxvXm+w0nHq3IjO6SASLMVnQDq9NdsL5c0jxwRJ35QA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690960774; 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; bh=FwhyQWrYrTRaPWXxrhNy1oYg4FzuyUAbcs+xMqgoAhw=; b=KWDaaIn3FyxRm3J5mhHh8c1Jvxt2YtUq82bGywrzjQm/TRp6/8Gaa48E62c9m++9su+VOM8F9fJgMARP101h++vwACKhcJ1JYt1SWzYF+cv/IRSfm3hBANzEMPJ0gAZLezZCTl33+V1CLtdAs2kZJPiuzG4EjIk2EF088neKlwc= 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 1690960774609231.03365730725693; Wed, 2 Aug 2023 00:19:34 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qR67W-0007Yo-UN; Wed, 02 Aug 2023 03:18:11 -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 1qR67T-0007YJ-2l for qemu-devel@nongnu.org; Wed, 02 Aug 2023 03:18:07 -0400 Received: from mail-oi1-x22c.google.com ([2607:f8b0:4864:20::22c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qR67Q-0002nP-9k for qemu-devel@nongnu.org; Wed, 02 Aug 2023 03:18:06 -0400 Received: by mail-oi1-x22c.google.com with SMTP id 5614622812f47-3a734b8a27fso1774811b6e.1 for ; Wed, 02 Aug 2023 00:18:03 -0700 (PDT) Received: from alarm.flets-east.jp ([2400:4050:a840:1e00:78d2:b862:10a7:d486]) by smtp.gmail.com with ESMTPSA id x12-20020a63b20c000000b00528da88275bsm11004440pge.47.2023.08.02.00.18.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Aug 2023 00:18:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20221208.gappssmtp.com; s=20221208; t=1690960683; x=1691565483; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=FwhyQWrYrTRaPWXxrhNy1oYg4FzuyUAbcs+xMqgoAhw=; b=KfnnQwMxvTspADCyE+ZezwsOOaTl5kW1LuPm9RS5vCP9bxidqnYboiPdQJC9B+cFgc fn7qXZ+U0rM9cZOcLORDMXskYo8VfLvkSXOJysFCodNYaAbKFKffi/FCFdAhChpi19H0 ArRrZpYnwCGsMJZAB7ckkQ+RbpzrBcXEBA+TjSyqfRwEpIeZne+obh+jj8yTyUX1yu4d LrBOra0Cx7eGnfhvnsxtuzAtjZ4mAESbwv56r6y8j4gM8ca1BjosemqhAeK5HAL3p8/c 5/hXXq5Kl3Ij1xjN2FBr1bGdHkbFSsg80NaxEheiQnsr/KTmqROb7vyqzjvVj1vZcNzb wbNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690960683; x=1691565483; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=FwhyQWrYrTRaPWXxrhNy1oYg4FzuyUAbcs+xMqgoAhw=; b=O9cIP2EI1q+9Q7KW3VTxda6w0T0aERk/68ci6JVixusJkUOkfMxQ+eSO+tAZZhfVfg 9J2410A5d7zVmTY8od2SbrVJ1kkTNQlAxfxwR0hb51XWe9QP0DddNPOp87wrSxpr/aGF vOgPDiMU+tieGWGOXlHO7gBvgGlah5lsBofRGZfizD/IqWHFdTxUcBKo7g3LvMkcbIGa nZTgYYTaLb8yaTwu2dkpRC7tLPTykI9VAscYGOBsGlR9asMXABBrB/2dYp1D9iJINRZA Bth0ntz/Fz0COxC8HLmhemhZOzx2Ll4xwDrwgRY39Lx6R0UE90B6Xpinx1uteqtja7tm hUYQ== X-Gm-Message-State: ABy/qLakyEW6jpEEFNI/Xu/41PZCacLQuZeQbvsEHS07mIDUtlSmtUHz pI2Xa02A6Uzlx3GyAQ7JDlyJ4g== X-Google-Smtp-Source: APBJJlGYqi/vWXoBh9ym0WilBKzP8JSMbNXV1XyHsAA2ucYZJbpLaNYM0evT77KPZb9C/j8i6CXrCw== X-Received: by 2002:a05:6808:1708:b0:3a4:8a41:c69c with SMTP id bc8-20020a056808170800b003a48a41c69cmr16081119oib.13.1690960682931; Wed, 02 Aug 2023 00:18:02 -0700 (PDT) From: Akihiko Odaki To: Cc: Helge Deller , Joel Stanley , Michael Tokarev , qemu-devel@nongnu.org, Laurent Vivier , Peter Maydell , Richard Henderson , Akihiko Odaki Subject: [PATCH v2 1/6] linux-user: Unset MAP_FIXED_NOREPLACE for host Date: Wed, 2 Aug 2023 16:17:47 +0900 Message-ID: <20230802071754.14876-2-akihiko.odaki@daynix.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230802071754.14876-1-akihiko.odaki@daynix.com> References: <20230802071754.14876-1-akihiko.odaki@daynix.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::22c; envelope-from=akihiko.odaki@daynix.com; helo=mail-oi1-x22c.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: 1690960775487100003 Content-Type: text/plain; charset="utf-8" Passing MAP_FIXED_NOREPLACE to host will fail if the virtual address space is reserved with mmap. Replace it with MAP_FIXED in such a case. Signed-off-by: Akihiko Odaki --- linux-user/mmap.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/linux-user/mmap.c b/linux-user/mmap.c index a5dfb56545..14cd1b9feb 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -610,6 +610,10 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, i= nt target_prot, goto fail; } =20 + if (reserved_va) { + flags =3D (flags & ~MAP_FIXED_NOREPLACE) | MAP_FIXED; + } + /* * worst case: we cannot map the file because the offset is not * aligned, so we read it --=20 2.41.0 From nobody Sun May 19 03:12:45 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=1690960754; cv=none; d=zohomail.com; s=zohoarc; b=U2a2uKaJm5L/oLV9Tsd20mdzo/0hntKbiglneL0T6URqle9QMHQBauwImws7ILwYQbhi6YjeB6b9K8BPmK2ZOc2V0m0pMu8miu9M0YAdJrk0NEDsYUzaatqQ6I8+62kjisPkbF7rNJ8u/6h8GAY5df7yqchi1RFKMsV6NZ5kf0k= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690960754; h=Content-Type: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; bh=rQcw8k68MOdrh2WluUA73b05P6tF+ayKNuHwzr1IbSc=; b=WYUGYR+QUW1K1P+3v3vquvvBtAymXkfNn3iTZEeY5vsyaNIazCnQnkGEkznHfJ8VNHLn33tzcJ2WbRbjh7tKqNcG7BferYZJZwgdgDcQonuIW4qzevVfNVdzKZ1qUQ0wNvHB425my5QIM334kfBlxusmOOeTKSKH8uIiVgImptA= 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 1690960754900498.6529824776288; Wed, 2 Aug 2023 00:19:14 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qR67W-0007Yk-Tm; Wed, 02 Aug 2023 03:18:10 -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 1qR67U-0007YO-2p for qemu-devel@nongnu.org; Wed, 02 Aug 2023 03:18:08 -0400 Received: from mail-ot1-x32c.google.com ([2607:f8b0:4864:20::32c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qR67S-0002no-EP for qemu-devel@nongnu.org; Wed, 02 Aug 2023 03:18:07 -0400 Received: by mail-ot1-x32c.google.com with SMTP id 46e09a7af769-6bca38a6618so2596750a34.3 for ; Wed, 02 Aug 2023 00:18:06 -0700 (PDT) Received: from alarm.flets-east.jp ([2400:4050:a840:1e00:78d2:b862:10a7:d486]) by smtp.gmail.com with ESMTPSA id x12-20020a63b20c000000b00528da88275bsm11004440pge.47.2023.08.02.00.18.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Aug 2023 00:18:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20221208.gappssmtp.com; s=20221208; t=1690960685; x=1691565485; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=rQcw8k68MOdrh2WluUA73b05P6tF+ayKNuHwzr1IbSc=; b=NyX1duMDzl0omaOufduLTSUfF+4Gnsgb/xeZVhod9YNLFlGB3qIdPAJQj5PWZ4CaD+ Q2tYH74bV/NWsnslvxkDxeavk88McFRKe/vJrCJT47LCt/DyPS9hBcKjRbrc9q48iOUy df+OU1FZwHBObL3c+w1lE3RRgldilU466a6+vJ+g7OAnglay9Y6OGUMv94g2zK3RXV8s A/Eo8ONF0F44GdfI+CQcozbzPz0CxPf5Sk/i1kCVaOAcnumfNQQU5kLW1HFfcXY6WGCD 977fj3R6+K8D7JOZMQtr24V67Gg7jagRdrXyoTsJQEJaeti1hb3/dLJtEnphGai5C7de Q5AA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690960685; x=1691565485; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=rQcw8k68MOdrh2WluUA73b05P6tF+ayKNuHwzr1IbSc=; b=Xhrekd89POs4TqC99NcZNuNbT7wxCXvg61z+NICK4wJbfiK25WMh4F/6gsvEnRkgg0 D69XEm+ZfJwqwN33owIEQzD8UoofvA4rPXSQOLvIo1h6JicE0geC4XwRajTDhuvrHuj+ bukkjiarygR/W0RU1JTtOelj63VolcocgdkL/otpIte6PiJMDknzkBqEke9meXN9juWW lBj1cZRit2rvyyNEFhQCsyhU6liGsAKIlkXZUGJ5IpZfnPc8ofKjwIbVVOpWsG9tNZbf x3VGpB5PSK0ccQNfpBmYgMpVLctd1l9CENnEAlgMLco11gJzAoKPgr3wgrxQWJLxysNl e8+w== X-Gm-Message-State: ABy/qLYhRq53VKKXrxV3gvC/M/tJAadsSrppnZgt+tIIrWj9dKZK0keA oAVkGzcwjbk6Kq0AIbmrcOhbFQ== X-Google-Smtp-Source: APBJJlHkstBSaPN/+i6XwILINkNFpZNpj9w5b8Se37w8YkvY7NqLIEiSmkFYizhOBAl2d4xQTR5ujw== X-Received: by 2002:a05:6358:7250:b0:139:cd0a:989a with SMTP id i16-20020a056358725000b00139cd0a989amr6779345rwa.16.1690960685308; Wed, 02 Aug 2023 00:18:05 -0700 (PDT) From: Akihiko Odaki To: Cc: Helge Deller , Joel Stanley , Michael Tokarev , qemu-devel@nongnu.org, Laurent Vivier , Peter Maydell , Richard Henderson , Akihiko Odaki Subject: [PATCH v2 2/6] linux-user: Fix MAP_FIXED_NOREPLACE on old kernels Date: Wed, 2 Aug 2023 16:17:48 +0900 Message-ID: <20230802071754.14876-3-akihiko.odaki@daynix.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230802071754.14876-1-akihiko.odaki@daynix.com> References: <20230802071754.14876-1-akihiko.odaki@daynix.com> 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: none client-ip=2607:f8b0:4864:20::32c; envelope-from=akihiko.odaki@daynix.com; helo=mail-ot1-x32c.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: 1690960756821100003 The man page states: > Note that older kernels which do not recognize the MAP_FIXED_NOREPLACE > flag will typically (upon detecting a collision with a preexisting > mapping) fall back to a =E2=80=9Cnon-MAP_FIXED=E2=80=9D type of behavior:= they will > return an address that is different from the requested address. > Therefore, backward-compatible software should check the returned > address against the requested address. https://man7.org/linux/man-pages/man2/mmap.2.html Signed-off-by: Akihiko Odaki --- linux-user/mmap.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/linux-user/mmap.c b/linux-user/mmap.c index 14cd1b9feb..1a8477dbb2 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -263,7 +263,11 @@ static bool mmap_frag(abi_ulong real_start, abi_ulong = start, abi_ulong last, void *p =3D mmap(host_start, qemu_host_page_size, target_to_host_prot(prot), flags | MAP_ANONYMOUS, -1, 0); - if (p =3D=3D MAP_FAILED) { + if (p !=3D host_start) { + if (p !=3D MAP_FAILED) { + munmap(p, qemu_host_page_size); + errno =3D EEXIST; + } return false; } prot_old =3D prot; @@ -686,7 +690,11 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, i= nt target_prot, } p =3D mmap(g2h_untagged(real_start), real_last - real_start + = 1, target_to_host_prot(target_prot), flags, fd, offset1); - if (p =3D=3D MAP_FAILED) { + if (p !=3D g2h_untagged(real_start)) { + if (p !=3D MAP_FAILED) { + munmap(p, real_last - real_start + 1); + errno =3D EEXIST; + } goto fail; } passthrough_start =3D real_start; --=20 2.41.0 From nobody Sun May 19 03:12:45 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=1690960739; cv=none; d=zohomail.com; s=zohoarc; b=kYpPfOELfzt6XJgGSMWSQuxiFAQAN9f87E1g39Nx/bYgprox7tcDx1pD/4aR0HhNau9Uw5gqau/yS6RiW1hwe7xglgT1EdBAQ01WX/ZredfsLXBZtHXr2e0BBTLtlXwYTsZ94KIi55O7HJPDEbQLBHvwd2I0wKI9SSokPs6AY+k= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690960739; 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; bh=lE8/yJChDEcdbTP5nW5GoFaGsy88Nd8G8Gi+fYS3eUs=; b=DkKBNVayRv+D5K9aRjhluCXOOZsxcQnvVjzIR+j90GWI24xvkmEEenSge+N2pvL7PMLCFjHBkNNeBF+8jAdiA9JnkJSvvHfrrseN32UZEis4zGQiRx8LwEx3IJwqqF69D3rQg03xfkRX8zyZWhQY0zM5e5HQ/am1EZu+AGr9zGU= 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 1690960739759618.7109142065776; Wed, 2 Aug 2023 00:18:59 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qR67g-0007aE-8x; Wed, 02 Aug 2023 03:18:20 -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 1qR67W-0007Yq-Sf for qemu-devel@nongnu.org; Wed, 02 Aug 2023 03:18:10 -0400 Received: from mail-ot1-x333.google.com ([2607:f8b0:4864:20::333]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qR67V-0002oB-6U for qemu-devel@nongnu.org; Wed, 02 Aug 2023 03:18:10 -0400 Received: by mail-ot1-x333.google.com with SMTP id 46e09a7af769-6bca7d82d54so2180491a34.3 for ; Wed, 02 Aug 2023 00:18:08 -0700 (PDT) Received: from alarm.flets-east.jp ([2400:4050:a840:1e00:78d2:b862:10a7:d486]) by smtp.gmail.com with ESMTPSA id x12-20020a63b20c000000b00528da88275bsm11004440pge.47.2023.08.02.00.18.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Aug 2023 00:18:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20221208.gappssmtp.com; s=20221208; t=1690960688; x=1691565488; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=lE8/yJChDEcdbTP5nW5GoFaGsy88Nd8G8Gi+fYS3eUs=; b=V/rYY/BUEya6d7KBLM/cKCvCh8iD6jaHYfROyxZDYcEM/gAgjQ0wq0TK+8r3XbPYg9 CVeSsNIjeHwef/2YdkFxPOomb81pY5fYK9zQV81yL0bm8NbvTMxxpgpf6Hp+wJ7L6dGK 49grsJvU2x/AHAtDrPMqtQ4wYgKlLwRPoY/wQrPZqIzRIM86nKZpFZ8E5K94pw/JP0Y0 L0qH6as7GZHTpdrrrINI+XxOGQRFCaI2gducNVi3immUHfK3cc/VMjwUp5LfmudXW+ta w9XgDENxRP86aceJuH7X43wmcbpPZuFWnoMaKsCOwtSSKaNO9qx+lGHjaUFwVNpUrtFe InVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690960688; x=1691565488; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lE8/yJChDEcdbTP5nW5GoFaGsy88Nd8G8Gi+fYS3eUs=; b=D8j8+8QBuZY38XMNGevrwJzZVyUh2bSq182ce2ETOxHXma418k1UB8WLZcdFRtpXJt FFxHWn5p1XL22YeIUkTb9LGvXAPAl+pF1aDs5Kw45QRXAAuQTBVhv7IzO89+wX86w0I7 zq4ZrXMYlaJaFhnslwdHz2H513zvIhyZXlO6WwNlDdZlFtaGvBBMFZxA/Zm5E92vyJtI DrzA7id9D+ErgQTHtjaWTCAxF1EnoYUKtJuGyehoWTtK0Jevcz+HsoDe9MtyQ1v5O/r+ HyAPzjYEo4sz4FDjJWkGzKGZ3i/aFMw9lQziayZv15vVPwZ0JLOpvJvDyTH7KeIqNv4T qnGQ== X-Gm-Message-State: ABy/qLZMf9R68pLiFE4AnpUAlgx9Kq1Ja2CTBA8EZwjzQWcjafZlMrAe qPL2iwHC3YBA/BC9GOj2z3fJLA== X-Google-Smtp-Source: APBJJlEiTKLDWYm+BsTaKnoVz/XY32BHoy6lD5DjrZqGKt/kSG/fieD0hyyclA/OLLogLXMfdU+o6Q== X-Received: by 2002:a05:6358:6f8a:b0:135:85ec:a080 with SMTP id s10-20020a0563586f8a00b0013585eca080mr4924403rwn.32.1690960687823; Wed, 02 Aug 2023 00:18:07 -0700 (PDT) From: Akihiko Odaki To: Cc: Helge Deller , Joel Stanley , Michael Tokarev , qemu-devel@nongnu.org, Laurent Vivier , Peter Maydell , Richard Henderson , Akihiko Odaki Subject: [PATCH v2 3/6] linux-user: Do not call get_errno() in do_brk() Date: Wed, 2 Aug 2023 16:17:49 +0900 Message-ID: <20230802071754.14876-4-akihiko.odaki@daynix.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230802071754.14876-1-akihiko.odaki@daynix.com> References: <20230802071754.14876-1-akihiko.odaki@daynix.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::333; envelope-from=akihiko.odaki@daynix.com; helo=mail-ot1-x333.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: 1690960742103100001 Content-Type: text/plain; charset="utf-8" Later the returned value is compared with -1, and negated errno is not expected. Fixes: 00faf08c95 ("linux-user: Don't use MAP_FIXED in do_brk()") Reviewed-by: Helge Deller Signed-off-by: Akihiko Odaki --- linux-user/syscall.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 95727a816a..b9d2ec02f9 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -862,9 +862,9 @@ abi_long do_brk(abi_ulong brk_val) */ if (new_host_brk_page > brk_page) { new_alloc_size =3D new_host_brk_page - brk_page; - mapped_addr =3D get_errno(target_mmap(brk_page, new_alloc_size, - PROT_READ|PROT_WRITE, - MAP_ANON|MAP_PRIVATE, 0, 0)); + mapped_addr =3D target_mmap(brk_page, new_alloc_size, + PROT_READ|PROT_WRITE, + MAP_ANON|MAP_PRIVATE, 0, 0); } else { new_alloc_size =3D 0; mapped_addr =3D brk_page; --=20 2.41.0 From nobody Sun May 19 03:12:45 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=1690960741; cv=none; d=zohomail.com; s=zohoarc; b=bmdquAE6LwBCdwwbldWCjrt0hSUmUkQb9QTu2WP1t5NmHnwEHXdsthBcYa8PhXRbsA7ypel2lCe3VxPVXfnZnIZs9wCL4RFdRb2jcci/QbC7zNKf6BHVCtKS1Iy5qPjf9WmsKIexIwR6+866Fs5GduTs+keeovsN2wjKkZ2iBO4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690960741; 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; bh=YOp4AdoD8Bkg7odyELEU2opCXFUALl4I3whyZmG1z+k=; b=RwY/3iSOBqNMEsutP5TIBc48QfOmk4wUSo45W5KmNfq+kCVDkg+oVZh8DoA9C9wyWEAtCrEIe6Ix3iXU3CbopGzF+9CZqh7OCP2U4vR6cBnOTtWPzeEt1Jnb1RziHgtqkBPNuwf/aJWlRwzf4fnziAwNAn+jYculcTHLmv0FmJ8= 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 169096074147463.14488045985263; Wed, 2 Aug 2023 00:19:01 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qR67a-0007a8-5l; Wed, 02 Aug 2023 03:18:14 -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 1qR67Y-0007Zd-P5 for qemu-devel@nongnu.org; Wed, 02 Aug 2023 03:18:12 -0400 Received: from mail-ot1-x32d.google.com ([2607:f8b0:4864:20::32d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qR67X-0002oM-9n for qemu-devel@nongnu.org; Wed, 02 Aug 2023 03:18:12 -0400 Received: by mail-ot1-x32d.google.com with SMTP id 46e09a7af769-6bb1409ebcfso4082824a34.2 for ; Wed, 02 Aug 2023 00:18:10 -0700 (PDT) Received: from alarm.flets-east.jp ([2400:4050:a840:1e00:78d2:b862:10a7:d486]) by smtp.gmail.com with ESMTPSA id x12-20020a63b20c000000b00528da88275bsm11004440pge.47.2023.08.02.00.18.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Aug 2023 00:18:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20221208.gappssmtp.com; s=20221208; t=1690960690; x=1691565490; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=YOp4AdoD8Bkg7odyELEU2opCXFUALl4I3whyZmG1z+k=; b=0sMpcNzaSHOgQ5HMuWBKh9HduZNQoRAmXe802GAk2hMFLD2Rx6UjYDI2pAiDpIhYB6 SNgPAjkgTa/KPYMiSj3zf3tHNZv2QCrSbiHL88Q+jVLLpX4amIKwufsw8+uEDa1w9YEr Gbt4WAA1xd8xefMzXRBVyUy0C5yhUlpn4tKZRvMWmFIuaWcepFn/pr9Vck3bLYIkdIg2 ain3/q2wQ5Tz+BWI3FG3TOiBOMfO9RMvW2QJhmevxYZAIL07lDCwhJgZmWoSq7szgsjs KQYEvT5C/F3nchP67p05EGAiTxKaSL1fw982PBsJQEnlqnl9zHoVUAeICplt4V29np/H qNbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690960690; x=1691565490; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=YOp4AdoD8Bkg7odyELEU2opCXFUALl4I3whyZmG1z+k=; b=lTDlLyxuIF4Bmow3iKjzhGnOqQRR4/de9hkDRMpbE8MD0XfynCiRvRP3NWVBdRY2tk 0M78p8ROsFvBnUPqWFfk+U6YZ+NMWF3Xv582xfBTt7pdlMM917EvXlqm5tuK6sXoh26F QEtsvjPkaJTHTkffH4J11mQFOB0ncF+ZuJV8F4ox7DYCz3lGfTTZhUv2RS2vI3ZFF25z rI7pHDFepHVH1pGV4y12U45p2QNlsedl8D2tSp2MECYapOfxIihsJemyGPBxZinph83T W5h5I9N7Fle4X/N+LotD+JSaMFA7Hnv48vg6Hczy5LuCdaS4B/U5fepqpRx2WsFJEvu6 ELvw== X-Gm-Message-State: ABy/qLYyOKzE96z5+yyxZ6q9nI+VjNrps5/9DQ46SlKydqL2uLrxy3jv FOW3kYi+Coi8z5DOCNW/8bqhhQ== X-Google-Smtp-Source: APBJJlHKGmLYH6bUvPiYfM56nzbTdx1EnEwWC6CON+dMO63RCq/GWgcECOZggdneQ8qXHoR6d7L64g== X-Received: by 2002:a05:6830:110f:b0:6b9:9bcd:32fe with SMTP id w15-20020a056830110f00b006b99bcd32femr12989622otq.17.1690960690220; Wed, 02 Aug 2023 00:18:10 -0700 (PDT) From: Akihiko Odaki To: Cc: Helge Deller , Joel Stanley , Michael Tokarev , qemu-devel@nongnu.org, Laurent Vivier , Peter Maydell , Richard Henderson , Akihiko Odaki Subject: [PATCH v2 4/6] linux-user: Use MAP_FIXED_NOREPLACE for do_brk() Date: Wed, 2 Aug 2023 16:17:50 +0900 Message-ID: <20230802071754.14876-5-akihiko.odaki@daynix.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230802071754.14876-1-akihiko.odaki@daynix.com> References: <20230802071754.14876-1-akihiko.odaki@daynix.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::32d; envelope-from=akihiko.odaki@daynix.com; helo=mail-ot1-x32d.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: 1690960742385100005 Content-Type: text/plain; charset="utf-8" MAP_FIXED_NOREPLACE can ensure the mapped address is fixed without concerning that the new mapping overwrites something else. Signed-off-by: Akihiko Odaki --- linux-user/syscall.c | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/linux-user/syscall.c b/linux-user/syscall.c index b9d2ec02f9..ac429a185a 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -854,17 +854,12 @@ abi_long do_brk(abi_ulong brk_val) return target_brk; } =20 - /* We need to allocate more memory after the brk... Note that - * we don't use MAP_FIXED because that will map over the top of - * any existing mapping (like the one with the host libc or qemu - * itself); instead we treat "mapped but at wrong address" as - * a failure and unmap again. - */ if (new_host_brk_page > brk_page) { new_alloc_size =3D new_host_brk_page - brk_page; mapped_addr =3D target_mmap(brk_page, new_alloc_size, - PROT_READ|PROT_WRITE, - MAP_ANON|MAP_PRIVATE, 0, 0); + PROT_READ | PROT_WRITE, + MAP_FIXED_NOREPLACE | MAP_ANON | MAP_PRI= VATE, + 0, 0); } else { new_alloc_size =3D 0; mapped_addr =3D brk_page; @@ -883,12 +878,6 @@ abi_long do_brk(abi_ulong brk_val) target_brk =3D brk_val; brk_page =3D new_host_brk_page; return target_brk; - } else if (mapped_addr !=3D -1) { - /* Mapped but at wrong address, meaning there wasn't actually - * enough space for this brk. - */ - target_munmap(mapped_addr, new_alloc_size); - mapped_addr =3D -1; } =20 #if defined(TARGET_ALPHA) --=20 2.41.0 From nobody Sun May 19 03:12:45 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=1690960745; cv=none; d=zohomail.com; s=zohoarc; b=c/bFyAhRgRwV3pEi9D9M9mLbdYPVvTE2q+jp+kb5HdM1Rkh+Iha18c9xFApGhk3Z6JxN7H2S/BwY7wyUvRasZK5RzXAo8DQWL37C5ZACQjTYB/PVSHm3IIKV6aeG0TE67TPt6YwAMCf1rTcDfFkW8vV5Qezg4f8MLTFjdC4ZnqE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690960745; 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; bh=VXFcqmHho+ugFR7XQfPhKsdc5kffqTAo6APhg2JvIkQ=; b=VwVmJIE4d0aLadK1GTS3UOLKKDzDuxs+mKymx5sOnHxhPoPUAyB7uL+QXKYKefwPRa6+/2/VKT2shX+RMNxGrMnV4knjWJb45LGK/aCm/hFCA4iyN0A4NRcj7Jr+aApMm/w1MQALM69HREwGSGvSFNwiNeYpUPqTKIARS/SMw70= 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 1690960745755932.5740724361668; Wed, 2 Aug 2023 00:19:05 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qR67s-0007bk-HB; Wed, 02 Aug 2023 03:18:32 -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 1qR67b-0007aA-Nb for qemu-devel@nongnu.org; Wed, 02 Aug 2023 03:18:20 -0400 Received: from mail-oo1-xc33.google.com ([2607:f8b0:4864:20::c33]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qR67a-0002oh-7u for qemu-devel@nongnu.org; Wed, 02 Aug 2023 03:18:15 -0400 Received: by mail-oo1-xc33.google.com with SMTP id 006d021491bc7-56661fe27cbso4395284eaf.3 for ; Wed, 02 Aug 2023 00:18:13 -0700 (PDT) Received: from alarm.flets-east.jp ([2400:4050:a840:1e00:78d2:b862:10a7:d486]) by smtp.gmail.com with ESMTPSA id x12-20020a63b20c000000b00528da88275bsm11004440pge.47.2023.08.02.00.18.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Aug 2023 00:18:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20221208.gappssmtp.com; s=20221208; t=1690960692; x=1691565492; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=VXFcqmHho+ugFR7XQfPhKsdc5kffqTAo6APhg2JvIkQ=; b=xTH2eYJfTZyhPX7K+Q4ZqSNnZd3+3Dp9UXy6tpuh4TzzQJUCnAadV20dajBoGcZfnc sZEyNWsoyzNM12ROgYmunVrpN/u5TZ5ECssm11xE9gTWoCoxEHqBkohWh/7Cs0H+6AMA ZC6crjOMotqnWiaCSe6JQabGW04gg9i5ArRrC9YeS6/lCvSOe5ku4P8tDGJ3PFKd4tMH 5xPFVwnUiIJwj8+f4/bCyfWPv6oruJEHAosz7HYe3UjyREHWL4N+sjjRBKtQP5v3ynaY oKCHoNhTtJV4oCznfvhXrn9vl4egYl1cEc+76KzSqLjlaHAF5obvQ/PYUYqg9buwh6Gt C4DQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690960692; x=1691565492; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VXFcqmHho+ugFR7XQfPhKsdc5kffqTAo6APhg2JvIkQ=; b=cg/vmLWqvROjhOGw8CthSiBC9la3fV+amrPUMruiR6iHkrn7BpPrf7NYvzZ2abdRpj w+9pGeCGORdOub2PCbOLzkvZL1qYXFSo+y5Y3xTcBnytkD9EqnKyFYaN2bvnaywvxfuO n8Z6NJm3yu437WKnvlyte2PLffV9LlWlixu3D3I+Q0pYzIx4hyWMgagebNrAS+Dv9tyW r2Cwo8WpQxm31TjrpLjlDvr1CG/lLdWwLHbV7Zchp9ibjOGUBwIgm4GRLYrSg49MMiU/ BRKMjKXHAWaidPP5/1gZeHRe39/Qcymwhlhy92op9vFughWZ0xl70a1IJ1P8NfN8XxBf ooeQ== X-Gm-Message-State: ABy/qLZAC/FwMv9vn6dUh0TdJVbozErMKKsRL7OX+KnNhNXxNKPsCMdV d8/mCdHcsgOo/bQztbFH3u5zMA== X-Google-Smtp-Source: APBJJlFnCFvjqvI2pzFlGFvlvxFRqNdhbTZV1rpaz2sVZ0MxqW2jH2OPvEVWidItBMfJe3GT898H6Q== X-Received: by 2002:a05:6358:4429:b0:139:c7cb:77d4 with SMTP id z41-20020a056358442900b00139c7cb77d4mr6462436rwc.24.1690960692581; Wed, 02 Aug 2023 00:18:12 -0700 (PDT) From: Akihiko Odaki To: Cc: Helge Deller , Joel Stanley , Michael Tokarev , qemu-devel@nongnu.org, Laurent Vivier , Peter Maydell , Richard Henderson , Akihiko Odaki Subject: [PATCH v2 5/6] linux-user: Do nothing if too small brk is specified Date: Wed, 2 Aug 2023 16:17:51 +0900 Message-ID: <20230802071754.14876-6-akihiko.odaki@daynix.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230802071754.14876-1-akihiko.odaki@daynix.com> References: <20230802071754.14876-1-akihiko.odaki@daynix.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::c33; envelope-from=akihiko.odaki@daynix.com; helo=mail-oo1-xc33.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: 1690960747816100001 Content-Type: text/plain; charset="utf-8" Linux 6.4.7 does nothing when a value smaller than the initial brk is specified. Fixes: 86f04735ac ("linux-user: Fix brk() to release pages") Reviewed-by: Helge Deller Signed-off-by: Akihiko Odaki --- linux-user/syscall.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/linux-user/syscall.c b/linux-user/syscall.c index ac429a185a..ebdc8c144c 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -820,14 +820,14 @@ abi_long do_brk(abi_ulong brk_val) =20 /* brk pointers are always untagged */ =20 - /* return old brk value if brk_val unchanged or zero */ - if (!brk_val || brk_val =3D=3D target_brk) { + /* return old brk value if brk_val unchanged */ + if (brk_val =3D=3D target_brk) { return target_brk; } =20 /* do not allow to shrink below initial brk value */ if (brk_val < initial_target_brk) { - brk_val =3D initial_target_brk; + return target_brk; } =20 new_brk =3D TARGET_PAGE_ALIGN(brk_val); --=20 2.41.0 From nobody Sun May 19 03:12:45 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=1690960764; cv=none; d=zohomail.com; s=zohoarc; b=dXPFJU1779RzD9PieIN1FredoFFk3XKM5iMPQfUPzNrAVKnPpVTNw8pKc5g+UrHF6gpjQ/D453qF948UNbAmYp/6P9+f9GWkX1O7QwJZSfGhM6w1IsxRyOdAsfs3s8rHtNwHgGNV13TFUtHBJT9bx3MTLGTZL3gxFUBtPoXOzV0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690960764; 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; bh=uZmw+WGj+dKArQBXoU9nV+Zof62NM5jUx/ZLEkyqwJU=; b=CgBVU84oz9LxfwhEIof4oovoXicTh0U9gkGnG8dXhvAVeh/cabA5PlG4/f2NLQioh7atggMEOfXFFr+xuy0r7JtkRYo/WIrjMU3H+GXzfs85Ks0Rc7bzg3amAgIv8FGJ2GkJt2zkHfA1WQBWhERW02QG5ZDBp1aopcDqemVM+ek= 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 1690960764241701.2150648139313; Wed, 2 Aug 2023 00:19:24 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qR67s-0007cI-LK; Wed, 02 Aug 2023 03:18:32 -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 1qR67e-0007aR-1w for qemu-devel@nongnu.org; Wed, 02 Aug 2023 03:18:20 -0400 Received: from mail-oo1-xc31.google.com ([2607:f8b0:4864:20::c31]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qR67c-0002p5-88 for qemu-devel@nongnu.org; Wed, 02 Aug 2023 03:18:17 -0400 Received: by mail-oo1-xc31.google.com with SMTP id 006d021491bc7-56c7eb17945so2623465eaf.2 for ; Wed, 02 Aug 2023 00:18:15 -0700 (PDT) Received: from alarm.flets-east.jp ([2400:4050:a840:1e00:78d2:b862:10a7:d486]) by smtp.gmail.com with ESMTPSA id x12-20020a63b20c000000b00528da88275bsm11004440pge.47.2023.08.02.00.18.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Aug 2023 00:18:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20221208.gappssmtp.com; s=20221208; t=1690960695; x=1691565495; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=uZmw+WGj+dKArQBXoU9nV+Zof62NM5jUx/ZLEkyqwJU=; b=rYSSEqcmx0IbNtgFqU3w8FIuMYsPWsAoTL+zBrE1KcrjpGBTPZ+sMxAg0q+fAMStG8 QWp1/IoJmBiw2t48gKlyYtUuh4Rd4G/BRiKj+O3JRZsD+MvAR7H93pdKM+Ca4ZXIlSzU 5jD7OG0RKe4F/NBRakLWie+/nf0qh05Uf1G0Og5lZsCY9CtkVOjOnoShUfkbqrEPg9wZ HDyIPBNfBbFasWd8bvJDxEjRes+uUZKAIPGPRj22XTYmKBq2lr2luXGWmPtC/uEZjfl8 qJPljyM+aye7gDGskH2WhLbz4LNG/6cgquxrJkKOmr4SWPGPDL24GpuLzGVHH2ovHBMd 2+WQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690960695; x=1691565495; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=uZmw+WGj+dKArQBXoU9nV+Zof62NM5jUx/ZLEkyqwJU=; b=RuqoYvpFvnwtzpcfkMDNtzsSpgYqHS1RJTEr2GvgzCzkA/WxfPNH3hWcqe4CBH9Oep lGQzSUXBVdagl7LuBebASVTLlnSy9Co0X1j0GpULh5rWhSuzqjtfEeJyEOQkJbkxIU+1 boS2gXc7/0l6QA1XrkBNdHMsIyKiQaPjAcFvLJ19KI6H50+Obola5W7PuOdEv3K9m6wN Nfff+vL9G6aN1fhdo+ulkTzBz57aAH20zys7TUSx6rOz+ko8OdWtlRWE5u2AoVP58Mkb kSBVr96utLiGWhtEZlPrdgK8GJdNRwDXIX8ku8sOFQh54fK3grsUw/MKQUD7SCM/wXvq IJVw== X-Gm-Message-State: ABy/qLYAvKJNRGNJjVCegFqeosHef1yuHlNanzU1O21zejdHsMgLxR6s P6Sv8x5+yjX5QNVxa0aTPgkoCA== X-Google-Smtp-Source: APBJJlESEnxpYEdyvngunhFrAvgKIz/dHRwX6wZfphGIXgBwfar3z2HfvcmWpj2muwkaklkgdyhvuw== X-Received: by 2002:a05:6358:2925:b0:134:e41a:9227 with SMTP id y37-20020a056358292500b00134e41a9227mr6121428rwb.5.1690960694864; Wed, 02 Aug 2023 00:18:14 -0700 (PDT) From: Akihiko Odaki To: Cc: Helge Deller , Joel Stanley , Michael Tokarev , qemu-devel@nongnu.org, Laurent Vivier , Peter Maydell , Richard Henderson , Akihiko Odaki Subject: [PATCH v2 6/6] linux-user: Do not align brk with host page size Date: Wed, 2 Aug 2023 16:17:52 +0900 Message-ID: <20230802071754.14876-7-akihiko.odaki@daynix.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230802071754.14876-1-akihiko.odaki@daynix.com> References: <20230802071754.14876-1-akihiko.odaki@daynix.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::c31; envelope-from=akihiko.odaki@daynix.com; helo=mail-oo1-xc31.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: 1690960765137100001 Content-Type: text/plain; charset="utf-8" do_brk() minimizes calls into target_mmap() by aligning the address with host page size, which is potentially larger than the target page size. However, the current implementation of this optimization has two bugs: - The start of brk is rounded up with the host page size while brk advertises an address aligned with the target page size as the beginning of brk. This makes the beginning of brk unmapped. - Content clearing after mapping is flawed. The size to clear is specified as HOST_PAGE_ALIGN(brk_page) - brk_page, but brk_page is aligned with the host page size so it is always zero. This optimization actually has no practical benefit. It makes difference when brk() is called multiple times with values in a range of the host page size. However, sophisticated memory allocators try to avoid to make such frequent brk() calls. For example, glibc 2.37 calls brk() to shrink the heap only when there is a room more than 128 KiB. It is rare to have a page size larger than 128 KiB if it happens. Let's remove the optimization to fix the bugs and make the code simpler. Fixes: 86f04735ac ("linux-user: Fix brk() to release pages") Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1616 Signed-off-by: Akihiko Odaki --- linux-user/elfload.c | 4 ++-- linux-user/syscall.c | 54 ++++++++++---------------------------------- 2 files changed, 14 insertions(+), 44 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 861ec07abc..2aee2298ec 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -3678,8 +3678,8 @@ int load_elf_binary(struct linux_binprm *bprm, struct= image_info *info) * to mmap pages in this space. */ if (info->reserve_brk) { - abi_ulong start_brk =3D HOST_PAGE_ALIGN(info->brk); - abi_ulong end_brk =3D HOST_PAGE_ALIGN(info->brk + info->reserve_br= k); + abi_ulong start_brk =3D TARGET_PAGE_ALIGN(info->brk); + abi_ulong end_brk =3D TARGET_PAGE_ALIGN(info->brk + info->reserve_= brk); target_munmap(start_brk, end_brk - start_brk); } =20 diff --git a/linux-user/syscall.c b/linux-user/syscall.c index ebdc8c144c..475260b7ce 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -802,81 +802,51 @@ static inline int host_to_target_sock_type(int host_t= ype) } =20 static abi_ulong target_brk, initial_target_brk; -static abi_ulong brk_page; =20 void target_set_brk(abi_ulong new_brk) { target_brk =3D TARGET_PAGE_ALIGN(new_brk); initial_target_brk =3D target_brk; - brk_page =3D HOST_PAGE_ALIGN(target_brk); } =20 /* do_brk() must return target values and target errnos. */ abi_long do_brk(abi_ulong brk_val) { abi_long mapped_addr; - abi_ulong new_alloc_size; - abi_ulong new_brk, new_host_brk_page; + abi_ulong new_brk; + abi_ulong old_brk; =20 /* brk pointers are always untagged */ =20 - /* return old brk value if brk_val unchanged */ - if (brk_val =3D=3D target_brk) { - return target_brk; - } - /* do not allow to shrink below initial brk value */ if (brk_val < initial_target_brk) { return target_brk; } =20 new_brk =3D TARGET_PAGE_ALIGN(brk_val); - new_host_brk_page =3D HOST_PAGE_ALIGN(brk_val); + old_brk =3D TARGET_PAGE_ALIGN(target_brk); =20 - /* brk_val and old target_brk might be on the same page */ - if (new_brk =3D=3D TARGET_PAGE_ALIGN(target_brk)) { - /* empty remaining bytes in (possibly larger) host page */ - memset(g2h_untagged(new_brk), 0, new_host_brk_page - new_brk); + /* new and old target_brk might be on the same page */ + if (new_brk =3D=3D old_brk) { target_brk =3D brk_val; return target_brk; } =20 /* Release heap if necesary */ - if (new_brk < target_brk) { - /* empty remaining bytes in (possibly larger) host page */ - memset(g2h_untagged(new_brk), 0, new_host_brk_page - new_brk); - - /* free unused host pages and set new brk_page */ - target_munmap(new_host_brk_page, brk_page - new_host_brk_page); - brk_page =3D new_host_brk_page; + if (new_brk < old_brk) { + target_munmap(new_brk, old_brk - new_brk); =20 target_brk =3D brk_val; return target_brk; } =20 - if (new_host_brk_page > brk_page) { - new_alloc_size =3D new_host_brk_page - brk_page; - mapped_addr =3D target_mmap(brk_page, new_alloc_size, - PROT_READ | PROT_WRITE, - MAP_FIXED_NOREPLACE | MAP_ANON | MAP_PRI= VATE, - 0, 0); - } else { - new_alloc_size =3D 0; - mapped_addr =3D brk_page; - } - - if (mapped_addr =3D=3D brk_page) { - /* Heap contents are initialized to zero, as for anonymous - * mapped pages. Technically the new pages are already - * initialized to zero since they *are* anonymous mapped - * pages, however we have to take care with the contents that - * come from the remaining part of the previous page: it may - * contains garbage data due to a previous heap usage (grown - * then shrunken). */ - memset(g2h_untagged(brk_page), 0, HOST_PAGE_ALIGN(brk_page) - brk_= page); + mapped_addr =3D target_mmap(old_brk, new_brk - old_brk, + PROT_READ | PROT_WRITE, + MAP_FIXED_NOREPLACE | MAP_ANON | MAP_PRIVATE, + 0, 0); =20 + if (mapped_addr =3D=3D old_brk) { target_brk =3D brk_val; - brk_page =3D new_host_brk_page; return target_brk; } =20 --=20 2.41.0