Received: by 10.223.164.202 with SMTP id h10csp595215wrb; Tue, 14 Nov 2017 06:39:08 -0800 (PST) X-Google-Smtp-Source: AGs4zMYDup6igI4weYKEwzV/pyOdiHEbe0nL8ZI8qK4dZAv00gYr8wQ4etQvSN0tqYE9iULMsjLf X-Received: by 10.99.122.3 with SMTP id v3mr11926624pgc.394.1510670348128; Tue, 14 Nov 2017 06:39:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510670348; cv=none; d=google.com; s=arc-20160816; b=Rab3utigJJf6IYCInsszW8/5t6ss3+MZkSiR+sH0RivzhfV3b0bAhKy+FmY6hQZcfs TJa87ylhuEWdgFKctSZhn6/pqqTCvoqxuPDSTDu88wSUfS4wg6Sp47AKP3MQsMmZVQXP 9gvX7ZEI62amfjXRc0GHT/HskcopN9s4rbSbDWg7FsvKID5d4+E0YCNpZsxWpiNGuLMy U1MRHXVeqF2uM6yUpFaNUweq6x07u08189w96lN/oYQ60vA4q4fKBsbehBUmFaAYHDjH P85DthUacvfe6Ii2JFZBrXXYeMfmLgT9+hNO+Ay9ZU+75hxTox9OBXjeXkCD96nxIN9L 1jGQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=QflZ5LNtNBglMESit1RMQZURV0PCurkYtbnSCKfKl/Y=; b=LZQlaQz3F6yOqILxmnr507DhbPripihPf+KA5utzGjwv4ZnIKWCd+rKwCYtKiwsxIr zewDK/zCbs5l6O53ssqyxsRp943FxrvvE43m9Ln4OZhJbFu/Kz3G7AEYQUz7/IJA2OrA 1lu9ewbR11D59NZ4GaAqnZd/sa4u5R/iSkobYWR5LDTvdsTwQIIeM6kgCSpkYDkb06zO guxC1ir+T/Vzkp3aVy8L7J6By6cdNSUED7oZ4M0h/aT4YWH/tv+4E/+ZZw3fl1kA8K1B qQcJEjV/8ptVZQx5/QcAgYsUIBUnN+DvhDuFrJJMmdWn6cyIkNO50kBRWhxs0WI6vGdG 3t9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=aLh7YrhR; 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=QUARANTINE sp=NONE dis=NONE) header.from=ti.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 189si17672115pfu.371.2017.11.14.06.38.55; Tue, 14 Nov 2017 06:39:08 -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=@ti.com header.s=ti-com-17Q1 header.b=aLh7YrhR; 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=QUARANTINE sp=NONE dis=NONE) header.from=ti.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755601AbdKNOgN (ORCPT + 88 others); Tue, 14 Nov 2017 09:36:13 -0500 Received: from lelnx194.ext.ti.com ([198.47.27.80]:17820 "EHLO lelnx194.ext.ti.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755543AbdKNOcQ (ORCPT ); Tue, 14 Nov 2017 09:32:16 -0500 Received: from dlelxv90.itg.ti.com ([172.17.2.17]) by lelnx194.ext.ti.com (8.15.1/8.15.1) with ESMTP id vAEEVdV4029738; Tue, 14 Nov 2017 08:31:39 -0600 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ti.com; s=ti-com-17Q1; t=1510669899; bh=yXsyK7/8gOvzhwgJEbjVtGdZuA7ZnKp6Ytuz9/sScUE=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=aLh7YrhRyzgKjq4n8s4H3jlWzTabVNPDDtU1JACaSWCvJJnk3zAVhbyu4OLRcQlLF VRnPZWeFX57peMamB6Tjq8/Wdjsaa0ZtvKbJJEoNboKBNULkeKWjWu91m91cscvLpf PpqRI4WxSRVcqb6QjiRaPG0o4XyEGUsmJS6fHccM= Received: from DFLE106.ent.ti.com (dfle106.ent.ti.com [10.64.6.27]) by dlelxv90.itg.ti.com (8.14.3/8.13.8) with ESMTP id vAEEVdiG032591; Tue, 14 Nov 2017 08:31:39 -0600 Received: from DFLE100.ent.ti.com (10.64.6.21) by DFLE106.ent.ti.com (10.64.6.27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.1.845.34; Tue, 14 Nov 2017 08:31:38 -0600 Received: from dlep32.itg.ti.com (157.170.170.100) by DFLE100.ent.ti.com (10.64.6.21) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_RSA_WITH_AES_256_CBC_SHA) id 15.1.845.34 via Frontend Transport; Tue, 14 Nov 2017 08:31:38 -0600 Received: from feketebors.ti.com (ileax41-snat.itg.ti.com [10.172.224.153]) by dlep32.itg.ti.com (8.14.3/8.13.8) with ESMTP id vAEEVX73018797; Tue, 14 Nov 2017 08:31:37 -0600 From: Peter Ujfalusi To: CC: , , , Subject: [PATCH 02/10] dmaengine: virt-dma: Support for race free transfer termination Date: Tue, 14 Nov 2017 16:32:04 +0200 Message-ID: <20171114143212.8311-3-peter.ujfalusi@ti.com> X-Mailer: git-send-email 2.15.0 In-Reply-To: <20171114143212.8311-1-peter.ujfalusi@ti.com> References: <20171114143212.8311-1-peter.ujfalusi@ti.com> MIME-Version: 1.0 Content-Type: text/plain X-EXCLAIMER-MD-CONFIG: e1e8a2fd-e40a-4ac6-ac9b-f7e9cc9ee180 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Even with the introduced vchan_synchronize() we can face race when terminating a cyclic transfer. If the terminate_all is called after the interrupt handler called vchan_cyclic_callback(), but before the vchan_complete tasklet is called: vc->cyclic is set to the cyclic descriptor, but the descriptor itself was freed up in the driver's terminate_all() callback. When the vhan_complete() is executed it will try to fetch the vc->cyclic vdesc, but the pointer is pointing now to uninitialized memory leading to (hard to reproduce) kernel crash. In order to fix this, drivers should: - call vchan_terminate_vdesc() from their terminate_all callback instead calling their free_desc function to free up the descriptor. - implement device_synchronize callback and call vchan_synchronize(). This way we can make sure that the descriptor is only going to be freed up after the vchan_callback was executed in a safe manner. Signed-off-by: Peter Ujfalusi --- drivers/dma/virt-dma.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/drivers/dma/virt-dma.h b/drivers/dma/virt-dma.h index 2edb05505102..b09b75ab0751 100644 --- a/drivers/dma/virt-dma.h +++ b/drivers/dma/virt-dma.h @@ -35,6 +35,7 @@ struct virt_dma_chan { struct list_head desc_completed; struct virt_dma_desc *cyclic; + struct virt_dma_desc *vd_terminated; }; static inline struct virt_dma_chan *to_virt_chan(struct dma_chan *chan) @@ -129,6 +130,25 @@ static inline void vchan_cyclic_callback(struct virt_dma_desc *vd) tasklet_schedule(&vc->task); } +/** + * vchan_terminate_vdesc - Disable pending cyclic callback + * @vd: virtual descriptor to be terminated + * + * vc.lock must be held by caller + */ +static inline void vchan_terminate_vdesc(struct virt_dma_desc *vd) +{ + struct virt_dma_chan *vc = to_virt_chan(vd->tx.chan); + + /* free up stuck descriptor */ + if (vc->vd_terminated) + vchan_vdesc_fini(vc->vd_terminated); + + vc->vd_terminated = vd; + if (vc->cyclic == vd) + vc->cyclic = NULL; +} + /** * vchan_next_desc - peek at the next descriptor to be processed * @vc: virtual channel to obtain descriptor from @@ -182,10 +202,20 @@ static inline void vchan_free_chan_resources(struct virt_dma_chan *vc) * Makes sure that all scheduled or active callbacks have finished running. For * proper operation the caller has to ensure that no new callbacks are scheduled * after the invocation of this function started. + * Free up the terminated cyclic descriptor to prevent memory leakage. */ static inline void vchan_synchronize(struct virt_dma_chan *vc) { + unsigned long flags; + tasklet_kill(&vc->task); + + spin_lock_irqsave(&vc->lock, flags); + if (vc->vd_terminated) { + vchan_vdesc_fini(vc->vd_terminated); + vc->vd_terminated = NULL; + } + spin_unlock_irqrestore(&vc->lock, flags); } #endif -- Peter Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki From 1584507071688026620@xxx Sun Nov 19 15:01:38 +0000 2017 X-GM-THRID: 1584506413281899864 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread