From nobody Sat May 18 14:01:45 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=1695139845; cv=none; d=zohomail.com; s=zohoarc; b=Wo7Z2bs6osGa2C1dEmsRjvTMSjUJsUNuOanfJdoLa8dCdlvkvkeQAUGCVS1/tHx+9+ZnVRnalhz27IqotMuo71cA7X9/0jNMWLLKPT+5mxrhjHGay3biKOinMtOVoVxlWvMYV6xRCu27C4Vjs1gUxaKSje9WObz9WdlyLfm6sCk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695139845; 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=j1FqEkxaSEL5MqdpoIkOol7amYA4OW0zeDlDVPlJ1D4=; b=JiHJy1FLFvnJZ1UHEdO/9NB/gJmSidkZEgqu7soDqpk7XwHDFxGK68r3py6hCNgU67WQ40OO5Odxn5+Jq1lubNGtBG6oRfVh/tr+4KfOQoF0stFtZimu/WyawhgTgVGASz+rAW7EtRUlujfpWS6GcWNjHch4WLRbpdMNosP/Pv0= 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 1695139845067822.5330061514236; Tue, 19 Sep 2023 09:10:45 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qidHX-0001q6-Mo; Tue, 19 Sep 2023 12:08:59 -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 1qidHL-0001nI-TP for qemu-devel@nongnu.org; Tue, 19 Sep 2023 12:08:50 -0400 Received: from mail-pl1-x631.google.com ([2607:f8b0:4864:20::631]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qidHH-0004gQ-B9 for qemu-devel@nongnu.org; Tue, 19 Sep 2023 12:08:46 -0400 Received: by mail-pl1-x631.google.com with SMTP id d9443c01a7336-1c39f2b4f5aso43101335ad.0 for ; Tue, 19 Sep 2023 09:08:42 -0700 (PDT) Received: from mnissler.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id l9-20020a170902d34900b001b8a1a25e6asm8086308plk.128.2023.09.19.09.08.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Sep 2023 09:08:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1695139722; x=1695744522; 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=j1FqEkxaSEL5MqdpoIkOol7amYA4OW0zeDlDVPlJ1D4=; b=Hbi9+1OOYOSRfXoQto+ObB0DEBgw+lpMwqk9ZhKgxMA5VxB3Hl8nTwlhcPD+7NHYN2 szwo3uqjU1mARm4KEQpmkFjQR+OQW1ijgTbu2ZKk6z2qiqqX3qlxpcO1uRWpOZJPXLuJ 1eil/YLd8v0HqXMug7ZjKZjbnbjtbIIByvsrGt21g6LUKIKTvx0eIoJyRjb2Xh2mgHV1 Ou1h5LewmrVYjSnYLmyhuj2B4kT5QFIRJ5pzi5LxbRM1Xj35y9c5YUe1J0EqejdDd0G4 oS308s3r1/PqpwHBGXkGLeXFw2j/wqB2fjnPsa3n7dUsF9Nc/6NsK30nYEuRwloqiUGU RsGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695139722; x=1695744522; 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=j1FqEkxaSEL5MqdpoIkOol7amYA4OW0zeDlDVPlJ1D4=; b=AmmoWf/ZHSwNJLVx0k2tpEv1fMN+hZPaNJW3sjJRDwyCWKhCxIMNrOi8Ety4KOynAg IPNly6oEGdN23S+C0b8zQUM3LGb+gNj+2++jMhJUAmb2w5oFpV9U4ScBvpgLOMB6w+ZT ziN/bKo7bvtI3PWMoLYDev0hOraVpbMbeHWl80wzrKz04CRSLBP1XKH/FZnWj6oQYptX KR/eVde/aZv3mRGbgQsx470bdgTTyc1jIQuChvFs9ZYhx7Ho2voShHswd2vByTwvgrfW 6O123ZCgt8i5Q5JOmqcQ62q3IWXh/w1NRghVjGoMqXXqRDIrXQ2ednWfj2SJx0eTaOie 7m9Q== X-Gm-Message-State: AOJu0YwB24H62ro6b+pPVOUm+Q4HceDo8o4QkXf7UyZinLgcE86jY/Or EGU36/U6sa4OhDWKqdirNPjqkHRwf9JlBgiLXMw= X-Google-Smtp-Source: AGHT+IHUB+xrEidOcoC1d//ybSqdubMC4y88z8ZiG++g8Zoebe/t3AiIl+WUC8rranXb/z1ZLQ5jHA== X-Received: by 2002:a17:903:1c8:b0:1c3:9544:cf63 with SMTP id e8-20020a17090301c800b001c39544cf63mr14665188plh.23.1695139721580; Tue, 19 Sep 2023 09:08:41 -0700 (PDT) From: Mattias Nissler To: qemu-devel@nongnu.org Cc: john.levon@nutanix.com, stefanha@redhat.com, Richard Henderson , Elena Ufimtseva , "Michael S. Tsirkin" , Paolo Bonzini , Jagannathan Raman , Peter Xu , David Hildenbrand , Marcel Apfelbaum , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Mattias Nissler Subject: [PATCH v4 1/5] softmmu: Per-AddressSpace bounce buffering Date: Tue, 19 Sep 2023 09:08:09 -0700 Message-Id: <20230919160814.3343727-2-mnissler@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230919160814.3343727-1-mnissler@rivosinc.com> References: <20230919160814.3343727-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::631; envelope-from=mnissler@rivosinc.com; helo=mail-pl1-x631.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: 1695139845405100001 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 Reviewed-by: Peter Xu --- include/exec/cpu-common.h | 2 - include/exec/memory.h | 45 ++++++++++++++++- softmmu/dma-helpers.c | 4 +- softmmu/memory.c | 7 +++ softmmu/physmem.c | 103 ++++++++++++++++---------------------- 5 files changed, 94 insertions(+), 67 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..f40cc564b8 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); - /* Write map_client_list before reading in_use. */ + QLIST_INSERT_HEAD(&as->map_client_list, client, link); + /* Write map_client_list before reading bounce_buffer_size. */ 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 14:01:45 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=1695139844; cv=none; d=zohomail.com; s=zohoarc; b=JAZpIxJFnl6VObDrcOfaYFM91NEzxEP16EdqxQKGaHuXV2+lnDISIedY0fv3mXY6JYz0xR3oPzVWJTTqz36WmFzAgMYEbTD/CyB/Rt+Zp2Y2M2eM9kKWQY96O2XDdng1lvLR4OhkxzU59kr3QOjWmne3BOA+trTSnMWgLtIx2FM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695139844; 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=bxwD+3/54X0vaMA3WFP5Xca7fbBneKsFm5XsokomaY4=; b=O1tE7t4g4bEerW580tE+voLS1KbbLtuQJ056dUkp0bPHnC7GKK3cXCL3hDdrqp9X2Y9Z1DnTG5UjhuCPXVYWnOhyoT89Xf1HbEpmr7ffLdBBriQJQXKdj6pQbbgnB5DWPwNVo6vS+NqjjfzLx+8oxlMaz6hlHfcSuMZJKg9EWeI= 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 1695139844625649.6454548901348; Tue, 19 Sep 2023 09:10:44 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qidHX-0001pd-1g; Tue, 19 Sep 2023 12:08:59 -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 1qidHM-0001nV-P0 for qemu-devel@nongnu.org; Tue, 19 Sep 2023 12:08:50 -0400 Received: from mail-pl1-x62a.google.com ([2607:f8b0:4864:20::62a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qidHJ-0004gf-06 for qemu-devel@nongnu.org; Tue, 19 Sep 2023 12:08:48 -0400 Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-1c44c7dbaf9so32761535ad.1 for ; Tue, 19 Sep 2023 09:08:44 -0700 (PDT) Received: from mnissler.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id l9-20020a170902d34900b001b8a1a25e6asm8086308plk.128.2023.09.19.09.08.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Sep 2023 09:08:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1695139723; x=1695744523; 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=bxwD+3/54X0vaMA3WFP5Xca7fbBneKsFm5XsokomaY4=; b=g+SCZFnQQiIdrmwliHj8RZ+D200Ji7fQWBbFCyS4dUAxSQYGQRIiIhnIQJKQ9LgDXW 6hTow5me9M7j0K06uaK/XKmOXu9tJtrw6uf0eqCXA5siXtxN5W2IlaV+tSJLBel0ZKRg /cJX7gyqf1kBBeXKkNiCTmKHBxaqwVXsLRiB3TaIbwgDI4FZpuDvPhL9yblnIrzmzitg 1ij0jARwrYQEF98YMgR360zgDj88IRZCB/a0kWFM3m4QR1IUeWyB72HXduWy3UWXQfZE NeccLz/Yvbkom3pZcj9kG64rQLh6GvPK+X+MKmHRaM+mh+rkkj1xmM9lABaujbUEDRds giPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695139723; x=1695744523; 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=bxwD+3/54X0vaMA3WFP5Xca7fbBneKsFm5XsokomaY4=; b=cD/BgymueqFW3EXBhrQDcg71s5i4r7NlGiZcR7zcawMk4eyp0i8aHWuLyTDrIZhynv VAmzLHMI1JZZ6GkbMSvyvyLIWoM+jF+4+j1+0Wzdv9km1Kpub+/RfP4xetCOjfWjhpKV Vtl1XMo4rsKgtvx0aXhFIKVCZlxSRjygdDMinfQESNKDgc/vURxRuk/7U6Bq1CWvEm7m tPNKqWTaiQswgeXOuFBHWZzbNuDzLEHGUERtsUOAWctb38Ne7PtCc5IECR1llt4PUuEj ThkhvEbp8xUsDLwGuuzFN2uQREsEikrnl4TgOhAKVPJdLF9D6VgZLdIli/6EXOWaYBs4 5eeA== X-Gm-Message-State: AOJu0YzpdEZgEbuiCgk2dJK/om5F/XUCMZ6NA3u+0VenoZyyaNQIHSBX Ul6pzC8p8cJ7kbXP7uECfTxKrR/+OPTvXVlGYyI48w== X-Google-Smtp-Source: AGHT+IFeBpdGS3dF1HVYpmIq6aUBxr0dpCBxt1fFgA7uRqmgYFr/3idPNcGHX35lDY6GEXwsD5NBkg== X-Received: by 2002:a17:902:728a:b0:1c0:6dcd:453d with SMTP id d10-20020a170902728a00b001c06dcd453dmr11608905pll.20.1695139722859; Tue, 19 Sep 2023 09:08:42 -0700 (PDT) From: Mattias Nissler To: qemu-devel@nongnu.org Cc: john.levon@nutanix.com, stefanha@redhat.com, Richard Henderson , Elena Ufimtseva , "Michael S. Tsirkin" , Paolo Bonzini , Jagannathan Raman , Peter Xu , David Hildenbrand , Marcel Apfelbaum , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Mattias Nissler Subject: [PATCH v4 2/5] softmmu: Support concurrent bounce buffers Date: Tue, 19 Sep 2023 09:08:10 -0700 Message-Id: <20230919160814.3343727-3-mnissler@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230919160814.3343727-1-mnissler@rivosinc.com> References: <20230919160814.3343727-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::62a; envelope-from=mnissler@rivosinc.com; helo=mail-pl1-x62a.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: 1695139846209100005 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 --- hw/pci/pci.c | 8 ++++ include/exec/memory.h | 14 +++--- include/hw/pci/pci_device.h | 3 ++ softmmu/memory.c | 5 ++- softmmu/physmem.c | 88 ++++++++++++++++++++++++------------- 5 files changed, 77 insertions(+), 41 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 f40cc564b8..12e374f249 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) { @@ -2953,7 +2967,7 @@ void address_space_register_map_client(AddressSpace *= as, QEMUBH *bh) QLIST_INSERT_HEAD(&as->map_client_list, client, link); /* 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,31 +3143,35 @@ 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; + + mr =3D memory_region_from_host(buffer, &addr1); + if (mr =3D=3D NULL) { + BounceBuffer *bounce =3D container_of(buffer, BounceBuffer, buffer= ); + assert(bounce->magic =3D=3D BOUNCE_BUFFER_MAGIC); =20 - mr =3D memory_region_from_host(buffer, &addr1); - assert(mr !=3D NULL); if (is_write) { - invalidate_and_set_dirty(mr, addr1, access_len); - } - if (xen_enabled()) { - xen_invalidate_map_cache_entry(buffer); + address_space_write(as, bounce->addr, MEMTXATTRS_UNSPECIFIED, + bounce->buffer, access_len); } - memory_region_unref(mr); + + memory_region_unref(bounce->mr); + qatomic_sub(&as->bounce_buffer_size, bounce->len); + /* Write bounce_buffer_size before reading map_client_list. */ + smp_mb(); + address_space_notify_map_clients(as); + bounce->magic =3D ~BOUNCE_BUFFER_MAGIC; + g_free(bounce); return; } + + if (xen_enabled()) { + xen_invalidate_map_cache_entry(buffer); + } 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_notify_map_clients(as); + invalidate_and_set_dirty(mr, addr1, access_len); + } } =20 void *cpu_physical_memory_map(hwaddr addr, --=20 2.34.1 From nobody Sat May 18 14:01:45 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=1695139763; cv=none; d=zohomail.com; s=zohoarc; b=hbpI6f+maIV9yB0MoKiTvW7epqBE3qHT7PCmgkySOVGQbEBMThFzVN4dMNEyIRHierM787zpHkJ2YiPhKwRTsHFGu61si5B8LblO/IFlL/TfU8rgwR15HaZR9WpIE8aJRgZ/N8yYsETppNwuSTDVZwKDLj1nJH2Vip1vdXaxzTk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695139763; 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=gBt8mYefhjzIU1KEv4SZcaUMlpGwHbvFeyc1ukZwv0nfAczsW/vgeaU4hPx8SWE01TFefA0n5kIk/Bq8zBJYMheEDf/HZTLyCDkKuYEpTVQK06RfOoTaxhAiUP5paBECrMFsaIdp4JaR+WXfQ7lvW4jKQKPkrIIsXXDnGSbqhvw= 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 1695139763246526.1506595249184; Tue, 19 Sep 2023 09:09:23 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qidHk-0001s0-4P; Tue, 19 Sep 2023 12:09: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 1qidHN-0001nW-B4 for qemu-devel@nongnu.org; Tue, 19 Sep 2023 12:08:50 -0400 Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qidHK-0004hF-Q2 for qemu-devel@nongnu.org; Tue, 19 Sep 2023 12:08:49 -0400 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-1c434c33ec0so32457415ad.3 for ; Tue, 19 Sep 2023 09:08:45 -0700 (PDT) Received: from mnissler.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id l9-20020a170902d34900b001b8a1a25e6asm8086308plk.128.2023.09.19.09.08.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Sep 2023 09:08:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1695139724; x=1695744524; 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=0XA0I1bRhoIX7gtgKYUjWyuntPcwloztTSEP54uJceyqy3GaixXcXtr/sMIPf2eiSY UpBUQc6x/1MnK2D6kp00o6vn5fh3p4xNi2Ls7HbErPp/RRJJCs82ovWj6jhpwr4XtMuQ RY0anzE0P/X4Z41m8WT2tMqX1mgNdZz0z1r5Xwgj+7VICXuvW0UcHMGW9NUBYPsmaOhz AZeDwTFHVaViXkyPUOpLEoqQEpgX2mXE4xbTpyCcnuGHJ7r9zgg48S1VPpuNq+qFfGN1 xGnfksarrkVvh+h+zqWZwk2PjY92R1rpwp1P6l8BLL17foIDn9ubg+6Wq/i2rEKrmV9q lQ/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695139724; x=1695744524; 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=CY0Shji6bK20GZsn+ea4hlrykWgyE9EgFnG0We7sf/VRuCgEmOLO6Vw84PWbcKTlnJ t16Sf+SkuZeBB751poTvsq7x70iAMX48c7KM2pmh1HXuo6Px7hiffUHpXz/1FWvBrVXr uN6yY4s6Q4SqPNtgdQzTffWu4jfDnk2Vn1obF2lmF8ljOFVi8FQBsWix7aTG9uBTBhuu 6j7tbTJf0g2wPHa/oQnKdLbVlKqKgJbtOicWnkGVUBl6yT5jlt4WlSF3Js6SdcXeYmKO EPydUoL5mcJusMeBYTw57g2qOplRcFzXZno0voXXsKRfegsLxO20ageqPpEw0gUbmFaB Fk0A== X-Gm-Message-State: AOJu0Yz3OvVvAGaOXJaPKVkSvfLMpTlpD5jmP+k8nk5Kb4zmXvC2YUfo WWLPrSIQs569ZfoJnQrsa/vlBsyUe2mzYsRFgK/cYA== X-Google-Smtp-Source: AGHT+IEMnigZRFyOpmGSUfIgP01c6q3pmUoxtl4Ll2aCY6jkL04hQoTUBuAOm7i5eIU7o48nq2u+rw== X-Received: by 2002:a17:902:9303:b0:1c3:a1a8:969a with SMTP id bc3-20020a170902930300b001c3a1a8969amr10622991plb.8.1695139724088; Tue, 19 Sep 2023 09:08:44 -0700 (PDT) From: Mattias Nissler To: qemu-devel@nongnu.org Cc: john.levon@nutanix.com, stefanha@redhat.com, Richard Henderson , Elena Ufimtseva , "Michael S. Tsirkin" , Paolo Bonzini , Jagannathan Raman , Peter Xu , David Hildenbrand , Marcel Apfelbaum , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Mattias Nissler Subject: [PATCH v4 3/5] Update subprojects/libvfio-user Date: Tue, 19 Sep 2023 09:08:11 -0700 Message-Id: <20230919160814.3343727-4-mnissler@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230919160814.3343727-1-mnissler@rivosinc.com> References: <20230919160814.3343727-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::62e; envelope-from=mnissler@rivosinc.com; helo=mail-pl1-x62e.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: 1695139763725100011 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 14:01:45 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=1695139758; cv=none; d=zohomail.com; s=zohoarc; b=Po56gvQdKhTQUejmS/7QIu/g5OugX2FD0sLI/0f+86qFTadyZ9V5S3nWmxgs+pbn55ycm7sXS2s1cX3yuWivWC3awAPOlpT4PJZG1Pgyj2KGYfD4TpBSCKouTndvlPKKUIV9VOCvNV4Cb5r1Fx2mlJC2Z8N0Z16gxvHEgWWz6EI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695139758; 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=ByFRKjanDlEhXqBL13ZauX8Tmtr+CN/OT0yVGfBa1fesCmlqFxBDaEpFUYjINVej/vQCwJymf+OaBA2gQ55XABzlUKIBJcGDert9p0X4eoIV20h5AGebu5UeDcgM4AtSSLu7gyloRSs1nAyUmSRvgpCKHOyhu0FJgOtww9zIe9s= 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 1695139758276819.1063193152752; Tue, 19 Sep 2023 09:09:18 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qidHS-0001p9-G9; Tue, 19 Sep 2023 12:08:55 -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 1qidHO-0001nZ-NE for qemu-devel@nongnu.org; Tue, 19 Sep 2023 12:08:51 -0400 Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qidHL-0004hP-Jf for qemu-devel@nongnu.org; Tue, 19 Sep 2023 12:08:50 -0400 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-1c465d59719so21834185ad.1 for ; Tue, 19 Sep 2023 09:08:46 -0700 (PDT) Received: from mnissler.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id l9-20020a170902d34900b001b8a1a25e6asm8086308plk.128.2023.09.19.09.08.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Sep 2023 09:08:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1695139725; x=1695744525; 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=Nvhvl5DxJ2C1v89pyOXBfNuHCR4jPKf/nF5QD3glfdrLOuvh1DRAqIZuI0V9b+6xXi ZN1ghioXmxj6RQSOfOkaatHdE6qJC4PajnTpf/ABrt3I0Wi6aaRe0Aa0ayKJ9Ls6gJdl p4MF2YZmx6hPnzXemowpA99uGaXQAX1cnF2RsDMLCvKtN/Dl9cffbXc/9UkhBdzE+VwO PKGED4HZQOtW2/qevGaU42+EXVijoCOsmU9mGpKN80cs4/sHcip+8+DAm4PhaO5U2XDx +CRs76AS50cIck5tViVoyRko+OI4lsOrQRHUwGcDidzLchYOuOPt93/ZMeg4PafC2ou5 kSxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695139725; x=1695744525; 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=JrnU3lJq0xknwUCLpZHb2yBA44LuvxPedkxM052ApVHDRNpqAYafxFs8Pxu2JEU/CI XrXRluBJ6GG0n1TAsD48uLxIInMfDFrFAWPijYA6DosJC3w38rGmFOKRP6Q0AtI2D2UI kqXkFzIFEaNNu++6dJnCI6WOm8Fokt55wYeqLZDa7zO5wf73qCuOBNwWbiJnPvKvfTg5 oSTt+eSwyhuz1tfU4hMw02jXakUC7PoGii3ebcNsyUvUtZzcowcdd/+qYgOYYIhF1EMi rH5Gu+6+K2XS563u2LsvI7HFv3uzCbaHgg/ZfE+hVvKrESsq6BEvNGXa5uEsPTGnGzeQ 1N1w== X-Gm-Message-State: AOJu0Yz9HD0noZmSpa8DqgKhr7E69XCnYPbIM2GONe4u4dO+7CrqfLAD x40HdCddmq6QM3GbtIJ14RGs3P4keoMmHLcxBfy7pw== X-Google-Smtp-Source: AGHT+IH27onfQTimMcZhgbWsmAY7m5c+Q+aO0KkgFv5wtFdzA2+COZ0kwhVkjWaZ0LLufYYwubhYjA== X-Received: by 2002:a17:903:48e:b0:1c4:42d4:b477 with SMTP id jj14-20020a170903048e00b001c442d4b477mr9471297plb.9.1695139725336; Tue, 19 Sep 2023 09:08:45 -0700 (PDT) From: Mattias Nissler To: qemu-devel@nongnu.org Cc: john.levon@nutanix.com, stefanha@redhat.com, Richard Henderson , Elena Ufimtseva , "Michael S. Tsirkin" , Paolo Bonzini , Jagannathan Raman , Peter Xu , David Hildenbrand , Marcel Apfelbaum , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Mattias Nissler Subject: [PATCH v4 4/5] vfio-user: Message-based DMA support Date: Tue, 19 Sep 2023 09:08:12 -0700 Message-Id: <20230919160814.3343727-5-mnissler@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230919160814.3343727-1-mnissler@rivosinc.com> References: <20230919160814.3343727-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::62e; envelope-from=mnissler@rivosinc.com; helo=mail-pl1-x62e.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: 1695139759462100001 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 14:01:45 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=1695139759; cv=none; d=zohomail.com; s=zohoarc; b=iiYzuYvRNHe6Cd/espQuph2Uf9FLgQ6EM2NBz4opKJRdFO0pybScvKbF5MdIOxaTpgcPdE2yVSwCglFgqzEPXVU21YnVecG6ksdDy8F4OD4uYxZA75im5U9nqkDSPvvmSNB0bRa1pwInEq69sMGjbGK3dJXU6gaSzidTZ7vQFoI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695139759; 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=FZiyfKBpHJJ86kW7Vpg4mX2csi65dSz4S8RG3seuhXi41NXOnuphLYlcIlmWs5KEhULMW5AJVm41AGoX8wXCiHG8X0B/GiZXyS4BN7AaJ5xRN01ER5VKdDuV77hQUPPeX4G/S/3R6OgKXF1/YaZf3o+Q1ohtUy1qLtg9/PnUsvU= 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 1695139759430445.2848685352907; Tue, 19 Sep 2023 09:09:19 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qidHR-0001p6-QK; Tue, 19 Sep 2023 12:08:54 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qidHO-0001nY-My for qemu-devel@nongnu.org; Tue, 19 Sep 2023 12:08:51 -0400 Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qidHM-0004hf-V9 for qemu-devel@nongnu.org; Tue, 19 Sep 2023 12:08:50 -0400 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-1c1e3a4a06fso46170715ad.3 for ; Tue, 19 Sep 2023 09:08:48 -0700 (PDT) Received: from mnissler.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id l9-20020a170902d34900b001b8a1a25e6asm8086308plk.128.2023.09.19.09.08.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Sep 2023 09:08:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1695139726; x=1695744526; 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=PF7sQjctIrynWSKWCdfg4m+CvXMQNf356/WYCqEPgqrtQcnqdrIWe/oPQRuqwOmxZ8 aB83BuxULfrSWlKE+ZuE5t8RUOz12aH1ZEi1J0AK1bx/SadQf5BlGAgu+yxU3CHK8JZU bGkC8VKGT9V+qb1QfmUWr6iBLEKbADXkAYz+LUIXOUv2WDHscbcwumeQJw7Dt/KN4aHT HomNMGMqSQiKjXo99VaXM8IIK5cnVSGI+cNN+0v1lsokOBi+ksbeu5MGEd6d/Ft0qYyo fhUNZAw9WWHaIR4KM5XTUTNHy44jzVPajavfdFcC4CQ2mYTCxU0wzXRw7TzLnqf0aF4p tHUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695139726; x=1695744526; 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=qsfAzsmrudlDT5FF6+HjytEsGIlttxowqsDFM+77ukFV11qtx+wRTpaU1hTthl8qOQ JK0mXHPk3kbk2+kcxxisFlZ6GZ2cRUZnILQGDfJ/MzrudiiJJucFyLLDwC4HSu6ENgK/ V+2ymttFEa+6b46HRXPrygZEw4LqIjzGsj9Oj4wH8J2hBTeCFbQpR+a6kmuBGXLN8RRe t2vUHaOJhSWReOGcs1YRvz5m9OuctpR1Wt7v7UKyVyAfediY+sMrQq9B80doT+rWFOVF V2Y8PMxlafnCoq31GyIEI6xf/rbMg37dIQlBbuwVlPpNibZYH3vrf9dDKL0GqiyF+K0g 1f5g== X-Gm-Message-State: AOJu0YxJZI04Gnsa/CJXa0prB44vuJRdomG9fVdh6GxTezXcxZuIl1pi E6MmP11kHCgpzX0SHo0kp2uosTH0+K/HMeIY24xZGg== X-Google-Smtp-Source: AGHT+IHYawBtyKq1aclH6J9xdKjkxTX+VN46y8E93SDILIVSj6fwGO3pG9CSG0Lu6qZc/yaEgM6YjA== X-Received: by 2002:a17:902:ea09:b0:1c3:29c4:c4e4 with SMTP id s9-20020a170902ea0900b001c329c4c4e4mr13423483plg.36.1695139726555; Tue, 19 Sep 2023 09:08:46 -0700 (PDT) From: Mattias Nissler To: qemu-devel@nongnu.org Cc: john.levon@nutanix.com, stefanha@redhat.com, Richard Henderson , Elena Ufimtseva , "Michael S. Tsirkin" , Paolo Bonzini , Jagannathan Raman , Peter Xu , David Hildenbrand , Marcel Apfelbaum , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Mattias Nissler Subject: [PATCH v4 5/5] vfio-user: Fix config space access byte order Date: Tue, 19 Sep 2023 09:08:13 -0700 Message-Id: <20230919160814.3343727-6-mnissler@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230919160814.3343727-1-mnissler@rivosinc.com> References: <20230919160814.3343727-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::62e; envelope-from=mnissler@rivosinc.com; helo=mail-pl1-x62e.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: 1695139761530100005 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