Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp2014494ybv; Sun, 23 Feb 2020 20:45:43 -0800 (PST) X-Google-Smtp-Source: APXvYqxNyJAf+p6F6OoBfMCc8BnDneai8ayv+CP3djNjWljH9m+UzmDEaUOTTUh1ClVnNKu18L2O X-Received: by 2002:a05:6808:191:: with SMTP id w17mr10929710oic.29.1582519543030; Sun, 23 Feb 2020 20:45:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582519543; cv=none; d=google.com; s=arc-20160816; b=E7pzm+05uDlZChcpiWRT4XRuQEd7RKPLufRT9nmzT6pbANR8zMVgMdX4Eev/CugXdY 4Is1J4qAlAaLbfKhgz1MYbIbZ1qB+TKNwfszGBUdJc8ZiZVB+kOQb7riEP+E96r7699G 7CBI5Z4WeFgixJuQV0ZxrGj4esI2DEvqQYEdVAaEG1TG2zjOxLKE0+5aHN7hEyKLlVY3 4JlvLqhJMeI0D9nzWHSl/gGLO++T76gj+9MjPcJox1rwBAVzAEHzHwNN6uCX25QSC0oj oLnq4DxZvkRosXbsFkXaPKcpinC2dMAzy36l6+sNi5Imlc3pBgZ/Kou80mwvMxhFPaNN xqmg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:dkim-signature:content-language :content-transfer-encoding:in-reply-to:mime-version:user-agent:date :message-id:references:cc:to:from:subject; bh=FgL2m4fXOwXtVm1YDLzjj9gGq1pHL5mo0ZGAZTooP+k=; b=i6jZXYqsTAD75oXyuM+GKvyc56uTjC8TlTIMemeryNlF34BoSj87CJ6VW9hIwdp2V5 59cnDatDVNb7lRiSgw4jxkoXj0D9HxmcpKRrb2sXfLvYLgiq9z6HcUoQLqIRZgERMUkr WJkfVeu+2yF8wJcfuAIbzSQ+XfR9L6DF/9HzIU03S/eA3iCYv2xRhEbsUvXBDZQPkUdf 750H/38pxcTZOCX9XfVXd6qI+5gFXeCtjlTPqBqlkVlgdgRPlz5rs67JHXYlLJti4CdF YnqL3zlFa+3+ZrXEKWRc47qZHtFYJUiAoNDahXSR3ppcIz1q3CbnbFv1IuSQ2RBKKFF3 K9kw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nvidia.com header.s=n1 header.b=mhvItGsY; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=nvidia.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id s128si4242846oig.204.2020.02.23.20.45.30; Sun, 23 Feb 2020 20:45:43 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@nvidia.com header.s=n1 header.b=mhvItGsY; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=nvidia.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727276AbgBXEpL (ORCPT + 99 others); Sun, 23 Feb 2020 23:45:11 -0500 Received: from hqnvemgate24.nvidia.com ([216.228.121.143]:16566 "EHLO hqnvemgate24.nvidia.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727189AbgBXEpL (ORCPT ); Sun, 23 Feb 2020 23:45:11 -0500 Received: from hqpgpgate101.nvidia.com (Not Verified[216.228.121.13]) by hqnvemgate24.nvidia.com (using TLS: TLSv1.2, DES-CBC3-SHA) id ; Sun, 23 Feb 2020 20:43:54 -0800 Received: from hqmail.nvidia.com ([172.20.161.6]) by hqpgpgate101.nvidia.com (PGP Universal service); Sun, 23 Feb 2020 20:45:10 -0800 X-PGP-Universal: processed; by hqpgpgate101.nvidia.com on Sun, 23 Feb 2020 20:45:10 -0800 Received: from [10.2.163.13] (10.124.1.5) by HQMAIL107.nvidia.com (172.20.187.13) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Mon, 24 Feb 2020 04:45:09 +0000 Subject: Re: [RFC PATCH v3 4/6] media: tegra: Add Tegra210 Video input driver From: Sowjanya Komatineni To: Hans Verkuil , , , , , CC: , , , , References: <1581704608-31219-1-git-send-email-skomatineni@nvidia.com> <1581704608-31219-5-git-send-email-skomatineni@nvidia.com> Message-ID: <12a36c2a-593c-e555-d44e-e2e6c4c1a562@nvidia.com> Date: Sun, 23 Feb 2020 20:45:23 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: X-Originating-IP: [10.124.1.5] X-ClientProxiedBy: HQMAIL101.nvidia.com (172.20.187.10) To HQMAIL107.nvidia.com (172.20.187.13) Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: quoted-printable Content-Language: en-US DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1582519434; bh=FgL2m4fXOwXtVm1YDLzjj9gGq1pHL5mo0ZGAZTooP+k=; h=X-PGP-Universal:Subject:From:To:CC:References:Message-ID:Date: User-Agent:MIME-Version:In-Reply-To:X-Originating-IP: X-ClientProxiedBy:Content-Type:Content-Transfer-Encoding: Content-Language; b=mhvItGsYXSiFtNz1X3+DppRrTX+dFSreZSJx2j4/v3zXgHP8Ggd0Hdj6pvi2ELyH7 QfLmADm0c7h5+P5+3UNFCVSAWL0/6ou70OdYiJIDnWuwgUhPoxcAuoav6wO76qjjy2 OtBvX67nahf+Vn9zGizDtg2jIN6/tqEL/jog+trcZZVg06/5EkjFBoac3V2CKhEihZ 08HY9spxNs0lkvTr5HyvM+jH6C8tveHsTtP/WrA0xbTcbLIiR8EvQ7U8AW/tfn8Rhf 71HujkEU9IiYYq+bk3KQaYba7YB6Sh/j8uZLxyo8XRWvGmX6S7tBJXPaEAiKFByynm Qz09Y9N8qfTvA== Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2/20/20 11:11 AM, Sowjanya Komatineni wrote: > > On 2/20/20 5:33 AM, Hans Verkuil wrote: >> External email: Use caution opening links or attachments >> >> >> (Replying to myself so I can explain this a bit more) >> >> On 2/20/20 1:44 PM, Hans Verkuil wrote: >>>> + >>>> +static int tegra_csi_tpg_channels_alloc(struct tegra_csi *csi) >>>> +{ >>>> +=C2=A0=C2=A0=C2=A0 struct device_node *node =3D csi->dev->of_node; >>>> +=C2=A0=C2=A0=C2=A0 unsigned int port_num; >>>> +=C2=A0=C2=A0=C2=A0 int ret; >>>> +=C2=A0=C2=A0=C2=A0 struct tegra_csi_channel *item; >>>> +=C2=A0=C2=A0=C2=A0 unsigned int tpg_channels =3D csi->soc->csi_max_ch= annels; >>>> + >>>> +=C2=A0=C2=A0=C2=A0 /* allocate CSI channel for each CSI x2 ports */ >>>> +=C2=A0=C2=A0=C2=A0 for (port_num =3D 0; port_num < tpg_channels; port= _num++) { >>>> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 it= em =3D devm_kzalloc(csi->dev, sizeof(*item), GFP_KERNEL); >>> Using devm_*alloc can be dangerous. If someone unbinds the driver, then >>> all memory allocated with devm_ is immediately freed. But if an=20 >>> application >>> still has a filehandle open, then when it closes it it might still=20 >>> reference >>> this already-freed memory. >>> >>> I recommend that you avoid using devm_*alloc for media drivers. >> A good test is to unbind & bind the driver: >> >> cd /sys/devices/platform/50000000.host1x/54080000.vi/driver >> echo -n 54080000.vi >unbind >> echo -n 54080000.vi >bind >> >> First just do this without the driver being used. That already >> gives me 'list_del corruption' kernel messages (list debugging >> is turned on in my kernel). Will fix in v4 to use kzalloc and also proper release v4l2 to make sure=20 unbind/bind works properly. BTW, tegra vi and csi are registered as clients to host1x video driver. So, unbind and bind should be done with host1x video driver "tegra-video" cd /sys/devices/platform/50000000.host1x/tegra-video/driver echo -n tegra-video > unbind echo -n tegra-video > bind >> >> Note that this first test is basically identical to a rmmod/modprobe >> of the driver. But when I compiled the driver as a module it didn't >> create any video device nodes! Nor did I see any errors in the kernel >> log. I didn't pursue this, and perhaps I did something wrong, but it's >> worth taking a look at. >> >> The next step would be to have a video node open with: >> >> v4l2-ctl --sleep 10 >> >> then while it is sleeping unbind the driver and see what happens >> when v4l2-ctl exits. >> >> Worst case is when you are streaming: >> >> v4l2-ctl --stream-mmap >> >> and then unbind. >> >> In general, the best way to get this to work correctly is: >> >> 1) don't use devm_*alloc >> 2) set the release callback of struct v4l2_device and do all freeing=20 >> there. >> 3) in the platform remove() callback you call media_device_unregister() >> =C2=A0=C2=A0=C2=A0 and video_unregister_device(). > Reg 3, in current patch, media_device_unregister is called in=20 > host1x_video_remove > video_unregister_device happens during host1x_video_remove ->=20 > host1x_device_exit -> tegra_vi_exit -> tegra_vi_channels_cleanup > >> It's worth getting this right in this early stage, rather than fixing it >> in the future. >> >> Regards, >> >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Hans