From nobody Fri Dec 19 20:17:28 2025 Received: from m16.mail.163.com (m16.mail.163.com [220.197.31.2]) (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 DE1BF245008; Tue, 9 Dec 2025 10:03:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=220.197.31.2 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765274585; cv=none; b=QMlVA+Yzoiy6henJmPWWS0PfMAM4AwhXbLMDoOEksnGCUcylfwOJ1FpZZRk6P+oR/761R4eqJpASCrIhI9eW3sPqH7Q5s40WLYVlCvruYBu6iwY6bJScFHZdHq1HapOYTSPiLsInkmJiu2EP2rTy3qR7ThPHX8OwHV0FdVHtXLw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765274585; c=relaxed/simple; bh=dyTFXYqc2OYzwy/ZfLakyLchgLEblR75XmfCu8c9BL0=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=dAVoHaiz5Sei/xBpl/AJLVPTCdl/dEDsokt8QGdsH9FhG5YGA6AL2Ws1O7SliWIlZS6Y+lZ0r+EG8JdQ0naSSBxeHSGbjwNcvTJHLVKii+1S3dKGWmTLWse+yftmPMkaKARRJtZ7hIVl+5fz5MJaNPTJDOX9bjYarrQYVq5WOss= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=Q6nBxCI6; arc=none smtp.client-ip=220.197.31.2 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="Q6nBxCI6" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=QF Cet6Y9n5L2eUpwhiFCan/BncIVl9TNPp0Dh5H0OCU=; b=Q6nBxCI6cZc6zCUb8P ZRW9glWW8YFiKFnX/w9UUUJRrhIzz5w5ZYqkrK8JiKgEaJs30EorlbAhSEJyE8jF VVxkjhBP/ZI6I8LIfX1n3yCV/qfp1FPjtaiBAhtuadwSTODG/b1HvnNeiKpEb8vr mUheoxgwKQhk3Y6lAbMXNweJI= Received: from hello.company.local (unknown []) by gzsmtp4 (Coremail) with SMTP id PygvCgAn+J+r8zdpGmM3Gw--.34S2; Tue, 09 Dec 2025 18:02:23 +0800 (CST) From: Liang Jie To: Sean Wang , Linus Walleij , Matthias Brugger , AngeloGioacchino Del Regno , linux-mediatek@lists.infradead.org (moderated list:PIN CONTROLLER - MEDIATEK), linux-gpio@vger.kernel.org (open list:PIN CONTROL SUBSYSTEM), linux-kernel@vger.kernel.org (open list:ARM/Mediatek SoC support), linux-arm-kernel@lists.infradead.org (moderated list:ARM/Mediatek SoC support) Cc: liangjie@lixiang.com, fanggeng Subject: [PATCH] pinctrl: mediatek: make devm allocations safer and clearer in mtk_eint_do_init() Date: Tue, 9 Dec 2025 18:02:17 +0800 Message-Id: <20251209100217.167581-1-buaajxlj@163.com> X-Mailer: git-send-email 2.25.1 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-CM-TRANSID: PygvCgAn+J+r8zdpGmM3Gw--.34S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxGFWkAF15ur1DuFykXF43KFg_yoWrWw48pF 4SgFnxKF48Jr4DKr13Zw1UXa4rtws7Xr4UWr4Y93yxArZxtryvgFWYya4UX3y0kryqyrWU Jr1rt347Z3yYyFUanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07j2WlkUUUUU= X-CM-SenderInfo: pexdtyx0omqiywtou0bp/1tbioBofIGk36B2dkwAAsg Content-Type: text/plain; charset="utf-8" From: Liang Jie mtk_eint_do_init() allocates several pointer arrays which are then populated in a per-instance loop and freed on error. The arrays are currently allocated with devm_kmalloc(), so their entries are left uninitialised until the per-instance allocations succeed. On a failure in the middle of the loop, the error path iterates over the full nbase range and calls devm_kfree() on each element. For indices which were never initialised, the corresponding array entries contain stack garbage. If any of those happen to be non-zero, devm_kfree() will pass them to devres_destroy(), which will WARN because there is no matching devm_kmalloc() resource for such bogus pointers. Improve the robustness and readability by: - Using devm_kcalloc() for the pointer arrays so that all entries start as NULL, ensuring that only genuinely initialised elements may be freed and preventing spurious WARN_ON()s in the error path. - Switching the allocations to sizeof(*ptr) / sizeof(**ptr) forms, avoiding hard-coded element types and making the code more resilient to future type changes. - Dropping the redundant NULL checks before devm_kfree(), as devm_kfree() safely handles NULL pointers. The functional behaviour in the successful initialisation path remains unchanged, while the error handling becomes simpler and less error-prone. Reviewed-by: fanggeng Signed-off-by: Liang Jie --- drivers/pinctrl/mediatek/mtk-eint.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/drivers/pinctrl/mediatek/mtk-eint.c b/drivers/pinctrl/mediatek= /mtk-eint.c index 9f175c73613f..2ea0902b4f66 100644 --- a/drivers/pinctrl/mediatek/mtk-eint.c +++ b/drivers/pinctrl/mediatek/mtk-eint.c @@ -539,24 +539,32 @@ int mtk_eint_do_init(struct mtk_eint *eint, struct mt= k_eint_pin *eint_pin) } } =20 - eint->pin_list =3D devm_kmalloc(eint->dev, eint->nbase * sizeof(u16 *), G= FP_KERNEL); + eint->pin_list =3D devm_kcalloc(eint->dev, eint->nbase, + sizeof(*eint->pin_list), GFP_KERNEL); if (!eint->pin_list) goto err_pin_list; =20 - eint->wake_mask =3D devm_kmalloc(eint->dev, eint->nbase * sizeof(u32 *), = GFP_KERNEL); + eint->wake_mask =3D devm_kcalloc(eint->dev, eint->nbase, + sizeof(*eint->wake_mask), GFP_KERNEL); if (!eint->wake_mask) goto err_wake_mask; =20 - eint->cur_mask =3D devm_kmalloc(eint->dev, eint->nbase * sizeof(u32 *), G= FP_KERNEL); + eint->cur_mask =3D devm_kcalloc(eint->dev, eint->nbase, + sizeof(*eint->cur_mask), GFP_KERNEL); if (!eint->cur_mask) goto err_cur_mask; =20 for (i =3D 0; i < eint->nbase; i++) { - eint->pin_list[i] =3D devm_kzalloc(eint->dev, eint->base_pin_num[i] * si= zeof(u16), + eint->pin_list[i] =3D devm_kzalloc(eint->dev, + eint->base_pin_num[i] * sizeof(**eint->pin_list), GFP_KERNEL); port =3D DIV_ROUND_UP(eint->base_pin_num[i], 32); - eint->wake_mask[i] =3D devm_kzalloc(eint->dev, port * sizeof(u32), GFP_K= ERNEL); - eint->cur_mask[i] =3D devm_kzalloc(eint->dev, port * sizeof(u32), GFP_KE= RNEL); + eint->wake_mask[i] =3D devm_kzalloc(eint->dev, + port * sizeof(**eint->wake_mask), + GFP_KERNEL); + eint->cur_mask[i] =3D devm_kzalloc(eint->dev, + port * sizeof(**eint->cur_mask), + GFP_KERNEL); if (!eint->pin_list[i] || !eint->wake_mask[i] || !eint->cur_mask[i]) goto err_eint; } @@ -592,12 +600,9 @@ int mtk_eint_do_init(struct mtk_eint *eint, struct mtk= _eint_pin *eint_pin) =20 err_eint: for (i =3D 0; i < eint->nbase; i++) { - if (eint->cur_mask[i]) - devm_kfree(eint->dev, eint->cur_mask[i]); - if (eint->wake_mask[i]) - devm_kfree(eint->dev, eint->wake_mask[i]); - if (eint->pin_list[i]) - devm_kfree(eint->dev, eint->pin_list[i]); + devm_kfree(eint->dev, eint->cur_mask[i]); + devm_kfree(eint->dev, eint->wake_mask[i]); + devm_kfree(eint->dev, eint->pin_list[i]); } devm_kfree(eint->dev, eint->cur_mask); err_cur_mask: --=20 2.25.1