From patchwork Mon Jul 24 08:53:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 705744 Delivered-To: patch@linaro.org Received: by 2002:a5d:60ca:0:b0:317:2194:b2bc with SMTP id x10csp1665770wrt; Mon, 24 Jul 2023 01:56:26 -0700 (PDT) X-Google-Smtp-Source: APBJJlHpnxMi/xzr+9sm4GN20F4lm9bpbX5AiMKDoORealqdA3KvkMGcR0sxFX/i6oIMyuaKFP8n X-Received: by 2002:ac8:7f8d:0:b0:403:b41b:a6c6 with SMTP id z13-20020ac87f8d000000b00403b41ba6c6mr6219067qtj.38.1690188986216; Mon, 24 Jul 2023 01:56:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690188986; cv=none; d=google.com; s=arc-20160816; b=E0mwCT9qq0Rmjxk4I0WcdTQhs/PUWltJAFsAjJDxDs9MXULbW/pcxLtPI4k6v0zj1q ggUsLaIunQnK/o7QQy+/SkT555Ani8//etQ0lA1jztVtJgYYuuA4CR7oEgcn4CQ3afUg u0bMjWUB7Rec5ifNJmJ251WQFlVzIeLI4dG1oi0DhL3zwEvAin3fKAlUqyZZ6f0Rk+Jm 20Ju/zbiDikkUqGg5cNHYPGviZk+Hq/2hKcCABNkhupy/04r/1P+LBXv5vgIajlRjxTj umbn9A1/LlK+UmZsLQhGt7p43zlW4Qpzv0EISm1C1+4Il06Z7eoKf9cqcdDZYH1tO9rr flAQ== 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=FtcjV7RLMVbCUSYkAiLh5IZllixWR3iJOTzr/2oXRS0=; fh=dar3VLbuhlF4sfQjYytRTFhcEwFCm4WmC7f2qzZT2aY=; b=eBf9JSQ4pYvehkAQlHTK1piMvs2Yi8JDwBYj8tVNGAsv+XfPd3g4h5/8SG0/aeK75p GBS4yk79siVZDjjUmBKRwaGLu12yQXYaK0qWD0LAOfWB1icC+m4065GRqBNceRc89cxI 1HCUexBHNIT5e8rco3fqmLGornb3GExCfjgW5OHQUATX1Yh79pwCYWAffCjHFX7Nvx1y qxSyYCULl7JbqKbinTF8ksGtWW4elLD+uDOYl2UlTPOfCu6rRFmc8NM0XIxaDThNNjhJ pgFG5RLUv4aE5dj6AjPZO4j9wKuVLVPPDAHzs7ht4CdhgtqdH/nvuLsn9nJcwlLGnD+q 7PDQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=cvcAyU+A; 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 p20-20020a05622a13d400b0040547442debsi4704026qtk.56.2023.07.24.01.56.25 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 24 Jul 2023 01:56:26 -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=cvcAyU+A; 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 1qNrJx-00021g-L0; Mon, 24 Jul 2023 04:53:37 -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 1qNrJw-00020E-1D for qemu-devel@nongnu.org; Mon, 24 Jul 2023 04:53:36 -0400 Received: from mail-wm1-x330.google.com ([2a00:1450:4864:20::330]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qNrJu-0000eP-By for qemu-devel@nongnu.org; Mon, 24 Jul 2023 04:53:35 -0400 Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-3fd190065a8so39351265e9.3 for ; Mon, 24 Jul 2023 01:53:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1690188813; x=1690793613; 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=FtcjV7RLMVbCUSYkAiLh5IZllixWR3iJOTzr/2oXRS0=; b=cvcAyU+AZgDpWf1Io/RLMTmwARxeWpyv0NX/Qw1BzUK/nboWFkW75BgBrvS6gfvL68 eZiPikHTEVYRF2/hSKo6pF9/nyWYc4SSf87FUH0PRnvedIv7xjGJGvGHUF+peQ8hKR8l D/NIUenF5dbkHS0Ck5nyi5DSGanLfaA38FPsqhBNFtdgouBlVDgzpS3+mdFYbMYHV4bi J3yfJkKLSWlZUX6o90n5uRf73Igk9i4Anqy0vo7sIHQJOZLqUFuys4r2RpRAfdKo+/Mm koWuZs08OODB73FfjKc0v5wrHWLfm9TwBYjtIoqxPgwxU9LrSfVGuQ5H6ZmbrHIKQFiM 2llQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690188813; x=1690793613; 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=FtcjV7RLMVbCUSYkAiLh5IZllixWR3iJOTzr/2oXRS0=; b=G20qr+lFaDf98JRGtTXLtype2CfsXiYOLfFMXiIaobDrRhfV35UrPw16kTLMu9b5sz KUpR92gnng5+/dVjUC+eAbxQ+w9kMJIJCzTeUujM2Ia68TDZbyHEQg2X2blagfR0yf5V Mm9QvDfYuXbO4ajvXAafUd7eGAK4yC/2nGfZWm2w2wXAHwPhkvAdJjl8q9u4eZLr/j2b V+HJCq2THs6sfFewlyGfv9eF8rhS34qxDCDK8Y9wKyEGS1LITcfnMzoAohGHWRI+/jaG zoMYc4Avqh6SlAiEMB3h6Ycbtg8Ijkxzswy+sBM6Q1M7tAfw8reIiSyTfZSK+K0zDNn5 QNKw== X-Gm-Message-State: ABy/qLY2l7ufELbGztY5wSzpfbqTncRObIEhE3Th0YAkR0j+Fo8L/jw5 ZXknYE9ZfJczO4qXnQ4dcIhak9FsIyYnGGs/OGh5kg== X-Received: by 2002:a05:600c:219a:b0:3fb:415f:a85b with SMTP id e26-20020a05600c219a00b003fb415fa85bmr7569973wme.3.1690188812917; Mon, 24 Jul 2023 01:53:32 -0700 (PDT) Received: from stoup.. ([62.252.144.58]) by smtp.gmail.com with ESMTPSA id t8-20020a1c7708000000b003fa74bff02asm9570994wmi.26.2023.07.24.01.53.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Jul 2023 01:53:32 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org Subject: [PULL 4/7] accel/tcg: Take mmap_lock in load_atomic*_or_exit Date: Mon, 24 Jul 2023 09:53:25 +0100 Message-Id: <20230724085328.4936-5-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230724085328.4936-1-richard.henderson@linaro.org> References: <20230724085328.4936-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::330; envelope-from=richard.henderson@linaro.org; helo=mail-wm1-x330.google.com X-Spam_score_int: 12 X-Spam_score: 1.2 X-Spam_bar: + X-Spam_report: (1.2 / 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, RCVD_IN_SBL_CSS=3.335, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no 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 For user-only, the probe for page writability may race with another thread's mprotect. Take the mmap_lock around the operation. This is still faster than the start/end_exclusive fallback. Reviewed-by: Peter Maydell Signed-off-by: Richard Henderson --- accel/tcg/ldst_atomicity.c.inc | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/accel/tcg/ldst_atomicity.c.inc b/accel/tcg/ldst_atomicity.c.inc index de70531a7a..e5c590a499 100644 --- a/accel/tcg/ldst_atomicity.c.inc +++ b/accel/tcg/ldst_atomicity.c.inc @@ -159,9 +159,11 @@ static uint64_t load_atomic8_or_exit(CPUArchState *env, uintptr_t ra, void *pv) * another process, because the fallback start_exclusive solution * provides no protection across processes. */ - if (!page_check_range(h2g(pv), 8, PAGE_WRITE_ORG)) { - uint64_t *p = __builtin_assume_aligned(pv, 8); - return *p; + WITH_MMAP_LOCK_GUARD() { + if (!page_check_range(h2g(pv), 8, PAGE_WRITE_ORG)) { + uint64_t *p = __builtin_assume_aligned(pv, 8); + return *p; + } } #endif @@ -186,25 +188,27 @@ static Int128 load_atomic16_or_exit(CPUArchState *env, uintptr_t ra, void *pv) return atomic16_read_ro(p); } -#ifdef CONFIG_USER_ONLY /* * We can only use cmpxchg to emulate a load if the page is writable. * If the page is not writable, then assume the value is immutable * and requires no locking. This ignores the case of MAP_SHARED with * another process, because the fallback start_exclusive solution * provides no protection across processes. + * + * In system mode all guest pages are writable. For user mode, + * we must take mmap_lock so that the query remains valid until + * the write is complete -- tests/tcg/multiarch/munmap-pthread.c + * is an example that can race. */ - if (!page_check_range(h2g(p), 16, PAGE_WRITE_ORG)) { - return *p; - } + WITH_MMAP_LOCK_GUARD() { +#ifdef CONFIG_USER_ONLY + if (!page_check_range(h2g(p), 16, PAGE_WRITE_ORG)) { + return *p; + } #endif - - /* - * In system mode all guest pages are writable, and for user-only - * we have just checked writability. Try cmpxchg. - */ - if (HAVE_ATOMIC128_RW) { - return atomic16_read_rw(p); + if (HAVE_ATOMIC128_RW) { + return atomic16_read_rw(p); + } } /* Ultimate fallback: re-execute in serial context. */