From nobody Thu Nov 28 10:43:51 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1692839022; cv=none; d=zohomail.com; s=zohoarc; b=nw20ry4SUY/5fSLOFPYun8cr72b+OJHC6z1eUgyEwN79KiWg40OarcyTWE+qdfjDmFhK0CoJTPYFIP4LdDC34quce4t/U4tGc5X0D28YHhAyC6U3pb7Jrce1QV+pwlKBWjWT49w9LVmhOOqgWwidU3maHfGf1z0eLc9BFz74XnM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1692839022; h=Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=UN+H+/GfTniCDQqNqoi86BdHI3S12YJ9y5ScYnnODH0=; b=c1sob0WKuFlPsDs1dI+4Rfd8QODZRV2K2TpxTRcYZoEv6RqM1S2QltgUi0RRXL3CxsfKuTTmPgaWelxyeU3GdpWPbSmPnngLIm/xHnmpP5trNQHl90qDS3GiO5G0A6ry7BvE3FW8YjMjibdAzzhUf7GywqXNYO7UeCj8c0LxzF0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1692839022482895.8434636331882; Wed, 23 Aug 2023 18:03:42 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qYykQ-0000V2-CU; Wed, 23 Aug 2023 21:02:54 -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 1qYykP-0000Qv-H8 for qemu-devel@nongnu.org; Wed, 23 Aug 2023 21:02:53 -0400 Received: from mail-pg1-x530.google.com ([2607:f8b0:4864:20::530]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qYykN-0000Sm-2s for qemu-devel@nongnu.org; Wed, 23 Aug 2023 21:02:53 -0400 Received: by mail-pg1-x530.google.com with SMTP id 41be03b00d2f7-563f8e8a53dso3647145a12.3 for ; Wed, 23 Aug 2023 18:02:50 -0700 (PDT) Received: from stoup.. ([2602:47:d483:7301:72f0:1ea7:85d2:141e]) by smtp.gmail.com with ESMTPSA id c10-20020aa78c0a000000b006875df4773fsm2050789pfd.163.2023.08.23.18.02.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Aug 2023 18:02:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1692838969; x=1693443769; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=UN+H+/GfTniCDQqNqoi86BdHI3S12YJ9y5ScYnnODH0=; b=OmIoDg8z1OjtcnAW2jF/7FS2KO+ERxDBZ96xWzyHurHPJQrMrFvSlXvWTOGQOWDd4G qOH94u+TkJ/bwhVkPFsrx8yYmWG7ZWfHc+RU4CUvA69cAnG2l/nCuFTo1dKerpsZ1WRy Yh7LNkEXiCgDrox+wDWGTaNwOEKLb9RX7wUzyWwBVAJZWt902ypLQyk1zJID/XitUyIf 9HuctgRO+eIUkn8YrsJtaE63fuxRbJLBGKXxqJxcSNS3AblkbxtsTACgxlBisoWmZ6Ow AIGUxMJZ4Pqi0ve5/IRJB2NTRw6ZE4SKPJ4CC5fKYXDCGjgCyfLtDgzTCwXlfIt8Jxvm 7k9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692838969; x=1693443769; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=UN+H+/GfTniCDQqNqoi86BdHI3S12YJ9y5ScYnnODH0=; b=RmnohysdnzkMAaqsiN9QlAh6a5H3Zi1zbKk9lQ3T+/W+dOjcgbnTmWqeq7bhUGWwLk vAkt/J3bDNhPOAkp35HumtXl1z8Yp1XtY6DhA2GiFoS4QQTtj8GSVvH3zDikDgy2FLsz e33gOpOF2tugAho8XVdD9Q5XMr1jY0oo1DBMizH8yHtNMnBxki7rALb7UlZLqPkblobz gBLMIJ9oHJwWwedcagjMEvW/OtkmgMCo9KrNtu2Xz+CsG7SWzUWfk/aIbZC77wOjv0UM ttnqRpI7aV6fTZ3hN0Mz1yD+FFnDaC2j3B+1Q0YXb/F/XBJOyQXRwPE3VT5T9Ea8wQcA 7HDA== X-Gm-Message-State: AOJu0YwhzVPRwSOBzUmE03UfZf4GL0ywMf68RDZoy3k7pjM6ykRQthpz gNc6lnS7VW9MpaJLW2FCi6JgUQ3jXgY6ip0Gv18= X-Google-Smtp-Source: AGHT+IFGOwhPb0tp2gvGCMfGSBEQdXxyU87HV6wtyE9gncPKj+BYT3zv51B5a1ZIhErgM3+Xu6uVHg== X-Received: by 2002:a05:6a21:3b4a:b0:14b:8023:33b3 with SMTP id zy10-20020a056a213b4a00b0014b802333b3mr3451667pzb.19.1692838969324; Wed, 23 Aug 2023 18:02:49 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH 13/13] linux-user: Track shm regions with an interval tree Date: Wed, 23 Aug 2023 18:02:37 -0700 Message-Id: <20230824010237.1379735-14-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230824010237.1379735-1-richard.henderson@linaro.org> References: <20230824010237.1379735-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::530; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x530.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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1692839025078100010 Content-Type: text/plain; charset="utf-8" Remove the fixed size shm_regions[] array. Remove references when other mappings completely remove or replace a region. Signed-off-by: Richard Henderson Reviewed-by: Helge Deller --- 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" =20 static pthread_mutex_t mmap_mutex =3D PTHREAD_MUTEX_INITIALIZER; static __thread int mmap_lock_count; =20 -#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++ =3D=3D 0) { @@ -73,6 +66,44 @@ void mmap_fork_end(int child) } } =20 +/* Protected by mmap_lock. */ +static IntervalTreeRoot shm_regions; + +static void shm_region_add(abi_ptr start, abi_ptr last) +{ + IntervalTreeNode *i =3D g_new0(IntervalTreeNode, 1); + + i->start =3D start; + i->last =3D last; + interval_tree_insert(i, &shm_regions); +} + +static abi_ptr shm_region_find(abi_ptr start) +{ + IntervalTreeNode *i; + + for (i =3D interval_tree_iter_first(&shm_regions, start, start); i; + i =3D interval_tree_iter_next(i, start, start)) { + if (i->start =3D=3D start) { + return i->last; + } + } + return 0; +} + +static void shm_region_rm_complete(abi_ptr start, abi_ptr last) +{ + IntervalTreeNode *i, *n; + + for (i =3D interval_tree_iter_first(&shm_regions, start, last); i; i = =3D n) { + n =3D interval_tree_iter_next(i, start, last); + if (i->start >=3D start && i->last <=3D 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, in= t 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(); =20 return 0; @@ -915,8 +948,10 @@ abi_long target_mremap(abi_ulong old_addr, abi_ulong o= ld_size, new_addr =3D h2g(host_addr); prot =3D 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 shm= id, =20 WITH_MMAP_LOCK_GUARD() { void *host_raddr; + abi_ulong last; =20 if (shmaddr) { host_raddr =3D shmat(shmid, (void *)g2h_untagged(shmaddr), shm= flg); @@ -1066,19 +1102,14 @@ abi_ulong target_shmat(CPUArchState *cpu_env, int s= hmid, return get_errno(-1); } raddr =3D h2g(host_raddr); + last =3D raddr + shm_info.shm_segsz - 1; =20 - 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)); =20 - for (int i =3D 0; i < N_SHM_REGIONS; i++) { - if (!shm_regions[i].in_use) { - shm_regions[i].in_use =3D true; - shm_regions[i].start =3D raddr; - shm_regions[i].size =3D shm_info.shm_segsz; - break; - } - } + shm_region_rm_complete(raddr, last); + shm_region_add(raddr, last); } =20 /* @@ -1102,23 +1133,17 @@ abi_long target_shmdt(abi_ulong shmaddr) /* shmdt pointers are always untagged */ =20 WITH_MMAP_LOCK_GUARD() { - int i; - - for (i =3D 0; i < N_SHM_REGIONS; ++i) { - if (shm_regions[i].in_use && shm_regions[i].start =3D=3D shmad= dr) { - break; - } - } - if (i =3D=3D N_SHM_REGIONS) { + abi_ulong last =3D shm_region_find(shmaddr); + if (last =3D=3D 0) { return -TARGET_EINVAL; } =20 rv =3D get_errno(shmdt(g2h_untagged(shmaddr))); if (rv =3D=3D 0) { - abi_ulong size =3D shm_regions[i].size; + abi_ulong size =3D last - shmaddr + 1; =20 - shm_regions[i].in_use =3D 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); } } --=20 2.34.1