From nobody Mon Nov 25 14:30:16 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 ARC-Seal: i=1; a=rsa-sha256; t=1715075021; cv=none; d=zohomail.com; s=zohoarc; b=inOVqm9ztbCleFJr0hSrR7BsxuNJOM9FvCUaUMZHUcZWeuBlb0VsQeS5wkJxvOMMtnHh55kX8bDQQWbw98L+Hi49PCIw9oS8LsA635DOyqRa70y5BxuXwzPSrFq3V/iy6vH6wcE/x33qil7JOo+ztQKVCv+9CEAWN7Ch0okcWEY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1715075021; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Zq/AsfKdGVYkpDOoF8EeenFxen7rsKomtQ+chi3yprY=; b=IvV6j0J3zEYxz13xDEaELAGdH7g0gjF8L9FfLq0uSNwZt6CLFY125ywU9OgWnMr6e6BBA0dLunxuCAkIOs38BvLLYNAKIoNjbANiwKojxNoCD/fZnV5Dq9997joPtGeS2J3+sYYFRKill71N2ORpaw2BcFu9OzHwo6uIbdWS93Y= 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1715075021940782.9753853856183; Tue, 7 May 2024 02:43:41 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1s4HLL-0004Jo-9C; Tue, 07 May 2024 05:42:42 -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 1s4HLD-0004IB-N9 for qemu-devel@nongnu.org; Tue, 07 May 2024 05:42:31 -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 1s4HLB-0007Pg-CU for qemu-devel@nongnu.org; Tue, 07 May 2024 05:42:31 -0400 Received: by mail-pf1-x42d.google.com with SMTP id d2e1a72fcca58-6f4603237e0so1655180b3a.0 for ; Tue, 07 May 2024 02:42:27 -0700 (PDT) Received: from mnissler.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id p4-20020a62ab04000000b006f45257d3f7sm6434114pff.45.2024.05.07.02.42.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 02:42:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1715074947; x=1715679747; 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=Zq/AsfKdGVYkpDOoF8EeenFxen7rsKomtQ+chi3yprY=; b=NeyLEst8hzEZEyZS3gmBnoo9T9PudFJ2asbyTf8+lXqnKyj0IrU7Yz0EbxA+7+SUq7 FIOtCQkCXUAMV/pk6vdlNthxDksFJe0V2fUTfWXRcF7yM/28FLClVxLylFS4Z+O/o5b7 d08wYVPnDGGDkasl0qGi4vAIInKzAEKQV8snMk+TOJHz4zb9KYrPuNchX66oKwTYuwp5 TNbq2rXM+ViGG1a2fkK9Lu9NHKC14JdciI6xmD4xng6efW0cpXzCqLYzwa0Ydstb7OXY I2uBfsVHbDiISLujONB3zbl8Fiu3bqQwKgim0jn88VC5E1SBewZMZJ8xbm4714PMaZ3n y76g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715074947; x=1715679747; 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=Zq/AsfKdGVYkpDOoF8EeenFxen7rsKomtQ+chi3yprY=; b=fGHSVYZGC4kfpStwXLLbPegZ0g+P5ScA80xQL0PSqKlveTZedpVD3oCgXF66qOYRYF fp1jY6SB/9STTymHK3yfnXB3EpOQJ2/5lstZkzqS36EVqs9lkFdnl6Fd/T9rt4iU/pKL eJYNhhZ8mxdbA9xvBXMKMjV5oJz9v/JiTndHNToTUeeQ2Ema7UsYn+MKSBwimEVnDQBI YrD+3ioFn3t/c5j+iuhJgQ8qw/uF/EGVoMVNGAWOUDoV9CwGJ8M/Nb+1+2B2s5Dxl4rw 7wYxQg1Zn2/Y2Qb6UVrrcYWx7B77KoEBb6xDnOG+BYiMZ8R4yGWHl0dzA5rVdszoPq3y ir6A== X-Forwarded-Encrypted: i=1; AJvYcCUvaPszoqsuw+pZ/laAwfGNpvfLTC084CX7AToktv641uFnq0P+X3YU2v/ZdRc9QZwQzQgvyPSEwl6M9bt6f0Uhm0MdsJw= X-Gm-Message-State: AOJu0YwVu1VCejdq9BVAvmx1xDzOQdsuG8TOhAmcp4crxFro3XHWmkMc o07VJ2Qqij3Yrd13/WDEzmy4VA7mE1scpS7zrBGSdYtvYdV0aBkAk2f4AROyPrY= X-Google-Smtp-Source: AGHT+IHQjdqGAu4ASsvD4sRT3TWZE5zYzPMfsC/EjILNGt3vaP0wtza0s3v69wYo1drkIciDNClQVw== X-Received: by 2002:a05:6a00:2d93:b0:6ed:ca65:68b with SMTP id d2e1a72fcca58-6f48e8696ebmr2994849b3a.4.1715074946970; Tue, 07 May 2024 02:42:26 -0700 (PDT) From: Mattias Nissler To: stefanha@redhat.com, qemu-devel@nongnu.org, peterx@redhat.com, jag.raman@oracle.com Cc: "Michael S. Tsirkin" , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Marcel Apfelbaum , David Hildenbrand , Richard Henderson , Paolo Bonzini , Elena Ufimtseva , john.levon@nutanix.com, Mattias Nissler , Jonathan Cameron Subject: [PATCH v9 1/5] softmmu: Per-AddressSpace bounce buffering Date: Tue, 7 May 2024 02:42:05 -0700 Message-ID: <20240507094210.300566-2-mnissler@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240507094210.300566-1-mnissler@rivosinc.com> References: <20240507094210.300566-1-mnissler@rivosinc.com> 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::42d; envelope-from=mnissler@rivosinc.com; helo=mail-pf1-x42d.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-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 @rivosinc-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1715075022981100001 Content-Type: text/plain; charset="utf-8" Instead of using a single global bounce buffer, give each AddressSpace its own bounce buffer. The MapClient callback mechanism moves to AddressSpace accordingly. This is in preparation for generalizing bounce buffer handling further to allow multiple bounce buffers, with a total allocation limit configured per AddressSpace. Reviewed-by: Peter Xu Tested-by: Jonathan Cameron Signed-off-by: Mattias Nissler --- include/exec/cpu-common.h | 2 - include/exec/memory.h | 45 ++++++++++++++++- system/dma-helpers.c | 4 +- system/memory.c | 7 +++ system/physmem.c | 101 ++++++++++++++++---------------------- 5 files changed, 93 insertions(+), 66 deletions(-) diff --git a/include/exec/cpu-common.h b/include/exec/cpu-common.h index 8bc397e251..815342d043 100644 --- a/include/exec/cpu-common.h +++ b/include/exec/cpu-common.h @@ -147,8 +147,6 @@ void *cpu_physical_memory_map(hwaddr addr, bool is_write); void cpu_physical_memory_unmap(void *buffer, hwaddr len, bool is_write, hwaddr access_len); -void cpu_register_map_client(QEMUBH *bh); -void cpu_unregister_map_client(QEMUBH *bh); =20 bool cpu_physical_memory_is_io(hwaddr phys_addr); =20 diff --git a/include/exec/memory.h b/include/exec/memory.h index dadb5cd65a..d417d7f363 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -1112,6 +1112,19 @@ struct MemoryListener { QTAILQ_ENTRY(MemoryListener) link_as; }; =20 +typedef struct AddressSpaceMapClient { + QEMUBH *bh; + QLIST_ENTRY(AddressSpaceMapClient) link; +} AddressSpaceMapClient; + +typedef struct { + MemoryRegion *mr; + void *buffer; + hwaddr addr; + hwaddr len; + bool in_use; +} BounceBuffer; + /** * struct AddressSpace: describes a mapping of addresses to #MemoryRegion = objects */ @@ -1129,6 +1142,12 @@ struct AddressSpace { struct MemoryRegionIoeventfd *ioeventfds; QTAILQ_HEAD(, MemoryListener) listeners; QTAILQ_ENTRY(AddressSpace) address_spaces_link; + + /* Bounce buffer to use for this address space. */ + BounceBuffer bounce; + /* List of callbacks to invoke when buffers free up */ + QemuMutex map_client_list_lock; + QLIST_HEAD(, AddressSpaceMapClient) map_client_list; }; =20 typedef struct AddressSpaceDispatch AddressSpaceDispatch; @@ -2946,8 +2965,8 @@ bool address_space_access_valid(AddressSpace *as, hwa= ddr addr, hwaddr len, * May return %NULL and set *@plen to zero(0), if resources needed to perf= orm * the mapping are exhausted. * Use only for reads OR writes - not for read-modify-write operations. - * Use cpu_register_map_client() to know when retrying the map operation is - * likely to succeed. + * Use address_space_register_map_client() to know when retrying the map + * operation is likely to succeed. * * @as: #AddressSpace to be accessed * @addr: address within that address space @@ -2972,6 +2991,28 @@ void *address_space_map(AddressSpace *as, hwaddr add= r, void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len, bool is_write, hwaddr access_len); =20 +/* + * address_space_register_map_client: Register a callback to invoke when + * resources for address_space_map() are available again. + * + * address_space_map may fail when there are not enough resources availabl= e, + * such as when bounce buffer memory would exceed the limit. The callback = can + * be used to retry the address_space_map operation. Note that the callback + * gets automatically removed after firing. + * + * @as: #AddressSpace to be accessed + * @bh: callback to invoke when address_space_map() retry is appropriate + */ +void address_space_register_map_client(AddressSpace *as, QEMUBH *bh); + +/* + * address_space_unregister_map_client: Unregister a callback that has + * previously been registered and not fired yet. + * + * @as: #AddressSpace to be accessed + * @bh: callback to unregister + */ +void address_space_unregister_map_client(AddressSpace *as, QEMUBH *bh); =20 /* Internal functions, part of the implementation of address_space_read. = */ MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr, diff --git a/system/dma-helpers.c b/system/dma-helpers.c index 9b221cf94e..74013308f5 100644 --- a/system/dma-helpers.c +++ b/system/dma-helpers.c @@ -169,7 +169,7 @@ static void dma_blk_cb(void *opaque, int ret) if (dbs->iov.size =3D=3D 0) { trace_dma_map_wait(dbs); dbs->bh =3D aio_bh_new(ctx, reschedule_dma, dbs); - cpu_register_map_client(dbs->bh); + address_space_register_map_client(dbs->sg->as, dbs->bh); return; } =20 @@ -197,7 +197,7 @@ static void dma_aio_cancel(BlockAIOCB *acb) } =20 if (dbs->bh) { - cpu_unregister_map_client(dbs->bh); + address_space_unregister_map_client(dbs->sg->as, dbs->bh); qemu_bh_delete(dbs->bh); dbs->bh =3D NULL; } diff --git a/system/memory.c b/system/memory.c index 49f1cb2c38..642a449f8c 100644 --- a/system/memory.c +++ b/system/memory.c @@ -3174,6 +3174,9 @@ void address_space_init(AddressSpace *as, MemoryRegio= n *root, const char *name) as->ioeventfds =3D NULL; QTAILQ_INIT(&as->listeners); QTAILQ_INSERT_TAIL(&address_spaces, as, address_spaces_link); + as->bounce.in_use =3D false; + qemu_mutex_init(&as->map_client_list_lock); + QLIST_INIT(&as->map_client_list); as->name =3D g_strdup(name ? name : "anonymous"); address_space_update_topology(as); address_space_update_ioeventfds(as); @@ -3181,6 +3184,10 @@ void address_space_init(AddressSpace *as, MemoryRegi= on *root, const char *name) =20 static void do_address_space_destroy(AddressSpace *as) { + assert(!qatomic_read(&as->bounce.in_use)); + assert(QLIST_EMPTY(&as->map_client_list)); + qemu_mutex_destroy(&as->map_client_list_lock); + assert(QTAILQ_EMPTY(&as->listeners)); =20 flatview_unref(as->current_map); diff --git a/system/physmem.c b/system/physmem.c index d3a3d8a45c..632da6508a 100644 --- a/system/physmem.c +++ b/system/physmem.c @@ -3046,55 +3046,37 @@ void cpu_flush_icache_range(hwaddr start, hwaddr le= n) NULL, len, FLUSH_CACHE); } =20 -typedef struct { - MemoryRegion *mr; - void *buffer; - hwaddr addr; - hwaddr len; - bool in_use; -} BounceBuffer; - -static BounceBuffer bounce; - -typedef struct MapClient { - QEMUBH *bh; - QLIST_ENTRY(MapClient) link; -} MapClient; - -QemuMutex map_client_list_lock; -static QLIST_HEAD(, MapClient) map_client_list - =3D QLIST_HEAD_INITIALIZER(map_client_list); - -static void cpu_unregister_map_client_do(MapClient *client) +static void +address_space_unregister_map_client_do(AddressSpaceMapClient *client) { QLIST_REMOVE(client, link); g_free(client); } =20 -static void cpu_notify_map_clients_locked(void) +static void address_space_notify_map_clients_locked(AddressSpace *as) { - MapClient *client; + AddressSpaceMapClient *client; =20 - while (!QLIST_EMPTY(&map_client_list)) { - client =3D QLIST_FIRST(&map_client_list); + while (!QLIST_EMPTY(&as->map_client_list)) { + client =3D QLIST_FIRST(&as->map_client_list); qemu_bh_schedule(client->bh); - cpu_unregister_map_client_do(client); + address_space_unregister_map_client_do(client); } } =20 -void cpu_register_map_client(QEMUBH *bh) +void address_space_register_map_client(AddressSpace *as, QEMUBH *bh) { - MapClient *client =3D g_malloc(sizeof(*client)); + AddressSpaceMapClient *client =3D g_malloc(sizeof(*client)); =20 - qemu_mutex_lock(&map_client_list_lock); + qemu_mutex_lock(&as->map_client_list_lock); client->bh =3D bh; - QLIST_INSERT_HEAD(&map_client_list, client, link); + QLIST_INSERT_HEAD(&as->map_client_list, client, link); /* Write map_client_list before reading in_use. */ smp_mb(); - if (!qatomic_read(&bounce.in_use)) { - cpu_notify_map_clients_locked(); + if (!qatomic_read(&as->bounce.in_use)) { + address_space_notify_map_clients_locked(as); } - qemu_mutex_unlock(&map_client_list_lock); + qemu_mutex_unlock(&as->map_client_list_lock); } =20 void cpu_exec_init_all(void) @@ -3110,28 +3092,27 @@ void cpu_exec_init_all(void) finalize_target_page_bits(); io_mem_init(); memory_map_init(); - qemu_mutex_init(&map_client_list_lock); } =20 -void cpu_unregister_map_client(QEMUBH *bh) +void address_space_unregister_map_client(AddressSpace *as, QEMUBH *bh) { - MapClient *client; + AddressSpaceMapClient *client; =20 - qemu_mutex_lock(&map_client_list_lock); - QLIST_FOREACH(client, &map_client_list, link) { + qemu_mutex_lock(&as->map_client_list_lock); + QLIST_FOREACH(client, &as->map_client_list, link) { if (client->bh =3D=3D bh) { - cpu_unregister_map_client_do(client); + address_space_unregister_map_client_do(client); break; } } - qemu_mutex_unlock(&map_client_list_lock); + qemu_mutex_unlock(&as->map_client_list_lock); } =20 -static void cpu_notify_map_clients(void) +static void address_space_notify_map_clients(AddressSpace *as) { - qemu_mutex_lock(&map_client_list_lock); - cpu_notify_map_clients_locked(); - qemu_mutex_unlock(&map_client_list_lock); + qemu_mutex_lock(&as->map_client_list_lock); + address_space_notify_map_clients_locked(as); + qemu_mutex_unlock(&as->map_client_list_lock); } =20 static bool flatview_access_valid(FlatView *fv, hwaddr addr, hwaddr len, @@ -3198,8 +3179,8 @@ flatview_extend_translation(FlatView *fv, hwaddr addr, * May map a subset of the requested range, given by and returned in *plen. * May return NULL if resources needed to perform the mapping are exhauste= d. * Use only for reads OR writes - not for read-modify-write operations. - * Use cpu_register_map_client() to know when retrying the map operation is - * likely to succeed. + * Use address_space_register_map_client() to know when retrying the map + * operation is likely to succeed. */ void *address_space_map(AddressSpace *as, hwaddr addr, @@ -3222,25 +3203,25 @@ void *address_space_map(AddressSpace *as, mr =3D flatview_translate(fv, addr, &xlat, &l, is_write, attrs); =20 if (!memory_access_is_direct(mr, is_write)) { - if (qatomic_xchg(&bounce.in_use, true)) { + if (qatomic_xchg(&as->bounce.in_use, true)) { *plen =3D 0; return NULL; } /* Avoid unbounded allocations */ l =3D MIN(l, TARGET_PAGE_SIZE); - bounce.buffer =3D qemu_memalign(TARGET_PAGE_SIZE, l); - bounce.addr =3D addr; - bounce.len =3D l; + as->bounce.buffer =3D qemu_memalign(TARGET_PAGE_SIZE, l); + as->bounce.addr =3D addr; + as->bounce.len =3D l; =20 memory_region_ref(mr); - bounce.mr =3D mr; + as->bounce.mr =3D mr; if (!is_write) { flatview_read(fv, addr, MEMTXATTRS_UNSPECIFIED, - bounce.buffer, l); + as->bounce.buffer, l); } =20 *plen =3D l; - return bounce.buffer; + return as->bounce.buffer; } =20 =20 @@ -3258,7 +3239,7 @@ void *address_space_map(AddressSpace *as, void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len, bool is_write, hwaddr access_len) { - if (buffer !=3D bounce.buffer) { + if (buffer !=3D as->bounce.buffer) { MemoryRegion *mr; ram_addr_t addr1; =20 @@ -3274,15 +3255,15 @@ void address_space_unmap(AddressSpace *as, void *bu= ffer, hwaddr len, return; } if (is_write) { - address_space_write(as, bounce.addr, MEMTXATTRS_UNSPECIFIED, - bounce.buffer, access_len); + address_space_write(as, as->bounce.addr, MEMTXATTRS_UNSPECIFIED, + as->bounce.buffer, access_len); } - qemu_vfree(bounce.buffer); - bounce.buffer =3D NULL; - memory_region_unref(bounce.mr); + qemu_vfree(as->bounce.buffer); + as->bounce.buffer =3D NULL; + memory_region_unref(as->bounce.mr); /* Clear in_use before reading map_client_list. */ - qatomic_set_mb(&bounce.in_use, false); - cpu_notify_map_clients(); + qatomic_set_mb(&as->bounce.in_use, false); + address_space_notify_map_clients(as); } =20 void *cpu_physical_memory_map(hwaddr addr, --=20 2.43.2