From nobody Sat May 18 20:15:30 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=1695197247; cv=none; d=zohomail.com; s=zohoarc; b=NOUHuTOsV6LKF0AOQzCnEA+EPaPsJQ8ttDw8YokQ3+PUybNzqI3vbhAyUbRHH+hFickAj078kmnaMhlu4z5gRtBZOSnwOflCVNK0BobhepTroR5OvhiGx0ckM1Dwv4kPL5VaNEE+hksibZbr+MaXEAMwtvMTGlB6mHJrqOyjv9Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695197247; h=Content-Transfer-Encoding:Cc: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=U+1tBBSiPlM8P1nwNw+CNc7/v01RNBynOoILjaqSTO8=; b=SGoz5OVwqwHn/tjPl17Xz+0nvf1ctxwTRZWVKEBqCeDhPZCRR/gbQEJfZ1+5T2ZaejS+pUgoORIehKzSXIrH0IcxBC1oMnoAOkRSqjZHw88V2aPl2LpLB2tUXughICq/iMBUq84UWbQ4fnjti/Jhcb6A/UzC+yXYtAv+2zIjUhI= 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 1695197246871143.46801302076415; Wed, 20 Sep 2023 01:07:26 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qisER-0005an-Ae; Wed, 20 Sep 2023 04:06:47 -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 1qisEP-0005YA-HA for qemu-devel@nongnu.org; Wed, 20 Sep 2023 04:06:45 -0400 Received: from mail-yw1-x112d.google.com ([2607:f8b0:4864:20::112d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qisEN-0002o6-80 for qemu-devel@nongnu.org; Wed, 20 Sep 2023 04:06:45 -0400 Received: by mail-yw1-x112d.google.com with SMTP id 00721157ae682-59c2ca01f27so41965847b3.2 for ; Wed, 20 Sep 2023 01:06:42 -0700 (PDT) Received: from mnissler.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id a16-20020aa78650000000b006862b2a6b0dsm2378596pfo.15.2023.09.20.01.06.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Sep 2023 01:06:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1695197202; x=1695802002; 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=U+1tBBSiPlM8P1nwNw+CNc7/v01RNBynOoILjaqSTO8=; b=Zppq91+IzK+1keqkFbCsetcJbN3gNcWQ31/BIjLB95bJjun2zTgP6Z1jy/In65eTti uG9jRRfg6hskC7xL99UgCdKyL5FwqZfh3P5wGlzAHuusJarSaXV3AtKPkJcl+NkltnqE YooG/2eiCdaqygAiXCx4EoFjk8jYtgJN8m6FjXgCkV1Rlwn7L6mRkjb+W0h1DNjMM8hP +hA/vr7b8NWMoUTCj6VvmFDwiZLsCEdEl11k5MGCKike5/inn22/QkyCBfO/fyzFbLm8 h4pKeIGVX/auK7EcIE/mmDa8zwXFMAjNDAJjuPpqtAH08p3o+n64B1pHogeusOtb8bej wamg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695197202; x=1695802002; 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=U+1tBBSiPlM8P1nwNw+CNc7/v01RNBynOoILjaqSTO8=; b=OASJ+3HVUpWmW1NNPLIQ9kR0EdUZu+EsNNNEzCSUaiNJpcgCIiLdh/UbR125yiVR28 bOP7ujEBiPTIitueae0sy9ibUgro6eAf5uAK46Fisyqo08+j2fIMxrh7T3neWV2QSaWt czULhc3v9lhrzE5pd3MBK5aOfSpVuzOmIDvmGKy+JX8LgkIg+18mvo1ARARCrZu2Vsgx zt+BoEAk9wXQFMXXByHZOkiwOXKECWZiod7FSnn/VC4FXY6evhfCnk8NRXcvf/5YwDtR FHfStOCU3oP+mlu9XtZKhxQrydXlDjUnRQMFkTZdbmoZuYRRCYiCobo0A9JL5+mP7VD1 MElg== X-Gm-Message-State: AOJu0YyCT8dNhBXL3th8CyEJr33YFzVZv905FV1ivHsV7VMc/VHY7WEa NvQMSQFfmXHCRpjGXYpnoLKhRoX2w+h7F605Y4BEuw== X-Google-Smtp-Source: AGHT+IEulF1vc0DUR93Hes/7Xo4Mm7LpBs3gHzVhJr9sWsRWBnjUkmpLA0GSE7SgFl4gVmUY4xEM0Q== X-Received: by 2002:a81:9e10:0:b0:579:effb:ae1c with SMTP id m16-20020a819e10000000b00579effbae1cmr1894010ywj.26.1695197201984; Wed, 20 Sep 2023 01:06:41 -0700 (PDT) From: Mattias Nissler To: qemu-devel@nongnu.org Cc: Elena Ufimtseva , David Hildenbrand , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , john.levon@nutanix.com, peterx@redhat.com, Marcel Apfelbaum , stefanha@redhat.com, Jagannathan Raman , Paolo Bonzini , "Michael S. Tsirkin" , Mattias Nissler Subject: [PATCH v5 1/5] softmmu: Per-AddressSpace bounce buffering Date: Wed, 20 Sep 2023 01:06:18 -0700 Message-Id: <20230920080622.3600226-2-mnissler@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230920080622.3600226-1-mnissler@rivosinc.com> References: <20230920080622.3600226-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::112d; envelope-from=mnissler@rivosinc.com; helo=mail-yw1-x112d.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: 1695197247800100003 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. Signed-off-by: Mattias Nissler --- include/exec/cpu-common.h | 2 - include/exec/memory.h | 45 ++++++++++++++++- softmmu/dma-helpers.c | 4 +- softmmu/memory.c | 7 +++ softmmu/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 41788c0bdd..63463c415d 100644 --- a/include/exec/cpu-common.h +++ b/include/exec/cpu-common.h @@ -138,8 +138,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 68284428f8..7d68936157 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -1076,6 +1076,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 */ @@ -1092,6 +1105,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; @@ -2832,8 +2851,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 @@ -2858,6 +2877,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/softmmu/dma-helpers.c b/softmmu/dma-helpers.c index 2463964805..d9fc26c063 100644 --- a/softmmu/dma-helpers.c +++ b/softmmu/dma-helpers.c @@ -167,7 +167,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); goto out; } =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/softmmu/memory.c b/softmmu/memory.c index 7d9494ce70..ffa37fc327 100644 --- a/softmmu/memory.c +++ b/softmmu/memory.c @@ -3105,6 +3105,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); @@ -3112,6 +3115,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/softmmu/physmem.c b/softmmu/physmem.c index 18277ddd67..29c9fc506c 100644 --- a/softmmu/physmem.c +++ b/softmmu/physmem.c @@ -2926,55 +2926,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) @@ -2990,28 +2972,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, @@ -3078,8 +3059,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, @@ -3102,25 +3083,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 @@ -3138,7 +3119,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 @@ -3154,15 +3135,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.34.1 From nobody Sat May 18 20:15:30 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=1695197259; cv=none; d=zohomail.com; s=zohoarc; b=D+JM/q2PByfCXnJICOo8v+kd6mekClxYbSVH+1yR+PhFe8OwWS+16JeVrxCshCmX3qowoiL+G3ZdSkA8WU+AbKfrTM3LYMglX3sXnZSMj8fvUGrfxqHy+Bba5+HJzjUlUk8FqgSMkEaGQqp14IJ2c17XGp/bSURedYiED0UJOuw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695197259; h=Content-Transfer-Encoding:Cc: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=uIJ+VwdS0qQmvIzOKIa1YaCzZZnGObbyv7RkPmyQcI0=; b=NMlVsDjDCMfczJlmKyZE42em+Q+nrUgDhq1oQUV2WeiFSc8IgSIaGUyDeisGbDZE33ucgSqAM2Ew5pfumVDgND7OIwi+zZ5arqQOtqqh8ZnxKhE5amFLA1IpujROTxPLS8yiAUCgan8T8qUuHa8NT3IrCAzQDes0bXJ4XAyZAtI= 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 1695197259618298.7707291829206; Wed, 20 Sep 2023 01:07:39 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qisET-0005cm-M7; Wed, 20 Sep 2023 04:06:49 -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 1qisEQ-0005au-Uv for qemu-devel@nongnu.org; Wed, 20 Sep 2023 04:06:47 -0400 Received: from mail-il1-x131.google.com ([2607:f8b0: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 1qisEO-0002oJ-Jf for qemu-devel@nongnu.org; Wed, 20 Sep 2023 04:06:46 -0400 Received: by mail-il1-x131.google.com with SMTP id e9e14a558f8ab-34deefc2016so21898135ab.2 for ; Wed, 20 Sep 2023 01:06:44 -0700 (PDT) Received: from mnissler.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id a16-20020aa78650000000b006862b2a6b0dsm2378596pfo.15.2023.09.20.01.06.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Sep 2023 01:06:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1695197203; x=1695802003; 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=uIJ+VwdS0qQmvIzOKIa1YaCzZZnGObbyv7RkPmyQcI0=; b=RMBMFBupFtFaldXQxAGvEgSULohlAqsCC5BRJ17baa56uyZAbIP9/CfiDRtBMr+3pM iVCSsiQ1JDuG1QxhhTm+aSkevtMQWBK0MRGfw/H8Q2Op4ZLEwPoUcFB0yKVaWVPE1aDf eObNj1rjmnlKg6j97JwTnPnv7jvYRH202CkzPjiOBIIuwftZklGUer3i0XcV/v3Uj6kZ NYs6FbZAMxSnID+J4+6WzxJU/Gj7bVq1bEhbirtSpVMtuBmdvBHTEbUHWkVzGyk5I9/e LihGr9vCK9SnrGW7M6YzTcVoMY1nm5bDl3oW3u2WU62MRMw6BPiDeChFVw+rAZ9WID2e E/EQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695197203; x=1695802003; 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=uIJ+VwdS0qQmvIzOKIa1YaCzZZnGObbyv7RkPmyQcI0=; b=fYL8q0x2SubQLMUlkPDzIpvrk3Y2O+Qa7dlUfUU87OfUJVyfHvOqVKO+pK2Nm/rVRk 58sxdz6pUXJYhG/dTa7WakrC61RFMk0PDaRl0WVgz2nYxXBFnqw+V1lQ0i2ZtcKXBHYB /BERzTW8ySPrQOjhHPY5VbDBExLs65VkVOKBzLW670oyvvJF3ORdge1nmba5/DCLsiR9 eXL85m2ELJDOyX6qG1ggCO3iad2UdEaLJHkPmCKTmZk27ouBSY87Z4KynALNgcxoZ5lp qDdb5ZzfPwB5q5ZM1Ub3D8RYG0h35DQx3rH3nKZFjBAqLZZXSZUWnUqaZvm116RLBFyH X6ow== X-Gm-Message-State: AOJu0YxtuoJ0Mc9dqURYPC+PlVBOJaAP0fVLt8FEqUO03zOsSqGenxyb rB04rIRQy7TxcTjSsPZQF964qEp6d7QUEdpfsnbJJA== X-Google-Smtp-Source: AGHT+IF8LXT076SyyjiKhryA6WmMFwOxzLpcOX1adCFsdja99IKkfOsJWkMYBN7430arXqWh6bQGig== X-Received: by 2002:a05:6e02:14c1:b0:34f:70ec:d4cf with SMTP id o1-20020a056e0214c100b0034f70ecd4cfmr2239352ilk.8.1695197203118; Wed, 20 Sep 2023 01:06:43 -0700 (PDT) From: Mattias Nissler To: qemu-devel@nongnu.org Cc: Elena Ufimtseva , David Hildenbrand , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , john.levon@nutanix.com, peterx@redhat.com, Marcel Apfelbaum , stefanha@redhat.com, Jagannathan Raman , Paolo Bonzini , "Michael S. Tsirkin" , Mattias Nissler Subject: [PATCH v5 2/5] softmmu: Support concurrent bounce buffers Date: Wed, 20 Sep 2023 01:06:19 -0700 Message-Id: <20230920080622.3600226-3-mnissler@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230920080622.3600226-1-mnissler@rivosinc.com> References: <20230920080622.3600226-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::131; envelope-from=mnissler@rivosinc.com; helo=mail-il1-x131.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: 1695197261535100003 Content-Type: text/plain; charset="utf-8" When DMA memory can't be directly accessed, as is the case when running the device model in a separate process without shareable DMA file descriptors, bounce buffering is used. It is not uncommon for device models to request mapping of several DMA regions at the same time. Examples include: * net devices, e.g. when transmitting a packet that is split across several TX descriptors (observed with igb) * USB host controllers, when handling a packet with multiple data TRBs (observed with xhci) Previously, qemu only provided a single bounce buffer per AddressSpace and would fail DMA map requests while the buffer was already in use. In turn, this would cause DMA failures that ultimately manifest as hardware errors from the guest perspective. This change allocates DMA bounce buffers dynamically instead of supporting only a single buffer. Thus, multiple DMA mappings work correctly also when RAM can't be mmap()-ed. The total bounce buffer allocation size is limited individually for each AddressSpace. The default limit is 4096 bytes, matching the previous maximum buffer size. A new x-max-bounce-buffer-size parameter is provided to configure the limit for PCI devices. Signed-off-by: Mattias Nissler Acked-by: Peter Xu --- hw/pci/pci.c | 8 ++++ include/exec/memory.h | 14 +++---- include/hw/pci/pci_device.h | 3 ++ softmmu/memory.c | 5 ++- softmmu/physmem.c | 80 +++++++++++++++++++++++++------------ 5 files changed, 74 insertions(+), 36 deletions(-) diff --git a/hw/pci/pci.c b/hw/pci/pci.c index 881d774fb6..d071ac8091 100644 --- a/hw/pci/pci.c +++ b/hw/pci/pci.c @@ -85,6 +85,8 @@ static Property pci_props[] =3D { QEMU_PCIE_ERR_UNC_MASK_BITNR, true), DEFINE_PROP_BIT("x-pcie-ari-nextfn-1", PCIDevice, cap_present, QEMU_PCIE_ARI_NEXTFN_1_BITNR, false), + DEFINE_PROP_SIZE("x-max-bounce-buffer-size", PCIDevice, + max_bounce_buffer_size, DEFAULT_MAX_BOUNCE_BUFFER_SIZ= E), DEFINE_PROP_END_OF_LIST() }; =20 @@ -1208,6 +1210,8 @@ static PCIDevice *do_pci_register_device(PCIDevice *p= ci_dev, "bus master container", UINT64_MAX); address_space_init(&pci_dev->bus_master_as, &pci_dev->bus_master_container_region, pci_dev->nam= e); + pci_dev->bus_master_as.max_bounce_buffer_size =3D + pci_dev->max_bounce_buffer_size; =20 if (phase_check(PHASE_MACHINE_READY)) { pci_init_bus_master(pci_dev); @@ -2664,6 +2668,10 @@ static void pci_device_class_init(ObjectClass *klass= , void *data) k->unrealize =3D pci_qdev_unrealize; k->bus_type =3D TYPE_PCI_BUS; device_class_set_props(k, pci_props); + object_class_property_set_description( + klass, "x-max-bounce-buffer-size", + "Maximum buffer size allocated for bounce buffers used for mapped " + "access to indirect DMA memory"); } =20 static void pci_device_class_base_init(ObjectClass *klass, void *data) diff --git a/include/exec/memory.h b/include/exec/memory.h index 7d68936157..67379bd9cc 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -1081,13 +1081,7 @@ typedef struct AddressSpaceMapClient { QLIST_ENTRY(AddressSpaceMapClient) link; } AddressSpaceMapClient; =20 -typedef struct { - MemoryRegion *mr; - void *buffer; - hwaddr addr; - hwaddr len; - bool in_use; -} BounceBuffer; +#define DEFAULT_MAX_BOUNCE_BUFFER_SIZE (4096) =20 /** * struct AddressSpace: describes a mapping of addresses to #MemoryRegion = objects @@ -1106,8 +1100,10 @@ struct AddressSpace { QTAILQ_HEAD(, MemoryListener) listeners; QTAILQ_ENTRY(AddressSpace) address_spaces_link; =20 - /* Bounce buffer to use for this address space. */ - BounceBuffer bounce; + /* Maximum DMA bounce buffer size used for indirect memory map request= s */ + uint64_t max_bounce_buffer_size; + /* Total size of bounce buffers currently allocated, atomically access= ed */ + uint64_t bounce_buffer_size; /* List of callbacks to invoke when buffers free up */ QemuMutex map_client_list_lock; QLIST_HEAD(, AddressSpaceMapClient) map_client_list; diff --git a/include/hw/pci/pci_device.h b/include/hw/pci/pci_device.h index d3dd0f64b2..f4027c5379 100644 --- a/include/hw/pci/pci_device.h +++ b/include/hw/pci/pci_device.h @@ -160,6 +160,9 @@ struct PCIDevice { /* ID of standby device in net_failover pair */ char *failover_pair_id; uint32_t acpi_index; + + /* Maximum DMA bounce buffer size used for indirect memory map request= s */ + uint64_t max_bounce_buffer_size; }; =20 static inline int pci_intx(PCIDevice *pci_dev) diff --git a/softmmu/memory.c b/softmmu/memory.c index ffa37fc327..24d90b10b2 100644 --- a/softmmu/memory.c +++ b/softmmu/memory.c @@ -3105,7 +3105,8 @@ 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; + as->max_bounce_buffer_size =3D DEFAULT_MAX_BOUNCE_BUFFER_SIZE; + as->bounce_buffer_size =3D 0; qemu_mutex_init(&as->map_client_list_lock); QLIST_INIT(&as->map_client_list); as->name =3D g_strdup(name ? name : "anonymous"); @@ -3115,7 +3116,7 @@ void address_space_init(AddressSpace *as, MemoryRegio= n *root, const char *name) =20 static void do_address_space_destroy(AddressSpace *as) { - assert(!qatomic_read(&as->bounce.in_use)); + assert(qatomic_read(&as->bounce_buffer_size) =3D=3D 0); assert(QLIST_EMPTY(&as->map_client_list)); qemu_mutex_destroy(&as->map_client_list_lock); =20 diff --git a/softmmu/physmem.c b/softmmu/physmem.c index 29c9fc506c..73d2c3eda5 100644 --- a/softmmu/physmem.c +++ b/softmmu/physmem.c @@ -2926,6 +2926,20 @@ void cpu_flush_icache_range(hwaddr start, hwaddr len) NULL, len, FLUSH_CACHE); } =20 +/* + * A magic value stored in the first 8 bytes of the bounce buffer struct. = Used + * to detect illegal pointers passed to address_space_unmap. + */ +#define BOUNCE_BUFFER_MAGIC 0xb4017ceb4ffe12ed + +typedef struct { + uint64_t magic; + MemoryRegion *mr; + hwaddr addr; + size_t len; + uint8_t buffer[]; +} BounceBuffer; + static void address_space_unregister_map_client_do(AddressSpaceMapClient *client) { @@ -2951,9 +2965,9 @@ void address_space_register_map_client(AddressSpace *= as, QEMUBH *bh) qemu_mutex_lock(&as->map_client_list_lock); client->bh =3D bh; QLIST_INSERT_HEAD(&as->map_client_list, client, link); - /* Write map_client_list before reading in_use. */ + /* Write map_client_list before reading bounce_buffer_size. */ smp_mb(); - if (!qatomic_read(&as->bounce.in_use)) { + if (qatomic_read(&as->bounce_buffer_size) < as->max_bounce_buffer_size= ) { address_space_notify_map_clients_locked(as); } qemu_mutex_unlock(&as->map_client_list_lock); @@ -3083,28 +3097,38 @@ 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(&as->bounce.in_use, true)) { + size_t size =3D qatomic_add_fetch(&as->bounce_buffer_size, l); + if (size > as->max_bounce_buffer_size) { + /* + * Note that the overshot might be larger than l if threads are + * racing and bump bounce_buffer_size at the same time. + */ + size_t excess =3D MIN(size - as->max_bounce_buffer_size, l); + l -=3D excess; + qatomic_sub(&as->bounce_buffer_size, excess); + } + + if (l =3D=3D 0) { *plen =3D 0; return NULL; } - /* Avoid unbounded allocations */ - l =3D MIN(l, TARGET_PAGE_SIZE); - as->bounce.buffer =3D qemu_memalign(TARGET_PAGE_SIZE, l); - as->bounce.addr =3D addr; - as->bounce.len =3D l; =20 + BounceBuffer *bounce =3D g_malloc0(l + sizeof(BounceBuffer)); + bounce->magic =3D BOUNCE_BUFFER_MAGIC; memory_region_ref(mr); - as->bounce.mr =3D mr; + bounce->mr =3D mr; + bounce->addr =3D addr; + bounce->len =3D l; + if (!is_write) { flatview_read(fv, addr, MEMTXATTRS_UNSPECIFIED, - as->bounce.buffer, l); + bounce->buffer, l); } =20 *plen =3D l; - return as->bounce.buffer; + return bounce->buffer; } =20 - memory_region_ref(mr); *plen =3D flatview_extend_translation(fv, addr, len, mr, xlat, l, is_write, attrs); @@ -3119,12 +3143,11 @@ 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 as->bounce.buffer) { - MemoryRegion *mr; - ram_addr_t addr1; + MemoryRegion *mr; + ram_addr_t addr1; =20 - mr =3D memory_region_from_host(buffer, &addr1); - assert(mr !=3D NULL); + mr =3D memory_region_from_host(buffer, &addr1); + if (mr !=3D NULL) { if (is_write) { invalidate_and_set_dirty(mr, addr1, access_len); } @@ -3134,15 +3157,22 @@ void address_space_unmap(AddressSpace *as, void *bu= ffer, hwaddr len, memory_region_unref(mr); return; } + + + BounceBuffer *bounce =3D container_of(buffer, BounceBuffer, buffer); + assert(bounce->magic =3D=3D BOUNCE_BUFFER_MAGIC); + if (is_write) { - address_space_write(as, as->bounce.addr, MEMTXATTRS_UNSPECIFIED, - as->bounce.buffer, access_len); - } - 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(&as->bounce.in_use, false); + address_space_write(as, bounce->addr, MEMTXATTRS_UNSPECIFIED, + bounce->buffer, access_len); + } + + qatomic_sub(&as->bounce_buffer_size, bounce->len); + bounce->magic =3D ~BOUNCE_BUFFER_MAGIC; + memory_region_unref(bounce->mr); + g_free(bounce); + /* Write bounce_buffer_size before reading map_client_list. */ + smp_mb(); address_space_notify_map_clients(as); } =20 --=20 2.34.1 From nobody Sat May 18 20:15:30 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=1695197250; cv=none; d=zohomail.com; s=zohoarc; b=RT0j7Ddj6zsH/Q2GGjs6WJ7Ob82lZcrTe9pvZtt9BJ/WHiob9xlFDmQWnh8OLvbMXceq6l6nVrNDh/KSRMbpyKfvNVhsUQnKbpDY2RMt8H6v0H/tOb99imyXcGs9YOYw3/pOph1LlozMtl8a8fWvBzQCwqnldG/JEKtf9nvgN+A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695197250; h=Content-Transfer-Encoding:Cc: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=ugWmU+RI6KuJRXphjeFR5kNJeU7Hhqdcf4zIt6LNeaY=; b=H8ni5UyHPJ62rf7/iVJIUWU/M8zMQqQ6XYwFYAtpJWT4IO+oq27gKyvPGGKdfAWlqRnZd7qjTKm5gFZeu0JwMMcLDTw4K4hK/2RrEFGsm3aiS7grZ23OyFXwFAk0MAzACB/jN6NQKh5E6Axuy2ywjbF6itpmYa2/n6843RMZ+p0= 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 169519725007636.59449000321797; Wed, 20 Sep 2023 01:07:30 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qisEU-0005dQ-GE; Wed, 20 Sep 2023 04:06:50 -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 1qisER-0005bh-QK for qemu-devel@nongnu.org; Wed, 20 Sep 2023 04:06:47 -0400 Received: from mail-pf1-x42f.google.com ([2607:f8b0:4864:20::42f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qisEQ-0002oU-1r for qemu-devel@nongnu.org; Wed, 20 Sep 2023 04:06:47 -0400 Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-690b8859c46so2086045b3a.3 for ; Wed, 20 Sep 2023 01:06:45 -0700 (PDT) Received: from mnissler.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id a16-20020aa78650000000b006862b2a6b0dsm2378596pfo.15.2023.09.20.01.06.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Sep 2023 01:06:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1695197204; x=1695802004; 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=ugWmU+RI6KuJRXphjeFR5kNJeU7Hhqdcf4zIt6LNeaY=; b=fUOUlR6orK48hnRZiFwplkldz9gGGrP5izHFdM8xKD4Zy0BEZFUF+AToMrt4T/FjRO VZw1JoeiuU4IADPRxZVqy5RhUwG2bF2DXIbnoswpkcc3FFqUYnUiPXxYaJQRqoEbfR45 7zl4ISzGH6FDffQRNiXrlW7XOZjCx9GgrkhuNSyuqIowSj7diDcgsTrgpVYKPjC4Tx+w A1oBzyodlTTCB5dVdmauynOLcCOcQVBBysCFsZwfwH1g7aAl2fECCSNPFxrgO6I4wlYf QWayE++Dw2m0yrx5+GufcQxucfMHYasvTk2kf8CQ0T9ETV5T1q+BVqshRn0B1B/GPEUX uSzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695197204; x=1695802004; 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=ugWmU+RI6KuJRXphjeFR5kNJeU7Hhqdcf4zIt6LNeaY=; b=UfMER7B3+Z8M+lnfd9gCEe5b9uDp/p/Df3H+AITgXFYclK/6B7WCpIhL/RbUS1vw4K YF6YVUrm22UbWtcTuexpjzoFoSmQgTniUkjpxH1t6+X1ius7a+MLHOU/us0PN32eohP9 41rKRDt90Iqsbw3888v6asY8qAnuVVpHczyOxKXm2mWMqsMUx63to5lVqEeVombH4tAx +6UeHP3EeCbPYnfVEuA4jtelDjrz4noZwjtZ9M5Uu5Uegaz6Pe+7BiZzG3k+RtrXwt0U ZLYDFq6pz9I15lUC3ZnTJ/3PUGLwwkOJfI8F/j+WEOPcB7yltcEXkUJ9c9VUswBd/H19 kghw== X-Gm-Message-State: AOJu0Yzree7EIAKk3vwqE1pJdEaZazPIubaivesUmsRXZnSdzttZcoFV C+bWPYFY93UvjtZfrwk21+TAG9roiMUGvCCvoolqMw== X-Google-Smtp-Source: AGHT+IE1a4jjlgDR3OPPD6bzNogKvNspUhKNAKL1181DN1A7pklyeaN2bKw+xzS8IzADNkaFRibNtg== X-Received: by 2002:a05:6a00:c8a:b0:690:25fb:bac1 with SMTP id a10-20020a056a000c8a00b0069025fbbac1mr2005125pfv.18.1695197204261; Wed, 20 Sep 2023 01:06:44 -0700 (PDT) From: Mattias Nissler To: qemu-devel@nongnu.org Cc: Elena Ufimtseva , David Hildenbrand , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , john.levon@nutanix.com, peterx@redhat.com, Marcel Apfelbaum , stefanha@redhat.com, Jagannathan Raman , Paolo Bonzini , "Michael S. Tsirkin" , Mattias Nissler Subject: [PATCH v5 3/5] Update subprojects/libvfio-user Date: Wed, 20 Sep 2023 01:06:20 -0700 Message-Id: <20230920080622.3600226-4-mnissler@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230920080622.3600226-1-mnissler@rivosinc.com> References: <20230920080622.3600226-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::42f; envelope-from=mnissler@rivosinc.com; helo=mail-pf1-x42f.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: 1695197251924100001 Content-Type: text/plain; charset="utf-8" Brings in assorted bug fixes. The following are of particular interest with respect to message-based DMA support: * bb308a2 "Fix address calculation for message-based DMA" Corrects a bug in DMA address calculation. * 1569a37 "Pass server->client command over a separate socket pair" Adds support for separate sockets for either command direction, addressing a bug where libvfio-user gets confused if both client and server send commands concurrently. Signed-off-by: Mattias Nissler --- subprojects/libvfio-user.wrap | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/subprojects/libvfio-user.wrap b/subprojects/libvfio-user.wrap index 416955ca45..cdf0a7a375 100644 --- a/subprojects/libvfio-user.wrap +++ b/subprojects/libvfio-user.wrap @@ -1,4 +1,4 @@ [wrap-git] url =3D https://gitlab.com/qemu-project/libvfio-user.git -revision =3D 0b28d205572c80b568a1003db2c8f37ca333e4d7 +revision =3D 1569a37a54ecb63bd4008708c76339ccf7d06115 depth =3D 1 --=20 2.34.1 From nobody Sat May 18 20:15:30 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=1695197229; cv=none; d=zohomail.com; s=zohoarc; b=BibFOm1+DgLIYaoGQpgcT4Rgiu6HPFMpPHGFbrFgF90o2PhJuaESNqbrdf8h/cmmzKHTKNXEJ37BgRjJH3RdyKunbvClcDLWiuQhH+Giws/GKw33hJwEpa24nwrG3TvyFJ0lw9XvHFso1l2TMdmJMqIup02mcn7Roc/bEyCPn/I= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695197229; h=Content-Transfer-Encoding:Cc: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=y6xthKlgZacOdOdKfVjLGvE40RnnIrZnmgq2bIqI3+Y=; b=E5xn9MyD8RqnUSuwDDxz0jGTmYrzDr5uBJtIdu5mDw+yP1716MugW7nclYq25uVBhWh9AKxJ6aKD4GTw9gQbvGtQurMu0Eu/1WVLSsfPIcuC8P7fQLLjxt26sG2XPYVJZShnDe0jGJf0pBz+DRqlwSCYRRtFYdAiB1dnaZVAS44= 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 1695197229297874.5992106650599; Wed, 20 Sep 2023 01:07:09 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qisEV-0005eE-LJ; Wed, 20 Sep 2023 04:06:52 -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 1qisET-0005cp-No for qemu-devel@nongnu.org; Wed, 20 Sep 2023 04:06:49 -0400 Received: from mail-oi1-x22e.google.com ([2607:f8b0:4864:20::22e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qisER-0002pl-Mz for qemu-devel@nongnu.org; Wed, 20 Sep 2023 04:06:49 -0400 Received: by mail-oi1-x22e.google.com with SMTP id 5614622812f47-3ade77970a9so872394b6e.2 for ; Wed, 20 Sep 2023 01:06:46 -0700 (PDT) Received: from mnissler.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id a16-20020aa78650000000b006862b2a6b0dsm2378596pfo.15.2023.09.20.01.06.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Sep 2023 01:06:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1695197205; x=1695802005; 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=y6xthKlgZacOdOdKfVjLGvE40RnnIrZnmgq2bIqI3+Y=; b=l3qcgg8TzFQU9Y3674MFzytoiM26i7zDrw9TMp7t4fYXRNLszU0yzN3leTQIjFi7zM KBysgY9PrycbDw3PXawcVmPxyV9u26d+4l3b5jpPoq9IVenYxaGKJRZN/haLUDxahQRU prFhnZLNLypsNcx/shgAY4ZidyZ04Cbr7hC6upvwbVOq8GQSMyMgIB28snNGsfPBk93X dpx+niD75Mjq5nxifV6ufY4dsM786LlfSicFp5PzGEZAFlJUZHhszSY3UyXPUqIoSRTv nGGct5D29mcEhc+rQ8NYiw/rR7z6+Ds+LzN5COjFK68jz14joU1gixDl67jtqJmYSyoX pRmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695197205; x=1695802005; 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=y6xthKlgZacOdOdKfVjLGvE40RnnIrZnmgq2bIqI3+Y=; b=YXUhf/38rQF/0kihursBcuCDiLLpOE3xS/CqD9VVVXKQwfz0kk5uz1yXZqTLNQtTwq Avcd6dS5gVnsRHoizetwEENOkkJ3YLGzsG4PxEEh5UqYh83FSWIC0kL6Dmx+QSy4vZKU Pk8PRQlZX6kmga1cYqs/jTPkcB9VXqek7ry3WaheLREQ9WrpoQDdrCsym5wcy0Q+FjhQ v6/5YxfVFFRHVnuPoCHqFacvhds+/u7lsHJcmuZ9aV+H0MiDUXJNvjBZiyAVKl6bSmYV wSzEuEsPuVfrofSXR/0HhAI7in2QOK0h2mDaCPc8xUp+UdDRlDB0edxzo4R3FDKCkdM+ Eb8A== X-Gm-Message-State: AOJu0YywI7uHhBpYn0ZHCbfIIIxpcsLiuFLgqh49Z2IduUCvpw97tB2e 0TecKvFQHMwv+Q3tJfElt1FOKFpk7xCPepdsOomwTA== X-Google-Smtp-Source: AGHT+IFWNimVOtNthOpPEIAiiCE6ykrAeCoe9ujIZ0TroK3prhOJxwROUV/4zip8mvKUk0WAlUZmsA== X-Received: by 2002:a05:6808:158c:b0:3a7:2d26:9456 with SMTP id t12-20020a056808158c00b003a72d269456mr1818930oiw.57.1695197205397; Wed, 20 Sep 2023 01:06:45 -0700 (PDT) From: Mattias Nissler To: qemu-devel@nongnu.org Cc: Elena Ufimtseva , David Hildenbrand , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , john.levon@nutanix.com, peterx@redhat.com, Marcel Apfelbaum , stefanha@redhat.com, Jagannathan Raman , Paolo Bonzini , "Michael S. Tsirkin" , Mattias Nissler Subject: [PATCH v5 4/5] vfio-user: Message-based DMA support Date: Wed, 20 Sep 2023 01:06:21 -0700 Message-Id: <20230920080622.3600226-5-mnissler@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230920080622.3600226-1-mnissler@rivosinc.com> References: <20230920080622.3600226-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::22e; envelope-from=mnissler@rivosinc.com; helo=mail-oi1-x22e.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: 1695197230030100001 Content-Type: text/plain; charset="utf-8" Wire up support for DMA for the case where the vfio-user client does not provide mmap()-able file descriptors, but DMA requests must be performed via the VFIO-user protocol. This installs an indirect memory region, which already works for pci_dma_{read,write}, and pci_dma_map works thanks to the existing DMA bounce buffering support. Note that while simple scenarios work with this patch, there's a known race condition in libvfio-user that will mess up the communication channel. See https://github.com/nutanix/libvfio-user/issues/279 for details as well as a proposed fix. Signed-off-by: Mattias Nissler --- hw/remote/trace-events | 2 + hw/remote/vfio-user-obj.c | 84 +++++++++++++++++++++++++++++++++++---- 2 files changed, 79 insertions(+), 7 deletions(-) diff --git a/hw/remote/trace-events b/hw/remote/trace-events index 0d1b7d56a5..358a68fb34 100644 --- a/hw/remote/trace-events +++ b/hw/remote/trace-events @@ -9,6 +9,8 @@ vfu_cfg_read(uint32_t offset, uint32_t val) "vfu: cfg: 0x%x= -> 0x%x" vfu_cfg_write(uint32_t offset, uint32_t val) "vfu: cfg: 0x%x <- 0x%x" vfu_dma_register(uint64_t gpa, size_t len) "vfu: registering GPA 0x%"PRIx6= 4", %zu bytes" vfu_dma_unregister(uint64_t gpa) "vfu: unregistering GPA 0x%"PRIx64"" +vfu_dma_read(uint64_t gpa, size_t len) "vfu: DMA read 0x%"PRIx64", %zu byt= es" +vfu_dma_write(uint64_t gpa, size_t len) "vfu: DMA write 0x%"PRIx64", %zu b= ytes" vfu_bar_register(int i, uint64_t addr, uint64_t size) "vfu: BAR %d: addr 0= x%"PRIx64" size 0x%"PRIx64"" vfu_bar_rw_enter(const char *op, uint64_t addr) "vfu: %s request for BAR a= ddress 0x%"PRIx64"" vfu_bar_rw_exit(const char *op, uint64_t addr) "vfu: Finished %s of BAR ad= dress 0x%"PRIx64"" diff --git a/hw/remote/vfio-user-obj.c b/hw/remote/vfio-user-obj.c index 8b10c32a3c..6a561f7969 100644 --- a/hw/remote/vfio-user-obj.c +++ b/hw/remote/vfio-user-obj.c @@ -300,6 +300,63 @@ static ssize_t vfu_object_cfg_access(vfu_ctx_t *vfu_ct= x, char * const buf, return count; } =20 +static MemTxResult vfu_dma_read(void *opaque, hwaddr addr, uint64_t *val, + unsigned size, MemTxAttrs attrs) +{ + MemoryRegion *region =3D opaque; + vfu_ctx_t *vfu_ctx =3D VFU_OBJECT(region->owner)->vfu_ctx; + uint8_t buf[sizeof(uint64_t)]; + + trace_vfu_dma_read(region->addr + addr, size); + + g_autofree dma_sg_t *sg =3D g_malloc0(dma_sg_size()); + vfu_dma_addr_t vfu_addr =3D (vfu_dma_addr_t)(region->addr + addr); + if (vfu_addr_to_sgl(vfu_ctx, vfu_addr, size, sg, 1, PROT_READ) < 0 || + vfu_sgl_read(vfu_ctx, sg, 1, buf) !=3D 0) { + return MEMTX_ERROR; + } + + *val =3D ldn_he_p(buf, size); + + return MEMTX_OK; +} + +static MemTxResult vfu_dma_write(void *opaque, hwaddr addr, uint64_t val, + unsigned size, MemTxAttrs attrs) +{ + MemoryRegion *region =3D opaque; + vfu_ctx_t *vfu_ctx =3D VFU_OBJECT(region->owner)->vfu_ctx; + uint8_t buf[sizeof(uint64_t)]; + + trace_vfu_dma_write(region->addr + addr, size); + + stn_he_p(buf, size, val); + + g_autofree dma_sg_t *sg =3D g_malloc0(dma_sg_size()); + vfu_dma_addr_t vfu_addr =3D (vfu_dma_addr_t)(region->addr + addr); + if (vfu_addr_to_sgl(vfu_ctx, vfu_addr, size, sg, 1, PROT_WRITE) < 0 || + vfu_sgl_write(vfu_ctx, sg, 1, buf) !=3D 0) { + return MEMTX_ERROR; + } + + return MEMTX_OK; +} + +static const MemoryRegionOps vfu_dma_ops =3D { + .read_with_attrs =3D vfu_dma_read, + .write_with_attrs =3D vfu_dma_write, + .endianness =3D DEVICE_HOST_ENDIAN, + .valid =3D { + .min_access_size =3D 1, + .max_access_size =3D 8, + .unaligned =3D true, + }, + .impl =3D { + .min_access_size =3D 1, + .max_access_size =3D 8, + }, +}; + static void dma_register(vfu_ctx_t *vfu_ctx, vfu_dma_info_t *info) { VfuObject *o =3D vfu_get_private(vfu_ctx); @@ -308,17 +365,30 @@ static void dma_register(vfu_ctx_t *vfu_ctx, vfu_dma_= info_t *info) g_autofree char *name =3D NULL; struct iovec *iov =3D &info->iova; =20 - if (!info->vaddr) { - return; - } - name =3D g_strdup_printf("mem-%s-%"PRIx64"", o->device, - (uint64_t)info->vaddr); + (uint64_t)iov->iov_base); =20 subregion =3D g_new0(MemoryRegion, 1); =20 - memory_region_init_ram_ptr(subregion, NULL, name, - iov->iov_len, info->vaddr); + if (info->vaddr) { + memory_region_init_ram_ptr(subregion, OBJECT(o), name, + iov->iov_len, info->vaddr); + } else { + /* + * Note that I/O regions' MemoryRegionOps handle accesses of at mo= st 8 + * bytes at a time, and larger accesses are broken down. However, + * many/most DMA accesses are larger than 8 bytes and VFIO-user can + * handle large DMA accesses just fine, thus this size restriction + * unnecessarily hurts performance, in particular given that each + * access causes a round trip on the VFIO-user socket. + * + * TODO: Investigate how to plumb larger accesses through memory + * regions, possibly by amending MemoryRegionOps or by creating a = new + * memory region type. + */ + memory_region_init_io(subregion, OBJECT(o), &vfu_dma_ops, subregio= n, + name, iov->iov_len); + } =20 dma_as =3D pci_device_iommu_address_space(o->pci_dev); =20 --=20 2.34.1 From nobody Sat May 18 20:15:30 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=1695197235; cv=none; d=zohomail.com; s=zohoarc; b=ZCzksbjLtwwccPK4H4auYv+1zr5g51mrpAyw74K2DnDl8667APF0vI6uZRqX5ozNb5MmRXOBWBFpISGRSlujp/lIFZDEVG0zsh9aZ9YGETGstTszAm2vQpXflG49+C0OBZjPMlfd0Gx+8wg+4o0X6q1z770o+GRhTNiZNdmZLn4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695197235; h=Content-Transfer-Encoding:Cc: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=VRT2ifSiyxkep05tvhCKGJzMwggYcgdJ7ZMgq7QI+ZE=; b=Ugv7/3ZlMzNb+FNpEeDOl9laRVC6+9fZHR38CvDqlhtv6bJhpO2eT+tvHikZdIXe2+YjSUD1vXDfIwo+560iTkyTPhj+nRk2vaq4OfdPnoU3fMXG8DmzzK948DNOBcnkNy2M3kL/OFv+7Qxl8lQ1gtuN0liWgw9aWRXImJaF43g= 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 1695197235330227.36271121813763; Wed, 20 Sep 2023 01:07:15 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qisEU-0005ds-Uj; Wed, 20 Sep 2023 04:06:50 -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 1qisET-0005cb-B6 for qemu-devel@nongnu.org; Wed, 20 Sep 2023 04:06:49 -0400 Received: from mail-yw1-x1131.google.com ([2607:f8b0:4864:20::1131]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qisER-0002pz-O5 for qemu-devel@nongnu.org; Wed, 20 Sep 2023 04:06:49 -0400 Received: by mail-yw1-x1131.google.com with SMTP id 00721157ae682-59be6605e1dso70750977b3.3 for ; Wed, 20 Sep 2023 01:06:47 -0700 (PDT) Received: from mnissler.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id a16-20020aa78650000000b006862b2a6b0dsm2378596pfo.15.2023.09.20.01.06.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Sep 2023 01:06:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1695197206; x=1695802006; 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=VRT2ifSiyxkep05tvhCKGJzMwggYcgdJ7ZMgq7QI+ZE=; b=A4xvtCwH5BMQY7pH8aMC+Eb4ET2V+Eg7sqPSX1P+h9mqzuuLRY8jqVYRlri52eZW+Y zsn5NVjSgTSFgcJzaj2CI4lAWBSTjTOXbriJ0UYrvl3kNBwc6+elAkW+JItmT4AYnRES oXG4tbIdcC/o1VbbXZbJfBvthgFOPblG3fDadhD2uOyPRAju0H2uIk3CbyhXBkdMSpOy LEHsMNdBT5U936/kqE8GCn8snM8DjYToMXjyGY7I3SrYT/94ziDX4fnqs2TYScEFAsnC vyZ+4SktIDzZAb02xqAIi20WyH936A7lmaKNnledvfCD6aC5mYHAKh3LKuUbdtFG3SEv KSmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695197206; x=1695802006; 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=VRT2ifSiyxkep05tvhCKGJzMwggYcgdJ7ZMgq7QI+ZE=; b=saILFNKzEXVl4O1Jqc1IgLaVSm/nI45ZxMqSwz7vG0RgajXzDbK8toDviCxqT1JJSf gALW6wEmw29iCh+sZgIU5TN2ctEckyO3yzNbPkmtBQLjNptNhEX5JwFdQddpyMYz1KAh JAq4vppSYg0qDzU4qpu/VDqry5plK1HUnFyxdDA+ehHlGbipDjuSSngHjuBwub1RPqPN Nm/egqKA/rLqx9KTF48zGb/s9L4UZqHfriK7AJ6n5CKyN9u+TiomHgoOXZ5WrZzXS0n9 cADHgXABZEhwoeKx1zmkMmuj1sDd79cihaUFsVtemNoBOM5051wJbgL7etG1HLIpi4iK oOmw== X-Gm-Message-State: AOJu0Yybo2CrCGQtqSdfSXBeZUm9rh6sMD+T7rHU5/Gj6PgwW9LSmHKF 65eLI4EYJmk5RINSeGp95UDzd8TVxD9kwt/JbL6GUg== X-Google-Smtp-Source: AGHT+IEtHrF4WWw3x8TE9HM2uauRb5dCJD8hc5mRQtDDKDyd+Fbl7MTVsXOkJCR33P5zUBwUbuUHJA== X-Received: by 2002:a0d:cac8:0:b0:583:f02e:b92c with SMTP id m191-20020a0dcac8000000b00583f02eb92cmr1614019ywd.17.1695197206539; Wed, 20 Sep 2023 01:06:46 -0700 (PDT) From: Mattias Nissler To: qemu-devel@nongnu.org Cc: Elena Ufimtseva , David Hildenbrand , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , john.levon@nutanix.com, peterx@redhat.com, Marcel Apfelbaum , stefanha@redhat.com, Jagannathan Raman , Paolo Bonzini , "Michael S. Tsirkin" , Mattias Nissler Subject: [PATCH v5 5/5] vfio-user: Fix config space access byte order Date: Wed, 20 Sep 2023 01:06:22 -0700 Message-Id: <20230920080622.3600226-6-mnissler@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230920080622.3600226-1-mnissler@rivosinc.com> References: <20230920080622.3600226-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::1131; envelope-from=mnissler@rivosinc.com; helo=mail-yw1-x1131.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: 1695197237469100003 Content-Type: text/plain; charset="utf-8" PCI config space is little-endian, so on a big-endian host we need to perform byte swaps for values as they are passed to and received from the generic PCI config space access machinery. Signed-off-by: Mattias Nissler --- hw/remote/vfio-user-obj.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hw/remote/vfio-user-obj.c b/hw/remote/vfio-user-obj.c index 6a561f7969..6043a91b11 100644 --- a/hw/remote/vfio-user-obj.c +++ b/hw/remote/vfio-user-obj.c @@ -281,7 +281,7 @@ static ssize_t vfu_object_cfg_access(vfu_ctx_t *vfu_ctx= , char * const buf, while (bytes > 0) { len =3D (bytes > pci_access_width) ? pci_access_width : bytes; if (is_write) { - memcpy(&val, ptr, len); + val =3D ldn_le_p(ptr, len); pci_host_config_write_common(o->pci_dev, offset, pci_config_size(o->pci_dev), val, len); @@ -289,7 +289,7 @@ static ssize_t vfu_object_cfg_access(vfu_ctx_t *vfu_ctx= , char * const buf, } else { val =3D pci_host_config_read_common(o->pci_dev, offset, pci_config_size(o->pci_dev),= len); - memcpy(ptr, &val, len); + stn_le_p(ptr, len, val); trace_vfu_cfg_read(offset, val); } offset +=3D len; --=20 2.34.1