Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8F62ACD98D2 for ; Sun, 14 Jun 2026 10:05:07 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wYhi3-0000tV-RU; Sun, 14 Jun 2026 06:04:55 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wYhi2-0000sY-Ni for qemu-arm@nongnu.org; Sun, 14 Jun 2026 06:04:54 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wYhi0-0003d3-N4 for qemu-arm@nongnu.org; Sun, 14 Jun 2026 06:04:54 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1781431490; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/4R8G+o9zCfTrKQp5PbdL8L6nzHQZMz3WByySAYZdbg=; b=iCmzYcLNSWFPtj9ubNF1xgybDOp8GIAMhXkFB1Dfh1h9gvz+bwtwNMs5xO+PThAj5o7pvD RqFjQCbE8Oa180nPk3dN6wCgAROxxMHdB6mfmTbffHcaAQkWL7Oz+cNZ6uYZtUrI2dDvZL hymeRfEgzot/zQ0IyYyv2qcoKxWW87o= Received: from mail-dy1-f197.google.com (mail-dy1-f197.google.com [74.125.82.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-10-ULV_GGSgMyGNKG59SuPzaw-1; Sun, 14 Jun 2026 06:04:49 -0400 X-MC-Unique: ULV_GGSgMyGNKG59SuPzaw-1 X-Mimecast-MFC-AGG-ID: ULV_GGSgMyGNKG59SuPzaw_1781431488 Received: by mail-dy1-f197.google.com with SMTP id 5a478bee46e88-304d0d0b28eso2076542eec.0 for ; Sun, 14 Jun 2026 03:04:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781431488; x=1782036288; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=/4R8G+o9zCfTrKQp5PbdL8L6nzHQZMz3WByySAYZdbg=; b=CUvoTZS/KKnmxJbqcu1u1BBvNx1o//3qcVvy72lGqJ+NghJKW7avRELBJQrb3oMFSl OyKHD4dNUE+kVlCJIC+JFhQbwCOPWKQy3D++NZzVRngK2JXca/KOBgAKoUZVqooI2EgH gEMNFyNyxmT+ruI/5xaeWPR4thf/ITt3Ok239VFdrgRMKFSgHYbsxuhVwBLZdV46eiUh msVNfNd2UEy/XHiy1nh+BaUWOkzmh0emgslz9nSKF12vqAcnqLdA70pd9B5y446XYCMU uRWqmHQmrW7OL/JtdP5N6EWR3urZVe65s80CWl+JSS3Lk6j+wIRYQ+F2n3e/1RtRQh+Q YEZQ== X-Gm-Message-State: AOJu0YzuDeIXjXbGz3HCMqiBMAFyF8sfG71Cn658Zpdx4LcyS4Z2mDwU Ng9crki+UissHu+9c4eqQhCb66RcCFWuOUUoXwnGm0/MMmZZZJX+NO4CWlStUklB6PQ5WO4g+bd GcRjULdbGUuKQ6wtvYVZ18nC4DRD80zME5WexBiy+W+4JbrkP1PlCpA== X-Gm-Gg: Acq92OGMawgqv6ZRUm6gjVgx02QrBkfOuLQFUZtAaWY72tqO854qY0FMU9rqDpXbAfv NyOpwzTZa/zzieNniZGpfzwTA8h7+duQ82nW9KbkB9uN8jGRfkB1Ib79kZEMaKwJu/6V5+xHahS Y3p1qWSqU9x64xYwqBfNLZGvA+9cnioVbCx3ghC76RB4udAinmJLmVU6gvPRnh65q2/0OV8zd4M mwgyuidOqnTqNafp6+hh9AAyokrtnqHUXbV2/wa8vfnQJ1jeRp4QDIuPPDTRhP2ZVzA/pID1xxJ K7ir5pBxUwbe3yD4k+iwgest54mUdkJZ6VR79Bo0zYuYSB0/vfe2c0imGThdMuTRQ3Crbx8XQgN OBZOSYEceSD8s8XErdmRTbptuD6Hc3zy+wO1Nxdb8ffWy/ZdPRHMT+bq31Nc9w/Gk X-Received: by 2002:a05:7300:818c:b0:2f1:6252:f8fe with SMTP id 5a478bee46e88-3093816c7cemr3586658eec.3.1781431487764; Sun, 14 Jun 2026 03:04:47 -0700 (PDT) X-Received: by 2002:a05:7300:818c:b0:2f1:6252:f8fe with SMTP id 5a478bee46e88-3093816c7cemr3586649eec.3.1781431487071; Sun, 14 Jun 2026 03:04:47 -0700 (PDT) Received: from [192.168.68.51] (n175-34-8-244.mrk21.qld.optusnet.com.au. [175.34.8.244]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-3081e489536sm10721263eec.2.2026.06.14.03.04.41 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 14 Jun 2026 03:04:46 -0700 (PDT) Message-ID: Date: Sun, 14 Jun 2026 20:04:38 +1000 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 1/2] system/memory: Use __builtin_mem{cpy, move} in accessors of ram device region To: "Michael S. Tsirkin" Cc: qemu-arm@nongnu.org, qemu-devel@nongnu.org, peterx@redhat.com, peter.maydell@linaro.org, berrange@redhat.com, david@kernel.org, alex@shazbot.org, clg@redhat.com, pbonzini@redhat.com, philmd@mailo.com, phrdina@redhat.com, jugraham@redhat.com, shan.gavin@gmail.com References: <20260612110307.1264798-1-gshan@redhat.com> <20260612110307.1264798-2-gshan@redhat.com> <20260612072144-mutt-send-email-mst@kernel.org> From: Gavin Shan In-Reply-To: <20260612072144-mutt-send-email-mst@kernel.org> X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: DBdbuE8c26cMctONvikgWX_tXwW-44fba_PvjixD3Yk_1781431488 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Received-SPF: pass client-ip=170.10.133.124; envelope-from=gshan@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -24 X-Spam_score: -2.5 X-Spam_bar: -- X-Spam_report: (-2.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.445, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-arm@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-arm-bounces+qemu-arm=archiver.kernel.org@nongnu.org Sender: qemu-arm-bounces+qemu-arm=archiver.kernel.org@nongnu.org On 6/12/26 9:22 PM, Michael S. Tsirkin wrote: > On Fri, Jun 12, 2026 at 09:03:06PM +1000, Gavin Shan wrote: >> All ram device regions was turned to be indirectly accessible by commit >> 4a2e242bbb ("memory: Don't use memcpy for ram_device regions"). This leads >> to guest hang on compiling 'cuda-samples' as reported by Julia. The guest >> is started by the following command lines, with a GH100 GPU card. >> >> host$ lspci | grep GH100 >> 0009:01:00.0 3D controller: NVIDIA Corporation GH100 [GH200 120GB / 480GB] (rev a1) >> host$ /home/sandbox/gavin/qemu.main/build/qemu-system-aarch64 \ >> -machine virt,gic-version=host,ras=on,highmem-mmio-size=4T \ >> -accel kvm -cpu host -smp cpus=48 -m size=8G \ >> -drive file=/home/gavin/sandbox/images/disk.qcow2,if=none,id=d0 \ >> -device virtio-blk-pci,id=vb0,bus=pcie.0,drive=d0,num-queues=4 \ >> -device vfio-pci-nohotplug,host=0009:01:00.0,bus=pcie.1.0 >> : >> guest$ cd cuda-samples/build >> guest$ make -j 20 clean >> guest$ make -j 20 >> : >> [ 54%] Linking CUDA executable graphMemoryNodes >> [ 54%] Built target graphMemoryNodes >> >> >> guest$ qemu-system-aarch64: virtio: bogus descriptor or out of resources >> [ 555.814025] virtio_blk virtio0: [vda] new size: 268435456 512-byte logical blocks (137 GB/128 GiB) >> >> When the GPU's driver (NVidia open driver) is loaded on guest bootup, >> the memory blocks residing in the PCI BAR#4 can be presented to the >> guest through memory hot-add. The page cache can be allocated from the >> hot added memory blocks when cuda-samples is being compiled. Afterwards, >> the page cache is sent to QEMU's virtio-blk device as part of the DMA >> request, the bounce buffer has to be used to accomodate the request as >> the corresponding memory region (MemoryRegion) is a RAM DEVICE region >> and indirectly accessible in qemu. However, the max bounce bufer size >> is only 4096 bytes by default. We're running out of that space quickly. >> >> QEMU >> ==== >> virtio_blk_handle_output >> virtio_blk_handle_vq >> virtio_blk_get_request >> virtqueue_pop >> virtqueue_split_pop >> virtqueue_map_desc >> address_space_map >> memory_access_is_direct # Return false >> memory_region_supports_direct_access >> >> (qemu) info mtree >> memory-region: pci_bridge_pci >> 0000000000000000-ffffffffffffffff (prio 0, container): pci_bridge_pci >> 0000042000000000-0000043fffffffff (prio 1, i/o): 0009:01:00.0 base BAR 4 >> 0000042000000000-0000043fffffffff (prio 0, i/o): 0009:01:00.0 BAR 4 >> 0000042000000000-000004379fffffff (prio 0, ramd): 0009:01:00.0 BAR 4 mmaps[0] >> >> This replaces mem{cpy, move} with __builtin_mem{cpy, move} in the memory >> accessors to ram device memory region, preparatory work to make ram device >> region directly accessible and bypass the bounce buffer in the DMA path >> in next patch. >> >> Reported-by: Julia Graham >> Suggested-by: Michael S. Tsirkin >> Suggested-by: Peter Xu >> Signed-off-by: Gavin Shan >> --- >> hw/remote/vfio-user-obj.c | 4 ++-- >> include/system/memory.h | 42 ++++++++++++++++++++++++++++++++++++++- >> system/physmem.c | 8 ++++---- >> 3 files changed, 47 insertions(+), 7 deletions(-) >> >> diff --git a/hw/remote/vfio-user-obj.c b/hw/remote/vfio-user-obj.c >> index 87fa7b6572..fe6f661fe2 100644 >> --- a/hw/remote/vfio-user-obj.c >> +++ b/hw/remote/vfio-user-obj.c >> @@ -375,9 +375,9 @@ static int vfu_object_mr_rw(MemoryRegion *mr, uint8_t *buf, hwaddr offset, >> ram_ptr = memory_region_get_ram_ptr(mr); >> >> if (is_write) { >> - memcpy((ram_ptr + offset), buf, size); >> + address_space_memcpy(ram_ptr + offset, buf, size); >> } else { >> - memcpy(buf, (ram_ptr + offset), size); >> + address_space_memcpy(buf, ram_ptr + offset, size); >> } >> >> return 0; >> diff --git a/include/system/memory.h b/include/system/memory.h >> index 1417132f6d..6bb2e13eea 100644 >> --- a/include/system/memory.h >> +++ b/include/system/memory.h >> @@ -2938,6 +2938,46 @@ static inline bool memory_access_is_direct(const MemoryRegion *mr, >> return true; >> } >> >> +static inline void address_space_memcpy(void *dest, const void *src, size_t n) >> +{ >> + switch (n) { >> + case 1: >> + __builtin_memcpy(dest, src, 1); >> + break; >> + case 2: >> + __builtin_memcpy(dest, src, 2); >> + break; >> + case 4: >> + __builtin_memcpy(dest, src, 4); >> + break; >> + case 8: >> + __builtin_memcpy(dest, src, 8); >> + break; >> + default: >> + __builtin_memcpy(dest, src, n); >> + } >> +} >> + >> +static inline void address_space_memmove(void *dest, const void *src, size_t n) >> +{ >> + switch (n) { >> + case 1: >> + __builtin_memmove(dest, src, 1); >> + break; >> + case 2: >> + __builtin_memmove(dest, src, 2); >> + break; >> + case 4: >> + __builtin_memmove(dest, src, 4); >> + break; >> + case 8: >> + __builtin_memmove(dest, src, 8); >> + break; >> + default: >> + __builtin_memmove(dest, src, n); >> + } >> +} >> + >> /** >> * address_space_read: read from an address space. >> * > > > The variable length probably should use the regular memcpy/memmove - > no reason to bypass fortification for these. > Fixed locally and the changes will be included in next revision. > >> @@ -2970,7 +3010,7 @@ MemTxResult address_space_read(AddressSpace *as, hwaddr addr, >> mr = flatview_translate(fv, addr, &addr1, &l, false, attrs); >> if (len == l && memory_access_is_direct(mr, false, attrs)) { >> ptr = qemu_map_ram_ptr(mr->ram_block, addr1); >> - memcpy(buf, ptr, len); >> + __builtin_memcpy(buf, ptr, len); >> } else { >> result = flatview_read_continue(fv, addr, attrs, buf, len, >> addr1, l, mr); >> diff --git a/system/physmem.c b/system/physmem.c >> index 7bcbf87573..5f46a9d676 100644 >> --- a/system/physmem.c >> +++ b/system/physmem.c >> @@ -3272,7 +3272,7 @@ static MemTxResult flatview_write_continue_step(MemTxAttrs attrs, >> uint8_t *ram_ptr = qemu_ram_ptr_length(mr->ram_block, mr_addr, l, >> false, true); >> >> - memmove(ram_ptr, buf, *l); >> + address_space_memmove(ram_ptr, buf, *l); >> invalidate_and_set_dirty(mr, mr_addr, *l); >> >> return MEMTX_OK; >> @@ -3365,7 +3365,7 @@ static MemTxResult flatview_read_continue_step(MemTxAttrs attrs, uint8_t *buf, >> uint8_t *ram_ptr = qemu_ram_ptr_length(mr->ram_block, mr_addr, l, >> false, false); >> >> - memcpy(buf, ram_ptr, *l); >> + address_space_memcpy(buf, ram_ptr, *l); >> >> return MEMTX_OK; >> } >> @@ -3503,8 +3503,8 @@ MemTxResult address_space_write_rom(AddressSpace *as, hwaddr addr, >> l = memory_access_size(mr, l, addr1); >> } else { >> /* ROM/RAM case */ >> - void *ram_ptr = qemu_map_ram_ptr(mr->ram_block, addr1); >> - memcpy(ram_ptr, buf, l); >> + address_space_memcpy(qemu_map_ram_ptr(mr->ram_block, addr1), >> + buf, l); >> invalidate_and_set_dirty(mr, addr1, l); >> } >> len -= l; >> -- >> 2.54.0 > Thanks, Gavin