From patchwork Sun Aug 20 20:44:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 715207 Delivered-To: patch@linaro.org Received: by 2002:a5d:484e:0:b0:317:ecd7:513f with SMTP id n14csp1531682wrs; Sun, 20 Aug 2023 13:45:03 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGlgi6K7UiOi+tN3GbiPoRK3IbhqUrC5BC4PtjOH17GfSR5laFg11MNyNVJmj6n7vopkcxt X-Received: by 2002:a05:620a:24c2:b0:767:ea44:daf9 with SMTP id m2-20020a05620a24c200b00767ea44daf9mr6653062qkn.31.1692564302775; Sun, 20 Aug 2023 13:45:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692564302; cv=none; d=google.com; s=arc-20160816; b=W1z5dgieMckpmn+8+FPTNFxGaPCBJjz6eH0sBcEh1J3wl0IhR/uC0+ySpUxz1sidqN GUuAcO8G56Syoa4ohTiSuvIb+Z7ayvbXfXPHP1f0IYNlqK2M3xlJqMToHDF4VuZ6KkIm V/b5KdpxdYWx9+PiwwBSYpAKTPI1t3K1EJt/QN5ZnQY5fY+HoCdVUVKGoeKf4jBb2O16 o1PTs2U8CL3++NbDutvKuQ3xj7zjpwMPnLL1iDwPsf+3uOCJnACLecnNqhu09XT+mTrB dDyyBEbCqRISQyHZ80Y1YMhLB6ipfRNguRLeKECRivioU3K/8tLgcqJQ6zPVAh6T/IC/ Y8yg== 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=K8v+5fH9IZqdhr0bQzZXGyV6Ph1LR31Y8PNv9iFBpfQ=; fh=VLyDSxEFBkv0vhe3LU868VMhL5H8YsEeY0KW/Q5O5ZU=; b=aZ9cUF0TfJCwQzTVcyiviF2RjgH3c3QZZvnynuqhlp9SV21XT1NJLSmFsbKpqB5lNt OenxSGgQwjp+jDrnXjhvMk/HckH7FaefRTQ8oqPp9jMjbFLRBariAjLCYCGR2aYmJg6v s5Y5o4UPwweUTpJMpw2n1uOrxD9NVQ6cNlii+ODocqOFbgUzHFky9TXzAsGrLANRVhSI mHmFJ1ZmI445Sdk5bxhriu6Z631C8sTDPkg3MoSropMz/8nDyCYyfj5Qqf8oJlcUGUjZ jIrij1stY+6MM3Q0z+kgVCu8XBkJS/ybWQxIgg0lE/bDy4p80SmWojT7zXmzBykMqhOF 19cQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=FlYT4HUQ; 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 ql13-20020a05620a890d00b007683ee37758si3848441qkn.86.2023.08.20.13.45.02 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 20 Aug 2023 13:45:02 -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=FlYT4HUQ; 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 1qXpHW-0004xk-5d; Sun, 20 Aug 2023 16:44:18 -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 1qXpHU-0004x4-Im for qemu-devel@nongnu.org; Sun, 20 Aug 2023 16:44:16 -0400 Received: from mail-pf1-x42d.google.com ([2607:f8b0:4864:20::42d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qXpHQ-0007kI-Dc for qemu-devel@nongnu.org; Sun, 20 Aug 2023 16:44:16 -0400 Received: by mail-pf1-x42d.google.com with SMTP id d2e1a72fcca58-68a3082c771so585064b3a.0 for ; Sun, 20 Aug 2023 13:44:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1692564251; x=1693169051; 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=K8v+5fH9IZqdhr0bQzZXGyV6Ph1LR31Y8PNv9iFBpfQ=; b=FlYT4HUQw9O/ln9UTuisOxHF/tfEgWKd9aRfnG7GQVZcj1/XzCezTIK8kbQjtdTJd+ 1kUqRkCCwKW3btrTru0G0M9FhUePt8lanzUla2cVExTXIZYey6PuErHjf1moaCC3YOKz 4j/9GYiqjI9gx2KSbXx7wk8P2Q58PAMz+AQD3rA53rwb0QQ8duiE5GJOkFiesH1hMhU/ 0yWEGKsLm5s8olb9UEbSoKGfWWEM+QGiXkVPrCDOc8Q2XGUtZ0uWa5Lmif8reuGCv+ty p1s5UoYiNslWm4AMB+HX9ZO2KIQrDoTOdYEVxTYlcUYY4qt5WiD4VUc/H7Ig+TUJn237 HA5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692564251; x=1693169051; 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=K8v+5fH9IZqdhr0bQzZXGyV6Ph1LR31Y8PNv9iFBpfQ=; b=B5XjCkyoHROGvwBns4o5rXAYQ6rst/JSciLA0XGhWWZ902M4iSMnCcoSOSxRki0kTD EwnPpZjBTl5TL1SGOyPS9v3SmoBcKhxBFsMZNICLzdO4fKD/ihmy32R8wWTJCcwe3dS4 8LdquazH0bV7CavHdb/cJnGk5gyCIl88foWe1NOAZcKuiN1E/Ty1GZadjNxhJwTH7fX9 +aXVyR+ZkSD4BUI3g0MKbCz7MBLE4tIk5aMbyLsGhaJdHh+UKVz/TR5VWom0yS7gJrU9 f5WMcsBDCPmZtmCvZYjz527uf4Y46GvAF+NA2SjhWkbOp4MgpbBY3uR0yP9usR+XvQUb dw+g== X-Gm-Message-State: AOJu0YwMBVzbq/sor40dTe6qzbPH8AdPY6LXdKTvl7uj3b1vd60TZBrk o8LXmUPIeLNmSdstxqtJrqioVqZ4etuXp211flY= X-Received: by 2002:a05:6a00:1d09:b0:666:c1ae:3b87 with SMTP id a9-20020a056a001d0900b00666c1ae3b87mr6706738pfx.12.1692564250876; Sun, 20 Aug 2023 13:44:10 -0700 (PDT) Received: from stoup.. ([2602:47:d483:7301:347d:4d37:bdd7:f5e5]) by smtp.gmail.com with ESMTPSA id c7-20020aa78c07000000b0064fde7ae1ffsm1430149pfd.38.2023.08.20.13.44.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 20 Aug 2023 13:44:10 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: laurent@vivier.eu, imp@bsdimp.com, kariem.taha2.7@gmail.com Subject: [PATCH 1/4] linux-user: Move shmat and shmdt implementations to mmap.c Date: Sun, 20 Aug 2023 13:44:05 -0700 Message-Id: <20230820204408.327348-2-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230820204408.327348-1-richard.henderson@linaro.org> References: <20230820204408.327348-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::42d; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x42d.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 Rename from do_* to target_*. Fix some minor checkpatch errors. Signed-off-by: Richard Henderson Reviewed-by: Philippe Mathieu-Daudé Tested-by: Philippe Mathieu-Daudé Reviewed-by: Warner Losh --- linux-user/user-mmap.h | 4 ++ linux-user/mmap.c | 138 +++++++++++++++++++++++++++++++++++++++ linux-user/syscall.c | 143 ++--------------------------------------- 3 files changed, 146 insertions(+), 139 deletions(-) diff --git a/linux-user/user-mmap.h b/linux-user/user-mmap.h index 0f4883eb57..b94bcdcf83 100644 --- a/linux-user/user-mmap.h +++ b/linux-user/user-mmap.h @@ -58,4 +58,8 @@ abi_ulong mmap_find_vma(abi_ulong, abi_ulong, abi_ulong); void mmap_fork_start(void); void mmap_fork_end(int child); +abi_ulong target_shmat(CPUArchState *cpu_env, int shmid, + abi_ulong shmaddr, int shmflg); +abi_long target_shmdt(abi_ulong shmaddr); + #endif /* LINUX_USER_USER_MMAP_H */ diff --git a/linux-user/mmap.c b/linux-user/mmap.c index 9aab48d4a3..3aeacd1ecd 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -17,6 +17,7 @@ * along with this program; if not, see . */ #include "qemu/osdep.h" +#include #include "trace.h" #include "exec/log.h" #include "qemu.h" @@ -27,6 +28,14 @@ static pthread_mutex_t mmap_mutex = PTHREAD_MUTEX_INITIALIZER; static __thread int mmap_lock_count; +#define N_SHM_REGIONS 32 + +static struct shm_region { + abi_ulong start; + abi_ulong size; + bool in_use; +} shm_regions[N_SHM_REGIONS]; + void mmap_lock(void) { if (mmap_lock_count++ == 0) { @@ -981,3 +990,132 @@ abi_long target_madvise(abi_ulong start, abi_ulong len_in, int advice) return ret; } + +#ifndef TARGET_FORCE_SHMLBA +/* + * For most architectures, SHMLBA is the same as the page size; + * some architectures have larger values, in which case they should + * define TARGET_FORCE_SHMLBA and provide a target_shmlba() function. + * This corresponds to the kernel arch code defining __ARCH_FORCE_SHMLBA + * and defining its own value for SHMLBA. + * + * The kernel also permits SHMLBA to be set by the architecture to a + * value larger than the page size without setting __ARCH_FORCE_SHMLBA; + * this means that addresses are rounded to the large size if + * SHM_RND is set but addresses not aligned to that size are not rejected + * as long as they are at least page-aligned. Since the only architecture + * which uses this is ia64 this code doesn't provide for that oddity. + */ +static inline abi_ulong target_shmlba(CPUArchState *cpu_env) +{ + return TARGET_PAGE_SIZE; +} +#endif + +abi_ulong target_shmat(CPUArchState *cpu_env, int shmid, + abi_ulong shmaddr, int shmflg) +{ + CPUState *cpu = env_cpu(cpu_env); + abi_ulong raddr; + void *host_raddr; + struct shmid_ds shm_info; + int i, ret; + abi_ulong shmlba; + + /* shmat pointers are always untagged */ + + /* find out the length of the shared memory segment */ + ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info)); + if (is_error(ret)) { + /* can't get length, bail out */ + return ret; + } + + shmlba = target_shmlba(cpu_env); + + if (shmaddr & (shmlba - 1)) { + if (shmflg & SHM_RND) { + shmaddr &= ~(shmlba - 1); + } else { + return -TARGET_EINVAL; + } + } + if (!guest_range_valid_untagged(shmaddr, shm_info.shm_segsz)) { + return -TARGET_EINVAL; + } + + mmap_lock(); + + /* + * We're mapping shared memory, so 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 (!(cpu->tcg_cflags & CF_PARALLEL)) { + cpu->tcg_cflags |= CF_PARALLEL; + tb_flush(cpu); + } + + if (shmaddr) { + host_raddr = shmat(shmid, (void *)g2h_untagged(shmaddr), shmflg); + } else { + abi_ulong mmap_start; + + /* In order to use the host shmat, we need to honor host SHMLBA. */ + mmap_start = mmap_find_vma(0, shm_info.shm_segsz, MAX(SHMLBA, shmlba)); + + if (mmap_start == -1) { + errno = ENOMEM; + host_raddr = (void *)-1; + } else { + host_raddr = shmat(shmid, g2h_untagged(mmap_start), + shmflg | SHM_REMAP); + } + } + + if (host_raddr == (void *)-1) { + mmap_unlock(); + return get_errno((intptr_t)host_raddr); + } + raddr = h2g((uintptr_t)host_raddr); + + page_set_flags(raddr, raddr + shm_info.shm_segsz - 1, + PAGE_VALID | PAGE_RESET | PAGE_READ | + (shmflg & SHM_RDONLY ? 0 : PAGE_WRITE)); + + for (i = 0; i < N_SHM_REGIONS; i++) { + if (!shm_regions[i].in_use) { + shm_regions[i].in_use = true; + shm_regions[i].start = raddr; + shm_regions[i].size = shm_info.shm_segsz; + break; + } + } + + mmap_unlock(); + return raddr; +} + +abi_long target_shmdt(abi_ulong shmaddr) +{ + int i; + abi_long rv; + + /* shmdt pointers are always untagged */ + + mmap_lock(); + + for (i = 0; i < N_SHM_REGIONS; ++i) { + if (shm_regions[i].in_use && shm_regions[i].start == shmaddr) { + shm_regions[i].in_use = false; + page_set_flags(shmaddr, shmaddr + shm_regions[i].size - 1, 0); + break; + } + } + rv = get_errno(shmdt(g2h_untagged(shmaddr))); + + mmap_unlock(); + + return rv; +} diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 9353268cc1..df001469eb 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -3725,14 +3725,6 @@ static abi_long do_socketcall(int num, abi_ulong vptr) } #endif -#define N_SHM_REGIONS 32 - -static struct shm_region { - abi_ulong start; - abi_ulong size; - bool in_use; -} shm_regions[N_SHM_REGIONS]; - #ifndef TARGET_SEMID64_DS /* asm-generic version of this struct */ struct target_semid64_ds @@ -4482,133 +4474,6 @@ static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf) return ret; } -#ifndef TARGET_FORCE_SHMLBA -/* For most architectures, SHMLBA is the same as the page size; - * some architectures have larger values, in which case they should - * define TARGET_FORCE_SHMLBA and provide a target_shmlba() function. - * This corresponds to the kernel arch code defining __ARCH_FORCE_SHMLBA - * and defining its own value for SHMLBA. - * - * The kernel also permits SHMLBA to be set by the architecture to a - * value larger than the page size without setting __ARCH_FORCE_SHMLBA; - * this means that addresses are rounded to the large size if - * SHM_RND is set but addresses not aligned to that size are not rejected - * as long as they are at least page-aligned. Since the only architecture - * which uses this is ia64 this code doesn't provide for that oddity. - */ -static inline abi_ulong target_shmlba(CPUArchState *cpu_env) -{ - return TARGET_PAGE_SIZE; -} -#endif - -static abi_ulong do_shmat(CPUArchState *cpu_env, int shmid, - abi_ulong shmaddr, int shmflg) -{ - CPUState *cpu = env_cpu(cpu_env); - abi_ulong raddr; - void *host_raddr; - struct shmid_ds shm_info; - int i, ret; - abi_ulong shmlba; - - /* shmat pointers are always untagged */ - - /* find out the length of the shared memory segment */ - ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info)); - if (is_error(ret)) { - /* can't get length, bail out */ - return ret; - } - - shmlba = target_shmlba(cpu_env); - - if (shmaddr & (shmlba - 1)) { - if (shmflg & SHM_RND) { - shmaddr &= ~(shmlba - 1); - } else { - return -TARGET_EINVAL; - } - } - if (!guest_range_valid_untagged(shmaddr, shm_info.shm_segsz)) { - return -TARGET_EINVAL; - } - - mmap_lock(); - - /* - * We're mapping shared memory, so 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 (!(cpu->tcg_cflags & CF_PARALLEL)) { - cpu->tcg_cflags |= CF_PARALLEL; - tb_flush(cpu); - } - - if (shmaddr) - host_raddr = shmat(shmid, (void *)g2h_untagged(shmaddr), shmflg); - else { - abi_ulong mmap_start; - - /* In order to use the host shmat, we need to honor host SHMLBA. */ - mmap_start = mmap_find_vma(0, shm_info.shm_segsz, MAX(SHMLBA, shmlba)); - - if (mmap_start == -1) { - errno = ENOMEM; - host_raddr = (void *)-1; - } else - host_raddr = shmat(shmid, g2h_untagged(mmap_start), - shmflg | SHM_REMAP); - } - - if (host_raddr == (void *)-1) { - mmap_unlock(); - return get_errno((intptr_t)host_raddr); - } - raddr = h2g((uintptr_t)host_raddr); - - page_set_flags(raddr, raddr + shm_info.shm_segsz - 1, - PAGE_VALID | PAGE_RESET | PAGE_READ | - (shmflg & SHM_RDONLY ? 0 : PAGE_WRITE)); - - for (i = 0; i < N_SHM_REGIONS; i++) { - if (!shm_regions[i].in_use) { - shm_regions[i].in_use = true; - shm_regions[i].start = raddr; - shm_regions[i].size = shm_info.shm_segsz; - break; - } - } - - mmap_unlock(); - return raddr; -} - -static inline abi_long do_shmdt(abi_ulong shmaddr) -{ - int i; - abi_long rv; - - /* shmdt pointers are always untagged */ - - mmap_lock(); - - for (i = 0; i < N_SHM_REGIONS; ++i) { - if (shm_regions[i].in_use && shm_regions[i].start == shmaddr) { - shm_regions[i].in_use = false; - page_set_flags(shmaddr, shmaddr + shm_regions[i].size - 1, 0); - break; - } - } - rv = get_errno(shmdt(g2h_untagged(shmaddr))); - - mmap_unlock(); - - return rv; -} - #ifdef TARGET_NR_ipc /* ??? This only works with linear mappings. */ /* do_ipc() must return target values and target errnos. */ @@ -4695,7 +4560,7 @@ static abi_long do_ipc(CPUArchState *cpu_env, default: { abi_ulong raddr; - raddr = do_shmat(cpu_env, first, ptr, second); + raddr = target_shmat(cpu_env, first, ptr, second); if (is_error(raddr)) return get_errno(raddr); if (put_user_ual(raddr, third)) @@ -4708,7 +4573,7 @@ static abi_long do_ipc(CPUArchState *cpu_env, } break; case IPCOP_shmdt: - ret = do_shmdt(ptr); + ret = target_shmdt(ptr); break; case IPCOP_shmget: @@ -11129,11 +10994,11 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1, #endif #ifdef TARGET_NR_shmat case TARGET_NR_shmat: - return do_shmat(cpu_env, arg1, arg2, arg3); + return target_shmat(cpu_env, arg1, arg2, arg3); #endif #ifdef TARGET_NR_shmdt case TARGET_NR_shmdt: - return do_shmdt(arg1); + return target_shmdt(arg1); #endif case TARGET_NR_fsync: return get_errno(fsync(arg1)); From patchwork Sun Aug 20 20:44:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 715206 Delivered-To: patch@linaro.org Received: by 2002:a5d:484e:0:b0:317:ecd7:513f with SMTP id n14csp1531663wrs; Sun, 20 Aug 2023 13:44:59 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEsvPNxoUN4LNxd2yl88veuusvG3fZ/S0jqYGtXhogArHmWdDaHHEyVZ8LgIiG5IPil2yP6 X-Received: by 2002:a0c:d989:0:b0:63c:ed11:7bf0 with SMTP id y9-20020a0cd989000000b0063ced117bf0mr5559236qvj.6.1692564299065; Sun, 20 Aug 2023 13:44:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692564299; cv=none; d=google.com; s=arc-20160816; b=JckIpMxheAE7SRO0eMrO+V+9G1YGct8bOCi0OWGdZ8FrbKaWkn45leuQ/FX4YK8dzg ttV5Fd1FXOL5kWbGS9qaUjsGQ+PhQs3XoXelD2da1bbK5dYc7eBKAQhkMG6YtKlRRxVa MTn94wvT4GUepFDp/7XPStJg2KD3KzhZjTvMqhb9i22ePGE2ou4AN3Qu/2Xc68zMjVno KItf5Q7UDHUb4FAJvb3fvOf2qcDuAZR0K57gtwWDbUgclNt5kMBZENZwTBZnQmnDMEE1 Sx2K/Ofl5L45YyE5y0h7bNNjZ45qEB7fbq4pD47VZ/q+wqUs1XkgyLwrZkA6PtcE3Vu0 MXqQ== 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=Y+pbl/I8yc9+qiiBx4oS2dhGsqACGwpE/srodEThD5g=; fh=VLyDSxEFBkv0vhe3LU868VMhL5H8YsEeY0KW/Q5O5ZU=; b=HAOXrb2o/XBR3zRNq2hqziSElkZoi73+cXh5057We4zhrbb/OVfLft5c1EHD4xqSu/ hFBXP+9ajgQVCvCxQHoeWgg+ZRB283uePDk6mYOrPQQjfZsI0h3LQzWUa0Fmbwd9HkyI XOynEaeUwwo6qfIS6N5TiouuA4l8lakhYfDmatQWuuI9B0Vc3DHOuiOp2o3ZlC3Nis56 C7pNGw3isLi9D+dQk0NefoTc0vutDj/MISyjGTgFTfe4Ppc2XymjR2WKVNKEyKEF3sly hl7WXvrzkmIGSV9LwUjf834Zg1ut+5aSLzr2NBkMVJvH4IFcrRogCnP54m1bN1+VDZjZ JODA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b="EZ/ByETh"; 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=fail (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 d9-20020a0cf0c9000000b0063cefebbd61si4047263qvl.106.2023.08.20.13.44.58 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 20 Aug 2023 13:44:59 -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=fail header.i=@linaro.org header.s=google header.b="EZ/ByETh"; 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=fail (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 1qXpHW-0004y7-Cx; Sun, 20 Aug 2023 16:44:18 -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 1qXpHU-0004wo-0d for qemu-devel@nongnu.org; Sun, 20 Aug 2023 16:44:16 -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 1qXpHR-0007kQ-Ef for qemu-devel@nongnu.org; Sun, 20 Aug 2023 16:44:15 -0400 Received: by mail-oi1-x22e.google.com with SMTP id 5614622812f47-3a85b9c6ccdso216947b6e.0 for ; Sun, 20 Aug 2023 13:44:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1692564252; x=1693169052; 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=Y+pbl/I8yc9+qiiBx4oS2dhGsqACGwpE/srodEThD5g=; b=EZ/ByEThr2TXyaggw5E8gnyMRfZU7/PAzpvzHWpiTU4RWB62j4M96vPVyEILfEAOwt 25HUZSFGex1w06NTq4E5rObGyNdCel1IunzfvcpnUH9kM7FY/yAySFg6hXbewsgq9MrT duDUGxMVChEtMnIb3pJlr1z750/UlSoPr7CTgKO++YKPPB0FYkiL5JTYatfuJUnIL+QL xBs1XwdLtSkKzJd+LxIGSbR3+jUZGWShgO86SE56oqhOxlUvsBuLekZVaMnFnkUExRuX 5SA67ETOiO1fG3m0gtr7jJ+HHbpjqBu4s9lmgtJrAl1NgkAEiQ0hrAwPg7YePg8hRi+x wNBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692564252; x=1693169052; 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=Y+pbl/I8yc9+qiiBx4oS2dhGsqACGwpE/srodEThD5g=; b=aq4Mzf8z3VaHij6qvRj1yGOp2tW5PgBt+PQYM4B3JpB/BWcntLqggkzRE15T4p4BTv yVvZmhqIaQoHL8p9VePbUzBbISlhC/3kCRsJ0OfCcDkFZT50voGjZbmjyDfqLTtHyU1H flZrMCYwZaGEg7DyJcscpcG0XFFLDyRXdxvW7lbh/p/OVwimvDwPo9DdNc3+XPAGNOeT hpp3Mknn7YEpX8TKCkO9CXRYAlisNxV1Atw0U8owU7r1Geu2+Hgy/v7FaG6h6demXZwI XzUNOOzxpYilTmPwMycVeD+sTu9y/gh0zm/RLNSadgFFSWaMvZzjw4sMDk/Q/RqdlRuO XtTQ== X-Gm-Message-State: AOJu0YxVt5xJ2IDqcvcbEqQHg1BZln6lllOibaT8VRt3Q2UCJ6j/nTEI opJN4dhDNFyftrmM1My9IlyaqctnynlXBKICDXs= X-Received: by 2002:a05:6808:1a0a:b0:3a7:4b9a:43c2 with SMTP id bk10-20020a0568081a0a00b003a74b9a43c2mr8484408oib.13.1692564251908; Sun, 20 Aug 2023 13:44:11 -0700 (PDT) Received: from stoup.. ([2602:47:d483:7301:347d:4d37:bdd7:f5e5]) by smtp.gmail.com with ESMTPSA id c7-20020aa78c07000000b0064fde7ae1ffsm1430149pfd.38.2023.08.20.13.44.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 20 Aug 2023 13:44:11 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: laurent@vivier.eu, imp@bsdimp.com, kariem.taha2.7@gmail.com Subject: [PATCH 2/4] linux-user: Use WITH_MMAP_LOCK_GUARD in target_{shmat, shmdt} Date: Sun, 20 Aug 2023 13:44:06 -0700 Message-Id: <20230820204408.327348-3-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230820204408.327348-1-richard.henderson@linaro.org> References: <20230820204408.327348-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 Move the CF_PARALLEL setting outside of the mmap lock. Signed-off-by: Richard Henderson --- linux-user/mmap.c | 98 ++++++++++++++++++++++------------------------- 1 file changed, 46 insertions(+), 52 deletions(-) diff --git a/linux-user/mmap.c b/linux-user/mmap.c index 3aeacd1ecd..f45b2d307c 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -1017,9 +1017,8 @@ abi_ulong target_shmat(CPUArchState *cpu_env, int shmid, { CPUState *cpu = env_cpu(cpu_env); abi_ulong raddr; - void *host_raddr; struct shmid_ds shm_info; - int i, ret; + int ret; abi_ulong shmlba; /* shmat pointers are always untagged */ @@ -1044,7 +1043,43 @@ abi_ulong target_shmat(CPUArchState *cpu_env, int shmid, return -TARGET_EINVAL; } - mmap_lock(); + WITH_MMAP_LOCK_GUARD() { + void *host_raddr; + + if (shmaddr) { + host_raddr = shmat(shmid, (void *)g2h_untagged(shmaddr), shmflg); + } else { + abi_ulong mmap_start; + + /* In order to use the host shmat, we need to honor host SHMLBA. */ + mmap_start = mmap_find_vma(0, shm_info.shm_segsz, + MAX(SHMLBA, shmlba)); + + if (mmap_start == -1) { + return -TARGET_ENOMEM; + } + host_raddr = shmat(shmid, g2h_untagged(mmap_start), + shmflg | SHM_REMAP); + } + + if (host_raddr == (void *)-1) { + return get_errno(-1); + } + raddr = h2g(host_raddr); + + page_set_flags(raddr, raddr + shm_info.shm_segsz - 1, + PAGE_VALID | PAGE_RESET | PAGE_READ | + (shmflg & SHM_RDONLY ? 0 : PAGE_WRITE)); + + for (int i = 0; i < N_SHM_REGIONS; i++) { + if (!shm_regions[i].in_use) { + shm_regions[i].in_use = true; + shm_regions[i].start = raddr; + shm_regions[i].size = shm_info.shm_segsz; + break; + } + } + } /* * We're mapping shared memory, so ensure we generate code for parallel @@ -1057,65 +1092,24 @@ abi_ulong target_shmat(CPUArchState *cpu_env, int shmid, tb_flush(cpu); } - if (shmaddr) { - host_raddr = shmat(shmid, (void *)g2h_untagged(shmaddr), shmflg); - } else { - abi_ulong mmap_start; - - /* In order to use the host shmat, we need to honor host SHMLBA. */ - mmap_start = mmap_find_vma(0, shm_info.shm_segsz, MAX(SHMLBA, shmlba)); - - if (mmap_start == -1) { - errno = ENOMEM; - host_raddr = (void *)-1; - } else { - host_raddr = shmat(shmid, g2h_untagged(mmap_start), - shmflg | SHM_REMAP); - } - } - - if (host_raddr == (void *)-1) { - mmap_unlock(); - return get_errno((intptr_t)host_raddr); - } - raddr = h2g((uintptr_t)host_raddr); - - page_set_flags(raddr, raddr + shm_info.shm_segsz - 1, - PAGE_VALID | PAGE_RESET | PAGE_READ | - (shmflg & SHM_RDONLY ? 0 : PAGE_WRITE)); - - for (i = 0; i < N_SHM_REGIONS; i++) { - if (!shm_regions[i].in_use) { - shm_regions[i].in_use = true; - shm_regions[i].start = raddr; - shm_regions[i].size = shm_info.shm_segsz; - break; - } - } - - mmap_unlock(); return raddr; } abi_long target_shmdt(abi_ulong shmaddr) { - int i; abi_long rv; /* shmdt pointers are always untagged */ - mmap_lock(); - - for (i = 0; i < N_SHM_REGIONS; ++i) { - if (shm_regions[i].in_use && shm_regions[i].start == shmaddr) { - shm_regions[i].in_use = false; - page_set_flags(shmaddr, shmaddr + shm_regions[i].size - 1, 0); - break; + WITH_MMAP_LOCK_GUARD() { + for (int i = 0; i < N_SHM_REGIONS; ++i) { + if (shm_regions[i].in_use && shm_regions[i].start == shmaddr) { + shm_regions[i].in_use = false; + page_set_flags(shmaddr, shmaddr + shm_regions[i].size - 1, 0); + break; + } } + rv = get_errno(shmdt(g2h_untagged(shmaddr))); } - rv = get_errno(shmdt(g2h_untagged(shmaddr))); - - mmap_unlock(); - return rv; } From patchwork Sun Aug 20 20:44:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 715205 Delivered-To: patch@linaro.org Received: by 2002:a5d:484e:0:b0:317:ecd7:513f with SMTP id n14csp1531637wrs; Sun, 20 Aug 2023 13:44:52 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEIPjF31+hpsV+vbBxNNxVAY1BqcCrn65A4Ryg1MUGZ64xC2QAKrMJXII4dTqp73tGplfNw X-Received: by 2002:a05:620a:2ac8:b0:76c:c20f:2d10 with SMTP id bn8-20020a05620a2ac800b0076cc20f2d10mr6154767qkb.61.1692564292592; Sun, 20 Aug 2023 13:44:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692564292; cv=none; d=google.com; s=arc-20160816; b=ixV4uguq2KqVaMhieXTRhO/ejFE0xTBU4M/6re1RhCmm3AhntYx2bKQVvW1oybKxWv HMEVJucz1xz/ULyGNngYzfD8RFVsfAC1Fy7ExExosVzDgby3vmhsn+32M/elpKUybBwy DGQrCP0QFhLtZsrydTP0FlZgh7+j6F2zE/3M9UNBMZDVjbHLjvac+cnu8qgsXQHFlVWb ibZUOUGCnIvAsqlt43YQbmI0TqcF6fInhnn8WC9PbdGSiv4cTO4zETXieo8UmuWPafl1 mRHms57KnYBdWyJaNgv1SAlFIcpHw1m4nrDj1H8XXP64VrHVnw0iBMV1JAtzmYMAZVRQ H3zw== 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=w5ksMQj35m+yCeIWGrGNigy7JAnPo0mf1QEQxYD2q74=; fh=VLyDSxEFBkv0vhe3LU868VMhL5H8YsEeY0KW/Q5O5ZU=; b=JrUG392mmZ6ciWf0vjqEebvvEJybabKNsuuyiBOz2objmg6HKuBAmNKL9LtiwnZZfT bjWT3A3IU1gPVIR/4vGqQZlYVW5KwEpJMOX8SpycdZFnrtvGHMHPtivyuUw/i6tprm9v fosMfJbmU3qRLCKmkJhLL4nk0Jvs+rnm1g9WHWRC9gVsdlI0jqQSNGQFEqNf4V227Xsr 2GoC0+k5fPog6dYUxoc16KeiafsxWKRC5vV5DkZqXsDkxzFd8WO2GIyGQSHbXkDd1+8J v9q6UmpO1mTxpycoPMxTNiwlZJAPjmuFlwNR3HEwkjhW3/e1X26q9My4mbz58wT408Cd caEA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=SuyWFW9e; 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 a17-20020a0ce391000000b00649a1860fe1si4140990qvl.399.2023.08.20.13.44.52 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 20 Aug 2023 13:44:52 -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=SuyWFW9e; 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 1qXpHV-0004xE-Jc; Sun, 20 Aug 2023 16:44:17 -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 1qXpHU-0004wp-0n for qemu-devel@nongnu.org; Sun, 20 Aug 2023 16:44:16 -0400 Received: from mail-oi1-x234.google.com ([2607:f8b0:4864:20::234]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qXpHR-0007kS-Pp for qemu-devel@nongnu.org; Sun, 20 Aug 2023 16:44:15 -0400 Received: by mail-oi1-x234.google.com with SMTP id 5614622812f47-3a8506f5b73so827941b6e.0 for ; Sun, 20 Aug 2023 13:44:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1692564252; x=1693169052; 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=w5ksMQj35m+yCeIWGrGNigy7JAnPo0mf1QEQxYD2q74=; b=SuyWFW9ej64lgYdJXhALlEdw5t87+LOrwCfZdIZu1IDlBJKfdCEb8LgNBMCy0cPYQg PpoUHuA0uB2V38JnFEcfSq0n59r54GyfswghCJvK3HjBXUT+UWbNxJkH5u+Sb2a24d7z l8i27ohh10xMH8wByxELPIpM5f0Q5hQ7uUTB/14w7Yp4sf/yhjHkn+diJk+2uNMk914I 8ZQm4+CDRUtJkS98+WUof4y3jpoIs2Gb9cDp7RZSt3vVEJeAI/ga5SVJxr4Lvmq2VZFg wxSWWz30M6GvLL6YiUFOeMrwNWRA2I1GvU72OeqJdYgyUuiM6sjIHQ0iVluJS+pJm+MC 42Lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692564252; x=1693169052; 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=w5ksMQj35m+yCeIWGrGNigy7JAnPo0mf1QEQxYD2q74=; b=c4phwQfm+IDhaaHfhu0TRNd7x+kdGci19afUuthHjNi2a4E42TDGsDwp1k6oxGMUBk sAaWdw+8qXdKQj7/nXK8SBXKfpJGmDmRMxiQ4P+GVaDrPvNSpnuYDlLrjlkWg2tqFYtY SEQYbLd+4sekGp6NzJvFY7BVX6vpwKR1IappysilBp9b9B1Gk599XI7E8wIXtOgIcwn+ DaO8kY2OnQozD0zR5AO+jgvo2nEf+eFE8G1HMPEAsYfvZyxgRSoJrRZ5K33uK3tR4sMK D77A88uo87/ngnQ+h7xP9abUFKC9033gARLJaoQ+DEK5QqpD6i6xChv9RJUPpKHct9LE Y9Vw== X-Gm-Message-State: AOJu0YzP8GWSV+mwLpr9h0LIzTAJcUK2dxchIX4D7WRQHEfYPyLFVDjg BohpK7gljuho3hlalFJsWQltzA9koOwu5Eg7Dkk= X-Received: by 2002:a05:6808:2391:b0:3a4:35bb:edae with SMTP id bp17-20020a056808239100b003a435bbedaemr7899256oib.29.1692564252746; Sun, 20 Aug 2023 13:44:12 -0700 (PDT) Received: from stoup.. ([2602:47:d483:7301:347d:4d37:bdd7:f5e5]) by smtp.gmail.com with ESMTPSA id c7-20020aa78c07000000b0064fde7ae1ffsm1430149pfd.38.2023.08.20.13.44.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 20 Aug 2023 13:44:12 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: laurent@vivier.eu, imp@bsdimp.com, kariem.taha2.7@gmail.com Subject: [PATCH 3/4] linux-user: Fix shmdt Date: Sun, 20 Aug 2023 13:44:07 -0700 Message-Id: <20230820204408.327348-4-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230820204408.327348-1-richard.henderson@linaro.org> References: <20230820204408.327348-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::234; envelope-from=richard.henderson@linaro.org; helo=mail-oi1-x234.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 If the shm region is not mapped at shmaddr, EINVAL. Do not unmap the region until the syscall succeeds. Use mmap_reserve_or_unmap to preserve reserved_va semantics. Signed-off-by: Richard Henderson --- linux-user/mmap.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/linux-user/mmap.c b/linux-user/mmap.c index f45b2d307c..44116c014b 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -1102,14 +1102,25 @@ abi_long target_shmdt(abi_ulong shmaddr) /* shmdt pointers are always untagged */ WITH_MMAP_LOCK_GUARD() { - for (int i = 0; i < N_SHM_REGIONS; ++i) { + int i; + + for (i = 0; i < N_SHM_REGIONS; ++i) { if (shm_regions[i].in_use && shm_regions[i].start == shmaddr) { - shm_regions[i].in_use = false; - page_set_flags(shmaddr, shmaddr + shm_regions[i].size - 1, 0); break; } } + if (i == N_SHM_REGIONS) { + return -TARGET_EINVAL; + } + rv = get_errno(shmdt(g2h_untagged(shmaddr))); + if (rv == 0) { + abi_ulong size = shm_regions[i].size; + + shm_regions[i].in_use = false; + page_set_flags(shmaddr, shmaddr + size - 1, 0); + mmap_reserve_or_unmap(shmaddr, size); + } } return rv; } From patchwork Sun Aug 20 20:44:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 715204 Delivered-To: patch@linaro.org Received: by 2002:a5d:484e:0:b0:317:ecd7:513f with SMTP id n14csp1531589wrs; Sun, 20 Aug 2023 13:44:37 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFwFiumL58lpLrLrLTkoDicqexQJGtZ6Sc+VKke7a+1mUfaJfyZJWvf5spMkdYHGnsbCuDR X-Received: by 2002:ac8:5989:0:b0:410:9b5c:6507 with SMTP id e9-20020ac85989000000b004109b5c6507mr997641qte.46.1692564276858; Sun, 20 Aug 2023 13:44:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692564276; cv=none; d=google.com; s=arc-20160816; b=w+NGKV1NqWjKigyeZQsMjSvqFfbhvFWH8YkaphYpxJd/2ZMJkjZrGPaYZOwrciK5GC u6vJ9hfYSjd5PBN+h9lTRr+nn/xzhgey/H9PtJLzCfXyytMWEk45/mFXg+rLa+JxyLEv whrhVzl9NhTQDltmTZgIXziP8t3lnSPZkHkHWKV10WGyYLFCYAdUFAMUcillNRXHT8pK 7dMVHFpAj0gApASLY51LEE4G80L00iHf73DdARuU2c/0XZHYVWQK8TxVIHkeOmqS0zBd 9m7UbFOGr9u25yjffiPd3berTK7Ttx/w4BDHl7wGHToCbfqB0xM9l27OMhd6XbyyZLDy 3yRA== 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=UN+H+/GfTniCDQqNqoi86BdHI3S12YJ9y5ScYnnODH0=; fh=VLyDSxEFBkv0vhe3LU868VMhL5H8YsEeY0KW/Q5O5ZU=; b=Y9802+cJQ/ofkNVQg8LFY1VGbHaALLsqNUcLofKpuRSfWoHATdTNVq7k57PJwUBEPB EeerSrJxNJ5kuCm+2VQnsli1MbU5/R8RCLZdlcw4rtygvoq/iDnszFj+bIKouoemeuaj /lBfvV+jHVHoR8yeiAW9Ye36KSK3h2YAkBIb2J2//npbBmfKeXPwCjeJACMaZBiPdSdB +dVss/PpwHwdI/TflahYOcdHMOQt6G9QzDsPmhkBMwZLine40ZV7A1vcr4eDnFLzw3WK WXk2OXq4i/X6wrwqHcHJBzpghkUZbuqv7Mwxvowamqxdv8jiQPniuemkOD11UNSHQcdG Hy9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=CxCjfDjM; 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 v5-20020a05622a014500b0040345bf42efsi4003351qtw.552.2023.08.20.13.44.36 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 20 Aug 2023 13:44:36 -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=CxCjfDjM; 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 1qXpHY-0004yk-CT; Sun, 20 Aug 2023 16:44:20 -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 1qXpHV-0004xF-OM for qemu-devel@nongnu.org; Sun, 20 Aug 2023 16:44:17 -0400 Received: from mail-ot1-x32a.google.com ([2607:f8b0:4864:20::32a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qXpHT-0007kk-9l for qemu-devel@nongnu.org; Sun, 20 Aug 2023 16:44:17 -0400 Received: by mail-ot1-x32a.google.com with SMTP id 46e09a7af769-6bca5d6dcedso2202182a34.1 for ; Sun, 20 Aug 2023 13:44:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1692564254; x=1693169054; 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=UN+H+/GfTniCDQqNqoi86BdHI3S12YJ9y5ScYnnODH0=; b=CxCjfDjMg+WEGWFKC6MY8TWAL6WjSzrsbcDmjQj6XF3hi4Qgsz+pqWv2q4/Iew0qIQ 8XJ6qUyQc6HrD6oaN8Ydee+6TLmA2X74u93dipKhxxdjbZWAo7VQIl0f2/F3AU0igFj+ 7OUQ1HrUavDwPvDXOmurOLrKlU5uc/qhBGn0o/sIMWTO/FT1vEjS4hzme05JooO9HApc 1TGnGeZYkEKBwGmRRi6rUF7ShYqLpehFJypubUxPb6WJpBPdbMYAVF3KgJfMuUPHmcCI 46FMX6miXNnPn8SZIro+H32dhY8/XtU1UIFyhFtFKpL6/hp//xW4uCaxRU4IK9IZOkFl ChQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692564254; x=1693169054; 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=UN+H+/GfTniCDQqNqoi86BdHI3S12YJ9y5ScYnnODH0=; b=lK4kxo+U8UqW42UclF+t7mhGY5fbo2OtPqx4GVH4nXVQf05gCSEhRJjeVqKn8hJRym G8Mtyt15R3yXYIMath4OCU4iZGx/qfBhvM3i57xcPac76IT6J5u1TcjKfdiWBp2x5OEI cztvxygwPbzFGBLCemOGZii5dthm5sCguXu4LZLW8PbbWvGce1SAojEaZUeDlSUdmoD4 wcWSCVvNZvDMIBfytF0YRdX6tY3nYGcjYvpx1pc3jXhr0EjWRlAT4qHliKdUo0PiwHJZ d1qTvBvSEysk+cW0tt+elKU8qRcumKFMS35/oM9IU8N3rIgBBYazL3Wfg0TVCNVWz6uD b6uQ== X-Gm-Message-State: AOJu0YyS0V/kjp06rYcWSZyUFvU6p3wcl/g0X9NvUnzm/PWiTPElL11X mvBbDC9GpJ2wfYJ62IFMp9sKC19NBYVudCEdWRA= X-Received: by 2002:a05:6870:b524:b0:1bb:a227:7008 with SMTP id v36-20020a056870b52400b001bba2277008mr6454963oap.3.1692564253964; Sun, 20 Aug 2023 13:44:13 -0700 (PDT) Received: from stoup.. ([2602:47:d483:7301:347d:4d37:bdd7:f5e5]) by smtp.gmail.com with ESMTPSA id c7-20020aa78c07000000b0064fde7ae1ffsm1430149pfd.38.2023.08.20.13.44.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 20 Aug 2023 13:44:13 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: laurent@vivier.eu, imp@bsdimp.com, kariem.taha2.7@gmail.com Subject: [PATCH 4/4] linux-user: Track shm regions with an interval tree Date: Sun, 20 Aug 2023 13:44:08 -0700 Message-Id: <20230820204408.327348-5-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230820204408.327348-1-richard.henderson@linaro.org> References: <20230820204408.327348-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::32a; envelope-from=richard.henderson@linaro.org; helo=mail-ot1-x32a.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 Remove the fixed size shm_regions[] array. Remove references when other mappings completely remove or replace a region. Signed-off-by: Richard Henderson --- linux-user/mmap.c | 81 +++++++++++++++++++++++++++++++---------------- 1 file changed, 53 insertions(+), 28 deletions(-) diff --git a/linux-user/mmap.c b/linux-user/mmap.c index 44116c014b..8eaf57b208 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -24,18 +24,11 @@ #include "user-internals.h" #include "user-mmap.h" #include "target_mman.h" +#include "qemu/interval-tree.h" static pthread_mutex_t mmap_mutex = PTHREAD_MUTEX_INITIALIZER; static __thread int mmap_lock_count; -#define N_SHM_REGIONS 32 - -static struct shm_region { - abi_ulong start; - abi_ulong size; - bool in_use; -} shm_regions[N_SHM_REGIONS]; - void mmap_lock(void) { if (mmap_lock_count++ == 0) { @@ -73,6 +66,44 @@ void mmap_fork_end(int child) } } +/* Protected by mmap_lock. */ +static IntervalTreeRoot shm_regions; + +static void shm_region_add(abi_ptr start, abi_ptr last) +{ + IntervalTreeNode *i = g_new0(IntervalTreeNode, 1); + + i->start = start; + i->last = last; + interval_tree_insert(i, &shm_regions); +} + +static abi_ptr shm_region_find(abi_ptr start) +{ + IntervalTreeNode *i; + + for (i = interval_tree_iter_first(&shm_regions, start, start); i; + i = interval_tree_iter_next(i, start, start)) { + if (i->start == start) { + return i->last; + } + } + return 0; +} + +static void shm_region_rm_complete(abi_ptr start, abi_ptr last) +{ + IntervalTreeNode *i, *n; + + for (i = interval_tree_iter_first(&shm_regions, start, last); i; i = n) { + n = interval_tree_iter_next(i, start, last); + if (i->start >= start && i->last <= last) { + interval_tree_remove(i, &shm_regions); + g_free(i); + } + } +} + /* * Validate target prot bitmask. * Return the prot bitmask for the host in *HOST_PROT. @@ -729,6 +760,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot, page_set_flags(passthrough_last + 1, last, page_flags); } } + shm_region_rm_complete(start, last); the_end: trace_target_mmap_complete(start); if (qemu_loglevel_mask(CPU_LOG_PAGE)) { @@ -826,6 +858,7 @@ int target_munmap(abi_ulong start, abi_ulong len) mmap_lock(); mmap_reserve_or_unmap(start, len); page_set_flags(start, start + len - 1, 0); + shm_region_rm_complete(start, start + len - 1); mmap_unlock(); return 0; @@ -915,8 +948,10 @@ abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, new_addr = h2g(host_addr); prot = page_get_flags(old_addr); page_set_flags(old_addr, old_addr + old_size - 1, 0); + shm_region_rm_complete(old_addr, old_addr + old_size - 1); page_set_flags(new_addr, new_addr + new_size - 1, prot | PAGE_VALID | PAGE_RESET); + shm_region_rm_complete(new_addr, new_addr + new_size - 1); } mmap_unlock(); return new_addr; @@ -1045,6 +1080,7 @@ abi_ulong target_shmat(CPUArchState *cpu_env, int shmid, WITH_MMAP_LOCK_GUARD() { void *host_raddr; + abi_ulong last; if (shmaddr) { host_raddr = shmat(shmid, (void *)g2h_untagged(shmaddr), shmflg); @@ -1066,19 +1102,14 @@ abi_ulong target_shmat(CPUArchState *cpu_env, int shmid, return get_errno(-1); } raddr = h2g(host_raddr); + last = raddr + shm_info.shm_segsz - 1; - page_set_flags(raddr, raddr + shm_info.shm_segsz - 1, + page_set_flags(raddr, last, PAGE_VALID | PAGE_RESET | PAGE_READ | (shmflg & SHM_RDONLY ? 0 : PAGE_WRITE)); - for (int i = 0; i < N_SHM_REGIONS; i++) { - if (!shm_regions[i].in_use) { - shm_regions[i].in_use = true; - shm_regions[i].start = raddr; - shm_regions[i].size = shm_info.shm_segsz; - break; - } - } + shm_region_rm_complete(raddr, last); + shm_region_add(raddr, last); } /* @@ -1102,23 +1133,17 @@ abi_long target_shmdt(abi_ulong shmaddr) /* shmdt pointers are always untagged */ WITH_MMAP_LOCK_GUARD() { - int i; - - for (i = 0; i < N_SHM_REGIONS; ++i) { - if (shm_regions[i].in_use && shm_regions[i].start == shmaddr) { - break; - } - } - if (i == N_SHM_REGIONS) { + abi_ulong last = shm_region_find(shmaddr); + if (last == 0) { return -TARGET_EINVAL; } rv = get_errno(shmdt(g2h_untagged(shmaddr))); if (rv == 0) { - abi_ulong size = shm_regions[i].size; + abi_ulong size = last - shmaddr + 1; - shm_regions[i].in_use = false; - page_set_flags(shmaddr, shmaddr + size - 1, 0); + page_set_flags(shmaddr, last, 0); + shm_region_rm_complete(shmaddr, last); mmap_reserve_or_unmap(shmaddr, size); } }