From patchwork Fri May 24 10:51:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgar E. Iglesias" X-Patchwork-Id: 13673066 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D5DCAC41513 for ; Fri, 24 May 2024 10:53:20 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sASWr-0006Rc-PK; Fri, 24 May 2024 06:52:05 -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 1sASWn-0006NS-Sg for qemu-devel@nongnu.org; Fri, 24 May 2024 06:52:01 -0400 Received: from mail-lj1-x232.google.com ([2a00:1450:4864:20::232]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sASWl-0007df-J6 for qemu-devel@nongnu.org; Fri, 24 May 2024 06:52:01 -0400 Received: by mail-lj1-x232.google.com with SMTP id 38308e7fff4ca-2e95a74d51fso11426781fa.2 for ; Fri, 24 May 2024 03:51:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716547916; x=1717152716; 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=oKmmum6z28VcY8ouSg3YJA3bHZvP87XkBXtO/hd8PR8=; b=IJSqbwhBuXiz4zeiwaZYzUDitgTNinLgSS4bIBNbBCuQdp9HfhThvTnuTQhE93p0ij NPR6RnJHzGPTL/Ig/XCaw7Iy3cY5q3PWyH3mO+iFvPCGEPmy1EhKBKiSfc+uYhs1YgKi KNAFBXhh7by7Ed0+NRuffvCFgG7n/4HZtoEQpHtaEk035Ya2kji7WDOuQmM+Ox0PaaEy hvgaDY2NPAAeNqNvRY62qfkTYXKFmhQuZ3VITpwOm9rkLN6Svbi993xEYJBgKuizEbob tRrexuzeV3sUr7ct1oHtTzih0qoi7g2ehEO5o0s8Awccy6jZ/NIYxq99MOmhG0PnhRyp 8O5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716547916; x=1717152716; 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=oKmmum6z28VcY8ouSg3YJA3bHZvP87XkBXtO/hd8PR8=; b=sGMSJ5LWv6sFBCpNHCHQ5RepOy3+a6dd9hUZj04QL6aCgeYiYd0yveo4kYNxZb1MFo jfUglJPkDkRdE1lgFBhtKKJZwtV2BFJn6WCjLnghC1dK1NS5SPBHmphq4XBI/NBafXVt LFHiMqykwD6sUbN61GoeqZc4XrVJjbL7MHaT3v3A40Q1z7ytA0PlhiHTHMJAmu7egzi+ oVwN3lnx02kaZ57Caw90/ytdpb09NIWUciQn0qlFdYnLwWsnYSH4YjEsb63HhmTm6CDU CxPO77lsts38YOurhnAo4+ZFv/oYt9U/6COGkQSpoEnS/0I3PQ7Ag6gobwymDeNDaYXd 98gw== X-Gm-Message-State: AOJu0YwVeJ3ysXpk4KekxKOCT09bnXIkUmz8cJgdBIsTO8StaSEJlM7H c82XpGvz5cepHKLpOsVbL8Qs/DHZagepfJ5OnSWRMYmNsZSuCY2yb0WWaay9 X-Google-Smtp-Source: AGHT+IEGg/j9kr5jKj/tv+syV4xN0QPHZQ3VcNTdBVPyyQs2WD0/L2C8QLd0gT/jlHUTsM+RTiWqvg== X-Received: by 2002:a2e:b784:0:b0:2e0:6313:fe3a with SMTP id 38308e7fff4ca-2e95b256875mr16230641fa.35.1716547916526; Fri, 24 May 2024 03:51:56 -0700 (PDT) Received: from gmail.com (213-67-3-247-no600.tbcn.telia.com. [213.67.3.247]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-2e95bcd7c20sm1491561fa.33.2024.05.24.03.51.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 03:51:56 -0700 (PDT) From: "Edgar E. Iglesias" To: qemu-devel@nongnu.org Cc: edgar.iglesias@gmail.com, sstabellini@kernel.org, jgross@suse.com, "Edgar E. Iglesias" , Anthony PERARD , Paul Durrant , xen-devel@lists.xenproject.org Subject: [PATCH v7 1/8] xen: mapcache: Make MCACHE_BUCKET_SHIFT runtime configurable Date: Fri, 24 May 2024 12:51:45 +0200 Message-Id: <20240524105152.1301842-2-edgar.iglesias@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240524105152.1301842-1-edgar.iglesias@gmail.com> References: <20240524105152.1301842-1-edgar.iglesias@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::232; envelope-from=edgar.iglesias@gmail.com; helo=mail-lj1-x232.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, FREEMAIL_FROM=0.001, 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: "Edgar E. Iglesias" Make MCACHE_BUCKET_SHIFT runtime configurable per cache instance. Signed-off-by: Edgar E. Iglesias Reviewed-by: Stefano Stabellini --- hw/xen/xen-mapcache.c | 54 ++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/hw/xen/xen-mapcache.c b/hw/xen/xen-mapcache.c index fa6813b1ad..bc860f4373 100644 --- a/hw/xen/xen-mapcache.c +++ b/hw/xen/xen-mapcache.c @@ -23,13 +23,10 @@ #if HOST_LONG_BITS == 32 -# define MCACHE_BUCKET_SHIFT 16 # define MCACHE_MAX_SIZE (1UL<<31) /* 2GB Cap */ #else -# define MCACHE_BUCKET_SHIFT 20 # define MCACHE_MAX_SIZE (1UL<<35) /* 32GB Cap */ #endif -#define MCACHE_BUCKET_SIZE (1UL << MCACHE_BUCKET_SHIFT) /* This is the size of the virtual address space reserve to QEMU that will not * be use by MapCache. @@ -65,7 +62,8 @@ typedef struct MapCache { /* For most cases (>99.9%), the page address is the same. */ MapCacheEntry *last_entry; unsigned long max_mcache_size; - unsigned int mcache_bucket_shift; + unsigned int bucket_shift; + unsigned long bucket_size; phys_offset_to_gaddr_t phys_offset_to_gaddr; QemuMutex lock; @@ -95,11 +93,14 @@ static inline int test_bits(int nr, int size, const unsigned long *addr) static MapCache *xen_map_cache_init_single(phys_offset_to_gaddr_t f, void *opaque, + unsigned int bucket_shift, unsigned long max_size) { unsigned long size; MapCache *mc; + assert(bucket_shift >= XC_PAGE_SHIFT); + mc = g_new0(MapCache, 1); mc->phys_offset_to_gaddr = f; @@ -108,12 +109,14 @@ static MapCache *xen_map_cache_init_single(phys_offset_to_gaddr_t f, QTAILQ_INIT(&mc->locked_entries); + mc->bucket_shift = bucket_shift; + mc->bucket_size = 1UL << bucket_shift; mc->max_mcache_size = max_size; mc->nr_buckets = (((mc->max_mcache_size >> XC_PAGE_SHIFT) + - (1UL << (MCACHE_BUCKET_SHIFT - XC_PAGE_SHIFT)) - 1) >> - (MCACHE_BUCKET_SHIFT - XC_PAGE_SHIFT)); + (1UL << (bucket_shift - XC_PAGE_SHIFT)) - 1) >> + (bucket_shift - XC_PAGE_SHIFT)); size = mc->nr_buckets * sizeof(MapCacheEntry); size = (size + XC_PAGE_SIZE - 1) & ~(XC_PAGE_SIZE - 1); @@ -126,6 +129,13 @@ void xen_map_cache_init(phys_offset_to_gaddr_t f, void *opaque) { struct rlimit rlimit_as; unsigned long max_mcache_size; + unsigned int bucket_shift; + + if (HOST_LONG_BITS == 32) { + bucket_shift = 16; + } else { + bucket_shift = 20; + } if (geteuid() == 0) { rlimit_as.rlim_cur = RLIM_INFINITY; @@ -146,7 +156,9 @@ void xen_map_cache_init(phys_offset_to_gaddr_t f, void *opaque) } } - mapcache = xen_map_cache_init_single(f, opaque, max_mcache_size); + mapcache = xen_map_cache_init_single(f, opaque, + bucket_shift, + max_mcache_size); setrlimit(RLIMIT_AS, &rlimit_as); } @@ -195,7 +207,7 @@ static void xen_remap_bucket(MapCache *mc, entry->valid_mapping = NULL; for (i = 0; i < nb_pfn; i++) { - pfns[i] = (address_index << (MCACHE_BUCKET_SHIFT-XC_PAGE_SHIFT)) + i; + pfns[i] = (address_index << (mc->bucket_shift - XC_PAGE_SHIFT)) + i; } /* @@ -266,8 +278,8 @@ static uint8_t *xen_map_cache_unlocked(MapCache *mc, bool dummy = false; tryagain: - address_index = phys_addr >> MCACHE_BUCKET_SHIFT; - address_offset = phys_addr & (MCACHE_BUCKET_SIZE - 1); + address_index = phys_addr >> mc->bucket_shift; + address_offset = phys_addr & (mc->bucket_size - 1); trace_xen_map_cache(phys_addr); @@ -294,14 +306,14 @@ tryagain: return mc->last_entry->vaddr_base + address_offset; } - /* size is always a multiple of MCACHE_BUCKET_SIZE */ + /* size is always a multiple of mc->bucket_size */ if (size) { cache_size = size + address_offset; - if (cache_size % MCACHE_BUCKET_SIZE) { - cache_size += MCACHE_BUCKET_SIZE - (cache_size % MCACHE_BUCKET_SIZE); + if (cache_size % mc->bucket_size) { + cache_size += mc->bucket_size - (cache_size % mc->bucket_size); } } else { - cache_size = MCACHE_BUCKET_SIZE; + cache_size = mc->bucket_size; } entry = &mc->entry[address_index % mc->nr_buckets]; @@ -422,7 +434,7 @@ static ram_addr_t xen_ram_addr_from_mapcache_single(MapCache *mc, void *ptr) trace_xen_ram_addr_from_mapcache_not_in_cache(ptr); raddr = RAM_ADDR_INVALID; } else { - raddr = (reventry->paddr_index << MCACHE_BUCKET_SHIFT) + + raddr = (reventry->paddr_index << mc->bucket_shift) + ((unsigned long) ptr - (unsigned long) entry->vaddr_base); } mapcache_unlock(mc); @@ -585,8 +597,8 @@ static uint8_t *xen_replace_cache_entry_unlocked(MapCache *mc, hwaddr address_index, address_offset; hwaddr test_bit_size, cache_size = size; - address_index = old_phys_addr >> MCACHE_BUCKET_SHIFT; - address_offset = old_phys_addr & (MCACHE_BUCKET_SIZE - 1); + address_index = old_phys_addr >> mc->bucket_shift; + address_offset = old_phys_addr & (mc->bucket_size - 1); assert(size); /* test_bit_size is always a multiple of XC_PAGE_SIZE */ @@ -595,8 +607,8 @@ static uint8_t *xen_replace_cache_entry_unlocked(MapCache *mc, test_bit_size += XC_PAGE_SIZE - (test_bit_size % XC_PAGE_SIZE); } cache_size = size + address_offset; - if (cache_size % MCACHE_BUCKET_SIZE) { - cache_size += MCACHE_BUCKET_SIZE - (cache_size % MCACHE_BUCKET_SIZE); + if (cache_size % mc->bucket_size) { + cache_size += mc->bucket_size - (cache_size % mc->bucket_size); } entry = &mc->entry[address_index % mc->nr_buckets]; @@ -609,8 +621,8 @@ static uint8_t *xen_replace_cache_entry_unlocked(MapCache *mc, return NULL; } - address_index = new_phys_addr >> MCACHE_BUCKET_SHIFT; - address_offset = new_phys_addr & (MCACHE_BUCKET_SIZE - 1); + address_index = new_phys_addr >> mc->bucket_shift; + address_offset = new_phys_addr & (mc->bucket_size - 1); trace_xen_replace_cache_entry_dummy(old_phys_addr, new_phys_addr); From patchwork Fri May 24 10:51:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgar E. Iglesias" X-Patchwork-Id: 13673072 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 86DD6C25B79 for ; Fri, 24 May 2024 10:53:44 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sASWv-0006Tg-ME; Fri, 24 May 2024 06:52:09 -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 1sASWp-0006Pr-9R for qemu-devel@nongnu.org; Fri, 24 May 2024 06:52:03 -0400 Received: from mail-lj1-x232.google.com ([2a00:1450:4864:20::232]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sASWn-0007do-M1 for qemu-devel@nongnu.org; Fri, 24 May 2024 06:52:03 -0400 Received: by mail-lj1-x232.google.com with SMTP id 38308e7fff4ca-2e95a75a90eso7845651fa.2 for ; Fri, 24 May 2024 03:52:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716547919; x=1717152719; 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=suqLmt7AH8VX+OrzngkGIik/kesngvj14F2vCYL7OFM=; b=g3iVZeH2rLzKjk86UDLyIpjROPNi5hcd1Vz9bSCOzqL/QKvCkVsL/yEVcMyaIRxqDP oGeZxquVKNQrxlDX1Azc3qo8hB1ZSrU1J1fdgTBSjY9g+F0o77o31/aMeJ2QcFPiLJJV GInaHlFttpbIur4SWkwvJl9jOnu3loXpp4gAtwaQfXlftUVl+r0Y1P8kcswCSNfZ0tsm LxK/JL9rjWiFmG/0h+zH17ItwtReTtx4fnewlKrnm2Jf9jaZBxDtByXzrQzcwrUgL6ev 6lN6G/jmiuXXw+f5CcYTGGPAfSJyIZsbdt92Z/BZbCkwKahJqQqiJFJIez07ilPMvCIS OUNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716547919; x=1717152719; 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=suqLmt7AH8VX+OrzngkGIik/kesngvj14F2vCYL7OFM=; b=b5Fg+VDp/AcLcAUg5nPVvY9YbKAHa7VmoZvC8JoRX7lNm/AQrN10j+mFFMczzk99Hz lp4V0FY7/mRC2lZLgamWFZcHeZ16py7Anmym482q5OcXYMp7OJMzcwMyKUFYuajSANlX 2F7tKuUzIz5KHVMFz5ICOedicO0bBH+H7k/jnNe4VBQ8UJ1wDanjJ9N77ocjvb5A7ccS RpUe3AvKsSvNe9IVX4Vc5B2jrc5rp0Ngk/E1p1I2xXxn7bhzqAvuIsR20rFrBTFv35ss X6DTSQWqecV5o9xLoZ2QF6XCV5L5CPtV42Fw8YC3yqKUSrgCxMUnqBTMupNKnXl1kb50 Stog== X-Gm-Message-State: AOJu0YzcYCPDxXoL8Bc//Thn74AFumhMeODWqJS6n1K7jMhVG0S0VSgu JcMCvaIDj9NJw4RhK58jxLcZu3rKstHdgrMhFvH2VT6gHkvJxK3aYFRfMlvO X-Google-Smtp-Source: AGHT+IG7/58L7fP8XfkA17hx/P2HA+GooUSMBu7K0MzZTbJxr4odywyDF44eC1SlJ9lDqF4xGtjtyw== X-Received: by 2002:a2e:8417:0:b0:2e5:751d:4b0f with SMTP id 38308e7fff4ca-2e95b2822ebmr10863631fa.34.1716547918939; Fri, 24 May 2024 03:51:58 -0700 (PDT) Received: from gmail.com (213-67-3-247-no600.tbcn.telia.com. [213.67.3.247]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-2e95bcf4c32sm1421121fa.66.2024.05.24.03.51.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 03:51:57 -0700 (PDT) From: "Edgar E. Iglesias" To: qemu-devel@nongnu.org Cc: edgar.iglesias@gmail.com, sstabellini@kernel.org, jgross@suse.com, "Edgar E. Iglesias" , Anthony PERARD , Paul Durrant , xen-devel@lists.xenproject.org Subject: [PATCH v7 2/8] xen: mapcache: Unmap first entries in buckets Date: Fri, 24 May 2024 12:51:46 +0200 Message-Id: <20240524105152.1301842-3-edgar.iglesias@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240524105152.1301842-1-edgar.iglesias@gmail.com> References: <20240524105152.1301842-1-edgar.iglesias@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::232; envelope-from=edgar.iglesias@gmail.com; helo=mail-lj1-x232.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, FREEMAIL_FROM=0.001, 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: "Edgar E. Iglesias" When invalidating memory ranges, if we happen to hit the first entry in a bucket we were never unmapping it. This was harmless for foreign mappings but now that we're looking to reuse the mapcache for transient grant mappings, we must unmap entries when invalidated. Signed-off-by: Edgar E. Iglesias Reviewed-by: Stefano Stabellini --- hw/xen/xen-mapcache.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/hw/xen/xen-mapcache.c b/hw/xen/xen-mapcache.c index bc860f4373..ec95445696 100644 --- a/hw/xen/xen-mapcache.c +++ b/hw/xen/xen-mapcache.c @@ -491,18 +491,23 @@ static void xen_invalidate_map_cache_entry_unlocked(MapCache *mc, return; } entry->lock--; - if (entry->lock > 0 || pentry == NULL) { + if (entry->lock > 0) { return; } - pentry->next = entry->next; ram_block_notify_remove(entry->vaddr_base, entry->size, entry->size); if (munmap(entry->vaddr_base, entry->size) != 0) { perror("unmap fails"); exit(-1); } + g_free(entry->valid_mapping); - g_free(entry); + if (pentry) { + pentry->next = entry->next; + g_free(entry); + } else { + memset(entry, 0, sizeof *entry); + } } typedef struct XenMapCacheData { From patchwork Fri May 24 10:51:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Edgar E. Iglesias" X-Patchwork-Id: 13673068 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 008FAC25B7C for ; Fri, 24 May 2024 10:53:21 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sASWz-0006Y4-Bq; Fri, 24 May 2024 06:52:13 -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 1sASWr-0006Rh-7V for qemu-devel@nongnu.org; Fri, 24 May 2024 06:52:05 -0400 Received: from mail-lj1-x22d.google.com ([2a00:1450:4864:20::22d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sASWp-0007e3-IF for qemu-devel@nongnu.org; Fri, 24 May 2024 06:52:04 -0400 Received: by mail-lj1-x22d.google.com with SMTP id 38308e7fff4ca-2e564cad1f1so98622171fa.0 for ; Fri, 24 May 2024 03:52:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716547921; x=1717152721; 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=YP28Iks2aCd2MOvUAbQc4yYuz/leCz2VtuKFu0fMfns=; b=ZwHKbq3LDOHkEFAlX7VFhqlOjB5h6uB5MhgEmvbGUA4OvEq3DW2Ca6DMDsxcaCcmji r90N8/OK+nB3wolEs2IQBJcgl94PKiYlyBp4Z+uJyv4tOniqqKFLrwk8R3soN9udgyX3 r+YJU0tPo5MKRvk5CufTKSYmJpkencQ2zGx1c7HxG3P7j+6/+IKQgwkWlL3bXyulor2x 5ge4l0bWcY3UwE2MS888OvogZm+ZOJgPvaEhZVkLziHNjL29k7eJHuSNYR8BHAj9W5Um FkY8q2roh1P2+8uipCTL1hob238Qm3p3nBuGK2H2KnqhaC1xVYjDjnbx6mLg06OQ4msE tCgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716547921; x=1717152721; 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=YP28Iks2aCd2MOvUAbQc4yYuz/leCz2VtuKFu0fMfns=; b=GYkcbBx5NJ7LRBGbX+r2S1v3JAPrXNqHy8mJQFBc90z8aOCpPaVoVbYtTbVGBkeUbA lam/LfAEZBSBw5YPVEOabpBw6P3F3uqxVGnrnFqfyFu4LeO1reQ7tvXzOogNEvxOJWU/ 1H4//vt3pK991SiXQZUdzvFBfso9x9Y9KQUqO2fKQwOIv+MA0q2NI9onrxYwgYC24nVP WnSVGkw/WjdMVioSR9pZPiA0rC+7+USCZvCNjH4EM6pwQLozkQ64DMCjvH7WrP32x7er wp+gVGkGIbnF1D3TjG5miPVLmasxFkuAQiAWbxhcnCpoTMi1d4KUmIaWS/nt6b02NwnR oLEw== X-Gm-Message-State: AOJu0Yz0O5pFmlcmDk2JvPpwLH1/ZYrzatxQYBTpPKbl7zPu2PEm7D6D n9e6r9JabwAunOFgP4caE7/xXQPNSdXPRG1KQFDMzUL0UJiiJnc3JbIdQ2V5 X-Google-Smtp-Source: AGHT+IHxf48OGn9vlQXF6Pl1G+8i2NXaRVtBjeCMc7C8+mcWr77Zr/it24OxvWZYA9xQzmTZ7G5dGA== X-Received: by 2002:a2e:6802:0:b0:2e5:67a8:2e6b with SMTP id 38308e7fff4ca-2e95b04115amr13608211fa.8.1716547920962; Fri, 24 May 2024 03:52:00 -0700 (PDT) Received: from gmail.com (213-67-3-247-no600.tbcn.telia.com. [213.67.3.247]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-2e95be00a9esm1417431fa.103.2024.05.24.03.51.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 03:51:59 -0700 (PDT) From: "Edgar E. Iglesias" To: qemu-devel@nongnu.org Cc: edgar.iglesias@gmail.com, sstabellini@kernel.org, jgross@suse.com, "Edgar E. Iglesias" , David Hildenbrand , Anthony PERARD , Paul Durrant , xen-devel@lists.xenproject.org Subject: [PATCH v7 3/8] xen: Add xen_mr_is_memory() Date: Fri, 24 May 2024 12:51:47 +0200 Message-Id: <20240524105152.1301842-4-edgar.iglesias@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240524105152.1301842-1-edgar.iglesias@gmail.com> References: <20240524105152.1301842-1-edgar.iglesias@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::22d; envelope-from=edgar.iglesias@gmail.com; helo=mail-lj1-x22d.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, FREEMAIL_FROM=0.001, 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: "Edgar E. Iglesias" Add xen_mr_is_memory() to abstract away tests for the xen_memory MR. No functional changes. Signed-off-by: Edgar E. Iglesias Reviewed-by: Stefano Stabellini Acked-by: David Hildenbrand Reviewed-by: Philippe Mathieu-Daudé --- hw/xen/xen-hvm-common.c | 10 ++++++++-- include/sysemu/xen.h | 8 ++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/hw/xen/xen-hvm-common.c b/hw/xen/xen-hvm-common.c index 2d1b032121..a0a0252da0 100644 --- a/hw/xen/xen-hvm-common.c +++ b/hw/xen/xen-hvm-common.c @@ -12,6 +12,12 @@ MemoryRegion xen_memory; +/* Check for xen memory. */ +bool xen_mr_is_memory(MemoryRegion *mr) +{ + return mr == &xen_memory; +} + void xen_ram_alloc(ram_addr_t ram_addr, ram_addr_t size, MemoryRegion *mr, Error **errp) { @@ -28,7 +34,7 @@ void xen_ram_alloc(ram_addr_t ram_addr, ram_addr_t size, MemoryRegion *mr, return; } - if (mr == &xen_memory) { + if (xen_mr_is_memory(mr)) { return; } @@ -55,7 +61,7 @@ static void xen_set_memory(struct MemoryListener *listener, { XenIOState *state = container_of(listener, XenIOState, memory_listener); - if (section->mr == &xen_memory) { + if (xen_mr_is_memory(section->mr)) { return; } else { if (add) { diff --git a/include/sysemu/xen.h b/include/sysemu/xen.h index 754ec2e6cb..dc72f83bcb 100644 --- a/include/sysemu/xen.h +++ b/include/sysemu/xen.h @@ -34,6 +34,8 @@ void xen_hvm_modified_memory(ram_addr_t start, ram_addr_t length); void xen_ram_alloc(ram_addr_t ram_addr, ram_addr_t size, struct MemoryRegion *mr, Error **errp); +bool xen_mr_is_memory(MemoryRegion *mr); + #else /* !CONFIG_XEN_IS_POSSIBLE */ #define xen_enabled() 0 @@ -47,6 +49,12 @@ static inline void xen_ram_alloc(ram_addr_t ram_addr, ram_addr_t size, g_assert_not_reached(); } +static inline bool xen_mr_is_memory(MemoryRegion *mr) +{ + g_assert_not_reached(); + return false; +} + #endif /* CONFIG_XEN_IS_POSSIBLE */ #endif From patchwork Fri May 24 10:51:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgar E. Iglesias" X-Patchwork-Id: 13673070 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CEAFAC27C43 for ; Fri, 24 May 2024 10:53:22 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sASWz-0006Y3-0f; Fri, 24 May 2024 06:52:13 -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 1sASWt-0006TX-RM for qemu-devel@nongnu.org; Fri, 24 May 2024 06:52:09 -0400 Received: from mail-lf1-x12f.google.com ([2a00:1450:4864:20::12f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sASWr-0007eM-5N for qemu-devel@nongnu.org; Fri, 24 May 2024 06:52:06 -0400 Received: by mail-lf1-x12f.google.com with SMTP id 2adb3069b0e04-529614b8c29so1052135e87.2 for ; Fri, 24 May 2024 03:52:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716547923; x=1717152723; 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=hr4mFL3kawAtq0YtjNZv8OoJ6UbTTaozHsbwpoCpDok=; b=G5+PFfsMP4LM0L76gD+BQ4UfrtzOr/eMoGSoC6+M5LBHsv6Y3qtAKnOrrwFRIVIG3B 1edTEYuzHqqeyx2vqhWwVzvi5Mk5mnSRj33xawjxPINXeAKRGbHI0s9FwiKQOd/aD0x0 8jsaUCPKUH10snDMRruWvj0Wt7BgcCo3c5X2qVAIGo1UbSvSEJaARmj58RdvCWAuML1Z hG0Kuc3SUIi+SCrd/H2XIthXrVek3YKpol4uLzTI+huhN/5dLaJlObpYpp1gOfFiUCnx 6pZuKu/+caFHYVKK9TtCJYngEMfpz1TC2Nkk+bIbd78ZF+NQjQXDkIXIrwcfew86pX/7 o9Lg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716547923; x=1717152723; 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=hr4mFL3kawAtq0YtjNZv8OoJ6UbTTaozHsbwpoCpDok=; b=hXAmRBq++Iyytq6Y/cBD8PvCklo4YBnbP3l2ArH+EeSA1rSj1I5OMP9P3arH4u8GV1 KWA2z6OqlAF1bqI1f4Ua0bkEdlEnEr06I+/XLfIX0xtepIqTWFkqm8C93IDTilENfKQz e0oYFyVUd0+obbvJao3ClHaOh0E+AMrH03/lQkBrVgwL27xqKGnG/5usyWHYOhmFtNDt ioIiEGnyMmQ5KZcs+koAKGxGyJZN6doSO8gjslW3TJdXTB/k9nR4DV7uTCz/qyDYuBb6 xRwVroBqT7s/O/+I6hRz3tsbanFjaBXMnDD9bTs79KO+g5tLBzEVZwgH/syktrBunGNN kv9Q== X-Gm-Message-State: AOJu0Yza08ZjFjiFWQmuc8QYJ6Q8xnhiFEfGU6C9S1i098EdB6A5F11U 8ifw8g5quxsPH1UZfNCdnxZKNW50k36u2LRhI/ASKq5ohDRTxi1Nrts/SMaW X-Google-Smtp-Source: AGHT+IHg8fKa6vFYJhd6cNlWBuyv0qb20RQ/YQYX1jzD6kM5Lukf3RHBt5yzeodjussqdQuBewdlWQ== X-Received: by 2002:ac2:46c9:0:b0:51c:3f4c:ef6c with SMTP id 2adb3069b0e04-52966006183mr1448573e87.40.1716547922924; Fri, 24 May 2024 03:52:02 -0700 (PDT) Received: from gmail.com (213-67-3-247-no600.tbcn.telia.com. [213.67.3.247]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-5296e886e42sm151898e87.46.2024.05.24.03.52.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 03:52:01 -0700 (PDT) From: "Edgar E. Iglesias" To: qemu-devel@nongnu.org Cc: edgar.iglesias@gmail.com, sstabellini@kernel.org, jgross@suse.com, "Edgar E. Iglesias" , David Hildenbrand , Paolo Bonzini , Peter Xu , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= Subject: [PATCH v7 4/8] softmmu: xen: Always pass offset + addr to xen_map_cache Date: Fri, 24 May 2024 12:51:48 +0200 Message-Id: <20240524105152.1301842-5-edgar.iglesias@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240524105152.1301842-1-edgar.iglesias@gmail.com> References: <20240524105152.1301842-1-edgar.iglesias@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::12f; envelope-from=edgar.iglesias@gmail.com; helo=mail-lf1-x12f.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, FREEMAIL_FROM=0.001, 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: "Edgar E. Iglesias" Always pass address with offset to xen_map_cache(). This is in preparation for support for grant mappings. Since this is within a block that checks for offset == 0, this has no functional changes. Signed-off-by: Edgar E. Iglesias Reviewed-by: Stefano Stabellini Reviewed-by: David Hildenbrand --- system/physmem.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/system/physmem.c b/system/physmem.c index 342b7a8fd4..5e6257ef65 100644 --- a/system/physmem.c +++ b/system/physmem.c @@ -2230,7 +2230,8 @@ static void *qemu_ram_ptr_length(RAMBlock *block, ram_addr_t addr, * In that case just map the requested area. */ if (block->offset == 0) { - return xen_map_cache(block->mr, addr, len, lock, lock, + return xen_map_cache(block->mr, block->offset + addr, + len, lock, lock, is_write); } From patchwork Fri May 24 10:51:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgar E. Iglesias" X-Patchwork-Id: 13673069 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 67674C25B79 for ; Fri, 24 May 2024 10:53:21 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sASWy-0006VT-RD; Fri, 24 May 2024 06:52:12 -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 1sASWv-0006Th-4W for qemu-devel@nongnu.org; Fri, 24 May 2024 06:52:09 -0400 Received: from mail-lj1-x234.google.com ([2a00:1450: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 1sASWt-0007eb-Kj for qemu-devel@nongnu.org; Fri, 24 May 2024 06:52:08 -0400 Received: by mail-lj1-x234.google.com with SMTP id 38308e7fff4ca-2e95a1eff78so9102331fa.0 for ; Fri, 24 May 2024 03:52:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716547924; x=1717152724; 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=nAPTdk2tBTLhsGMXcyf3g+dPATMhben3YmhgWkc5O5I=; b=m8o7LBFT3+98efzsb0IHYK0suZpryeJlTF/C9SPHvc7QK5QHpzwHkVjoZGaa2sltTI NWWSEvDO/QnTVzcoL7B+V7oS7VpKgIAFiHxGE9y/rK6e2qGAoYzfzMSEnV69f8Ngfhtb rPsLYbRJ/MpzbT5hjjf7ki9UwMjxvKb0e4VkSE2I04dXIOOjkOIT+HIQrEhQGnzBnsij QULSsDXTiPYzmmHuZE01+tpeOPd1f0G3nkfukTQ+NrROjq5RjNUfUMBrOtRvIkw6y2+o e1rJWRNUgi333S2Aw1qnDV+7Xvx6zeBPoj1q6nXZEAwSPIgx+t3Q8oN0EAR7grbfvneg 2maQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716547924; x=1717152724; 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=nAPTdk2tBTLhsGMXcyf3g+dPATMhben3YmhgWkc5O5I=; b=BP9pq0arLsrENc+8rlhwe46HvRDLK0zZCFpWjEtw12E9M2j/5HD6XzSgaESJyZIB7r T4NpZYE7jrhgEpt/Rs3mK3jJj7Phl7k89gJkCsqkFzwLusXit9GTNYUKBSI5F8DhRYIG CRlTFHmbEerDPxobfOPF+wR4/cbUM35Vl+gBeZoC8nBzGhvqKUrBmUh4HmOMe54/uuTg rwAyw4fekgwu1aocpimifwb36sxUq/U7z46+GDOjM/MBIVDXnBXS52aX8iyks0K8XHU/ Qg6DlL00YlqI+lTR0T12y8NwEzqizoyd9TNKy6bGwk1lM7XXORDESlm3LHccsvXelMSb S3LQ== X-Gm-Message-State: AOJu0YyB9UWbUDQTS3RiCeXWLB9WnE0JhiZph6sF5Iasioc1XNUmaVGM x458nUzM4EZZdRQC4jXhNesUqXEib4rK1ThTh/zazHM4tLg9ZbtSEEjzwS9d X-Google-Smtp-Source: AGHT+IFUjna/f3aSbZaPkjcscT5RiiQTauvOvojfkAQqidjrCDubERIlSddZtrR+moSv9e6MGInP8w== X-Received: by 2002:a2e:b04e:0:b0:2e6:f51e:ec1b with SMTP id 38308e7fff4ca-2e95b042c6fmr16760271fa.3.1716547924259; Fri, 24 May 2024 03:52:04 -0700 (PDT) Received: from gmail.com (213-67-3-247-no600.tbcn.telia.com. [213.67.3.247]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-2e95bf26002sm1473161fa.136.2024.05.24.03.52.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 03:52:03 -0700 (PDT) From: "Edgar E. Iglesias" To: qemu-devel@nongnu.org Cc: edgar.iglesias@gmail.com, sstabellini@kernel.org, jgross@suse.com, "Edgar E. Iglesias" , David Hildenbrand , Paolo Bonzini , Peter Xu , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= Subject: [PATCH v7 5/8] softmmu: Replace check for RAMBlock offset 0 with xen_mr_is_memory Date: Fri, 24 May 2024 12:51:49 +0200 Message-Id: <20240524105152.1301842-6-edgar.iglesias@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240524105152.1301842-1-edgar.iglesias@gmail.com> References: <20240524105152.1301842-1-edgar.iglesias@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::234; envelope-from=edgar.iglesias@gmail.com; helo=mail-lj1-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, FREEMAIL_FROM=0.001, 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: "Edgar E. Iglesias" For xen, when checking for the first RAM (xen_memory), use xen_mr_is_memory() rather than checking for a RAMBlock with offset 0. All Xen machines create xen_memory first so this has no functional change for existing machines. Signed-off-by: Edgar E. Iglesias Reviewed-by: Stefano Stabellini Reviewed-by: David Hildenbrand --- system/physmem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/physmem.c b/system/physmem.c index 5e6257ef65..b7847db1a2 100644 --- a/system/physmem.c +++ b/system/physmem.c @@ -2229,7 +2229,7 @@ static void *qemu_ram_ptr_length(RAMBlock *block, ram_addr_t addr, * because we don't want to map the entire memory in QEMU. * In that case just map the requested area. */ - if (block->offset == 0) { + if (xen_mr_is_memory(block->mr)) { return xen_map_cache(block->mr, block->offset + addr, len, lock, lock, is_write); From patchwork Fri May 24 10:51:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Edgar E. Iglesias" X-Patchwork-Id: 13673071 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 37EDFC25B74 for ; Fri, 24 May 2024 10:53:34 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sASX1-0006dE-CW; Fri, 24 May 2024 06:52:15 -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 1sASWv-0006Tw-PB for qemu-devel@nongnu.org; Fri, 24 May 2024 06:52:09 -0400 Received: from mail-lf1-x131.google.com ([2a00:1450:4864:20::131]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sASWt-0007g9-RU for qemu-devel@nongnu.org; Fri, 24 May 2024 06:52:09 -0400 Received: by mail-lf1-x131.google.com with SMTP id 2adb3069b0e04-52327368e59so9717757e87.1 for ; Fri, 24 May 2024 03:52:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716547926; x=1717152726; 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=GFr6EaV5waf3qgpK3N8zYV0AbUxb9xkgU9bBgeST51Y=; b=AEaYfDZx9QWchE7IwSeik/t4VNuh2Xzz46jmFxCze8/mHWPk4hNehCCIVFP/vVLNEn ByplXAyer8qltzuqfGVDvhNeCXSHnuWNuh5Ts4tUTuEM92bxZA8Nk/BVWU7I3VOJjVRU czfpjA7tzGwF6MqQWU9wiPRvaEvG6t/ISvF9Si8cUZZxCyn9Pw7uxNBozCkzRUMvMbkb dtSblSve8JKWslmytn7w69U3hu3zoT/sbiNDtsuOxny7Nkzo9xSXbcWXBzpxasknSraK eCzc5MQj5iO25+cwadI4txGx8P+mHSqBIFx9VVB7bgUJ8PZVRmMD9I1hTnH3H2sYxyCH HhTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716547926; x=1717152726; 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=GFr6EaV5waf3qgpK3N8zYV0AbUxb9xkgU9bBgeST51Y=; b=KzZYkOJASnRUEVQzoxrEMnwQCv3bVB+dFbM6iUk1tXU5cY7U0UOeYD2AW4oRBhIYuV fPNxOVIkxRaFr1B81jzh9Zyv8CckmO5jLiaPXJSMi9j86eJYAAn+35VZzEM4iGwJbqlU QGLW3v+j+3C1OCn9wKkJQOLEHO4X0u/fXSs0K9NN6NUYqG6zmxl6cAK0KnRmWtBYdCv1 3qqg2PqdGDczRl2NIK6bZZA8IzWrb+k2QHrW1Wzea62gcAKpivrOJzSminZMc4HBxxUj +X4oihwj14sDe+ylaCeAczDVFj9BSEKEWod++mDgTgoRU+zukE3EJ3efMtxsbwf7wY0C EdFw== X-Gm-Message-State: AOJu0YzDzPZ8nHh4L618jiCEkaebLXvcraLqkle1D5lL7HfDnZreRBlE UDvbNTMyteFp9eMlCFLBvbkiwjrxgCxS+bjJVSChm8KssWbasuI7a7PdXvtr X-Google-Smtp-Source: AGHT+IFH/GXyxK3UUwsYBMOqKtKS+ki8gwKfJtjwB628taZMcmokUZth9SWriOw8oaOj3H6sRigelA== X-Received: by 2002:a19:9148:0:b0:523:bbf5:4b36 with SMTP id 2adb3069b0e04-529646df136mr834902e87.20.1716547925723; Fri, 24 May 2024 03:52:05 -0700 (PDT) Received: from gmail.com (213-67-3-247-no600.tbcn.telia.com. [213.67.3.247]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-5296e887b1asm152787e87.32.2024.05.24.03.52.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 03:52:04 -0700 (PDT) From: "Edgar E. Iglesias" To: qemu-devel@nongnu.org Cc: edgar.iglesias@gmail.com, sstabellini@kernel.org, jgross@suse.com, "Edgar E. Iglesias" , David Hildenbrand , Anthony PERARD , Paul Durrant , Paolo Bonzini , Peter Xu , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , xen-devel@lists.xenproject.org Subject: [PATCH v7 6/8] xen: mapcache: Pass the ram_addr offset to xen_map_cache() Date: Fri, 24 May 2024 12:51:50 +0200 Message-Id: <20240524105152.1301842-7-edgar.iglesias@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240524105152.1301842-1-edgar.iglesias@gmail.com> References: <20240524105152.1301842-1-edgar.iglesias@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::131; envelope-from=edgar.iglesias@gmail.com; helo=mail-lf1-x131.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, FREEMAIL_FROM=0.001, 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: "Edgar E. Iglesias" Pass the ram_addr offset to xen_map_cache. This is in preparation for adding grant mappings that need to compute the address within the RAMBlock. No functional changes. Signed-off-by: Edgar E. Iglesias Reviewed-by: David Hildenbrand Reviewed-by: Stefano Stabellini Reviewed-by: Philippe Mathieu-Daudé --- hw/xen/xen-mapcache.c | 16 +++++++++++----- include/sysemu/xen-mapcache.h | 2 ++ system/physmem.c | 9 +++++---- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/hw/xen/xen-mapcache.c b/hw/xen/xen-mapcache.c index ec95445696..a07c47b0b1 100644 --- a/hw/xen/xen-mapcache.c +++ b/hw/xen/xen-mapcache.c @@ -167,7 +167,8 @@ static void xen_remap_bucket(MapCache *mc, void *vaddr, hwaddr size, hwaddr address_index, - bool dummy) + bool dummy, + ram_addr_t ram_offset) { uint8_t *vaddr_base; xen_pfn_t *pfns; @@ -266,6 +267,7 @@ static void xen_remap_bucket(MapCache *mc, static uint8_t *xen_map_cache_unlocked(MapCache *mc, hwaddr phys_addr, hwaddr size, + ram_addr_t ram_offset, uint8_t lock, bool dma, bool is_write) { MapCacheEntry *entry, *pentry = NULL, @@ -337,14 +339,16 @@ tryagain: if (!entry) { entry = g_new0(MapCacheEntry, 1); pentry->next = entry; - xen_remap_bucket(mc, entry, NULL, cache_size, address_index, dummy); + xen_remap_bucket(mc, entry, NULL, cache_size, address_index, dummy, + ram_offset); } else if (!entry->lock) { if (!entry->vaddr_base || entry->paddr_index != address_index || entry->size != cache_size || !test_bits(address_offset >> XC_PAGE_SHIFT, test_bit_size >> XC_PAGE_SHIFT, entry->valid_mapping)) { - xen_remap_bucket(mc, entry, NULL, cache_size, address_index, dummy); + xen_remap_bucket(mc, entry, NULL, cache_size, address_index, dummy, + ram_offset); } } @@ -391,13 +395,15 @@ tryagain: uint8_t *xen_map_cache(MemoryRegion *mr, hwaddr phys_addr, hwaddr size, + ram_addr_t ram_addr_offset, uint8_t lock, bool dma, bool is_write) { uint8_t *p; mapcache_lock(mapcache); - p = xen_map_cache_unlocked(mapcache, phys_addr, size, lock, dma, is_write); + p = xen_map_cache_unlocked(mapcache, phys_addr, size, ram_addr_offset, + lock, dma, is_write); mapcache_unlock(mapcache); return p; } @@ -632,7 +638,7 @@ static uint8_t *xen_replace_cache_entry_unlocked(MapCache *mc, trace_xen_replace_cache_entry_dummy(old_phys_addr, new_phys_addr); xen_remap_bucket(mc, entry, entry->vaddr_base, - cache_size, address_index, false); + cache_size, address_index, false, old_phys_addr); if (!test_bits(address_offset >> XC_PAGE_SHIFT, test_bit_size >> XC_PAGE_SHIFT, entry->valid_mapping)) { diff --git a/include/sysemu/xen-mapcache.h b/include/sysemu/xen-mapcache.h index 1ec9e66752..b5e3ea1bc0 100644 --- a/include/sysemu/xen-mapcache.h +++ b/include/sysemu/xen-mapcache.h @@ -19,6 +19,7 @@ typedef hwaddr (*phys_offset_to_gaddr_t)(hwaddr phys_offset, void xen_map_cache_init(phys_offset_to_gaddr_t f, void *opaque); uint8_t *xen_map_cache(MemoryRegion *mr, hwaddr phys_addr, hwaddr size, + ram_addr_t ram_addr_offset, uint8_t lock, bool dma, bool is_write); ram_addr_t xen_ram_addr_from_mapcache(void *ptr); @@ -37,6 +38,7 @@ static inline void xen_map_cache_init(phys_offset_to_gaddr_t f, static inline uint8_t *xen_map_cache(MemoryRegion *mr, hwaddr phys_addr, hwaddr size, + ram_addr_t ram_addr_offset, uint8_t lock, bool dma, bool is_write) diff --git a/system/physmem.c b/system/physmem.c index b7847db1a2..33d09f7571 100644 --- a/system/physmem.c +++ b/system/physmem.c @@ -2231,13 +2231,14 @@ static void *qemu_ram_ptr_length(RAMBlock *block, ram_addr_t addr, */ if (xen_mr_is_memory(block->mr)) { return xen_map_cache(block->mr, block->offset + addr, - len, lock, lock, - is_write); + len, block->offset, + lock, lock, is_write); } block->host = xen_map_cache(block->mr, block->offset, - block->max_length, 1, - lock, is_write); + block->max_length, + block->offset, + 1, lock, is_write); } return ramblock_ptr(block, addr); From patchwork Fri May 24 10:51:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgar E. Iglesias" X-Patchwork-Id: 13673067 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D2DA2C25B74 for ; Fri, 24 May 2024 10:53:20 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sASX6-0006i5-W2; Fri, 24 May 2024 06:52:21 -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 1sASWx-0006Vv-Up for qemu-devel@nongnu.org; Fri, 24 May 2024 06:52:12 -0400 Received: from mail-lf1-x131.google.com ([2a00:1450:4864:20::131]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sASWv-0007gK-M7 for qemu-devel@nongnu.org; Fri, 24 May 2024 06:52:11 -0400 Received: by mail-lf1-x131.google.com with SMTP id 2adb3069b0e04-52449b7aa2bso6161594e87.3 for ; Fri, 24 May 2024 03:52:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716547927; x=1717152727; 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=3di7O7tdApxOVr1CI1JAQSoCPPyk5/k7Mjm3E5eqE1c=; b=RNAkY/FWLU/bI/xjizY496fy7fHYcBccJJDEJHStC5gNoleXf678OV8FZ2kMvjmyjn +GTtH2pwTGm+HkNEvIUrJDy/QVQOv+jE5MzkGWHUKsrFUE51eYL7R/QspH5tScYHAgI+ CD3EZlg/KMR3MEbm410T+p9QruXuvX0N69t6dTH8SwnMpFh7cqJeksNRV9RvXP37+f8V u1xt2tjzyYXzeFBA0bvw/0kREFvBueyauzgv+XtdARg+aw2Ofo+1aXdMCrNvIrdJwiXe do3/P6lT88R4ewloNzO/sgGPXY8rWAe3Njpe421dGClhcSUvbLZpvaPvmVN0HnkoFrTi O+GA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716547927; x=1717152727; 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=3di7O7tdApxOVr1CI1JAQSoCPPyk5/k7Mjm3E5eqE1c=; b=VKVQ+fYYxQPVqrUOKdVINija+GR/ju7Db8m0bCX3B78u2+DK9CrXjFSDSQ8Dh7XVkr loWI7ll/AA36FITws/b0/QFYD0uTINvsS9a7AEiGjCdF6w3sZZEpsBHoNtz4pLGEHPuE r69O2+RXFtCP7G7ACQFmDjTuAYWEOMcDDae3EEm4N3+sWIB+JWxio/s5WZI34pnYe+ud qh6T4hAKYfZVQYEWUv2W196Awszn1AJoKy/YvrBjNfXmRk68RDGzNKSFJTAquBHDu7so y8usw8f5+MOa4pUrUh2zMTY82C7H9k1ec7XwZVP1Um40fGxCMhMbIgY06bPHymVfMILW o0GQ== X-Gm-Message-State: AOJu0Yym3OgBAfBfkcUAqxVvS/HvndkN818ogUsLRyXblD4fvZmZhKVT ux/T1sthTtYK/hYHTEL/TMehV/E780YSxvgl0Y4mJDEXzSqp8xmOzujpuVHu X-Google-Smtp-Source: AGHT+IE5CtpteyyNazgBiJLIGSkabCL02X/CfF2Skfk8qN39LOCmJrRorp9B6cUUA2WoRJaRtcGV5Q== X-Received: by 2002:ac2:5932:0:b0:521:b1ca:9c99 with SMTP id 2adb3069b0e04-529645e2556mr1097227e87.5.1716547927180; Fri, 24 May 2024 03:52:07 -0700 (PDT) Received: from gmail.com (213-67-3-247-no600.tbcn.telia.com. [213.67.3.247]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-5296e887a61sm154331e87.62.2024.05.24.03.52.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 03:52:06 -0700 (PDT) From: "Edgar E. Iglesias" To: qemu-devel@nongnu.org Cc: edgar.iglesias@gmail.com, sstabellini@kernel.org, jgross@suse.com, "Edgar E. Iglesias" , Manos Pitsidianakis , Anthony PERARD , Paul Durrant , xen-devel@lists.xenproject.org Subject: [PATCH v7 7/8] xen: mapcache: Add support for grant mappings Date: Fri, 24 May 2024 12:51:51 +0200 Message-Id: <20240524105152.1301842-8-edgar.iglesias@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240524105152.1301842-1-edgar.iglesias@gmail.com> References: <20240524105152.1301842-1-edgar.iglesias@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::131; envelope-from=edgar.iglesias@gmail.com; helo=mail-lf1-x131.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, FREEMAIL_FROM=0.001, 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: "Edgar E. Iglesias" Add a second mapcache for grant mappings. The mapcache for grants needs to work with XC_PAGE_SIZE granularity since we can't map larger ranges than what has been granted to us. Like with foreign mappings (xen_memory), machines using grants are expected to initialize the xen_grants MR and map it into their address-map accordingly. CC: Manos Pitsidianakis Signed-off-by: Edgar E. Iglesias Reviewed-by: Stefano Stabellini --- hw/xen/xen-hvm-common.c | 12 ++- hw/xen/xen-mapcache.c | 165 +++++++++++++++++++++++++------- include/hw/xen/xen-hvm-common.h | 3 + include/sysemu/xen.h | 7 ++ 4 files changed, 150 insertions(+), 37 deletions(-) diff --git a/hw/xen/xen-hvm-common.c b/hw/xen/xen-hvm-common.c index a0a0252da0..b8ace1c368 100644 --- a/hw/xen/xen-hvm-common.c +++ b/hw/xen/xen-hvm-common.c @@ -10,12 +10,18 @@ #include "hw/boards.h" #include "hw/xen/arch_hvm.h" -MemoryRegion xen_memory; +MemoryRegion xen_memory, xen_grants; -/* Check for xen memory. */ +/* Check for any kind of xen memory, foreign mappings or grants. */ bool xen_mr_is_memory(MemoryRegion *mr) { - return mr == &xen_memory; + return mr == &xen_memory || mr == &xen_grants; +} + +/* Check specifically for grants. */ +bool xen_mr_is_grants(MemoryRegion *mr) +{ + return mr == &xen_grants; } void xen_ram_alloc(ram_addr_t ram_addr, ram_addr_t size, MemoryRegion *mr, diff --git a/hw/xen/xen-mapcache.c b/hw/xen/xen-mapcache.c index a07c47b0b1..5f23b0adbe 100644 --- a/hw/xen/xen-mapcache.c +++ b/hw/xen/xen-mapcache.c @@ -14,6 +14,7 @@ #include +#include "hw/xen/xen-hvm-common.h" #include "hw/xen/xen_native.h" #include "qemu/bitmap.h" @@ -21,6 +22,8 @@ #include "sysemu/xen-mapcache.h" #include "trace.h" +#include +#include #if HOST_LONG_BITS == 32 # define MCACHE_MAX_SIZE (1UL<<31) /* 2GB Cap */ @@ -41,6 +44,7 @@ typedef struct MapCacheEntry { unsigned long *valid_mapping; uint32_t lock; #define XEN_MAPCACHE_ENTRY_DUMMY (1 << 0) +#define XEN_MAPCACHE_ENTRY_GRANT (1 << 1) uint8_t flags; hwaddr size; struct MapCacheEntry *next; @@ -71,6 +75,8 @@ typedef struct MapCache { } MapCache; static MapCache *mapcache; +static MapCache *mapcache_grants; +static xengnttab_handle *xen_region_gnttabdev; static inline void mapcache_lock(MapCache *mc) { @@ -131,6 +137,12 @@ void xen_map_cache_init(phys_offset_to_gaddr_t f, void *opaque) unsigned long max_mcache_size; unsigned int bucket_shift; + xen_region_gnttabdev = xengnttab_open(NULL, 0); + if (xen_region_gnttabdev == NULL) { + error_report("mapcache: Failed to open gnttab device"); + exit(EXIT_FAILURE); + } + if (HOST_LONG_BITS == 32) { bucket_shift = 16; } else { @@ -159,6 +171,15 @@ void xen_map_cache_init(phys_offset_to_gaddr_t f, void *opaque) mapcache = xen_map_cache_init_single(f, opaque, bucket_shift, max_mcache_size); + + /* + * Grant mappings must use XC_PAGE_SIZE granularity since we can't + * map anything beyond the number of pages granted to us. + */ + mapcache_grants = xen_map_cache_init_single(f, opaque, + XC_PAGE_SHIFT, + max_mcache_size); + setrlimit(RLIMIT_AS, &rlimit_as); } @@ -168,17 +189,24 @@ static void xen_remap_bucket(MapCache *mc, hwaddr size, hwaddr address_index, bool dummy, + bool grant, + bool is_write, ram_addr_t ram_offset) { uint8_t *vaddr_base; - xen_pfn_t *pfns; - int *err; + g_autofree uint32_t *refs = NULL; + g_autofree xen_pfn_t *pfns = NULL; + g_autofree int *err; unsigned int i; hwaddr nb_pfn = size >> XC_PAGE_SHIFT; trace_xen_remap_bucket(address_index); - pfns = g_new0(xen_pfn_t, nb_pfn); + if (grant) { + refs = g_new0(uint32_t, nb_pfn); + } else { + pfns = g_new0(xen_pfn_t, nb_pfn); + } err = g_new0(int, nb_pfn); if (entry->vaddr_base != NULL) { @@ -207,21 +235,51 @@ static void xen_remap_bucket(MapCache *mc, g_free(entry->valid_mapping); entry->valid_mapping = NULL; - for (i = 0; i < nb_pfn; i++) { - pfns[i] = (address_index << (mc->bucket_shift - XC_PAGE_SHIFT)) + i; + if (grant) { + hwaddr grant_base = address_index - (ram_offset >> XC_PAGE_SHIFT); + + for (i = 0; i < nb_pfn; i++) { + refs[i] = grant_base + i; + } + } else { + for (i = 0; i < nb_pfn; i++) { + pfns[i] = (address_index << (mc->bucket_shift - XC_PAGE_SHIFT)) + i; + } } - /* - * If the caller has requested the mapping at a specific address use - * MAP_FIXED to make sure it's honored. - */ + entry->flags &= ~XEN_MAPCACHE_ENTRY_GRANT; + if (!dummy) { - vaddr_base = xenforeignmemory_map2(xen_fmem, xen_domid, vaddr, - PROT_READ | PROT_WRITE, - vaddr ? MAP_FIXED : 0, - nb_pfn, pfns, err); + if (grant) { + int prot = PROT_READ; + + if (is_write) { + prot |= PROT_WRITE; + } + + entry->flags |= XEN_MAPCACHE_ENTRY_GRANT; + assert(vaddr == NULL); + vaddr_base = xengnttab_map_domain_grant_refs(xen_region_gnttabdev, + nb_pfn, + xen_domid, refs, + prot); + } else { + /* + * If the caller has requested the mapping at a specific address use + * MAP_FIXED to make sure it's honored. + * + * We don't yet support upgrading mappings from RO to RW, to handle + * models using ordinary address_space_rw(), foreign mappings ignore + * is_write and are always mapped RW. + */ + vaddr_base = xenforeignmemory_map2(xen_fmem, xen_domid, vaddr, + PROT_READ | PROT_WRITE, + vaddr ? MAP_FIXED : 0, + nb_pfn, pfns, err); + } if (vaddr_base == NULL) { - perror("xenforeignmemory_map2"); + perror(grant ? "xengnttab_map_domain_grant_refs" + : "xenforeignmemory_map2"); exit(-1); } } else { @@ -260,15 +318,13 @@ static void xen_remap_bucket(MapCache *mc, bitmap_set(entry->valid_mapping, i, 1); } } - - g_free(pfns); - g_free(err); } static uint8_t *xen_map_cache_unlocked(MapCache *mc, hwaddr phys_addr, hwaddr size, ram_addr_t ram_offset, - uint8_t lock, bool dma, bool is_write) + uint8_t lock, bool dma, + bool grant, bool is_write) { MapCacheEntry *entry, *pentry = NULL, *free_entry = NULL, *free_pentry = NULL; @@ -340,7 +396,7 @@ tryagain: entry = g_new0(MapCacheEntry, 1); pentry->next = entry; xen_remap_bucket(mc, entry, NULL, cache_size, address_index, dummy, - ram_offset); + grant, is_write, ram_offset); } else if (!entry->lock) { if (!entry->vaddr_base || entry->paddr_index != address_index || entry->size != cache_size || @@ -348,7 +404,7 @@ tryagain: test_bit_size >> XC_PAGE_SHIFT, entry->valid_mapping)) { xen_remap_bucket(mc, entry, NULL, cache_size, address_index, dummy, - ram_offset); + grant, is_write, ram_offset); } } @@ -399,12 +455,26 @@ uint8_t *xen_map_cache(MemoryRegion *mr, uint8_t lock, bool dma, bool is_write) { + bool grant = xen_mr_is_grants(mr); + MapCache *mc = grant ? mapcache_grants : mapcache; uint8_t *p; - mapcache_lock(mapcache); - p = xen_map_cache_unlocked(mapcache, phys_addr, size, ram_addr_offset, - lock, dma, is_write); - mapcache_unlock(mapcache); + if (grant && !lock) { + /* + * Grants are only supported via address_space_map(). Anything + * else is considered a user/guest error. + * + * QEMU generally doesn't expect these mappings to ever fail, so + * if this happens we report an error message and abort(). + */ + error_report("Tried to access a grant reference without mapping it."); + abort(); + } + + mapcache_lock(mc); + p = xen_map_cache_unlocked(mc, phys_addr, size, ram_addr_offset, + lock, dma, grant, is_write); + mapcache_unlock(mc); return p; } @@ -449,7 +519,14 @@ static ram_addr_t xen_ram_addr_from_mapcache_single(MapCache *mc, void *ptr) ram_addr_t xen_ram_addr_from_mapcache(void *ptr) { - return xen_ram_addr_from_mapcache_single(mapcache, ptr); + ram_addr_t addr; + + addr = xen_ram_addr_from_mapcache_single(mapcache, ptr); + if (addr == RAM_ADDR_INVALID) { + addr = xen_ram_addr_from_mapcache_single(mapcache_grants, ptr); + } + + return addr; } static void xen_invalidate_map_cache_entry_unlocked(MapCache *mc, @@ -460,6 +537,7 @@ static void xen_invalidate_map_cache_entry_unlocked(MapCache *mc, hwaddr paddr_index; hwaddr size; int found = 0; + int rc; QTAILQ_FOREACH(reventry, &mc->locked_entries, next) { if (reventry->vaddr_req == buffer) { @@ -502,7 +580,14 @@ static void xen_invalidate_map_cache_entry_unlocked(MapCache *mc, } ram_block_notify_remove(entry->vaddr_base, entry->size, entry->size); - if (munmap(entry->vaddr_base, entry->size) != 0) { + if (entry->flags & XEN_MAPCACHE_ENTRY_GRANT) { + rc = xengnttab_unmap(xen_region_gnttabdev, entry->vaddr_base, + entry->size >> mc->bucket_shift); + } else { + rc = munmap(entry->vaddr_base, entry->size); + } + + if (rc) { perror("unmap fails"); exit(-1); } @@ -521,14 +606,24 @@ typedef struct XenMapCacheData { uint8_t *buffer; } XenMapCacheData; +static void xen_invalidate_map_cache_entry_single(MapCache *mc, uint8_t *buffer) +{ + mapcache_lock(mc); + xen_invalidate_map_cache_entry_unlocked(mc, buffer); + mapcache_unlock(mc); +} + +static void xen_invalidate_map_cache_entry_all(uint8_t *buffer) +{ + xen_invalidate_map_cache_entry_single(mapcache, buffer); + xen_invalidate_map_cache_entry_single(mapcache_grants, buffer); +} + static void xen_invalidate_map_cache_entry_bh(void *opaque) { XenMapCacheData *data = opaque; - mapcache_lock(mapcache); - xen_invalidate_map_cache_entry_unlocked(mapcache, data->buffer); - mapcache_unlock(mapcache); - + xen_invalidate_map_cache_entry_all(data->buffer); aio_co_wake(data->co); } @@ -543,9 +638,7 @@ void coroutine_mixed_fn xen_invalidate_map_cache_entry(uint8_t *buffer) xen_invalidate_map_cache_entry_bh, &data); qemu_coroutine_yield(); } else { - mapcache_lock(mapcache); - xen_invalidate_map_cache_entry_unlocked(mapcache, buffer); - mapcache_unlock(mapcache); + xen_invalidate_map_cache_entry_all(buffer); } } @@ -597,6 +690,7 @@ void xen_invalidate_map_cache(void) bdrv_drain_all(); xen_invalidate_map_cache_single(mapcache); + xen_invalidate_map_cache_single(mapcache_grants); } static uint8_t *xen_replace_cache_entry_unlocked(MapCache *mc, @@ -632,13 +726,16 @@ static uint8_t *xen_replace_cache_entry_unlocked(MapCache *mc, return NULL; } + assert((entry->flags & XEN_MAPCACHE_ENTRY_GRANT) == 0); + address_index = new_phys_addr >> mc->bucket_shift; address_offset = new_phys_addr & (mc->bucket_size - 1); trace_xen_replace_cache_entry_dummy(old_phys_addr, new_phys_addr); xen_remap_bucket(mc, entry, entry->vaddr_base, - cache_size, address_index, false, old_phys_addr); + cache_size, address_index, false, + false, false, old_phys_addr); if (!test_bits(address_offset >> XC_PAGE_SHIFT, test_bit_size >> XC_PAGE_SHIFT, entry->valid_mapping)) { diff --git a/include/hw/xen/xen-hvm-common.h b/include/hw/xen/xen-hvm-common.h index 65a51aac2e..3d796235dc 100644 --- a/include/hw/xen/xen-hvm-common.h +++ b/include/hw/xen/xen-hvm-common.h @@ -16,6 +16,7 @@ #include extern MemoryRegion xen_memory; +extern MemoryRegion xen_grants; extern MemoryListener xen_io_listener; extern DeviceListener xen_device_listener; @@ -29,6 +30,8 @@ extern DeviceListener xen_device_listener; do { } while (0) #endif +#define XEN_GRANT_ADDR_OFF (1ULL << 63) + static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page, int i) { return shared_page->vcpu_ioreq[i].vp_eport; diff --git a/include/sysemu/xen.h b/include/sysemu/xen.h index dc72f83bcb..19dccf4d71 100644 --- a/include/sysemu/xen.h +++ b/include/sysemu/xen.h @@ -35,6 +35,7 @@ void xen_ram_alloc(ram_addr_t ram_addr, ram_addr_t size, struct MemoryRegion *mr, Error **errp); bool xen_mr_is_memory(MemoryRegion *mr); +bool xen_mr_is_grants(MemoryRegion *mr); #else /* !CONFIG_XEN_IS_POSSIBLE */ @@ -55,6 +56,12 @@ static inline bool xen_mr_is_memory(MemoryRegion *mr) return false; } +static inline bool xen_mr_is_grants(MemoryRegion *mr) +{ + g_assert_not_reached(); + return false; +} + #endif /* CONFIG_XEN_IS_POSSIBLE */ #endif From patchwork Fri May 24 10:51:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Edgar E. Iglesias" X-Patchwork-Id: 13673073 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7F80DC25B74 for ; Fri, 24 May 2024 10:53:51 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sASX7-0006ik-0k; Fri, 24 May 2024 06:52:21 -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 1sASWz-0006Yh-Ad; Fri, 24 May 2024 06:52:13 -0400 Received: from mail-lj1-x235.google.com ([2a00:1450:4864:20::235]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sASWx-0007gf-MQ; Fri, 24 May 2024 06:52:13 -0400 Received: by mail-lj1-x235.google.com with SMTP id 38308e7fff4ca-2e564cad1f1so98623311fa.0; Fri, 24 May 2024 03:52:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716547929; x=1717152729; 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=iFOOOjt9lX3R5TOZr7m7NgtcNi/9J4Kc7/wWsAI8IDg=; b=GAaLE7eboMDtAkdj14C/A0sgKyE8AW39fZH8iiZ9V9Xi5/Lz27tATDcZeRAZOuaFSG ddcGhR2qZ4DPC99xKDDQu63Jo1HHxq1jFlsuog7vWb8oX8wODC8DxRUKY/x2OlTWvzMP n4Cd0SSmU7yONu27K36MsezCZKWr1UQEGFm9gj6kctQIBVchC4hpFcCII0F72D32y+Uk BOKSEOaDmD8rfE2ZJNUcIwjC3jQoZtdeFMnJa7R/Tb3+YeWq9nu5uyVQAAmWniI6rPpG YVQ0AEDkHAHh3y9vrQAvOFNCm45Ds23wmGjqqZPi+VG1k4t8uFWMSYgMVeXF8sA0dBZ8 CjFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716547929; x=1717152729; 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=iFOOOjt9lX3R5TOZr7m7NgtcNi/9J4Kc7/wWsAI8IDg=; b=lD/zmOXlMGLfk0xPePnyyIN/TFuXkWIROBJLhOhjsxQIbmXu5cuVXlXKgluI6z6x6M C112jTihdcQgONYRLCHJrRLHTtZnAJ0CgUqpL8TJSO+NDbbQHiQBChUgWiZSoxfrdccb 1mQpxoWXizEbADfcVbucaXF/c2M7iwvwRFj2dMp7HHJpEjxd41R//c/O25bJIww7Q4c0 yqgLCHAf7Ms0uCZP+i2OqjWuanXkLSEsa69HF4cdDE+0SPRFw2/JjQzggN6i4mKBwB08 ce3pMjVxm44hVUfB4J4mwDtIYvM6HzejA36+duqyV/aY/XZ83+vnqT7EmZO+m6LkVshn TK3g== X-Forwarded-Encrypted: i=1; AJvYcCX2hIOiaFEs0iP+yTvByAqz6Fg9X21EW1qrloScoV9EBceCsfsfG3mg9gBdf1qDqTxSLrCsoR4gMhIhf10T3GqG/y4n X-Gm-Message-State: AOJu0YwZkY3OP9WD+p3NPtGPFeSQaAo+U2aidawyBXxLJJ0PoLwftl/u plntYLh7C40LMaWyDLTpW01Dna8wej1CxN9vuS2zdldwiF7zTRKHfmDuMXto X-Google-Smtp-Source: AGHT+IHSI9CNPRRu7Ye/VFS79S68fxE0qFl4Z1InLUDdeJoB4YjKFWugYIlOcsTrWaIDEdaNthfHvg== X-Received: by 2002:a2e:92c3:0:b0:2e6:ed20:9059 with SMTP id 38308e7fff4ca-2e95b27b213mr11228821fa.48.1716547929075; Fri, 24 May 2024 03:52:09 -0700 (PDT) Received: from gmail.com (213-67-3-247-no600.tbcn.telia.com. [213.67.3.247]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-2e95bf26002sm1473431fa.136.2024.05.24.03.52.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 03:52:08 -0700 (PDT) From: "Edgar E. Iglesias" To: qemu-devel@nongnu.org Cc: edgar.iglesias@gmail.com, sstabellini@kernel.org, jgross@suse.com, "Edgar E. Iglesias" , Manos Pitsidianakis , Peter Maydell , qemu-arm@nongnu.org Subject: [PATCH v7 8/8] hw/arm: xen: Enable use of grant mappings Date: Fri, 24 May 2024 12:51:52 +0200 Message-Id: <20240524105152.1301842-9-edgar.iglesias@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240524105152.1301842-1-edgar.iglesias@gmail.com> References: <20240524105152.1301842-1-edgar.iglesias@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::235; envelope-from=edgar.iglesias@gmail.com; helo=mail-lj1-x235.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, FREEMAIL_FROM=0.001, 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: "Edgar E. Iglesias" Signed-off-by: Edgar E. Iglesias Reviewed-by: Stefano Stabellini Reviewed-by: Manos Pitsidianakis Reviewed-by: Philippe Mathieu-Daudé --- hw/arm/xen_arm.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/hw/arm/xen_arm.c b/hw/arm/xen_arm.c index 15fa7dfa84..6fad829ede 100644 --- a/hw/arm/xen_arm.c +++ b/hw/arm/xen_arm.c @@ -125,6 +125,11 @@ static void xen_init_ram(MachineState *machine) GUEST_RAM1_BASE, ram_size[1]); memory_region_add_subregion(sysmem, GUEST_RAM1_BASE, &ram_hi); } + + /* Setup support for grants. */ + memory_region_init_ram(&xen_grants, NULL, "xen.grants", block_len, + &error_fatal); + memory_region_add_subregion(sysmem, XEN_GRANT_ADDR_OFF, &xen_grants); } void arch_handle_ioreq(XenIOState *state, ioreq_t *req)