Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp555855ybk; Wed, 20 May 2020 06:30:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwXnPfQ1pLi0Zi1t6ZMtjXMIN47h9cqlGi1WatufwMyOOp2HlgUrI1oB7RY2IRUWnmT7p6d X-Received: by 2002:a17:906:2a45:: with SMTP id k5mr3618238eje.149.1589981429864; Wed, 20 May 2020 06:30:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589981429; cv=none; d=google.com; s=arc-20160816; b=SggZIDyXSK3DQ2k2Vh6tc1rUdNIMeHkh+zRFmp2wx/6v8OeOT86olChHrURBFikDVr AAQ7UZb7Wmm/1pft2Bh3d0tvNW1o4w3MaYWPJ6hzYm3rIQ2SvoD8k/Cx+AM9NT3BZl3i +dl3Uy/nZC0GwZ36a/NgsoTNtdCLBrPs7px0ZU/hYO/TduuxtryX1GaPwmgMXoDvDf2A IpQCXOFZfBDPf2tGL8n4/NcUiElUsSzKQM0/TGcDIqpW+68CRXiFhCECXbaRvu2kJ81s makGW0nqH9yigXvt/6XrPxtSq5W4ZH5YtOtstzP22QU8nrbDtUxtPNTbqwdFecICRg7p lLbQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:cms-type:content-language :content-transfer-encoding:in-reply-to:mime-version:user-agent:date :message-id:from:cc:to:subject:dkim-signature:dkim-filter; bh=wt7ZAzus8jfvwuKXh+8jgmyBochZXbmDRoFuEwJS3xI=; b=C48JqOvhX3UlBQAxpEgaiITBSgLp52hS5lkK2j3SyT+hwzFqAA36RTow81V8n1RQ8p O0QBhx96ntmOQTh/9ewlCu3P+OD6Qo49Ceqd5jpbRCvdlwS0ppb4kyUFRMhNpf81NhCQ WOnSTXzSjVau+UoEnT1+KihiHUqIvaM6544mmTYGhHdzzTMR1rHvZSxq48hyabaoMwX9 /WLYONaD2PQG3K2RZS2bmOvRa5y3Vf8MF8xd2qoGSv10W96R9qLMaGFWWle8jiqE0EKi UgkHYS8pDi7n33tDo66DsU9cxi/7T1k7OlXqm2tQjTsAV8SM77texgTbl3OX/3VBbeh2 pSVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@samsung.com header.s=mail20170921 header.b=GaEH5Uaq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=samsung.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z3si1425152eda.578.2020.05.20.06.30.06; Wed, 20 May 2020 06:30:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@samsung.com header.s=mail20170921 header.b=GaEH5Uaq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=samsung.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726844AbgETN2O (ORCPT + 99 others); Wed, 20 May 2020 09:28:14 -0400 Received: from mailout1.w1.samsung.com ([210.118.77.11]:53080 "EHLO mailout1.w1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726436AbgETN2N (ORCPT ); Wed, 20 May 2020 09:28:13 -0400 Received: from eucas1p2.samsung.com (unknown [182.198.249.207]) by mailout1.w1.samsung.com (KnoxPortal) with ESMTP id 20200520132811euoutp01ef1d8fa2ef0da7120e101cb72fdb751d~Qv4_YBE-20040800408euoutp01S for ; Wed, 20 May 2020 13:28:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout1.w1.samsung.com 20200520132811euoutp01ef1d8fa2ef0da7120e101cb72fdb751d~Qv4_YBE-20040800408euoutp01S DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1589981291; bh=wt7ZAzus8jfvwuKXh+8jgmyBochZXbmDRoFuEwJS3xI=; h=Subject:To:Cc:From:Date:In-Reply-To:References:From; b=GaEH5UaqVxhEfzkhvwVHUPCfT6w9ZAb2t34zEKdrqpzpbzHcmOZ/05U0FrQIKcds7 KGWq31kO24Cej4iRVPjZIK7NbkhYI969OWbdnPHOZfJfJul89fGuWP6p+Ksq30lk1e U74CwRoBUfdeDdLEvbYjsgurdnF42vDtTXI1TkvU= Received: from eusmges2new.samsung.com (unknown [203.254.199.244]) by eucas1p1.samsung.com (KnoxPortal) with ESMTP id 20200520132811eucas1p17d3caed7aaf8e435b5ae7b144d6bb35e~Qv4_LSZDa1895018950eucas1p1A; Wed, 20 May 2020 13:28:11 +0000 (GMT) Received: from eucas1p2.samsung.com ( [182.198.249.207]) by eusmges2new.samsung.com (EUCPMTA) with SMTP id D3.2E.60679.B6035CE5; Wed, 20 May 2020 14:28:11 +0100 (BST) Received: from eusmtrp2.samsung.com (unknown [182.198.249.139]) by eucas1p1.samsung.com (KnoxPortal) with ESMTPA id 20200520132811eucas1p155ffc4369d7cf1cf7047cca91361cee1~Qv49wwr_O0413504135eucas1p1d; Wed, 20 May 2020 13:28:11 +0000 (GMT) Received: from eusmgms2.samsung.com (unknown [182.198.249.180]) by eusmtrp2.samsung.com (KnoxPortal) with ESMTP id 20200520132811eusmtrp23a14ae78a3dd1695ec1d268558f9c045~Qv49wIN3U0288702887eusmtrp28; Wed, 20 May 2020 13:28:11 +0000 (GMT) X-AuditID: cbfec7f4-0e5ff7000001ed07-cd-5ec5306bce29 Received: from eusmtip1.samsung.com ( [203.254.199.221]) by eusmgms2.samsung.com (EUCPMTA) with SMTP id A2.00.07950.B6035CE5; Wed, 20 May 2020 14:28:11 +0100 (BST) Received: from [106.210.88.143] (unknown [106.210.88.143]) by eusmtip1.samsung.com (KnoxPortal) with ESMTPA id 20200520132810eusmtip1cf37ae1106ea84b84b45fea842669ed2~Qv49XL-8d2058120581eusmtip1P; Wed, 20 May 2020 13:28:10 +0000 (GMT) Subject: Re: Bad kfree of dma_parms in v5.7-rc5 To: Ulf Hansson , Tomi Valkeinen Cc: Linux Media Mailing List , Mauro Carvalho Chehab , LKML , Greg Kroah-Hartman From: Marek Szyprowski Message-ID: <9f72aeab-8d63-7fc1-d5ff-7d5c4f11012b@samsung.com> Date: Wed, 20 May 2020 15:28:10 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.8.0 MIME-Version: 1.0 In-Reply-To: Content-Transfer-Encoding: 7bit Content-Language: en-US X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFlrKKsWRmVeSWpSXmKPExsWy7djP87rZBkfjDNpvqVk0L17PZnF51xw2 i54NW1ktlm36w2Sxfv4tNovja8Md2Dw2repk87hzbQ+bx/65a9g9jt/YzuTxeZNcAGsUl01K ak5mWWqRvl0CV8bNhc1MBefkK37+vs7awDhVqouRk0NCwETi8eUpjF2MXBxCAisYJaa+ecAK 4XxhlLh2dhkThPOZUWLGs/1sMC1rby4Fs4UEljNKrP/sDFH0nlFi54mFQAkODmEBfYmzM+xB akQEgiT+7LgKtoJZ4ACjxIaec4wgCTYBQ4mut11gg3gF7CSebJ/GCmKzCKhKHGxZCxYXFYiV OL14MyNEjaDEyZlPWEBsToFAibe3/zOD2MwC8hLb386BssUlbj2ZD3a1hMAqdommX9tYIa52 kfj05hUThC0s8er4FnYIW0bi/06YhmZGiYfn1rJDOD2MEpebZjBCVFlL3Dn3C+w1ZgFNifW7 9CHCjhITz85nBwlLCPBJ3HgrCHEEn8SkbdOZIcK8Eh1tQhDVahKzjq+DW3vwwiXmCYxKs5C8 NgvJO7OQvDMLYe8CRpZVjOKppcW56anFRnmp5XrFibnFpXnpesn5uZsYgYnn9L/jX3Yw7vqT dIhRgINRiYe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dfbSkjOld+Kvzf/MrOTxYKpgtOGh huZXhx5laYtM2V7x9ifP/dTzNQfu/mhw//cmcWqMwMK6o3ZJvV9nTuJ6GZ2mxFKckWioxVxU nAgAVmKblMkCAAA= X-CMS-MailID: 20200520132811eucas1p155ffc4369d7cf1cf7047cca91361cee1 X-Msg-Generator: CA Content-Type: text/plain; charset="utf-8" X-RootMTR: 20200520090109eucas1p17270805f81f6958cd5084a7b910efc6c X-EPHeader: CA CMS-TYPE: 201P X-CMS-RootMailID: 20200520090109eucas1p17270805f81f6958cd5084a7b910efc6c References: <227465a5-c6e6-5b4d-abbd-7789727843a6@ti.com> <29a21e64-a63f-6721-c938-d713488767c1@samsung.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Ulf, On 20.05.2020 15:12, Ulf Hansson wrote: > + Greg > > On Wed, 20 May 2020 at 14:54, Marek Szyprowski wrote: >> On 20.05.2020 14:43, Tomi Valkeinen wrote: >>> On 20/05/2020 12:22, Marek Szyprowski wrote: >>>> On 20.05.2020 11:18, Tomi Valkeinen wrote: >>>>> On 20/05/2020 12:13, Marek Szyprowski wrote: >>>>>> On 20.05.2020 11:00, Tomi Valkeinen wrote: >>>>>>> Commit 9495b7e92f716ab2bd6814fab5e97ab4a39adfdd ("driver core: >>>>>>> platform: Initialize dma_parms for platform devices") v5.7-rc5 causes >>>>>>> at least some v4l2 platform drivers to break when freeing resources. >>>>>>> >>>>>>> E.g. drivers/media/platform/ti-vpe/cal.c uses >>>>>>> vb2_dma_contig_set_max_seg_size() and >>>>>>> vb2_dma_contig_clear_max_seg_size() to manage the dma_params, and >>>>>>> similar pattern is seen in other drivers too. >>>>>>> >>>>>>> After 9495b7e92f716ab2, vb2_dma_contig_set_max_seg_size() will not >>>>>>> allocate anything, but vb2_dma_contig_clear_max_seg_size() will still >>>>>>> kfree the dma_params. >>>>>>> >>>>>>> I'm not sure what's the proper fix here. A flag somewhere to indicate >>>>>>> that vb2_dma_contig_set_max_seg_size() did allocate, and thus >>>>>>> vb2_dma_contig_clear_max_seg_size() must free? >>>>>>> >>>>>>> Or drop the kzalloc and kfree totally, if dma_params is now supposed >>>>>>> to always be there? >>>>>> Thanks for reporting this issue! >>>>>> >>>>>> Once the mentioned commit has been merged, the code should assume that >>>>>> the platform devices does have a struct dma_params allocated, so the >>>>>> proper fix is to alloc dma_params only if the bus is not a platform >>>>>> bus: >>>>>> >>>>>> if (!dev_is_platform(dev) && !dev->dma_parms) { >>>>>> dev->dma_parms = kzalloc(sizeof(*dev->dma_parms), GFP_KERNEL); >>>>>> >>>>>> same check for the free path. >>>>> There is also "amba: Initialize dma_parms for amba devices". And the >>>>> commit message says PCI devices do this too. >>>>> >>>>> Guessing this based on the device type doesn't sound like a good idea >>>>> to me. >>>> Indeed. Then replace the allocation with a simple check for NULL >>>> dma_parms and return an error in such case. This should be enough for >>>> v5.8. Later we can simply get rid of those helpers and inline setting >>>> max segment size directly to the drivers. > That seems like a good idea, in the long run. > >>> Is that valid either? Then we assume that dma_parms is always set up >>> by someone else. That's true for platform devices and apparently some >>> other devices, but is it true for all devices now? >> # git grep vb2_dma_contig_set_max_seg_size | wc -l >> >> 18 >> >> I've checked all clients of the vb2_dma_contig_set_max_seg_size >> function. There are only 9 drivers, all of them are platform device >> drivers. We don't care about off-tree users, so the proposed approach is >> imho fine. > Thanks for reporting and for looking into this. I apologize for the mess! > > There is one case, where the above solution could be a problem (unless > I am wrong). That is, s5p_mfc_configure_2port_memory() that calls > s5p_mfc_alloc_memdev(), which allocates/initializes an internal struct > *device. Thus, this doesn't have the dev->dma_parms > allocated/assigned. Indeed, this one will fail. > In other words, we would need to manage alloc/free for the > dev->dma_parms to have a complete fix. Maybe in > s5p_mfc_configure|unconfigure_2port_memory()!? That would be the best place to allocate it. > Additionally, I think reverting the offending commit, as discussed > above, could cause even more issues, as it's even included for > v5.6-stable kernels. I will go through all cases, more carefully this > time, of how ->dma_parms is managed, to be sure there are no more > conflicting cases. I've already posted a fix for ExynosDRM driver, which is also affected: https://patchwork.kernel.org/patch/11559965/ Best regards -- Marek Szyprowski, PhD Samsung R&D Institute Poland