Received: by 2002:ab2:1c04:0:b0:1f7:53ba:1ebe with SMTP id f4csp75062lqg; Fri, 26 Apr 2024 09:07:48 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVouhe1zbsfOAHFUVlrSHGjpxzHGVCz3mwZB35IpNUZczIoSSIUPpX0X46FeQ9xZewR21JGQjzY6BzOlX0iKTDbQHoQciP+MbnVJAguEg== X-Google-Smtp-Source: AGHT+IFJOLdx1Sp8uMNEnK8a2RC4wlpChlC3goEyEnN2XPVHwxKyXWx55Hr6F/TCyrQhaSBtoMlp X-Received: by 2002:a05:6a00:170a:b0:6ed:d47e:625d with SMTP id h10-20020a056a00170a00b006edd47e625dmr3561923pfc.30.1714147668535; Fri, 26 Apr 2024 09:07:48 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714147668; cv=pass; d=google.com; s=arc-20160816; b=PukWCiIajfpxSI6sK/hiSyOqKOORCYrAbFRwG3OuXc0UK/ukIP5mMgJPs7f/SG+Yli D5moQ1kLgqeqbY7gBvoTshP77LEQjsdFzRtKkibZeqet4JY50njr4pEZP8YlO5shnUEr 5X+F7r66Vi9M37qzz4dDcn3a9nyMBKp1xGLPvlnRVC0/F47LuFNZnVAcDCCBQXVgSkN8 2dSJYTiWoHqRu7izUmglWK0rKh9X01dj1lQas1v7WGaWIyLH1P1xzMr/e5VXNP/q3Oy8 JedZfUvbzuoUD2N29yO4fnCKG+rLHOJBqDPg/nJZOO1If6eq/nvwQrSSceGoquXgreSP S2zw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=7VnGZicWFW/d+/1fPpNzYRf/LnrOSTnWU9XlyME7agc=; fh=UENz1OXBSlu/skbAasqEnO1vrKDRR/ERBA9VPKK/o78=; b=zSsAU18eiVL9unvNyyAC99Gm0d1iZ4KsSzxsvhjfu3r6e55OHNzxDhbzVYkrD1IBm9 3NVdimeQieFJHUcSMTnCyRuxYaKeRLHDNH6Opb/NysHq6I9s5HaUUEYCWVqslHdK1Tiv Y+gc1koyDXflxo5lbvbU6vft0gyoS20P3nZiPe1n9H39MeIMaRQN8vKWLFRigH4D65Q4 mOUIzPiOhhGI1LXGJcRd1fZWMrLJrgNNoKOkvUcv6HB9fr/MHql1tW/78DLW9CdC3pL/ Vj4H57HEVX1dMz8C9fEaC2lH3tVfRxje5J+rDJVpLEmRKhU1+koTDiYOmfEsXQ25T3LO pcsA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Xm9f32QV; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-160316-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-160316-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id x64-20020a636343000000b005f7ff353750si34344pgb.207.2024.04.26.09.07.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Apr 2024 09:07:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-160316-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Xm9f32QV; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-160316-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-160316-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 7B5D6B224A1 for ; Fri, 26 Apr 2024 15:42:12 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 39A641DFFC; Fri, 26 Apr 2024 15:42:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Xm9f32QV" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9FF6D149005 for ; Fri, 26 Apr 2024 15:42:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714146124; cv=none; b=IRaG9pKgJL3xT5aaugRktqG5k/h33BjdLr5XqJ0YUkG/UKMAnp54pcqlHOeQRdLvFQT4oXGshmBDTGai9MNrJ75pwBiEj9SblJmbANMa4eer/CIbkmyD/D31Qe8HYQF1wzA0x+Tfu71bm/e1CNBWKi8owWd4kdz8S6m6eY69Irk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714146124; c=relaxed/simple; bh=14+rpldCaSzHef8aS06UJj1525hCkyWYEThn+dTnrNw=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=JROFF6G0TitLb5POOpOSBYSSBQZZUPjGNe12osq7W9L4pCw0rgYOB8HLjgzyB0x7uXTklERcZhZoWQYBfdJvKUWOkNHwrgQbU7RdFmFbkjDuF9qgkJqZGvlEZZQNE0mFH9Klsemn0xAuIixISatNKs0XNXiSZrmyuyg7oJZtHkQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Xm9f32QV; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1714146121; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=7VnGZicWFW/d+/1fPpNzYRf/LnrOSTnWU9XlyME7agc=; b=Xm9f32QVH1D6c1KxpEVVExoUzHK8LSKxZX1Ij7gipQeZlCNhNXZxrRzg3mZFYjlPtr1xUa Njeey8Ggs5oZGqtDhyCKfCXjRkgR7POeZCVXwd56U4IaenGtuiNYEssTiKpY5wpHMzbSKM pMqAWxrOyoYFZR/5DFj3jUSliBxcAsE= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-269-dZOs051oNW2hkGqkS1G_6g-1; Fri, 26 Apr 2024 11:41:59 -0400 X-MC-Unique: dZOs051oNW2hkGqkS1G_6g-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 54EF318065AA; Fri, 26 Apr 2024 15:41:59 +0000 (UTC) Received: from chopper.lyude.net (unknown [10.22.8.17]) by smtp.corp.redhat.com (Postfix) with ESMTP id 138C651BF; Fri, 26 Apr 2024 15:41:58 +0000 (UTC) From: Lyude Paul To: nouveau@lists.freedesktop.org Cc: dri-devel@lists.freedesktop.org, Karol Herbst , Danilo Krummrich , David Airlie , Daniel Vetter , Dave Airlie , Ben Skeggs , Justin Stitt , Kees Cook , linux-kernel@vger.kernel.org (open list) Subject: [PATCH 1/2] drm/nouveau/firmware: Fix SG_DEBUG error with nvkm_firmware_ctor() Date: Fri, 26 Apr 2024 11:41:28 -0400 Message-ID: <20240426154138.64643-1-lyude@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 Currently, enabling SG_DEBUG in the kernel will cause nouveau to hit a BUG() on startup: kernel BUG at include/linux/scatterlist.h:187! invalid opcode: 0000 [#1] PREEMPT SMP NOPTI CPU: 7 PID: 930 Comm: (udev-worker) Not tainted 6.9.0-rc3Lyude-Test+ #30 Hardware name: MSI MS-7A39/A320M GAMING PRO (MS-7A39), BIOS 1.I0 01/22/2019 RIP: 0010:sg_init_one+0x85/0xa0 Code: 69 88 32 01 83 e1 03 f6 c3 03 75 20 a8 01 75 1e 48 09 cb 41 89 54 24 08 49 89 1c 24 41 89 6c 24 0c 5b 5d 41 5c e9 7b b9 88 00 <0f> 0b 0f 0b 0f 0b 48 8b 05 5e 46 9a 01 eb b2 66 66 2e 0f 1f 84 00 RSP: 0018:ffffa776017bf6a0 EFLAGS: 00010246 RAX: 0000000000000000 RBX: ffffa77600d87000 RCX: 000000000000002b RDX: 0000000000000001 RSI: 0000000000000000 RDI: ffffa77680d87000 RBP: 000000000000e000 R08: 0000000000000000 R09: 0000000000000000 R10: ffff98f4c46aa508 R11: 0000000000000000 R12: ffff98f4c46aa508 R13: ffff98f4c46aa008 R14: ffffa77600d4a000 R15: ffffa77600d4a018 FS: 00007feeb5aae980(0000) GS:ffff98f5c4dc0000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00007f22cb9a4520 CR3: 00000001043ba000 CR4: 00000000003506f0 Call Trace: ? die+0x36/0x90 ? do_trap+0xdd/0x100 ? sg_init_one+0x85/0xa0 ? do_error_trap+0x65/0x80 ? sg_init_one+0x85/0xa0 ? exc_invalid_op+0x50/0x70 ? sg_init_one+0x85/0xa0 ? asm_exc_invalid_op+0x1a/0x20 ? sg_init_one+0x85/0xa0 nvkm_firmware_ctor+0x14a/0x250 [nouveau] nvkm_falcon_fw_ctor+0x42/0x70 [nouveau] ga102_gsp_booter_ctor+0xb4/0x1a0 [nouveau] r535_gsp_oneinit+0xb3/0x15f0 [nouveau] ? srso_return_thunk+0x5/0x5f ? srso_return_thunk+0x5/0x5f ? nvkm_udevice_new+0x95/0x140 [nouveau] ? srso_return_thunk+0x5/0x5f ? srso_return_thunk+0x5/0x5f ? ktime_get+0x47/0xb0 ? srso_return_thunk+0x5/0x5f nvkm_subdev_oneinit_+0x4f/0x120 [nouveau] nvkm_subdev_init_+0x39/0x140 [nouveau] ? srso_return_thunk+0x5/0x5f nvkm_subdev_init+0x44/0x90 [nouveau] nvkm_device_init+0x166/0x2e0 [nouveau] nvkm_udevice_init+0x47/0x70 [nouveau] nvkm_object_init+0x41/0x1c0 [nouveau] nvkm_ioctl_new+0x16a/0x290 [nouveau] ? __pfx_nvkm_client_child_new+0x10/0x10 [nouveau] ? __pfx_nvkm_udevice_new+0x10/0x10 [nouveau] nvkm_ioctl+0x126/0x290 [nouveau] nvif_object_ctor+0x112/0x190 [nouveau] nvif_device_ctor+0x23/0x60 [nouveau] nouveau_cli_init+0x164/0x640 [nouveau] nouveau_drm_device_init+0x97/0x9e0 [nouveau] ? srso_return_thunk+0x5/0x5f ? pci_update_current_state+0x72/0xb0 ? srso_return_thunk+0x5/0x5f nouveau_drm_probe+0x12c/0x280 [nouveau] ? srso_return_thunk+0x5/0x5f local_pci_probe+0x45/0xa0 pci_device_probe+0xc7/0x270 really_probe+0xe6/0x3a0 __driver_probe_device+0x87/0x160 driver_probe_device+0x1f/0xc0 __driver_attach+0xec/0x1f0 ? __pfx___driver_attach+0x10/0x10 bus_for_each_dev+0x88/0xd0 bus_add_driver+0x116/0x220 driver_register+0x59/0x100 ? __pfx_nouveau_drm_init+0x10/0x10 [nouveau] do_one_initcall+0x5b/0x320 do_init_module+0x60/0x250 init_module_from_file+0x86/0xc0 idempotent_init_module+0x120/0x2b0 __x64_sys_finit_module+0x5e/0xb0 do_syscall_64+0x83/0x160 ? srso_return_thunk+0x5/0x5f entry_SYSCALL_64_after_hwframe+0x71/0x79 RIP: 0033:0x7feeb5cc20cd Code: ff c3 66 2e 0f 1f 84 00 00 00 00 00 90 f3 0f 1e fa 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d 1b cd 0c 00 f7 d8 64 89 01 48 RSP: 002b:00007ffcf220b2c8 EFLAGS: 00000246 ORIG_RAX: 0000000000000139 RAX: ffffffffffffffda RBX: 000055fdd2916aa0 RCX: 00007feeb5cc20cd RDX: 0000000000000000 RSI: 000055fdd29161e0 RDI: 0000000000000035 RBP: 00007ffcf220b380 R08: 00007feeb5d8fb20 R09: 00007ffcf220b310 R10: 000055fdd2909dc0 R11: 0000000000000246 R12: 000055fdd29161e0 R13: 0000000000020000 R14: 000055fdd29203e0 R15: 000055fdd2909d80 We hit this because when initializing firmware of type NVKM_FIRMWARE_IMG_DMA we allocate coherent memory and then attempt to include that coherent memory in a scatterlist. What we actually mean to do here though is to pass a CPU-allocated memory address, since that's the only thing that would make sense to put in the scatterlist. Signed-off-by: Lyude Paul --- drivers/gpu/drm/nouveau/nvkm/core/firmware.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/drivers/gpu/drm/nouveau/nvkm/core/firmware.c b/drivers/gpu/drm/nouveau/nvkm/core/firmware.c index adc60b25f8e6c..141b0a513bf52 100644 --- a/drivers/gpu/drm/nouveau/nvkm/core/firmware.c +++ b/drivers/gpu/drm/nouveau/nvkm/core/firmware.c @@ -205,7 +205,9 @@ nvkm_firmware_dtor(struct nvkm_firmware *fw) break; case NVKM_FIRMWARE_IMG_DMA: nvkm_memory_unref(&memory); - dma_free_coherent(fw->device->dev, sg_dma_len(&fw->mem.sgl), fw->img, fw->phys); + dma_unmap_single(fw->device->dev, fw->phys, sg_dma_len(&fw->mem.sgl), + DMA_TO_DEVICE); + kfree(fw->img); break; case NVKM_FIRMWARE_IMG_SGT: nvkm_memory_unref(&memory); @@ -235,14 +237,17 @@ nvkm_firmware_ctor(const struct nvkm_firmware_func *func, const char *name, fw->img = kmemdup(src, fw->len, GFP_KERNEL); break; case NVKM_FIRMWARE_IMG_DMA: { - dma_addr_t addr; - len = ALIGN(fw->len, PAGE_SIZE); - fw->img = dma_alloc_coherent(fw->device->dev, len, &addr, GFP_KERNEL); - if (fw->img) { - memcpy(fw->img, src, fw->len); - fw->phys = addr; + fw->img = kmalloc(len, GFP_KERNEL); + if (!fw->img) + return -ENOMEM; + + memcpy(fw->img, src, fw->len); + fw->phys = dma_map_single(fw->device->dev, fw->img, len, DMA_TO_DEVICE); + if (dma_mapping_error(fw->device->dev, fw->phys)) { + kfree(fw->img); + return -EFAULT; } sg_init_one(&fw->mem.sgl, fw->img, len); -- 2.44.0