From patchwork Fri Mar 1 23:05:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 777091 Delivered-To: patch@linaro.org Received: by 2002:adf:e94d:0:b0:33d:f458:43ce with SMTP id m13csp1602761wrn; Fri, 1 Mar 2024 15:13:22 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCU/GKRGZOaG+Na7uaUt9XH/xXx0oGf0SMJOxFpy6df9v4bsPhfmj21CA9UeVp23yTZQc6TPQxcY5QLwWnHMlK/j X-Google-Smtp-Source: AGHT+IE+aW31zPor2NagfOPKp/B1Kp9Vd1Syo3Nu7hKmoP6/3mNSnwonwV6h7x8278V54IzXzX+0 X-Received: by 2002:a67:e2ca:0:b0:472:4fd5:1dc1 with SMTP id i10-20020a67e2ca000000b004724fd51dc1mr3008366vsm.14.1709334802011; Fri, 01 Mar 2024 15:13:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1709334801; cv=none; d=google.com; s=arc-20160816; b=P6xECIYpUD+q6luzX8cCqiD7DY0LDDvGrDpQB1cY8ucRfxOFak88fjT5en4BneUE6K A98LoafNPFCzTrQBJ+ENaPBoX+q+l2I/0WgdmGCDQ2XTC3fi1wgcyeduf9L1+/Ic8Q36 3OE6S+LN79mZG/Kbe9fJaowBG11rlYAh/R16wa8pGV2MuhnES1n7vYplz6MXDcREH0BV dxTC99xHVQAae5dbpWViV0d1qwMDbIh/MV2WeCHBjFi3m61mDZI+3MZi3FY/oEGK+HFn Dp1oSC8uO91jeDcrvXuak8RrZQIub4iz8BNAz9vBpGMXiKF+TsG+wruSaRDO7q6I0yQd OjqQ== 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=48MAJNKY8itF/Uvxdy9leMjwL6Lq94BoHatvMOlA6tE=; fh=H/IsS+LNYM6dBDHt5BZPsM5V9tnyjdUlJ92VVuTs62g=; b=rEzEfArH8x7PuqtSoiwScyXajYJyPTx+Rju+PjhSfUun3CO2PVNTGQ6BpUFcHrkET+ LfxU0FZLlZBEvdsuGiaRtVKbqcs4I9tzHJRM/fDFfat9MvxfChCVI7kSLXIrw++qu+Hj dLDtoVfUxIoRCryUJMimY2EGQZq0AaGY2kGLB6nvRJZQVEDY4coL2Lw/t7lmbLD008wp j96sY2AeX/GGFh1WZXJe2XEAJUDEUwjSBoHYoP94VCDJBptNoMnVcoqD41D6+sT4C0m+ BiWPlzDJl2ro+PPMFsceElJJMrB0cQWCx5laj8nuNWSojEMC6uzxf58ZVaGyiGuAIp0n SC1w==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Iuf2pmTX; 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 k9-20020a05622a03c900b0042e724f7f88si4814394qtx.274.2024.03.01.15.13.21 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 01 Mar 2024 15:13:21 -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=Iuf2pmTX; 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 1rgByX-0006La-5x; Fri, 01 Mar 2024 18:07:34 -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 1rgByF-0005fJ-LY for qemu-devel@nongnu.org; Fri, 01 Mar 2024 18:07:16 -0500 Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rgByD-0004N1-RH for qemu-devel@nongnu.org; Fri, 01 Mar 2024 18:07:15 -0500 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-1dc0e5b223eso26021765ad.1 for ; Fri, 01 Mar 2024 15:07:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1709334432; x=1709939232; 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=48MAJNKY8itF/Uvxdy9leMjwL6Lq94BoHatvMOlA6tE=; b=Iuf2pmTXfNbaEDQ3d60gi7CqM53oX//yskS3tzvQm3W0qH6fIq2I4Dd8Gi5tF9lX5Z bh6ThCvDAGDzKRubzYvhPBid8+dwka9JDJPKvNDD5Fv677F65fvzdcpfx/W+COepGV4r cNjl9URJ2QJMikTScA4Phmq4B4ZnZk7PGkZSlybrZh5/yvtsPk7HdW857x8AgKnugbx6 Cm/ahIhfkfy73qIk/JLYHl9AW5cSXS0JA4r0Sb5srI0JL30bp417236AJlZdiVeWA/5+ 0JtV7u0a30aWdes+o6HkirZoXk93+1//EjR7dOskJ+hTxneygtY+/TVki8kC65tSYg1Q j9iA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709334432; x=1709939232; 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=48MAJNKY8itF/Uvxdy9leMjwL6Lq94BoHatvMOlA6tE=; b=fvFgiGO3etv6ztIJ67nReTVYsIoZO93uP/ZnzEkiA1s9zgSS22PQncTncv9GEWlE8k hNDgB5FY19sLwPesmw1B6BxnnJ2QU0uR5FlLO1dbGfnak7A1ZH56RJR7o3fj2s50IQj5 hJvJF+/3NI7AbSB+Vv1PJoDvjWjh8voAIaQKG0IgJ5bt9goMhyk6192WoGeip7Eqlni6 CJUxwhoNmdbLTFzRmtbNZRIpkg7OcicVLKPu92IoTj4yM6K3jatZBfWhGn934ok0pQsd 4BHICILQX9pAE3Xsi7iAAm2+gzaNE3NWw7jSc2V273fGdM69FzfgrUsFsVQhfT1Zxle3 kQtA== X-Gm-Message-State: AOJu0YzXu1e1cqwJp1/IlJ0vkYA9HxVq5lBzTK1Hn66QvqVWqUG/Tdmh OCd/Ui2wEIJiPBODv1QtZfpW8823BLkKysCaX/yjFAH1GsLYRWn493H1X31U1xRFLJub08MbVgS h X-Received: by 2002:a17:902:c20c:b0:1db:f389:2deb with SMTP id 12-20020a170902c20c00b001dbf3892debmr2647282pll.17.1709334432587; Fri, 01 Mar 2024 15:07:12 -0800 (PST) Received: from stoup.. (098-147-055-211.res.spectrum.com. [98.147.55.211]) by smtp.gmail.com with ESMTPSA id lc11-20020a170902fa8b00b001dc668e145asm3988580plb.200.2024.03.01.15.07.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 15:07:12 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Ilya Leoshkevich , Helge Deller Subject: [PATCH 35/60] linux-user: Move some mmap checks outside the lock Date: Fri, 1 Mar 2024 13:05:54 -1000 Message-Id: <20240301230619.661008-36-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240301230619.661008-1-richard.henderson@linaro.org> References: <20240301230619.661008-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::636; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x636.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 Acked-by: Helge Deller Message-Id: <20240102015808.132373-18-richard.henderson@linaro.org> --- 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; }