Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp838556pxb; Tue, 19 Oct 2021 14:14:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxnnUmVI8aQvV4WK31+NXrbCnt+nFRg8ykVnxqQh/OVLj2jt+E4/XcTYbJgi5IMnx5XXXq1 X-Received: by 2002:a17:906:3693:: with SMTP id a19mr40395192ejc.237.1634678057903; Tue, 19 Oct 2021 14:14:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634678057; cv=none; d=google.com; s=arc-20160816; b=KL4grU3/rtKQcEYBHLq8WAYx0oZAc+WIe/dWTJpUiickQICpH1EAwgpLwP8TJy9DfZ 6TVV3znRh4FU284s+h/nwuallzR9UQTywKwropwgwto4jkA2BJzOuuU+0DQWbLPrsAGi uShzFvuAxx6RHh1uijuijjeuKqL4GetIff9ucFzL6NJJLsyW4kG58OSJfVXmkxp362aq TgG+88tLk+PrEp1peS3aVkKt9jhOPTU2YoUwMYBH+6uz9lNgXH5ZJP9k4zIC1u21M+Gm qArb/ITsjZFGR/ZWqMcQ3T9EfigZErQvFvfw9h8CSVDnIfUABhTd+buvAaSLcKO7Sgjc Jr/g== 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-transfer-encoding :content-disposition:mime-version:references:mail-followup-to :message-id:subject:cc:to:from:date:dkim-signature; bh=OwyRLUcszsyadBr824dsTniinvYZj7Tk0bJGgvItSRM=; b=EfMSucE0NlJO6IRF+vrmKb53RRDt9iWSokWZ9gBG00KcPrADuYo7F63KlmN+Nt6GRW h7S7ZbMafiL/4Z+6M1xzCxXJgqelAXcD5Lq+5OxSlWiMTzletimoh5csh5rkN33IIlmq W0I0jTNr+qMlXItR+iwWEBvhEZh8QfDTvx3IPaBJbc5Vcx7qeCRveg7o0UZOBFSuMlOr uh+6tqvLlHpNiiHXOybt8omP74pJpd7qDFUAVkHMGZ05bc9B00lrn6NOvnZtb1Ki+JDp X0yA8x1jpVvyFXl1+Wrhz0tKaochSPOqEyhH3MchWJKnmIBx/vf5S3DMLXKfKjg0sayf U62g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ffwll.ch header.s=google header.b=UuFOxeYn; 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 p4si386959edj.221.2021.10.19.14.13.53; Tue, 19 Oct 2021 14:14:17 -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=UuFOxeYn; 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 S231380AbhJSVNz (ORCPT + 99 others); Tue, 19 Oct 2021 17:13:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43940 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231327AbhJSVNy (ORCPT ); Tue, 19 Oct 2021 17:13:54 -0400 Received: from mail-ed1-x529.google.com (mail-ed1-x529.google.com [IPv6:2a00:1450:4864:20::529]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0B41EC061746 for ; Tue, 19 Oct 2021 14:11:41 -0700 (PDT) Received: by mail-ed1-x529.google.com with SMTP id 5so17272356edw.7 for ; Tue, 19 Oct 2021 14:11:40 -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:content-transfer-encoding :in-reply-to; bh=OwyRLUcszsyadBr824dsTniinvYZj7Tk0bJGgvItSRM=; b=UuFOxeYnA6bmCHsx6C3eM8RhW7dy8MiborwVIsDzXcWw30k69JKWDiVtNm6tzfQYNS jKiqu/iSWMz34Bs26vA6cuq5v9Mmweone0oANMssKmhBtWARTGXALWfHR+h8jbZ8pvcy 0krFAiUyPTdfBf/P4H/lPhWeu8aOt+4rtcYRs= 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 :content-transfer-encoding:in-reply-to; bh=OwyRLUcszsyadBr824dsTniinvYZj7Tk0bJGgvItSRM=; b=4qjg42mpZopTE4M3Moq3v11/rW2hNCrGBlVRaj7wg1+zSnTBSrYBOOQKncBSUhvPfu p5jkaQsOXg5hQy7nISrMHwFAP1TteIlJIqFtp+sWNdJYAXn6i2hcugtxXaWDkEX3nJXE fw4dpBdPOe2OdFznY/DgdRpktA9Mniul0FVL6TbFM8v4mUAGthE76y43b2BbTsCjclO4 sML2OS5UlAjLXB9YOTVbXmy2Yux1z/elotxRqmI62rytZstEcidhaw2/Aompnbzea+BM i7URHSGcMwELMImjlN1B5HPtfJSd9JAGpG4z81asTK4AU+VnmhG0OKO60Ifyc8WOZawc tqJw== X-Gm-Message-State: AOAM532D6LKlliiy9+pzNJy2wiIHYrq6TlsuZXk+ahI68G1WL4eV9JTK 8j4sgTDeYYIwcY42DFwVTTOIpg== X-Received: by 2002:a05:6402:2787:: with SMTP id b7mr56873542ede.159.1634677899626; Tue, 19 Oct 2021 14:11:39 -0700 (PDT) Received: from phenom.ffwll.local ([2a02:168:57f4:0:efd0:b9e5:5ae6:c2fa]) by smtp.gmail.com with ESMTPSA id q14sm92905edj.42.2021.10.19.14.11.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Oct 2021 14:11:39 -0700 (PDT) Date: Tue, 19 Oct 2021 23:11:37 +0200 From: Daniel Vetter To: Christian =?iso-8859-1?Q?K=F6nig?= Cc: 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 Subject: Re: [Linaro-mm-sig] [PATCH] dma-buf: add attachments empty check for dma_buf_release Message-ID: Mail-Followup-To: Christian =?iso-8859-1?Q?K=F6nig?= , 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> <8cca7188-6484-d3a5-2b87-400f6500e742@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <8cca7188-6484-d3a5-2b87-400f6500e742@gmail.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 05:37:27PM +0200, Christian K?nig wrote: > > > Am 19.10.21 um 14:41 schrieb Daniel Vetter: > > 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. > > Well I would call that overkill. I think if done right the object debug stuff should be able to give you a backtrace. Which might be useful if you have a dma-buf heaps design where you really have no clue why a buffer was allocated/attached without some hints. > > 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). > > You also don't need the WARN(!list_empty...) because a few line below we > already have a "WARN_ON(!list_empty(&dmabuf->attachments));". Yeah this patch here alone isn't really that useful I think. Maybe we could add the dmabuf->exp_name or so to that warning, but otherwise the info printed here isn't all that useful for debugging. Grabbing a backtrace of the allocator or attacher otoh should fairly immedialy point at the buggy code. -Daniel > > Christian. > > > -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