From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-oo1-f43.google.com (mail-oo1-f43.google.com [209.85.161.43]) (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 AC771387569 for ; Wed, 15 Apr 2026 20:49:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.43 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776286201; cv=none; b=d/ezFO+QL5eos6fjZn14QW+HrHc98I/7zU1C2WwEczRrhguiFlnLa3jgDAKwupQUPol+bJVsX/lbMJVwpTMnMwQ0OZ14MrthvZZJLJ4DOxK7bsXs0/Oglgao0ergOotTgR/9WvUJacjYd7PtExvH/4V5GpmPSX70WGHxnEhlbEY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776286201; c=relaxed/simple; bh=XPyATpRpP6Fw9JKvgIKVLeMTg5EY/XceYweZor7TSdg=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=fMmfv7FjBr9bjsYkQZbxDs6QI3X7KmH9qzIpW/oSfkVvro52XbIHRfr5bsNC5iS1KaA85a04N7pCGT7OCDtWB1R0KxCQWDXDmK+w71iA7QfSsy4m3CZzIroOfuipgcBtziImtWGwm8MMLzBeA7hrZzVnzi5IjyUl8vzVWlbBHeQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linuxfoundation.org; spf=pass smtp.mailfrom=linuxfoundation.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=dvRAq16f; arc=none smtp.client-ip=209.85.161.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linuxfoundation.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linuxfoundation.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="dvRAq16f" Received: by mail-oo1-f43.google.com with SMTP id 006d021491bc7-679f6ee3fb0so2638520eaf.2 for ; Wed, 15 Apr 2026 13:49:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=google; t=1776286198; x=1776890998; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=fXRmZFUi3/NUGdrIU0HiWh6hUGnEEJ0BOLVJoZTzlo4=; b=dvRAq16feIyt2mDV03XY9P99ydpDN+LnSgdDiu9LzSOVQYjkv8M+2Zh4CSGX03tki5 GZ9ItndsswpCd4JWi37rzpFQK3Wp9xG98LETDEpwcKbfE9e9IV+KM8sYgPEKZd/yhhtO RQTxkPnXXE7dgnHiTjKGa+OrdeMcrYPxRLwZM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776286198; x=1776890998; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=fXRmZFUi3/NUGdrIU0HiWh6hUGnEEJ0BOLVJoZTzlo4=; b=p4Bg51RAVp/5Z4qhWGpPJaGgFMofPcaT5g4K6IuSmfFGKeKDQ0pqCN1Ef0WQuQAEzH SZUQjoPD+pRibp5BoI+aK1/5k4yVliVi/sgthk/hL0N92VsuXlvMbvVL6RHqeSAtsHOP 5Ft70GYGnmx0IWl2+TwQ5RNryfF8aqKhuLTQEOEltGqZBknU34598JvAwGD/xNnUGSJZ Wz27zxGHyuRrzXC6vYydQD6HoafyNfFCvqbUlacNpTqDFaEMkB09NrQNEioGMHNmxoLU AUpk7H2H2RmJQMZsK1xL44+/x6CiK1O72TQXo79Izf3AWGBknPNuv9MrncMOcJGRKJzC m3kg== X-Forwarded-Encrypted: i=1; AFNElJ+gD+sQygJKV0KbG2g5iP31jWymeKLCBZuodVVGgZbMNJyCVtS37dN08OLUsIFjP97ee5Y6FjQ2oxkUxq0=@vger.kernel.org X-Gm-Message-State: AOJu0YyxSfXkzPmqsx5hIkRagBk9BPhcVvUfdpih3MeoUK7ScGHxckCs kJ4Q1X7e0E+32qGevVU851uK/pCJdn13feLLHk5oQg6GWqYTkeRBcEGr/+SpROudXjk= X-Gm-Gg: AeBDievNAdQ1IB1RCzjRJKCqSaSyxG9lZnHovP+xDp79IQyfxFvTHYAIayxHPqV4c1F XJMerBnLZjwTA62eYh7Ge+ka2z0RREhF5g2PnsVT1rVczUI/Q4ZCmdFXHWJ/ppkYHsbI3cy0gOZ H5c8kZXBloYPLqFcvzPJ7O/3JEDSWKkOn/iisperGku2MjaX1fa1BGLQQsg0QGKJH18/+zheoaS r60yxOdHuizovEGpjgPuyC7FKmcO2u0RBGEqIIPXX4lMnX52kT7iJNhJd5XsFBfPLBomv2gp67O h3FMJmJ72wGPwn7rCmW9LyHgM4h4FkSHSEq3RWztpZDJDBab2kqwZTShc7WpHhcJVijn/UUcbGi sHOPPCbt36IxAFusatFgxqSjYglB0xTmUXoeTxKiNxlcEwveJ6rAv0yZhRHhIVNrbeYwhfyZPfS k53RIKdDKcoU1s1jJKJFq2etnO0Ikx7Wk= X-Received: by 2002:a05:6820:168c:b0:681:22fd:a589 with SMTP id 006d021491bc7-68be5a636d6mr12796440eaf.11.1776286198589; Wed, 15 Apr 2026 13:49:58 -0700 (PDT) Received: from [192.168.1.14] ([38.15.57.99]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-42689699dc8sm2210443fac.14.2026.04.15.13.49.56 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 15 Apr 2026 13:49:56 -0700 (PDT) Message-ID: Date: Wed, 15 Apr 2026 14:49:56 -0600 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] media: vimc: fix reference leak on failed device registration To: Guangshuo Li Cc: Kieran Bingham , Mauro Carvalho Chehab , Hans Verkuil , Dafna Hirschfeld , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, Shuah Khan References: <20260415154537.3451732-1-lgs201920130244@gmail.com> <75275f6e-8314-4dd6-a54e-95320c2224e2@linuxfoundation.org> Content-Language: en-US From: Shuah Khan In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 4/15/26 10:56, Guangshuo Li wrote: > Hi Shuah, > > Thanks for reviewing. > > On Thu, 16 Apr 2026 at 00:01, Shuah Khan wrote: >> > >> >> Can you share your manual review? >> >> Can other static analysis tools for example scripts/coccinelle support >> your findings? >> Did you try other static analysis tools in the kernel? >>> >>> Fixes: 4babf057c143f ("media: vimc: allocate vimc_device dynamically") >>> Cc: stable@vger.kernel.org >>> Signed-off-by: Guangshuo Li >>> --- >>> drivers/media/test-drivers/vimc/vimc-core.c | 1 + >>> 1 file changed, 1 insertion(+) >>> >>> diff --git a/drivers/media/test-drivers/vimc/vimc-core.c b/drivers/media/test-drivers/vimc/vimc-core.c >>> index 15167e127461..fee0c7a09c4f 100644 >>> --- a/drivers/media/test-drivers/vimc/vimc-core.c >>> +++ b/drivers/media/test-drivers/vimc/vimc-core.c >>> @@ -421,6 +421,7 @@ static int __init vimc_init(void) >>> if (ret) { >>> dev_err(&vimc_pdev.dev, >>> "platform device registration failed (err=%d)\n", ret); >>> + platform_device_put(&vimc_pdev); >> >> Where does platform_device_get() happen when platform_device_register() fails? >> >> thanks, >> -- Shuah > > My manual review was based on the platform_device_register() call > chain and its documented lifetime rules. > > The relevant code path is: > > ret = platform_device_register(&vimc_pdev); > if (ret) { > dev_err(&vimc_pdev.dev, > "platform device registration failed (err=%d)\n", ret); > return ret; > } > > and > > int platform_device_register(struct platform_device *pdev) > { > device_initialize(&pdev->dev); > setup_pdev_dma_masks(pdev); > return platform_device_add(pdev); > } > > If platform_device_add() fails, platform_device_register() returns an > error, but the reference initialized by device_initialize() is still > owned by the caller. The API documentation for platform_device_register() > also explicitly says: > > "Never directly free @pdev after calling this function, even if it > returned an error! Always use platform_device_put() to give up the > reference initialised in this function instead." > > So there is no matching platform_device_get() on the failure path. > The reference comes from device_initialize(), and platform_device_put() > is needed to drop that initial reference when registration fails. > > That was also how I manually confirmed the issue after the tool report: > I checked the platform_device_register() / platform_device_add() > implementation and verified that the vimc failure path returns directly > without calling platform_device_put(). > > I found this issue using a tool I recently developed. The scan was run > on kernel version v7.0-1262-g4fa12523f7bc. There are several calls to platform_device_register() all over the kernel. Did your tool find all other cases or just this one? thanks, -- Shuah