From patchwork Tue Jan 2 01:57:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 759336 Delivered-To: patch@linaro.org Received: by 2002:a5d:67c6:0:b0:336:6142:bf13 with SMTP id n6csp5856085wrw; Mon, 1 Jan 2024 17:59:22 -0800 (PST) X-Google-Smtp-Source: AGHT+IExc8Visq6VCYPH+3wU2sU1IDj9u+9fD56QwFFZ71SnfzddNWmKs+p9ms4/jXOKyxSOhA4+ X-Received: by 2002:a05:620a:1474:b0:781:7239:ae4e with SMTP id j20-20020a05620a147400b007817239ae4emr8368958qkl.79.1704160762430; Mon, 01 Jan 2024 17:59:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704160762; cv=none; d=google.com; s=arc-20160816; b=eRO7zFTb0xzUmIBIorLbK2fcdyUgLYE73D3oc9vOWzW8MdHGtfNjhsMhgA3YEem2Ms yo3dUW1jl6vBNAqHpWtJeXqgRvFOtE0QBJTCFIMgvQzohsikZVBIskYBXsp9nFiLcL8b T1Fe7jzLtQvKf4FSOj0wy5Ynsk8CklnBrwutCYB0wAISiKyaWuBdau9AXjpmX/87EHli 4E7IJ6X38WhV15n7h3X4QrMtQIB5jXBABmp5330/7SksZgvzRMJ0HTPyH7XVk2MwrZGq LDIBm7sGux7ZPQ+kakRRzoMrjwtCpUGRfUcuNx2TUtk114SfgneV5FEOX3ITR5QxMN3z 2oIQ== 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=tnSJisjhmOEPjG/usSlqH7jpNx1heRPl0XkUSpAoUU4=; fh=kQadAhxV42GJ3cd2VUw1RyURv/zJQGvmUdF7iLEVTtc=; b=TQX7vCO48MFmf59L5yG6X2p8a9BS1ywZ8IBilOm7HanTAs1JQbfZ3a2FEsklufuVvs mMc+nbBko5+x5mDoJlzoCZcvIud5+Qt/bfYYDbaQnUSB07uUnq5MBkawT2gCn4wNLCWS JinA2dFlRdneFTjXAZ5J7Hj04YupjSnbtGP/qx3I8GFjueeIqTHr+plcvOuwcWYBc+2Y BooBaj9nvWU/fxw2C5I7+WqucrWf4DnN9Yfc8z+s5D90SrX5GHOFNWrJmPZ3MO2IvHIq 8XPHd8x53BONjsdzAoEUCYNGEE90/t8FoBUfIcfX4PrYi7hx52wpHrJJ2EROSa1LNmyg SzKA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=aDr1PqNl; 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 bl13-20020a05620a1a8d00b0078170866e89si11612160qkb.479.2024.01.01.17.59.22 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 01 Jan 2024 17:59:22 -0800 (PST) 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=aDr1PqNl; 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 1rKU3R-0003VH-K2; Mon, 01 Jan 2024 20:58:53 -0500 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 1rKU3P-0003Lj-7U for qemu-devel@nongnu.org; Mon, 01 Jan 2024 20:58:51 -0500 Received: from mail-pf1-x430.google.com ([2607:f8b0:4864:20::430]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rKU3N-00072u-Hi for qemu-devel@nongnu.org; Mon, 01 Jan 2024 20:58:50 -0500 Received: by mail-pf1-x430.google.com with SMTP id d2e1a72fcca58-6d9bba6d773so4527187b3a.1 for ; Mon, 01 Jan 2024 17:58:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1704160728; x=1704765528; 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=tnSJisjhmOEPjG/usSlqH7jpNx1heRPl0XkUSpAoUU4=; b=aDr1PqNlVmx4prsdKZJYRIm1vVLFrlbT6SY+3REZiZoWHYh3Oj+ALdajLfoNkPsZh7 PUx6vukihpxMCfj0LC0oRtdffGQukkr80+Egwfr6iE8lzzLGYtjqlKy6vNGA01kyF7Tg RyzeiFyIClFuFE97n/IymEcNvGUmBlIUR+hmNzAcj6WfVP+7om1Bvfm2ODNkrZUP/Goy rxd9w8cUPUl+pAMHEGyOA6/C6rP/AMhq0TCLUDBpMP1vuy9QOc1dLcZ2v1loOFhasIpo G1Yj0MVvUGy6np5T0Eof3ZI82ryXuFCYhbSZarF2GJyQzy2tNNa/KrbGF1hJo3dCGzhd MvrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704160728; x=1704765528; 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=tnSJisjhmOEPjG/usSlqH7jpNx1heRPl0XkUSpAoUU4=; b=MEB1pD6rQCL/PwJDuhOsNwFhns2lJ2ZkJIrLxq432uvz3jj+Mx1wAZit4SR1eF9jZf vRl0bXcz6k2panp6KnG0uSIQ54SgFN9Fww7vebqQYhk38+9Pp1dH87oF++J8HyISsg4g QWmy71O+7hcmo1/tYfb2webZwEByx9B1xdpxTg288oot5prnsmk57OSARQ64JvhiQvOK 4Ou+K5cwlDRfg1gshaYsRplqJ+zdqvZJf5b20epDobtJYPp/Lkp/XbgJMTbBuGxGOVl4 4D0i1lHUOfJYxs2xy94BJo1Dn3Aqyg4Yb7hzeESXOsHDt0cCbaJ3vbCSCsXFA8cagwaV Qbag== X-Gm-Message-State: AOJu0Yz40f9+oiqNHmSysAPvqJuGXeV69CriohRRvGsB3IwFCZBPAD/5 VchgXQ3pkEMs80ATrPt4o0VosRi5PtMcIB/aNr4FrS8p/2w= X-Received: by 2002:a05:6a00:1d96:b0:6d9:af31:293b with SMTP id z22-20020a056a001d9600b006d9af31293bmr17005737pfw.61.1704160728179; Mon, 01 Jan 2024 17:58:48 -0800 (PST) Received: from stoup.. (124-149-254-207.tpgi.com.au. [124.149.254.207]) by smtp.gmail.com with ESMTPSA id g33-20020a635221000000b0058ee60f8e4dsm19561891pgb.34.2024.01.01.17.58.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Jan 2024 17:58:47 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= Subject: [PATCH v3 17/33] linux-user: Move some mmap checks outside the lock Date: Tue, 2 Jan 2024 12:57:52 +1100 Message-Id: <20240102015808.132373-18-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240102015808.132373-1-richard.henderson@linaro.org> References: <20240102015808.132373-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::430; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x430.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 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 Reviewed-by: Ilya Leoshkevich --- 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 b4c3cc65aa..fbaea832c5 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -491,52 +491,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; @@ -616,23 +578,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)) { @@ -778,13 +726,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; }