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); } }