From patchwork Fri Sep 1 02:23:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 719468 Delivered-To: patch@linaro.org Received: by 2002:ab3:1981:0:b0:23f:8cfb:5ad7 with SMTP id 1csp16770lti; Thu, 31 Aug 2023 19:24:44 -0700 (PDT) X-Google-Smtp-Source: AGHT+IETfKO4zegbXGYQRrRo8kQc6ZskuwPAw3PMmzM1N+H3pSXA9xRzBAWROCysORXvBfIZldUi X-Received: by 2002:a05:622a:1c2:b0:403:b44a:a5e4 with SMTP id t2-20020a05622a01c200b00403b44aa5e4mr1608631qtw.52.1693535083831; Thu, 31 Aug 2023 19:24:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693535083; cv=none; d=google.com; s=arc-20160816; b=ezAvCLVVgq265P5dcGeScjJkwVjsPYrOaQDmSg5ozS9q005UYJgrwQQCjOq2yYcs09 0tn2+/fyApKseBWvtoNr+8ZLl8GdfysknNHOnMafOjNDm1buFFaCAiYns3CvU/fPkj09 l4xZIqi0gqDX9IgDL2rHlEkdI3Xf0W1M3cpEOLOQneNkcyni06y2bqNF1BYqTfKSh8Ht MtN6t9fx7dF+L3LD8XMKwgkl2kR4SlCBnJMojUxe5H5qcHeKmerLKDc+DF1tUsrCANCG bGZvI5oQXrm5ANcr7tLH3hDm8jivibuM09iaYTJUzycfJ/Fzmn8TmBl2Q0t9e4LPOL4H 0eAQ== 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=u/C8gVjHYR4xtzLye5oGAh9WlueBxM9IX1xH8cPHtm8=; fh=SfxhhCERWPFe9EfsSkQ0zw+VJgdUi+jLlAbxANM8dQo=; b=jTk65AHqosTVtGLP6rz4vh2sGPRtRdk74sr7e7LsPfCu/+2SsjXGKKTUJbO1o5Y62V 6GhDKNvBJXDfR130bK+M7qQ3JI1Sy++JeqNsQXxLv1qH6hiTMLB/FN1BURL6vNm31Y19 pKAptPcINy4nkJx+fnl6YfRTIiWInAZFlJGetRKxmIw3mzg5ADvqTejYWYySq4f/fZN1 eNjGzOZ9L/nqt4wLI4hZskb1adl6tRRYAvWxb6paavQSZHQYkRw972+H0zu17WYw+eMA 5swtjVjyILWn740SLxLG6EQMsXIvwm4GNJNQQPT4uq4KXL52s0AsTccesNPbFAj6oqIC XNhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="A04/Oj5v"; 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 bp36-20020a05622a1ba400b0040fd5df3824si1780754qtb.36.2023.08.31.19.24.43 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 31 Aug 2023 19:24:43 -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="A04/Oj5v"; 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 1qbtp9-0006Ys-BS; Thu, 31 Aug 2023 22:23:51 -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 1qbtp8-0006YK-4a for qemu-devel@nongnu.org; Thu, 31 Aug 2023 22:23:50 -0400 Received: from mail-oi1-x22e.google.com ([2607:f8b0:4864:20::22e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qbtp5-0001Qm-On for qemu-devel@nongnu.org; Thu, 31 Aug 2023 22:23:49 -0400 Received: by mail-oi1-x22e.google.com with SMTP id 5614622812f47-3a85b9c6ccdso907131b6e.0 for ; Thu, 31 Aug 2023 19:23:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1693535026; x=1694139826; darn=nongnu.org; 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=u/C8gVjHYR4xtzLye5oGAh9WlueBxM9IX1xH8cPHtm8=; b=A04/Oj5vpw+9XMsincDcdd4kf2Hbgvt/cMD/5rA06VfBZ7waHGvenw2t0cVP324h9f k7AAFRCLRqwo9ODci3qAW+X2+hZUlCeuh19jXtgEnILSZ4+TpALy3yIk33oJyhPb/NZK emMJ2vB2DcjGmPY3wwMF+zxhvTNFShXL0tkxOKj4I84tAQ4WJHNfUeSEkOP+X4CWX2BX HgPdGh3f7A3mZbIFd3VjzT0fMeEknypoU1WJvSK69u01LAR0yBUXXgI34EI7cD2qrNoi D2y3u7rCbDvoiokTM/5WtyIP0dXtuxjvEij1OjS2ZnujO+nkTxJqyTKWfnI10hN6VpOD YBaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693535026; x=1694139826; 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=u/C8gVjHYR4xtzLye5oGAh9WlueBxM9IX1xH8cPHtm8=; b=fuweONwPvHtxWCH39dHTz+WwTZ4AidLtwJyLzkrqPcux1VtWzLY51wgUewFPIBHnEo dgQQdS82h1h+Q298fZlktHeRpFu9vHksWO/DIPe5v/48qcB2jhcbfWmEyLF+3a0SHgh+ SwLkJ95yMnSH0ltyNVknKagTkQWNB7sfCxdi6586MEzOQGPmD5Lcw0tzdnnitw+ymCsT +1+MqsuD73o2pEOzLm5IRP1bK6fSVCcfDg7pdjnsGDuW9VYVjdLg9wluCQCnWZQmfHPy Yrt98ZGJ/eele0glPuDAYvOVuqAkQ7uHBfSdmlcIpqY3vA/a3HXCSBy5tTx50VghuuIp M48g== X-Gm-Message-State: AOJu0YziGb+mv+LihtNP4f2N8ul3C9y1AB1j6pqCOvhxq5W3H0Cl5wSD P9zRgVfJ50v7tAKrjMh6KWRHIDZOkmWrkXmjxnc= X-Received: by 2002:a05:6808:2091:b0:3a5:a607:650d with SMTP id s17-20020a056808209100b003a5a607650dmr1690031oiw.45.1693535026708; Thu, 31 Aug 2023 19:23:46 -0700 (PDT) Received: from stoup.. ([71.212.131.115]) by smtp.gmail.com with ESMTPSA id c25-20020aa781d9000000b0068a54866ca8sm1906977pfn.134.2023.08.31.19.23.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Aug 2023 19:23:46 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: laurent@vivier.eu, alex.bennee@linaro.org, =?utf-8?q?Philippe_Mathieu-Da?= =?utf-8?q?ud=C3=A9?= Subject: [PATCH v2 17/33] linux-user: Move some mmap checks outside the lock Date: Thu, 31 Aug 2023 19:23:15 -0700 Message-Id: <20230901022331.115247-18-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230901022331.115247-1-richard.henderson@linaro.org> References: <20230901022331.115247-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::22e; envelope-from=richard.henderson@linaro.org; helo=mail-oi1-x22e.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 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 Basic validation of operands does not require the lock. Hoist them from target_mmap__locked back into target_mmap. Reviewed-by: Philippe Mathieu-Daudé Signed-off-by: Richard Henderson --- linux-user/mmap.c | 107 +++++++++++++++++++++++----------------------- 1 file changed, 53 insertions(+), 54 deletions(-) diff --git a/linux-user/mmap.c b/linux-user/mmap.c index 448f168df1..85d16a29c1 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -447,52 +447,14 @@ abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size, abi_ulong align) } static abi_long target_mmap__locked(abi_ulong start, abi_ulong len, - int target_prot, int flags, + int target_prot, int flags, int page_flags, int fd, off_t offset) { int host_page_size = qemu_real_host_page_size(); abi_ulong ret, last, real_start, real_last, retaddr, host_len; abi_ulong passthrough_start = -1, passthrough_last = 0; - int page_flags; off_t host_offset; - if (!len) { - errno = EINVAL; - return -1; - } - - page_flags = validate_prot_to_pageflags(target_prot); - if (!page_flags) { - errno = EINVAL; - return -1; - } - - /* Also check for overflows... */ - len = TARGET_PAGE_ALIGN(len); - if (!len) { - errno = ENOMEM; - return -1; - } - - if (offset & ~TARGET_PAGE_MASK) { - errno = EINVAL; - return -1; - } - - /* - * If we're mapping shared memory, ensure we generate code for parallel - * execution and flush old translations. This will work up to the level - * supported by the host -- anything that requires EXCP_ATOMIC will not - * be atomic with respect to an external process. - */ - if (flags & MAP_SHARED) { - CPUState *cpu = thread_cpu; - if (!(cpu->tcg_cflags & CF_PARALLEL)) { - cpu->tcg_cflags |= CF_PARALLEL; - tb_flush(cpu); - } - } - real_start = start & -host_page_size; host_offset = offset & -host_page_size; @@ -572,23 +534,9 @@ static abi_long target_mmap__locked(abi_ulong start, abi_ulong len, passthrough_start = start; passthrough_last = last; } else { - if (start & ~TARGET_PAGE_MASK) { - errno = EINVAL; - return -1; - } last = start + len - 1; real_last = ROUND_UP(last, host_page_size) - 1; - /* - * 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 (last < start || !guest_range_valid_untagged(start, len)) { - errno = ENOMEM; - return -1; - } - if (flags & MAP_FIXED_NOREPLACE) { /* Validate that the chosen range is empty. */ if (!page_check_range_empty(start, last)) { @@ -733,13 +681,64 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot, int flags, int fd, off_t offset) { abi_long ret; + int page_flags; trace_target_mmap(start, len, target_prot, flags, fd, offset); + + if (!len) { + errno = EINVAL; + return -1; + } + + page_flags = validate_prot_to_pageflags(target_prot); + if (!page_flags) { + errno = EINVAL; + return -1; + } + + /* Also check for overflows... */ + len = TARGET_PAGE_ALIGN(len); + if (!len || len != (size_t)len) { + errno = ENOMEM; + return -1; + } + + if (offset & ~TARGET_PAGE_MASK) { + errno = EINVAL; + return -1; + } + if (flags & (MAP_FIXED | MAP_FIXED_NOREPLACE)) { + if (start & ~TARGET_PAGE_MASK) { + errno = EINVAL; + return -1; + } + if (!guest_range_valid_untagged(start, len)) { + errno = ENOMEM; + return -1; + } + } + mmap_lock(); - ret = target_mmap__locked(start, len, target_prot, flags, fd, offset); + ret = target_mmap__locked(start, len, target_prot, flags, + page_flags, fd, offset); mmap_unlock(); + + /* + * If we're mapping shared memory, ensure we generate code for parallel + * execution and flush old translations. This will work up to the level + * supported by the host -- anything that requires EXCP_ATOMIC will not + * be atomic with respect to an external process. + */ + if (ret != -1 && (flags & MAP_TYPE) != MAP_PRIVATE) { + CPUState *cpu = thread_cpu; + if (!(cpu->tcg_cflags & CF_PARALLEL)) { + cpu->tcg_cflags |= CF_PARALLEL; + tb_flush(cpu); + } + } + return ret; }