From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 55F1630E84D; Mon, 23 Jun 2025 13:33:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750685626; cv=none; b=Nj388DuB55rtU5rNk+NO5v0A3CVTNcNlh1gj0ro925+PF4JY/Jpw6BLDKKcIg91jq30v9LuiBFH8I4TDJYbJByl+1VJvDroMdWH1NpAA5jSoVH3b20jj4ATpsgjDhJ/jD+Bs2XG83AgUu8IBZYj07RtkcPrsFVDFsTp3URHI48g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750685626; c=relaxed/simple; bh=DdDqXTIJG2mYCcwbRzW19nBq8lGqxOWjIZdWUE0iGMw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gDdKEiuGmYlIlrqp4YPMplEVdT/IkNe+FwaQXxX7GFZSwaxtJhOHDM54KTMJLYbmfJhpuvuYNn5SyiHzUum58MtpetM0YK5ECXPuS8Cvw2lcQ/Jbp9IblppdkS/Vc43PcMrdZQgM8gepXHRtGZIN6FzXwbaTckzt1/yw/bVBMUs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=R3XDh64U; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="R3XDh64U" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DDF78C4CEEA; Mon, 23 Jun 2025 13:33:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1750685626; bh=DdDqXTIJG2mYCcwbRzW19nBq8lGqxOWjIZdWUE0iGMw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=R3XDh64UBt83KbHpdeaSrzWcrp4NeaK/G12i+CQ+CAdgBbSDO6xz9s+ObKJ3hYTVp +bDDpu5Ouc9L/aMc/gzUK6DK8jfI4ejeeBDi759BJxGAKXRzKXBuE/LPcHQot6q90m W/J7Zn32nq4rNw/q6WzcCeHb057uw3YUmWhmWe9A= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Ma Ke , Sakari Ailus , Hans Verkuil Subject: [PATCH 5.4 108/222] media: v4l2-dev: fix error handling in __video_register_device() Date: Mon, 23 Jun 2025 15:07:23 +0200 Message-ID: <20250623130615.358012277@linuxfoundation.org> X-Mailer: git-send-email 2.50.0 In-Reply-To: <20250623130611.896514667@linuxfoundation.org> References: <20250623130611.896514667@linuxfoundation.org> User-Agent: quilt/0.68 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 5.4-stable review patch. If anyone has any objections, please let me know. ------------------ From: Ma Ke commit 2a934fdb01db6458288fc9386d3d8ceba6dd551a upstream. Once device_register() failed, we should call put_device() to decrement reference count for cleanup. Or it could cause memory leak. And move callback function v4l2_device_release() and v4l2_device_get() before put_device(). As comment of device_register() says, 'NOTE: _Never_ directly free @dev after calling this function, even if it returned an error! Always use put_device() to give up the reference initialized in this function instead.' Found by code review. Cc: stable@vger.kernel.org Fixes: dc93a70cc7f9 ("V4L/DVB (9973): v4l2-dev: use the release callback from device instead of cdev") Signed-off-by: Ma Ke Reviewed-by: Sakari Ailus Signed-off-by: Hans Verkuil Signed-off-by: Greg Kroah-Hartman --- drivers/media/v4l2-core/v4l2-dev.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) --- a/drivers/media/v4l2-core/v4l2-dev.c +++ b/drivers/media/v4l2-core/v4l2-dev.c @@ -1010,25 +1010,25 @@ int __video_register_device(struct video vdev->dev.class = &video_class; vdev->dev.devt = MKDEV(VIDEO_MAJOR, vdev->minor); vdev->dev.parent = vdev->dev_parent; + vdev->dev.release = v4l2_device_release; dev_set_name(&vdev->dev, "%s%d", name_base, vdev->num); + + /* Increase v4l2_device refcount */ + v4l2_device_get(vdev->v4l2_dev); + mutex_lock(&videodev_lock); ret = device_register(&vdev->dev); if (ret < 0) { mutex_unlock(&videodev_lock); pr_err("%s: device_register failed\n", __func__); - goto cleanup; + put_device(&vdev->dev); + return ret; } - /* Register the release callback that will be called when the last - reference to the device goes away. */ - vdev->dev.release = v4l2_device_release; if (nr != -1 && nr != vdev->num && warn_if_nr_in_use) pr_warn("%s: requested %s%d, got %s\n", __func__, name_base, nr, video_device_node_name(vdev)); - /* Increase v4l2_device refcount */ - v4l2_device_get(vdev->v4l2_dev); - /* Part 5: Register the entity. */ ret = video_register_media_controller(vdev);