Received: by 2002:a05:7412:d1aa:b0:fc:a2b0:25d7 with SMTP id ba42csp916793rdb; Tue, 30 Jan 2024 02:31:08 -0800 (PST) X-Google-Smtp-Source: AGHT+IHudJWRT+3TgbCUJA/wSpb8/jG21OpsZU9qQjChdbLkereFcdBsw8oveEGlGkKNXjp6K+ck X-Received: by 2002:a17:902:da89:b0:1d8:bb09:6bf5 with SMTP id j9-20020a170902da8900b001d8bb096bf5mr4841056plx.28.1706610667884; Tue, 30 Jan 2024 02:31:07 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706610667; cv=pass; d=google.com; s=arc-20160816; b=dE2nnu5b/aB+FDbAZ5IH9q06uQEHuFPtz8tl6BfliixaJ0oL0HKN2AHFKs74qSm7WP Ua/jnd/ju40XyR5PvOxOHOSRjOpRhREo3hzh/FMLxGkeJSCsv1eMyMUcDHSVWW37uJY+ CHkuPWPpQNi4pSzKEKFXkYZHtayQ/Oy7xkUEYuV0ySje4e3mbM0cLsEPjPwf0NcATqB9 j2wLMfC1FymzN+nVNFhcVGsGrdqjkDLkzH+oIb99suNHPXYOykZGKJ9M88zoE4h54b+l ZVIw+MKgXz1DhJCBODLZnz7HDKkVOykCpMaA6/G3/x1sQ6ozHBK8BtM3/2wfZKE5ng1a 9G1g== 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:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=sMxRirpa263d6PmLZyC9XRaAp28SbobvUK5ST0uEzHo=; fh=T29NK0a2UgpyPMJNdRdxOwtTMCeLNbJCj+OzYaaxEOg=; b=ZRlungsShKAYLq1Cl/KdEDl8pH2K/A3M6yWvjzLzFUmCraCnspNICxDHet7m26yelD cDZXnpBPSBezk5E+jtVFNTIIRCubXFnRK1xalTaMXQ09zvWtanHuh8l5ydTxTBJWT3D8 m2A3N3I5b7Lt/LH11SlQDShSFkaT0YFB5wL0zLsAma/w4G7XY78PJ/iVPQh/VJYwIMUD BPXiN78EGr/s+xXK5s9v2xt5WHPmmSsKYgqMHXq1pDpgNjf4Q08EFqWV5dPKJHaQ/u+0 0wsS0YUsAIpwBhtUmKeTXurHQSWc39zeSSZZ3iKLfLDGORCtbuGLywCylRDdYwynKhD2 DZnw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=j0zXpjZv; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-44333-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-44333-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id h15-20020a170902f54f00b001d7711d26c2si7237028plf.157.2024.01.30.02.31.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jan 2024 02:31:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-44333-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=j0zXpjZv; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-44333-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-44333-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org 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 1B9BDB3035A for ; Tue, 30 Jan 2024 10:01:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C006766B46; Tue, 30 Jan 2024 09:57:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="j0zXpjZv" Received: from mail-pg1-f173.google.com (mail-pg1-f173.google.com [209.85.215.173]) (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 5D168664CC for ; Tue, 30 Jan 2024 09:57:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.173 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706608624; cv=none; b=cDBJMjTqbk4WQ62kyWnB4/Pm9FAdBf3Z2/HHjqntJvTBWUVk+TtcRmeJw8LXjzNFlGbPVeLSD1Jpp9SwJhT6jQGdIwXOw94s+RuzpGa5ASiZ9tSnEbH/x3uyB+vTwsnk9R5edyfEzPHJHadM5C7GK+C2+cDx0lQ4GPuRqxL/o5E= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706608624; c=relaxed/simple; bh=T0JtsjOKE5HCH9JwYSPgNeRdUS2v7ajLCNvW4doHSaY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Zyf0Gh60U8bwBCQjVxTekbnEisR1JFnTLI69qo8puR31iQXK4qKsPR/YwN2YoB2dAwVVMRS6pUPjPXw8n8goOKzZt4KXVyM3mcHYbsY6Rg/fd/kB/cenzcssTX8YcbGIa4G/+8X5MmSfoYyQ1lwf+WomiZEECaG6NP7QRyUs5V4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=j0zXpjZv; arc=none smtp.client-ip=209.85.215.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-pg1-f173.google.com with SMTP id 41be03b00d2f7-5cdbc4334edso1707752a12.3 for ; Tue, 30 Jan 2024 01:57:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1706608622; x=1707213422; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=sMxRirpa263d6PmLZyC9XRaAp28SbobvUK5ST0uEzHo=; b=j0zXpjZvcSjmhpN2odRPSr8w1meoWgS4+CpHBaSbrN3VAbrKVVbVJwL9UAHrf4YEy6 1VuxkQJj4umUnFFuVpsj8LUCDJjUXrUplTEV33XWvyaHDk05wX2SbxChUk3AN4qg2P7+ QNnTtkrmBOunq0NET/2Ooj4ZOC348X21NZWh4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706608622; x=1707213422; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=sMxRirpa263d6PmLZyC9XRaAp28SbobvUK5ST0uEzHo=; b=hF6rLGzaP4XFlIWuaLhQzszTAc9XGnlvSyqlIUFfzLldCiqR2gs0z0wG3Gfh5+61z4 wGGus5hEsbXDSN5GEXOTnHl7hGZqfzat5wA4QnVvY1yeEzAtxGTCXQnP24Ww4lcQUzbg j/MIVpmq6Zqk4Eb/QtNgSuEQ2FPzF82Or1lHHpxrl9Y4y9WJhY7OmZAKqQ59d2QdIxgk auaWC+PZT7U/casE/BvDtnRNfF7cvHzJ0e9qwIGBsqCyRwkYq5oY3ZMVXpkAc7nIFd+L jVhGfx1cKVZDbE7c7rtOy3U5aRsxD0o8rV3lI36xHeKrebz54V4M115Dj+OlbSVIF8kU hNhQ== X-Gm-Message-State: AOJu0YwvSKN83CifQGCXyYiFfgQZWeRvb6HtqJexLCRMT/2NJmJpxtZG nClpk975WbcCGHnhjKi4KQSlRwU6NZzDBh38HA3ZPCrF4Rn3kW0GjmVD1dL22w== X-Received: by 2002:a05:6a20:3f95:b0:199:c8f1:58 with SMTP id ay21-20020a056a203f9500b00199c8f10058mr4049745pzb.41.1706608622688; Tue, 30 Jan 2024 01:57:02 -0800 (PST) Received: from wenstp920.tpe.corp.google.com ([2401:fa00:1:10:469:110f:d748:6896]) by smtp.gmail.com with ESMTPSA id sm5-20020a17090b2e4500b0028ffea988a2sm8069810pjb.37.2024.01.30.01.57.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jan 2024 01:57:02 -0800 (PST) From: Chen-Yu Tsai To: Matthias Brugger , AngeloGioacchino Del Regno , Srinivas Kandagatla Cc: Chen-Yu Tsai , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, William-tw Lin Subject: [PATCH 1/3] soc: mediatek: mtk-socinfo: Clean up NVMEM cell read Date: Tue, 30 Jan 2024 17:56:51 +0800 Message-ID: <20240130095656.3712469-2-wenst@chromium.org> X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog In-Reply-To: <20240130095656.3712469-1-wenst@chromium.org> References: <20240130095656.3712469-1-wenst@chromium.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The mtk-socinfo grabs the NVMEM device devm_nvmem_device_get(), but then proceeds to put the device directly with nvmem_device_put() if the read is successful. If the device fails to probe and goes through the devres release path, the device would be put a second time, triggering a use-after-free error from KASAN. Fix this by dropping the devres part. Since the NVMEM cell data is read only once, there is no need to keep the reference around. While at it, clean up the function to directly reference the NVMEM device node and use that to find the NVMEM device, instead of finding it by name, which is more fragile. The cell node is always a direct child of the NVMEM device node, courtesy of the legacy NVMEM cell layout. Thus of_get_child_by_name() is a better way of finding the cell. Last, correctly put the device node once its use is over. Fixes: 423a54da3c7e ("soc: mediatek: mtk-socinfo: Add driver for getting chip information") Signed-off-by: Chen-Yu Tsai --- drivers/soc/mediatek/mtk-socinfo.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/drivers/soc/mediatek/mtk-socinfo.c b/drivers/soc/mediatek/mtk-socinfo.c index 0094f43e1e08..3909d22062ce 100644 --- a/drivers/soc/mediatek/mtk-socinfo.c +++ b/drivers/soc/mediatek/mtk-socinfo.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -82,25 +83,28 @@ static int mtk_socinfo_create_socinfo_node(struct mtk_socinfo *mtk_socinfop) static u32 mtk_socinfo_read_cell(struct device *dev, const char *name) { struct nvmem_device *nvmemp; - struct device_node *np = dev->of_node; + struct device_node *np, *nvmem_node = dev->parent->of_node; u32 offset; u32 cell_val = CELL_NOT_USED; - nvmemp = devm_nvmem_device_get(dev, "mtk-efuse0"); + /* should never fail since the nvmem driver registers this child */ + nvmemp = nvmem_device_find(nvmem_node, device_match_of_node); if (IS_ERR(nvmemp)) goto out; - np = of_find_node_by_name(NULL, name); + np = of_get_child_by_name(nvmem_node, name); if (!np) - goto out; + goto put_device; if (of_property_read_u32_index(np, "reg", 0, &offset)) - goto out; + goto put_node; nvmem_device_read(nvmemp, offset, sizeof(cell_val), &cell_val); +put_node: + of_node_put(np); +put_device: nvmem_device_put(nvmemp); - out: return cell_val; } -- 2.43.0.429.g432eaa2c6b-goog