From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm2-f13.google.com (mail-wm2-f13.google.com [74.125.225.141]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3D8705012A5 for ; Thu, 17 Sep 2026 16:30:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.225.141 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789662636; cv=none; b=sosvbtOyNw743PxzykXIgs93eEJUFlHkaH+sJ+Mv4w+9m8WqK8jsxRq+ltROymQb6xo5C0hoybrSoIIslOPLtjzauIp3s7otycMfgGdeutyRpJAYa0d77r7FKFvQeLUUYt1VxO6mIPMlcpmlHTNwUIiLDB3APWhF88XIZR/zXQc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789662636; c=relaxed/simple; bh=1uKQgdK2ZQ67pQ69htT9iF0krrGap0M3PIVWMzu37pA=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=WSK1RhMguDAqYuzyNv5WQY4/eWpmVwWB3amnR37tBld9SuU4fThR7X+j01wCK1syD91zOIRRoAJKtt1WZFKXjoEJbpMuBL3CgnQKnXCX0fpY+XRW0VIYLaLLpXp0jd3lLlJXlUib/AJK4k8Yk42q8GbOgAdi/rYFHodsSfJo0S0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=aqMWM1TP; arc=none smtp.client-ip=74.125.225.141 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="aqMWM1TP" Received: by mail-wm2-f13.google.com with SMTP id 5b1f17b1804b1-49cd4ba9f68so13871615e9.1 for ; Thu, 17 Sep 2026 09:30:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1789662632; x=1790267432; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=epvtes+QTuG/8pdcW3OWOMHUppdH8T7oJtRmUkiI2+8=; b=aqMWM1TPIyqazhICTpRfrGH6VWZ43iEOepqgEHCW0GmOJ7CnvhBlmFza3iajce7mfv MkURq553STUfSpkKMNPKGoNnlv6EGe3YgvUozgo+ckV/1SvEAJD7HLroDrGiVx6+Enq4 1DVWly2htn88ghQ+EBELu+a1SicSmabI949ZpY/io215oeXuyTTmHPwrzl3aqIcv4NvL EtoIOK3zfhHd9Ur0ZU6dDnCqR7WC+GsWNQknTFV1OHwpnK9idNPq2kQvW2sAd7t+Q0P0 JWtTT5JKZF6oiBsmdp10TQAMK1dPNgKn0NcJckoLJL9ezL/ugL6RVhkg4adoF10pYz53 kTtg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789662632; x=1790267432; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=epvtes+QTuG/8pdcW3OWOMHUppdH8T7oJtRmUkiI2+8=; b=ZgBgi0ygKdnUeK7658oeNEJKXYLGmXLy8WcAHaJYWX1jFhnmMJLzKePfapcCfbNOvQ FSPGssL9nx+A2C9szBKxGe6NOwNt5SNeHl969XXTLodtUnbpDLd1z/KqsoKRRazBfizY suSdUpGZWYKlteGWMMGShHHLKbgq+1p9MX1HEkoMpWX2Y6M3Wb0eNSXB4m1A1orE7gc2 0UoDvf2/OJ6qQCTD4Z6B2wra5rHeMb5Fc8pmAisdIYZxKxMhctEj9oeyyR7LPDMSMGb4 XSTs9r1/g2NsK8jy++InPHknMh9MX1R6Je2E/ZMWcXYa6vsoTbQilVXaO3tz2eOR1XsM H9SQ== X-Forwarded-Encrypted: i=1; AKwUvBz6RDU+RoQaEP92Vr8FbfwUVStjBjjrgl2gPhDEBSD8u0OyeX6Ac9DTazNf+6cFNSKtODiK+M68tIU=@vger.kernel.org X-Gm-Message-State: AFuF++k8gSSLfdZ4Zd1D5LkEIVGAG/YEW/8/LJNbzZJcw8BX7GS0Xmsw la525Yzog2OMqZHg8II5zVSmn9D5FvPgaOP7cY83Sq6AHpTTcT5c8NdE X-Gm-Gg: AYBFou1P420NWiIWTUyCp0HmzsXUh6N7D1xuVqGMrqDE3hpmLq6AjMb6mtNbNfcJ3wT 5ygQAkaeZol90ejVZZuEwh+ZwRymdorJwyLPHmMrUdWj6uKUBc83HAXKHRFpPyJjOZIiVbg3cuC IYdS64Ekei31Fu3ftOqABHfd0TR+/+gMRkvJ+ahuUMI9SMvYXb03m17NAnHh0frZDzet11tnx+7 hesxADZ/ky8l6RBpW4If6rBcoJpe4cOV879q+gTbq6AUDw04L3vk7ion4/rnmiYQdkKxQANsBlC A6IHnuMfqHLMXxCscEde5+WR4pJIHnqckWUZ0dDLXmhZZEKiCsb9HeOz0iHbhN3saHSxyAeKL1n /72AOkz228Tr1Ak2a/YOzkjSeMztOgJy8xTF80hIgQvSb9iMWy+EgGNfQz2NarB9Dr1kgS4gP4M gB7GH74wQQPPC4yd94fH55bI0iApwlozPSDKXWroP6RQ9uPFnrj2zJK4fSvj7DIlpYTdpgj81lb seGdUVwwJ5XI9DN+yuN X-Received: by 2002:a05:600c:e549:10b0:49e:745d:c317 with SMTP id 5b1f17b1804b1-49f1d516934mr56467995e9.12.1789662632070; Thu, 17 Sep 2026 09:30:32 -0700 (PDT) Received: from localhost.localdomain ([72.255.58.127]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-49fbf515cfdsm77508595e9.11.2026.09.17.09.30.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Sep 2026 09:30:31 -0700 (PDT) From: Mahad Ibrahim To: Keguang Zhang , Vinod Koul Cc: Frank Li , Frank Li , linux-mips@vger.kernel.org, dmaengine@vger.kernel.org, linux-kernel@vger.kernel.org, Mahad Ibrahim Subject: [PATCH v3] dmaengine: loongson1-apb-dma: avoid using iterator variable after list_for_each_entry() Date: Thu, 17 Sep 2026 16:30:25 +0000 Message-ID: <20260917163025.7171-1-mahad.ibrahim.dev@gmail.com> X-Mailer: git-send-email 2.54.0 Precedence: bulk X-Mailing-List: dmaengine@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit ls1x_dma_tx_status() locates the descriptor actively being processed by walking the LLI list and comparing the hardware reported next descriptor pointer against each element's next-descriptor pointer. A list_for_each_entry macro is used in the comparison phase, which at the end of the loop leaves the lli pointer at the currently executing LLI. However this also subsequently runs for a non-match lli, in which it points at the head. This causes a type confusion bug which treats the head, which is a ls1x_dma_desc, as a ls1x_dma_lli object. Additionally it goes forwards and prints garbage via the dev_dbg. Fix the type confusion bug by only allowing matched LLI descriptor chains to print the current LLI and residue calculation, as failing to match should be treated as an unexpected condition. Found by the following Coccinelle check: scripts/coccinelle/iterators/use_after_iter.cocci drivers/dma/loongson/loongson1-apb-dma.c:461:6-9: ERROR: invalid reference to the index variable of the iterator on line 450 Compile test only; Hardware testing by Keguang Zhang. Signed-off-by: Mahad Ibrahim Reviewed-by: Keguang Zhang Tested-by: Keguang Zhang # on LS1B & LS1C --- v2: - encapsulate residue calculation and dev_dbg inside the list_for_each_entry() macro. Treat non-matching LLI as an unexpected case. - link: https://lore.kernel.org/all/20260729143247.6111-1-mahad.ibrahim.dev@gmail.com/ v3: - Collect tags from Keguang Zhang. - Repost patch in a new thread. - Change chan2dev to the newer dmaengine_chan_dev. drivers/dma/loongson/loongson1-apb-dma.c | 27 ++++++++++++++---------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/drivers/dma/loongson/loongson1-apb-dma.c b/drivers/dma/loongson/loongson1-apb-dma.c index 46b4bfef45e2..e62ab26c8327 100644 --- a/drivers/dma/loongson/loongson1-apb-dma.c +++ b/drivers/dma/loongson/loongson1-apb-dma.c @@ -441,22 +441,27 @@ static enum dma_status ls1x_dma_tx_status(struct dma_chan *dchan, /* locate the current lli */ next_phys = chan->curr_lli->hw[LS1X_DMADESC_NEXT]; - list_for_each_entry(lli, &desc->lli_list, node) - if (lli->hw[LS1X_DMADESC_NEXT] == next_phys) - break; + list_for_each_entry(lli, &desc->lli_list, node) { + if (lli->hw[LS1X_DMADESC_NEXT] != next_phys) + continue; - dev_dbg(dmaengine_chan_dev(dchan), "current lli_phys=%pad", - &lli->phys); + dev_dbg(dmaengine_chan_dev(dchan), "current lli_phys=%pad\n", + &lli->phys); - /* count the residues */ - list_for_each_entry_from(lli, &desc->lli_list, node) - bytes += lli->hw[LS1X_DMADESC_LENGTH] * - chan->bus_width; + /* count the residues */ + list_for_each_entry_from(lli, &desc->lli_list, node) + bytes += lli->hw[LS1X_DMADESC_LENGTH] * + chan->bus_width; + + dma_set_residue(state, bytes); + return status; + } + + dev_warn(dmaengine_chan_dev(dchan), + "unable to locate current lli.\n"); } } - dma_set_residue(state, bytes); - return status; } -- 2.54.0