Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp109437ybg; Tue, 22 Oct 2019 17:03:11 -0700 (PDT) X-Google-Smtp-Source: APXvYqzGfB2suuGpXj2d2vfrFuKU/541UDu+uhho35gOwU8jteSXfCZ9s7gHGk5Oz7REwM8j3DjH X-Received: by 2002:a17:906:154a:: with SMTP id c10mr29178678ejd.238.1571788991830; Tue, 22 Oct 2019 17:03:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571788991; cv=none; d=google.com; s=arc-20160816; b=pAGETtd+MuJINv1qyjWzuzFUVWk8PwdPXOuNSYPPQdLnECEJgvgqesTWVz/nKkQJHQ AfyZ9pAUcMxdmjd0Z7O9GcrNppdFtNLHRv0XMZRJ11B1j1V7GWaficcbS3i4UfBDHx1L MA+Y1opj9lui1QozVOm1Wp06FPgPBOj/R+9swFD7pwA2Jrr3+gG4IHf74zDU2f3GQF5M oZGL/7z/Rtzoczn29ewZPxLMBmrGwIOY/9hzUa2z+bnmo1Etn4d5KvnSF/AWWNtm54vg VghsoXSxbFvrE7cxApjQ2W/iTxjwx3EuhD+X5bKl2nJvQOwinGrWdP6T+o9qO07KTBff /DwA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:subject:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:cc:to:from; bh=QpZ4dBgtGaccQ8DyKK4CV0ganWRBlRelqdlDJ3FUHSo=; b=gE0ZMD9OmN2FWv7HetVF35jaLyqsIYcGuIuMNTwbm0S0es5dhD2Jvr71suC5xVfseP NlNM3PXilOOV3VMAGKtyQE3coNFRfcaqKqEeSU4RdIeG5ewFMGUAdzU7T0N5JIGVuhRk LFKPHM2wldAVuVweSR4kh7tRpKSFb1oy4Ij02RiO78iiOGggbQ66MXo58m2rteT2H766 UO5zmaa12mgbkCl7rkjZPeLSrnQChRhaWZoLrCUer+fmrF9rd+EI8r3aDGWWL1XRAimO whezXEFNAXdZ9oS5qn4D11Ll+fpgJi948si5XdIGJAlOaGMbXeV032GecViqyeMPltju xDMA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m5si1850043ejr.417.2019.10.22.17.02.47; Tue, 22 Oct 2019 17:03:11 -0700 (PDT) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733250AbfJVVqY (ORCPT + 99 others); Tue, 22 Oct 2019 17:46:24 -0400 Received: from ale.deltatee.com ([207.54.116.67]:33076 "EHLO ale.deltatee.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733237AbfJVVqX (ORCPT ); Tue, 22 Oct 2019 17:46:23 -0400 Received: from cgy1-donard.priv.deltatee.com ([172.16.1.31]) by ale.deltatee.com with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1iN1ys-00049N-Rg; Tue, 22 Oct 2019 15:46:22 -0600 Received: from gunthorp by cgy1-donard.priv.deltatee.com with local (Exim 4.92) (envelope-from ) id 1iN1ys-00024x-3E; Tue, 22 Oct 2019 15:46:18 -0600 From: Logan Gunthorpe To: linux-kernel@vger.kernel.org, dmaengine@vger.kernel.org, Vinod Koul Cc: Dan Williams , Logan Gunthorpe Date: Tue, 22 Oct 2019 15:46:12 -0600 Message-Id: <20191022214616.7943-2-logang@deltatee.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191022214616.7943-1-logang@deltatee.com> References: <20191022214616.7943-1-logang@deltatee.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SA-Exim-Connect-IP: 172.16.1.31 X-SA-Exim-Rcpt-To: linux-kernel@vger.kernel.org, dmaengine@vger.kernel.org, vkoul@kernel.org, dan.j.williams@intel.com, logang@deltatee.com X-SA-Exim-Mail-From: gunthorp@deltatee.com X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on ale.deltatee.com X-Spam-Level: X-Spam-Status: No, score=-8.7 required=5.0 tests=ALL_TRUSTED,BAYES_00, GREYLIST_ISWHITE,MYRULES_NO_TEXT autolearn=ham autolearn_force=no version=3.4.2 Subject: [PATCH 1/5] dmaengine: Store module owner in dma_device struct X-SA-Exim-Version: 4.2.1 (built Tue, 02 Aug 2016 21:08:31 +0000) X-SA-Exim-Scanned: Yes (on ale.deltatee.com) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org dma_chan_to_owner() dereferences the driver from the struct device to obtain the owner and call module_[get|put](). However, if the backing device is unbound before the dma_device is unregistered, the driver will be cleared and this will cause a NULL pointer dereference. Instead, store a pointer to the owner module in the dma_device struct so the module reference can be properly put when the channel is put, even if the backing device was destroyed first. This change helps to support a safer unbind of DMA engines. If the dma_device is unregistered in the driver's remove function, there's no guarantee that there are no existing clients and a users action may trigger the WARN_ONCE in dma_async_device_unregister() which is unlikely to leave the system in a consistent state. Instead, a better approach is to allow the backing driver to go away and fail any subsequent requests to it. Signed-off-by: Logan Gunthorpe --- drivers/dma/dmaengine.c | 4 +++- include/linux/dmaengine.h | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/drivers/dma/dmaengine.c b/drivers/dma/dmaengine.c index 03ac4b96117c..4b604086b1b3 100644 --- a/drivers/dma/dmaengine.c +++ b/drivers/dma/dmaengine.c @@ -179,7 +179,7 @@ __dma_device_satisfies_mask(struct dma_device *device, static struct module *dma_chan_to_owner(struct dma_chan *chan) { - return chan->device->dev->driver->owner; + return chan->device->owner; } /** @@ -919,6 +919,8 @@ int dma_async_device_register(struct dma_device *device) return -EIO; } + device->owner = device->dev->driver->owner; + if (dma_has_cap(DMA_MEMCPY, device->cap_mask) && !device->device_prep_dma_memcpy) { dev_err(device->dev, "Device claims capability %s, but op is not defined\n", diff --git a/include/linux/dmaengine.h b/include/linux/dmaengine.h index 8fcdee1c0cf9..13aa0abb71de 100644 --- a/include/linux/dmaengine.h +++ b/include/linux/dmaengine.h @@ -674,6 +674,7 @@ struct dma_filter { * @fill_align: alignment shift for memset operations * @dev_id: unique device ID * @dev: struct device reference for dma mapping api + * @owner: owner module (automatically set based on the provided dev) * @src_addr_widths: bit mask of src addr widths the device supports * Width is specified in bytes, e.g. for a device supporting * a width of 4 the mask should have BIT(4) set. @@ -737,6 +738,7 @@ struct dma_device { int dev_id; struct device *dev; + struct module *owner; u32 src_addr_widths; u32 dst_addr_widths; -- 2.20.1