Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp419022pxb; Tue, 19 Oct 2021 05:44:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw4WxC1O8gDDvAgLYwF+JhJ6b/fyh3elta3AIhkNTTkn4GkZEemg9rDhPUv9y5UdmSCD1Qb X-Received: by 2002:a05:6402:270f:: with SMTP id y15mr52878172edd.126.1634647442681; Tue, 19 Oct 2021 05:44:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634647442; cv=none; d=google.com; s=arc-20160816; b=oY0Twdo2OOec3+JJ14J84MlixxeTS7I3pu7thvsg8ItX9Lhw8v+ZP1Ef4P0AenyF80 Or/5wHZSgQz+TG/GtJ4iJL1HARHoUTakQ8mNxZ67CZwm7x6sj6mPIrYjSqtAC1qLFM15 H/Tw3PxTnsjRi0rUPBYO3dGtSMlB3Smncxpi9N80nR7P8Ow0HfNiIrEDO5/9VP+vuz+v mXzU5Ke0smh2lfFt1hukza24erMvukbj0pITnrIHJlKGl9EECQvgHaO//9ERKH2Cz72k egRpgDGOJoifnts6ooiAy9riT3BLnFIwXJ+de3otPDn3hmELXemiINxwlVdDd8je1ctx sJMQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:mail-followup-to:message-id:subject:cc:to:from:date :dkim-signature; bh=EHf1d6jYC47N493Pv/6pFABfQdMKx4e8O3xhKroDPbE=; b=iHmt+em65kleL/o6BN2ZFqZGUB3fJkZXfnEP6uyGPywfiyJbqmpAFlef1hUVFKDo8w eNEbN84KMCjghxt3osIzl5Iwgs/Ux4CyVO3x7Jx3mG5xbjGIWd7yF7AfofaDg58d2vYe EVjAhCUW3UPhPtzMOXy8LXKz+N0Bi1f9eLJReOp9MUNIL9oPuvAKlbOoFc8RSqhtWMBg wZ4oWS1oOisiGmuspzYYV/m91gv8/msRfNbJi31R+s1XiuGfBSECzgKnOuzcPKiUpdIq xKimT7KgiVTfLKu9JJoTl2W7pTLmq8lxdMfLn4Z1S9xBsFla3IJzVZtEYxUkTqks/QfI 9WOA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ffwll.ch header.s=google header.b=NyuJgkOE; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d7si22470393edy.224.2021.10.19.05.43.39; Tue, 19 Oct 2021 05:44:02 -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=@ffwll.ch header.s=google header.b=NyuJgkOE; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230231AbhJSMnc (ORCPT + 99 others); Tue, 19 Oct 2021 08:43:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39820 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230514AbhJSMnb (ORCPT ); Tue, 19 Oct 2021 08:43:31 -0400 Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [IPv6:2a00:1450:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CD589C061745 for ; Tue, 19 Oct 2021 05:41:18 -0700 (PDT) Received: by mail-ed1-x532.google.com with SMTP id z20so12218624edc.13 for ; Tue, 19 Oct 2021 05:41:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ffwll.ch; s=google; h=date:from:to:cc:subject:message-id:mail-followup-to:references :mime-version:content-disposition:in-reply-to; bh=EHf1d6jYC47N493Pv/6pFABfQdMKx4e8O3xhKroDPbE=; b=NyuJgkOEqVr2HgSuFzucrmFl6uCC47sSvbC4sO8TJI5tKjSPUp2CuXuUVzlYed/0Wd UuwdEFSyvQRiZn4dT7Vi6Yr2OGeienXD0cr3JRJIZxso6VZBlHhPqR4DRBDUcM22HpyY aTuV8ClK37PNqKNrFn6vFa4AlhIToFy9MbcfY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id :mail-followup-to:references:mime-version:content-disposition :in-reply-to; bh=EHf1d6jYC47N493Pv/6pFABfQdMKx4e8O3xhKroDPbE=; b=vhlIAYWBhP36HiZg7+RCU70GG+wWOzkJjYpytOaFUab8iVIFqdJAdxbtm9FS5yGKG6 9MINJzp/JzRkPvfNTM0wt8LfDSgiqgPP+m9cOwbmsa5TyG7uDUT8sijlhT9ibBF3qcyc 4ehLRfXIrnPHdKgBez6g721cHWmwZzmcDu9LG/qYmrdqKJH1xa28IeiKBPa6YWR7KGoa I6FOYqR3SZafywIPCnMaGAHhgyIifvoVXrL/ycJUj5euPWCkuNPFc8dmujy0H9MtTHtF +B6NRIBNNUPumhqwBbzJ56JBGp5LK8YX5Syc/mtOmOO6IkZHNII9LbTzaUKAOcYSI96W +CRA== X-Gm-Message-State: AOAM530Y5BON5UK0w93rTFVD8NpbtwXJYurlJzh+yvC6WDnujiZz9h3L ci+AJfdzR6bEM1KcDQ6aMf9yTA== X-Received: by 2002:a05:6402:84d:: with SMTP id b13mr54514480edz.110.1634647277354; Tue, 19 Oct 2021 05:41:17 -0700 (PDT) Received: from phenom.ffwll.local ([2a02:168:57f4:0:efd0:b9e5:5ae6:c2fa]) by smtp.gmail.com with ESMTPSA id p23sm12148807edw.94.2021.10.19.05.41.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Oct 2021 05:41:16 -0700 (PDT) Date: Tue, 19 Oct 2021 14:41:14 +0200 From: Daniel Vetter To: guangming.cao@mediatek.com Cc: Sumit Semwal , Christian =?iso-8859-1?Q?K=F6nig?= , Matthias Brugger , "open list:DMA BUFFER SHARING FRAMEWORK" , "open list:DMA BUFFER SHARING FRAMEWORK" , "moderated list:DMA BUFFER SHARING FRAMEWORK" , open list , "moderated list:ARM/Mediatek SoC support" , "moderated list:ARM/Mediatek SoC support" , wsd_upstream@mediatek.com Subject: Re: [PATCH] dma-buf: add attachments empty check for dma_buf_release Message-ID: Mail-Followup-To: guangming.cao@mediatek.com, Sumit Semwal , Christian =?iso-8859-1?Q?K=F6nig?= , Matthias Brugger , "open list:DMA BUFFER SHARING FRAMEWORK" , "open list:DMA BUFFER SHARING FRAMEWORK" , "moderated list:DMA BUFFER SHARING FRAMEWORK" , open list , "moderated list:ARM/Mediatek SoC support" , "moderated list:ARM/Mediatek SoC support" , wsd_upstream@mediatek.com References: <20211019122345.160555-1-guangming.cao@mediatek.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20211019122345.160555-1-guangming.cao@mediatek.com> X-Operating-System: Linux phenom 5.10.0-8-amd64 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Oct 19, 2021 at 08:23:45PM +0800, guangming.cao@mediatek.com wrote: > From: Guangming Cao > > Since there is no mandatory inspection for attachments in dma_buf_release. > There will be a case that dma_buf already released but attachment is still > in use, which can points to the dmabuf, and it maybe cause > some unexpected issues. > > With IOMMU, when this cases occurs, there will have IOMMU address > translation fault(s) followed by this warning, > I think it's useful for dma devices to debug issue. > > Signed-off-by: Guangming Cao This feels a lot like hand-rolling kobject debugging. If you want to do this then I think adding kobject debug support to dma_buf/dma_buf_attachment would be better than hand-rolling something bespoke here. Also on the patch itself: You don't need the trylock. For correctly working code non one else can get at the dma-buf, so no locking needed to iterate through the attachment list. For incorrect code the kernel will be on fire pretty soon anyway, trying to do locking won't help :-) And without the trylock we can catch more bugs (e.g. if you also forgot to unlock and not just forgot to detach). -Daniel > --- > drivers/dma-buf/dma-buf.c | 23 +++++++++++++++++++++++ > 1 file changed, 23 insertions(+) > > diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c > index 511fe0d217a0..672404857d6a 100644 > --- a/drivers/dma-buf/dma-buf.c > +++ b/drivers/dma-buf/dma-buf.c > @@ -74,6 +74,29 @@ static void dma_buf_release(struct dentry *dentry) > */ > BUG_ON(dmabuf->cb_shared.active || dmabuf->cb_excl.active); > > + /* attachment check */ > + if (dma_resv_trylock(dmabuf->resv) && WARN(!list_empty(&dmabuf->attachments), > + "%s err, inode:%08lu size:%08zu name:%s exp_name:%s flags:0x%08x mode:0x%08x, %s\n", > + __func__, file_inode(dmabuf->file)->i_ino, dmabuf->size, > + dmabuf->name, dmabuf->exp_name, > + dmabuf->file->f_flags, dmabuf->file->f_mode, > + "Release dmabuf before detach all attachments, dump attach:\n")) { > + int attach_cnt = 0; > + dma_addr_t dma_addr; > + struct dma_buf_attachment *attach_obj; > + /* dump all attachment info */ > + list_for_each_entry(attach_obj, &dmabuf->attachments, node) { > + dma_addr = (dma_addr_t)0; > + if (attach_obj->sgt) > + dma_addr = sg_dma_address(attach_obj->sgt->sgl); > + pr_err("attach[%d]: dev:%s dma_addr:0x%-12lx\n", > + attach_cnt, dev_name(attach_obj->dev), dma_addr); > + attach_cnt++; > + } > + pr_err("Total %d devices attached\n\n", attach_cnt); > + dma_resv_unlock(dmabuf->resv); > + } > + > dmabuf->ops->release(dmabuf); > > if (dmabuf->resv == (struct dma_resv *)&dmabuf[1]) > -- > 2.17.1 > -- Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch