From patchwork Mon Apr 27 12:23:24 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jon Hunter X-Patchwork-Id: 6279611 Return-Path: X-Original-To: patchwork-dmaengine@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 4AB52BF4A6 for ; Mon, 27 Apr 2015 12:23:41 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 2D4D320443 for ; Mon, 27 Apr 2015 12:23:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D6C612038A for ; Mon, 27 Apr 2015 12:23:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932214AbbD0MXi (ORCPT ); Mon, 27 Apr 2015 08:23:38 -0400 Received: from hqemgate15.nvidia.com ([216.228.121.64]:14071 "EHLO hqemgate15.nvidia.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932284AbbD0MXh (ORCPT ); Mon, 27 Apr 2015 08:23:37 -0400 Received: from hqnvupgp07.nvidia.com (Not Verified[216.228.121.13]) by hqemgate15.nvidia.com id ; Mon, 27 Apr 2015 05:23:29 -0700 Received: from hqemhub02.nvidia.com ([172.20.150.31]) by hqnvupgp07.nvidia.com (PGP Universal service); Mon, 27 Apr 2015 05:22:30 -0700 X-PGP-Universal: processed; by hqnvupgp07.nvidia.com on Mon, 27 Apr 2015 05:22:30 -0700 Received: from jonathanh-lm.nvidia.com (172.20.144.16) by hqemhub02.nvidia.com (172.20.150.31) with Microsoft SMTP Server (TLS) id 8.3.342.0; Mon, 27 Apr 2015 05:23:36 -0700 From: Jon Hunter To: Vinod Koul CC: Dan Williams , dmaengine@vger.kernel.org, linux-kernel@vger.kernel.org, linux-tegra@vger.kernel.org, Jon Hunter , Stephen Warren Subject: [PATCH] dmaengine: Fix dma_get_any_slave_channel() handling of private channels Date: Mon, 27 Apr 2015 13:23:24 +0100 Message-ID: <1430137404-30236-1-git-send-email-jonathanh@nvidia.com> X-Mailer: git-send-email 2.3.6 X-NVConfidentiality: public MIME-Version: 1.0 Sender: dmaengine-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: dmaengine@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The function dma_get_any_slave_channel() allocates private DMA channels by calling the internal private_candidate() function. However, when doing so, if a channel is successfully allocated, neither the DMA_PRIVATE flag is set or the privatecnt variable is incremented for the DMA controller. This will cause the following problems ... 1. A DMA controller initialised with the DMA_PRIVATE flag set (ie. channels should always be private) will become public incorrectly when a channel is allocated and then released. This is because: - A DMA controller initialised with DMA_PRIVATE set will have a initial privatecnt of 1. - The privatecnt is not incremented by dma_get_any_slave_channel(). - When the channel is released via dma_release_channel(), the privatecnt is decremented and the DMA_PRIVATE flag is cleared because the privatecnt value is 0. 2. For a DMA controller initialised with the DMA_PRIVATE flag set, if more than one DMA channel is allocated successfully via dma_get_any_slave_channel() and then one channel is released, the following issues can occur: i). All channels currently allocated will appear as public because the DMA_PRIVATE will be cleared (as described in #1). ii). Subsequent calls to dma_get_any_slave_channel() will fail even if there are channels available. The reason this fails is that the private_candidate() function (called by dma_get_any_slave_channel()) will detect the DMA controller is not private but has active channels and so cannot allocate any private channels (see below code snippet). /* devices with multiple channels need special handling as we need to * ensure that all channels are either private or public. */ if (dev->chancnt > 1 && !dma_has_cap(DMA_PRIVATE, dev->cap_mask)) list_for_each_entry(chan, &dev->channels, device_node) { /* some channels are already publicly allocated */ if (chan->client_count) return NULL; } 3. For a DMA controller initialised with the DMA_PRIVATE flag unset, if a private channel is allocated via dma_get_any_slave_channel(), then the DMA controller will still appear as public because the DMA_PRIVATE flag is not set and this will cause: i). The allocated channel to appear as public ii). Prevent any further private channels being allocated via dma_get_any_slave_channel() (because private_candidate() will fail in the same way as described in 2.ii above). Fix this by incrementing the privatecnt in dma_get_any_slave_channel(). If dma_get_any_slave_channel() allocates a channel also ensure the DMA_PRIVATE flag is set, in case it was not before. If the privatecnt becomes 0 then the DMA_PRIVATE flag should be cleared. Cc: Stephen Warren Signed-off-by: Jon Hunter --- This issue was found when attempting to open and close a serial interface, that uses DMA, multiple times on a tegra device. When opening the serial device a 2nd time after closing, the DMA channel allocation would fail. drivers/dma/dmaengine.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/drivers/dma/dmaengine.c b/drivers/dma/dmaengine.c index 0e035a8cf401..03b0e22b4a68 100644 --- a/drivers/dma/dmaengine.c +++ b/drivers/dma/dmaengine.c @@ -571,11 +571,16 @@ struct dma_chan *dma_get_any_slave_channel(struct dma_device *device) chan = private_candidate(&mask, device, NULL, NULL); if (chan) { + dma_cap_set(DMA_PRIVATE, device->cap_mask); + device->privatecnt++; err = dma_chan_get(chan); if (err) { pr_debug("%s: failed to get %s: (%d)\n", __func__, dma_chan_name(chan), err); chan = NULL; + + if (--device->privatecnt == 0) + dma_cap_clear(DMA_PRIVATE, device->cap_mask); } }