From patchwork Thu Aug 3 01:52:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 709579 Delivered-To: patch@linaro.org Received: by 2002:ab3:5b92:0:b0:238:c968:2573 with SMTP id j18csp1354119ltf; Wed, 2 Aug 2023 18:54:28 -0700 (PDT) X-Google-Smtp-Source: APBJJlEOXdo2nC1JV410juzfFrxDrIH/KdPcW+ek13/e39/FFZO54M1KIqqgKlQ0OOJmE65kDEo6 X-Received: by 2002:a0c:e287:0:b0:63c:d763:77b4 with SMTP id r7-20020a0ce287000000b0063cd76377b4mr15230584qvl.8.1691027668185; Wed, 02 Aug 2023 18:54:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691027668; cv=none; d=google.com; s=arc-20160816; b=oRc5L0cCWWiw8GM1EGioNHHAh1HleD/d7EsJODw6yJgERliVocyQtfDWz6WI3DYfGa DDOvVhbkAH64q0gvm6TyG1JBcdTb7asiKgzqe0YaOtalQvF4f6vTa1qP0vrnfw/Url4q IP/iIDTPGkPJ/tu5E7wwVrqaVq+LcSrVSzFy1rUnvDzW4Ypb4jirrEaRkZuznp4WIEri ksoFz+r87RXgQfLzqcgJRteIFRaYA9wOw3j56JkfepcnBASuoBtIwzmreDUrZ+zBW9T+ fWi5khgPvhj7wG8RKsOFjXqIDls5uHuKacHV8BgrgG6+P5PwNpQu9DIdcILLdngUN1F0 0SrA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=/eOGiWVqZ8Y4POmSjtXETDABSVmvc7JlojTPUk6j1vw=; fh=fmVFRXV+XPDbp8+eafcQB359/x6dDRQDsVzPMZJ0Hao=; b=qlW7DTLzBA/WkBwcarm0v2mS5goh7p2fuoM1kECOhjlxgLEY5wj8fIsyJIESx0ghIQ zmtSWHxYFWqX2V5igOBx5BWjnq48SCufpTs2azGAG2Hb8qpTFVl29IRakUCNfnlTjCC8 tkmtho/DSolX6nt9jOEJJOhNYbuXqTiPbj1HpELNrU3H+VZMNg0BReSoEJBqIA5Kbuqi p2WHLV1dW6rR0gQZ254fb3CLChRBBqvsO9stroILN8MkYmwNQd+EqMKC78/ViPAGfhDu 68Req80zXZKo8f4tuegZFR/d2t35JZRXnMvj9nLLxY/FL/LxwR5lrGp6AqNlaarLgL1T dO8Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=iOvJMZoW; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id g7-20020a37b607000000b0076cb9d1d455si3998770qkf.355.2023.08.02.18.54.28 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Wed, 02 Aug 2023 18:54:28 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=iOvJMZoW; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qRNWY-0000eC-VQ; Wed, 02 Aug 2023 21:53: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 1qRNWX-0000dZ-DZ for qemu-devel@nongnu.org; Wed, 02 Aug 2023 21:53:09 -0400 Received: from mail-pg1-x531.google.com ([2607:f8b0:4864:20::531]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qRNWV-0000jb-DE for qemu-devel@nongnu.org; Wed, 02 Aug 2023 21:53:09 -0400 Received: by mail-pg1-x531.google.com with SMTP id 41be03b00d2f7-563f8e8a53dso222173a12.3 for ; Wed, 02 Aug 2023 18:53:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691027586; x=1691632386; 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=/eOGiWVqZ8Y4POmSjtXETDABSVmvc7JlojTPUk6j1vw=; b=iOvJMZoWdwCGfePsK/gD0VKxJ+aJG3D0f0wBWW7Np+urrLTYrNdVjBPbHdxR3SpHSM uc9of+mgcJTnDmQ6XARMwP1rwfQQptvgEvNUvFzG6O1MHx7b/bYW+ppy8b2hBJYveiJX sC+F6RNFaR2rI306vX4aO89mIzZaauRRpRoZ7YJgkRTB8z/OrpeiaDoENbVi4ggN1ihb WHPgDgwfvCz5h9fgL5V2t8kOVA2YZipyDElVCrZ68UawhE9WJJGMoY7lEHlTxtQhKvpy +H6MI5RazYQyPLX1mOoFZSbZhQrwiq4AK4XHSQuDQGwHjhskj9z2PnHt1QFKwiastTpm CEMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691027586; x=1691632386; 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=/eOGiWVqZ8Y4POmSjtXETDABSVmvc7JlojTPUk6j1vw=; b=DMm6zn6t4r/ONnBvCwsyOeDnhgV7ZIvZRujApuyLWj1BRobathK0U6mbjEJuI0t9cJ vGsefxibNKalwVdFtz/d9OEwx+TVg0XgOIe/3tTy83021csCkKoxxqZj8qR5btoaZHJw oCyaLzWr80nSUCQC3CWbtnArqWEwIbNO/8eqUsftM5JZs8usVppqMgJn5CAuTtV/BhgR /wMmoN+F4/w7Dhf+bqgv4FtTqY5+ahHXr+qJ7GnPPNE2k8pHxKCtrQLEHHrIvAXhN2oe 1wiebkV60zvx0S5i/Il0QjrY8k9SBvTm9GsGYAlZ7cFTh0diz2T2ElUz9yQC6wiqo0nU QQcg== X-Gm-Message-State: ABy/qLY/mZbt4ToEaLw4Rexs8gVCMxxOrZTzy3brW+fGue91kmNBQ6Xn MPnCbeT5luqA+8aPX44J8lxzD9BFtDyxWjfpcj8= X-Received: by 2002:a17:90a:bb81:b0:268:5f1a:ede1 with SMTP id v1-20020a17090abb8100b002685f1aede1mr14640604pjr.36.1691027585864; Wed, 02 Aug 2023 18:53:05 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:b659:bbad:1a22:7ef9]) by smtp.gmail.com with ESMTPSA id s25-20020a63a319000000b005649cee408fsm157123pge.0.2023.08.02.18.53.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Aug 2023 18:53:05 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: deller@gmx.de, laurent@vivier.eu, akihiko.odaki@daynix.com, joel@jms.id.au Subject: [PATCH v7 02/14] linux-user: Fix MAP_FIXED_NOREPLACE on old kernels Date: Wed, 2 Aug 2023 18:52:50 -0700 Message-Id: <20230803015302.407219-3-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230803015302.407219-1-richard.henderson@linaro.org> References: <20230803015302.407219-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::531; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x531.google.com 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_HELO_NONE=0.001, SPF_PASS=-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+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org From: Akihiko Odaki 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 “non-MAP_FIXED” 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 Message-Id: <20230802071754.14876-3-akihiko.odaki@daynix.com> Signed-off-by: Richard Henderson --- linux-user/mmap.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/linux-user/mmap.c b/linux-user/mmap.c index a11c630a7b..90b3ef2140 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 = mmap(host_start, qemu_host_page_size, target_to_host_prot(prot), flags | MAP_ANONYMOUS, -1, 0); - if (p == MAP_FAILED) { + if (p != host_start) { + if (p != MAP_FAILED) { + munmap(p, qemu_host_page_size); + errno = EEXIST; + } return false; } prot_old = prot; @@ -687,17 +691,25 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot, /* map the middle (easier) */ if (real_start < real_last) { - void *p; + void *p, *want_p; off_t offset1; + size_t len1; if (flags & MAP_ANONYMOUS) { offset1 = 0; } else { offset1 = offset + real_start - start; } - p = mmap(g2h_untagged(real_start), real_last - real_start + 1, - target_to_host_prot(target_prot), flags, fd, offset1); - if (p == MAP_FAILED) { + len1 = real_last - real_start + 1; + want_p = g2h_untagged(real_start); + + p = mmap(want_p, len1, target_to_host_prot(target_prot), + flags, fd, offset1); + if (p != want_p) { + if (p != MAP_FAILED) { + munmap(p, len1); + errno = EEXIST; + } goto fail; } passthrough_start = real_start;