From nobody Tue Dec 2 02:52:24 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 3947826F280 for ; Mon, 17 Nov 2025 19:04:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763406241; cv=none; b=G30x4Eg42tVXe4MqASISwkUdKB1WtDztLUgRH7FjiNS0cdAXuZoJvOlj9uZ+K4Qil5CRsxHHL4k2o+D2AkqXL5rrSd4oye94XltocGRREVPKH4PwQ6XK/OFHAhOMoUZjfQ6vrfw12ku2giQrywVYeEur8a7O3JNMVRN960LIufQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763406241; c=relaxed/simple; bh=OMRBt4BcOwZJEA/mt+axoCwvIBP4LXPc20txidvA3HQ=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=ltEomwt4v7M8rlwkjuzI0hfOR8x3cmkOmOh8iJuoPrYPzYcxGyxy5nY5FOU+TTki6wQZbhzU3fnqkgOL37+ViIEunYR11ya8yQwAwvr8pUZuxYRHsjVf2e8YHl84jOWx1DvMicOn4pgVTbJiIgcJmQMJqMC/kqCI2qP0tCjegac= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine 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=dLmB+aeX; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="dLmB+aeX" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763406239; 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=VKt60h4e/D77yoMk24RHMBXqjTCJQtx23LHDLJefZVQ=; b=dLmB+aeX2AoQpfwG2LS+lfdEvr9+YYwfIaWWDDLQuYq9zPKEqPFsy6KBlTZLk9Q/ghjVDs j7cbTdi/m8yMfKsQEx+HsiDqdqY/wIy3GE3CSxeXQdL8YvqNc7ZEP0tBvmw8QuTUeLyxVS tTzo4jY1WCqgCSsV5yF1oAS03h62Ow4= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-364-hV9jXuTwNdaMZzfCYRdSmA-1; Mon, 17 Nov 2025 14:03:52 -0500 X-MC-Unique: hV9jXuTwNdaMZzfCYRdSmA-1 X-Mimecast-MFC-AGG-ID: hV9jXuTwNdaMZzfCYRdSmA_1763406231 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (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 mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 7274918AB425; Mon, 17 Nov 2025 19:03:49 +0000 (UTC) Received: from cmirabil.redhat.com (unknown [10.22.65.154]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id DB31D1800451; Mon, 17 Nov 2025 19:03:44 +0000 (UTC) From: Charles Mirabile To: linux-kernel@vger.kernel.org Cc: spacemit@lists.linux.dev, linux-riscv@lists.infradead.org, linux-clk@vger.kernel.org, mturquette@baylibre.com, sboyd@kernel.org, dlan@gentoo.org, heylenay@4d2.org, elder@riscstar.com, troy.mitchell@linux.spacemit.com, inochiama@outlook.com, Charles Mirabile , Yanko Kaneti Subject: [PATCH] clk: spacemit: fix UBSAN warning during probe Date: Mon, 17 Nov 2025 14:03:29 -0500 Message-ID: <20251117190329.1337250-1-cmirabil@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: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Content-Type: text/plain; charset="utf-8" When booting with KASAN enabled the following splat is encountered during probe of the k1 clock driver: UBSAN: array-index-out-of-bounds in drivers/clk/spacemit/ccu-k1.c:1044:16 index 0 is out of range for type 'clk_hw *[*]' CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Not tainted 6.18.0-rc5+ #1 PREEMPT(laz= y) Hardware name: Unknown Unknown Product/Unknown Product, BIOS 2022.10spacemi= t 10/01/2022 Call Trace: [] dump_backtrace+0x28/0x38 [] show_stack+0x3a/0x50 [] dump_stack_lvl+0x5a/0x80 [] dump_stack+0x18/0x20 [] ubsan_epilogue+0x10/0x48 [] __ubsan_handle_out_of_bounds+0xa6/0xa8 [] k1_ccu_probe+0x37e/0x420 [] platform_probe+0x56/0x98 [] really_probe+0x9e/0x350 [] __driver_probe_device+0x80/0x138 [] driver_probe_device+0x3a/0xd0 [] __driver_attach+0xac/0x1b8 [] bus_for_each_dev+0x6c/0xc8 [] driver_attach+0x26/0x38 [] bus_add_driver+0x13e/0x268 [] driver_register+0x52/0x100 [] __platform_driver_register+0x28/0x38 [] k1_ccu_driver_init+0x22/0x38 [] do_one_initcall+0x62/0x2a0 [] do_initcalls+0x170/0x1a8 [] kernel_init_freeable+0x16a/0x1e0 [] kernel_init+0x2c/0x180 [] ret_from_fork_kernel+0x16/0x1d8 [] ret_from_fork_kernel_asm+0x16/0x18 Reviewed-by: Alex Elder Reviewed-by: Troy Mitchell Reviewed-by: Yixun Lan Tested-by: Yanko Kaneti ---[ end trace ]--- This is bogus and is simply a result of KASAN consulting the `.num` member of the struct for bounds information (as it should due to `__counted_by`) and finding 0 set by kzalloc because it has not been initialized before the loop that fills in the array. The easy fix is to just move the line that sets `num` to before the loop that fills the array so that KASAN has the information it needs to accurately conclude that the access is valid. Fixes: 1b72c59db0add ("clk: spacemit: Add clock support for SpacemiT K1 SoC= ") Tested-by: Yanko Kaneti Signed-off-by: Charles Mirabile --- drivers/clk/spacemit/ccu-k1.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/clk/spacemit/ccu-k1.c b/drivers/clk/spacemit/ccu-k1.c index f5a9fe6ba1859..4761bc1e3b6e6 100644 --- a/drivers/clk/spacemit/ccu-k1.c +++ b/drivers/clk/spacemit/ccu-k1.c @@ -1018,6 +1018,8 @@ static int spacemit_ccu_register(struct device *dev, if (!clk_data) return -ENOMEM; =20 + clk_data->num =3D data->num; + for (i =3D 0; i < data->num; i++) { struct clk_hw *hw =3D data->hws[i]; struct ccu_common *common; @@ -1044,8 +1046,6 @@ static int spacemit_ccu_register(struct device *dev, clk_data->hws[i] =3D hw; } =20 - clk_data->num =3D data->num; - ret =3D devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, clk_data); if (ret) dev_err(dev, "failed to add clock hardware provider (%d)\n", ret); base-commit: 6a23ae0a96a600d1d12557add110e0bb6e32730c --=20 2.51.1