From mboxrd@z Thu Jan 1 00:00:00 1970 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 E2B20CD98C5 for ; Sun, 14 Jun 2026 15:42:35 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wYmyT-0001I0-OD; Sun, 14 Jun 2026 11:42:13 -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 1wYmyS-0001HT-1v for qemu-arm@nongnu.org; Sun, 14 Jun 2026 11:42:12 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wYmyQ-0005BT-5P for qemu-arm@nongnu.org; Sun, 14 Jun 2026 11:42:11 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1781451728; 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=uHth61AMnV8KAZ6R6fy19N4iioXaFMs9iX00iwFTPy8=; b=GOCF9e6aiRwB8whOhTNopQi0U/L6NtIoQIKthC7sAWyBBoupUpPBlaixbLuHvZMbNgfEF2 +2Bel66l9ScQu0392qxa5hXFhQuOV/lB204O/3Gq83GZHafcmFtOEx8IP1oS7vu+Z//4wX oxYOzX93RRxzQFoiuvBX+WgdK9HOjuY= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-479-cfpFJvyDNdO0cMJC7K6mtQ-1; Sun, 14 Jun 2026 11:42:07 -0400 X-MC-Unique: cfpFJvyDNdO0cMJC7K6mtQ-1 X-Mimecast-MFC-AGG-ID: cfpFJvyDNdO0cMJC7K6mtQ_1781451726 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-45ef5e38a18so1131390f8f.1 for ; Sun, 14 Jun 2026 08:42:06 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781451726; x=1782056526; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=uHth61AMnV8KAZ6R6fy19N4iioXaFMs9iX00iwFTPy8=; b=tCxaI4WQJb8CiTf/wDQlaWAdP4qqv8gDaash5XZr33UwoON1FUtb36DTA4l4cl7l9d MXnfJe6mQiWXafkLEXNDIytoXx4PXxCiNKNTiRuvz064ldh+lguhDUiOgrxAjHkbHOIl YRVH0Kp62z0iJpG4nB8klXrul3YOrTXYa+COb2neTrpEYTQ/VXyF13cUoZ9SVAy3VC9T 8o3kK4tkZkQTw8Nw0InaNItR1/C1up0ZGhboWHJXi1LNXerEnDwhJAJVA/THtKDNDTZO GSyXrnm8CkNRS9pYd+av40q0Ls+frQ7lcKthHf+KBpQ6BAghhLX2DRH0OLcs/YCAGT4Q 5vGA== X-Forwarded-Encrypted: i=1; AFNElJ/anm3dzMUIwnwu3faI1dVt2E4lqxtnZnWUa+8tIRFe9RbDEm+8f2HoPR/sivf13Z8fGP/VnpyLhw==@nongnu.org X-Gm-Message-State: AOJu0YwtwXzZNvPGFPJ0JxkD2r8FZo87HPS9vlXdmUc21lDxFraw+ZoS HboivHYAnEtDpP+2sAWFUGoI0E1oJ1MUuhOFthYTXWU3LRJlME2ySGH16RGyxMDpLA3Tmqh/rEC /S6SsrPmg97q6LajXpMOpVer3RnX28sLudQYnR9b9sNrq0AL8IIt0og== X-Gm-Gg: Acq92OGGkAbIVjyOxz43OBWfQR+MdjBILYsWnq0DQeTuQytzREr+nkfLOip59H20HMn 3CecRNCFzXZCUWFT/ci4RAbQ20KDbtmgMBp0M8OEj4en0vrS1VEOGRxi5UbIwtsGPLGUpUMzaFH ru4LnmSu0dA8/uEDK6kDRC2G6bAsaGLyLRZtwbrV/aeVYphbzcB9c0vvjDP3n9qX6vspbfJhg8K 3I99p4TWjEGhbUZofiFNVIqAah1TRvdljb53CIHuAK0EfdqNUZOQ+8mDxjHCvW6BzKiC+RFr/TL LC39XaoeQmmuAPBtjfk0A/2yhT8p2xWkQj1a+IwA7E1gUKTzXzevMZQhwZzG8EFSVacfR7RpYYu kYC6BjMSrf13XGfMHKSrZGwAzxBAzT/P5Mvh7Z0sWxO4= X-Received: by 2002:a05:6000:2082:b0:45e:8526:7dc8 with SMTP id ffacd0b85a97d-46076b18ccbmr8845885f8f.25.1781451725968; Sun, 14 Jun 2026 08:42:05 -0700 (PDT) X-Received: by 2002:a05:6000:2082:b0:45e:8526:7dc8 with SMTP id ffacd0b85a97d-46076b18ccbmr8845841f8f.25.1781451725426; Sun, 14 Jun 2026 08:42:05 -0700 (PDT) Received: from redhat.com (IGLD-80-230-85-71.inter.net.il. [80.230.85.71]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4606f2c3782sm22631620f8f.25.2026.06.14.08.42.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Jun 2026 08:42:04 -0700 (PDT) Date: Sun, 14 Jun 2026 11:42:01 -0400 From: "Michael S. Tsirkin" To: Richard Henderson Cc: Gavin Shan , 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 Subject: Re: [PATCH 1/2] system/memory: Use __builtin_mem{cpy, move} in accessors of ram device region Message-ID: <20260614112224-mutt-send-email-mst@kernel.org> References: <20260612110307.1264798-1-gshan@redhat.com> <20260612110307.1264798-2-gshan@redhat.com> <223f5f94-b9b7-46b0-8466-5be655203a0e@linaro.org> MIME-Version: 1.0 In-Reply-To: <223f5f94-b9b7-46b0-8466-5be655203a0e@linaro.org> X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: _R2SaFm-FQ9UBtaShb-NtifePrkGlxTp6re0XMVuj30_1781451726 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=170.10.129.124; envelope-from=mst@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_H3=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 Fri, Jun 12, 2026 at 08:29:34AM -0700, Richard Henderson wrote: > On 6/12/26 04:03, 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. > > memcpy/memmove *always* compile to __builtin_memcpy/memmove, and the > compiler later decides whether or not to expand inline. Did some research. The issue that prompted use of __builtin_memcpy in bswap.h is musl's fortify headers that Alpine Linux was using back in 2019. Try this: git clone --depth=1 https://github.com/jvoisin/fortify-headers /tmp/fortify-headers git -C /tmp/fortify-headers checkout 5aabc7e~1 now: musl-gcc -O2 -D_FORTIFY_SOURCE=2 -isystem /tmp/fortify-headers/include -S -o- test.c where test.c is: #include #include void test_memcpy(void *dst, const void *src) { memcpy(dst, src, 4); } void test_builtin_memcpy(void *dst, const void *src) { __builtin_memcpy(dst, src, 4); } On x86:  test_memcpy: .LFB13: .cfi_startproc cmpq %rsi, %rdi jnb .L2 leaq 4(%rdi), %rax cmpq %rax, %rsi jb .L6 .L4: movl $4, %edx jmp memcpy .p2align 4,,10 .p2align 3 .L2: cmpq %rdi, %rsi jnb .L4 leaq 4(%rsi), %rax cmpq %rax, %rdi jb .L3 movl $4, %edx jmp memcpy .L6: jmp .L3 .cfi_endproc .section .text.unlikely .cfi_startproc .type test_memcpy.cold, @function test_memcpy.cold: .LFSB13: .L3: ud2 .cfi_endproc .LFE13: .text .size test_memcpy, .-test_memcpy A jmp so a function call. While builtin: test_builtin_memcpy: .LFB14: .cfi_startproc movl (%rsi), %eax movl %eax, (%rdi) ret .cfi_endproc .LFE14: .size test_builtin_memcpy, .-test_builtin_memcpy So it's a fortify headers Version 1.0 bug. Version 1.1 has the fix. I guess we should rewrite bswap.h then. > So, this doesn't do what you think it does. > My real question is: what are you attempting to achieve? > > (1) is the problem unaligned access to a mapped physical device? > (2) is the problem vector access to a mapped physical device? > (3) something else? > > > r~