Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp907770rdh; Sun, 24 Sep 2023 18:09:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGfDN1xmRFLhe+6kN/uxmU1jjF/WCeOicG89BQW9ZIgEWv7Vwev4swL0HhE/0HyyUB6IGn3 X-Received: by 2002:aa7:8885:0:b0:68c:5cec:30d4 with SMTP id z5-20020aa78885000000b0068c5cec30d4mr4119499pfe.27.1695604164205; Sun, 24 Sep 2023 18:09:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695604164; cv=none; d=google.com; s=arc-20160816; b=E8ePoijo4/7S4z9doSy2WjE60R56+vVSiLKEa5P7ldezY2efOfjxrl+l3oY9SsCvn2 Ry+/UiUPpVNk8I6qXx0nFtOY7xM1VqAE9ok0EaH78iwx8KcbSVNIuPwB3bApbD04D7Oy mRF80YWv/h6hv+9p7a+FQR4wenONXVcFowaJfquklfS0dYN1v0jfExB4k84enl1tpBOk BW0KhAZCkCPa/h+8NvMUU/H429ZcgPF56XaUjmtGuecsZG+zpCLs/h2ujdm2TMzqMsak P32qlk2zF1O5UFZ0IX0XKF9RM/tHpI0iQJA7Hp9HSTrVCtE6L5ps+6hHJTCITDbtK3hO viwQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:references :cc:to:from:subject:user-agent:mime-version:date:message-id; bh=KW0tePw6M0461eHKeWqF2mI6WLSEwoot9Y8q+HSoItA=; fh=hVzLXeIwPcPCmCjZQeRfdioqcw1LrJGG+t1qc6Vm0zc=; b=qJbp10QbHeiwr5lQDJ3c4l9dD0VnfMVVpc8oQc05GsdRqmNBhdaQ/0Hd8p9U1AdYBn uWDs0ez9wiIFnlK8e4Rr9FGMBrLm1tqeFl6DqmpoTnRT+yT39BLP242hnwYzWSVikkSW SKDs4r2knpXrFNtHo5sp0GbjGijD5HrfMC4jkbcnb/SL3WW8AMQbICLzk8eZVJeMP029 XSx5uVzVvPq0X2jAj2cO2nmCJLNR1xts+NymF7ngxkVHaWwf3QnR11LAmDYhjS6F7bvI g8eDga+ezod/tkla8Fs5gEqpJqm5dkiAUFXy6+lgNlhehXucDjRZXtmzWqX157xL8VOI Ub2Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id s7-20020a056a00178700b00690258a9766si9446904pfg.373.2023.09.24.18.09.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 Sep 2023 18:09:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 7D87580B7AC5; Sun, 24 Sep 2023 18:09:21 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231397AbjIYBJT (ORCPT + 99 others); Sun, 24 Sep 2023 21:09:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34490 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229514AbjIYBJS (ORCPT ); Sun, 24 Sep 2023 21:09:18 -0400 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 03100C4; Sun, 24 Sep 2023 18:09:11 -0700 (PDT) Received: from kwepemi500020.china.huawei.com (unknown [172.30.72.53]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4Rv4TY5XJwz15NRR; Mon, 25 Sep 2023 09:06:57 +0800 (CST) Received: from [10.67.121.175] (10.67.121.175) by kwepemi500020.china.huawei.com (7.221.188.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.31; Mon, 25 Sep 2023 09:09:09 +0800 Message-ID: <282d736e-ce03-b09d-8ff2-b3824f01eb79@huawei.com> Date: Mon, 25 Sep 2023 09:09:06 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: Re: [PATCH] dmaengine: virt-dma : fix vchan error on multi-thread From: Jie Hai To: CC: , References: <20230720114212.51224-1-haijie1@huawei.com> In-Reply-To: <20230720114212.51224-1-haijie1@huawei.com> Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [10.67.121.175] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To kwepemi500020.china.huawei.com (7.221.188.8) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-2.2 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Sun, 24 Sep 2023 18:09:21 -0700 (PDT) Hi, Vkoul, Kindly ping... Thanks, Jie Hai On 2023/7/20 19:42, Jie Hai wrote: > List desc_allocated was introduced for the case of a transfer > submitted multiple times. But elegating descriptors on the list > causes other problems. > > For example, in the multi-thread scenario, which tasks are > continuously created and submitted by each thread. If one of > the threads calls dmaengine_terminate_all, for dirvers using > vchan_get_all_descriptors, all descriptors will be freed. If > there's another thread submitting a transfer A by > vchan_tx_submit, the following results may be generated: > 1. desc A is freeing -> visit wrong address of node prep/next. > 2. desc A is freed -> visit invalid address of A. > > In the above case, calltrace is generated and the system is > suspended. This can be tested by dmatest. > > This patch removes desc_allocated from vchan_get_all_descriptors, > and add new function 'vchan_get_all_allocated_descs' to get all > descriptors ever allocated. > > And apply vchan_get_all_allocated_descs to free chan resource and > vchan_get_all_descriptors to terminate all transfers, respectively. > This avoids freeing up descriptors in use by other threads. > > Signed-off-by: Jie Hai > --- > drivers/dma/fsl-edma-common.c | 2 +- > drivers/dma/fsl-qdma.c | 2 +- > drivers/dma/sf-pdma/sf-pdma.c | 2 +- > drivers/dma/virt-dma.h | 20 ++++++++++++++++++-- > 4 files changed, 21 insertions(+), 5 deletions(-) > > diff --git a/drivers/dma/fsl-edma-common.c b/drivers/dma/fsl-edma-common.c > index a06a1575a2a5..c6d2e54ab85d 100644 > --- a/drivers/dma/fsl-edma-common.c > +++ b/drivers/dma/fsl-edma-common.c > @@ -674,7 +674,7 @@ void fsl_edma_free_chan_resources(struct dma_chan *chan) > if (edma->drvdata->dmamuxs) > fsl_edma_chan_mux(fsl_chan, 0, false); > fsl_chan->edesc = NULL; > - vchan_get_all_descriptors(&fsl_chan->vchan, &head); > + vchan_get_all_allocated_descs(&fsl_chan->vchan, &head); > fsl_edma_unprep_slave_dma(fsl_chan); > spin_unlock_irqrestore(&fsl_chan->vchan.lock, flags); > > diff --git a/drivers/dma/fsl-qdma.c b/drivers/dma/fsl-qdma.c > index eddb2688f234..5ffd7ba92058 100644 > --- a/drivers/dma/fsl-qdma.c > +++ b/drivers/dma/fsl-qdma.c > @@ -311,7 +311,7 @@ static void fsl_qdma_free_chan_resources(struct dma_chan *chan) > LIST_HEAD(head); > > spin_lock_irqsave(&fsl_chan->vchan.lock, flags); > - vchan_get_all_descriptors(&fsl_chan->vchan, &head); > + vchan_get_all_allocated_descs(&fsl_chan->vchan, &head); > spin_unlock_irqrestore(&fsl_chan->vchan.lock, flags); > > vchan_dma_desc_free_list(&fsl_chan->vchan, &head); > diff --git a/drivers/dma/sf-pdma/sf-pdma.c b/drivers/dma/sf-pdma/sf-pdma.c > index d1c6956af452..f35dc68e1a7c 100644 > --- a/drivers/dma/sf-pdma/sf-pdma.c > +++ b/drivers/dma/sf-pdma/sf-pdma.c > @@ -144,7 +144,7 @@ static void sf_pdma_free_chan_resources(struct dma_chan *dchan) > sf_pdma_disable_request(chan); > kfree(chan->desc); > chan->desc = NULL; > - vchan_get_all_descriptors(&chan->vchan, &head); > + vchan_get_all_allocated_descs(&chan->vchan, &head); > sf_pdma_disclaim_chan(chan); > spin_unlock_irqrestore(&chan->vchan.lock, flags); > vchan_dma_desc_free_list(&chan->vchan, &head); > diff --git a/drivers/dma/virt-dma.h b/drivers/dma/virt-dma.h > index e9f5250fbe4d..65b4f3bdecf7 100644 > --- a/drivers/dma/virt-dma.h > +++ b/drivers/dma/virt-dma.h > @@ -177,13 +177,29 @@ static inline struct virt_dma_desc *vchan_next_desc(struct virt_dma_chan *vc) > static inline void vchan_get_all_descriptors(struct virt_dma_chan *vc, > struct list_head *head) > { > - list_splice_tail_init(&vc->desc_allocated, head); > list_splice_tail_init(&vc->desc_submitted, head); > list_splice_tail_init(&vc->desc_issued, head); > list_splice_tail_init(&vc->desc_completed, head); > list_splice_tail_init(&vc->desc_terminated, head); > } > > +/** > + * vchan_get_all_allocated_descs - obtain all descriptors > + * @vc: virtual channel to get descriptors from > + * @head: list of descriptors found > + * > + * vc.lock must be held by caller > + * > + * Removes all descriptors from internal lists, and provides a list of all > + * descriptors found > + */ > +static inline void vchan_get_all_allocated_descs(struct virt_dma_chan *vc, > + struct list_head *head) > +{ > + list_splice_tail_init(&vc->desc_allocated, head); > + vchan_get_all_descriptors(vc, head); > +} > + > static inline void vchan_free_chan_resources(struct virt_dma_chan *vc) > { > struct virt_dma_desc *vd; > @@ -191,7 +207,7 @@ static inline void vchan_free_chan_resources(struct virt_dma_chan *vc) > LIST_HEAD(head); > > spin_lock_irqsave(&vc->lock, flags); > - vchan_get_all_descriptors(vc, &head); > + vchan_get_all_allocated_descs(vc, &head); > list_for_each_entry(vd, &head, node) > dmaengine_desc_clear_reuse(&vd->tx); > spin_unlock_irqrestore(&vc->lock, flags);