Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp499758rwl; Thu, 30 Mar 2023 20:33:08 -0700 (PDT) X-Google-Smtp-Source: AKy350amP/3kHr87g8fRgbnFu6rx88Lvjhg493z/5tfaPIVziUCe1ebDd1OxAK+DIqiCFcg0XWLZ X-Received: by 2002:a17:906:28e:b0:947:404b:eb2 with SMTP id 14-20020a170906028e00b00947404b0eb2mr4513374ejf.0.1680233588214; Thu, 30 Mar 2023 20:33:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680233588; cv=none; d=google.com; s=arc-20160816; b=fXhWcsqCkAHCZDiuFOFNqozK0rQE695Q95rvWx6PPVEeybzKXPC94p9WwVR6sN+xhc SB+vT3ulCC6Lso62OuA01hngA4r3KSzmS63f6ra1G4I/LG4V7ybOsck1nsmL7TPoolqn ctvvBdZbQ66XYgPSQbW0D0wgkmm0NAXpLiyQAaMz9/7dR4ixo8aBqL3VKm0Lo+LEgGsW qAee2aXk8RaY1L1WztdOs0YRlmCu27JRTnnlcmLktOpqa94H5FKzAdrm91SkVm3JN/FH asKx8pydYrsDPUPW90hyHx3kmOsHHaAv1l74XzRKCMUJzPgDSUaES2rvTvdvAIs9EUve bLDw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=w3buwZbi8UwI0JR//L3crQYz+6oBSYG/M98AK9MATFQ=; b=d7o5/p9EQ9Ljj60G6WMzul764UjjaoekeqtlAbiVNlCgysUeY8RB2Cc19WLFQLRxBZ 2vRaOCqhfig3X8gB6Mc52NCbjoR79jgOHBmXUqWSNHuBfWplTBngR2diVSkT2qf++EvA 4x26z3T//aN+ZzU46/Bc4Af7D6RO5tEGWdMoqXnvmrffv1LjiX8Kp0UElz370yAXlCvN awHWqc+BKdWlHMuuUYNKpNyy96rm6W/slHEdlzPsepdZbFCMPKypJASjEkT1R6NuGVp1 LUvwbGQWreP751kUCN/aJXHshwxfKVm8B3lIlnls+HQ6iA9fNYr3F2h5u+q5MDRWqzOl fC3g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=fuXLi9xS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v19-20020a170906381300b0091f4b3423d4si1083220ejc.670.2023.03.30.20.32.42; Thu, 30 Mar 2023 20:33:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=fuXLi9xS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229664AbjCaDcC (ORCPT + 99 others); Thu, 30 Mar 2023 23:32:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42062 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229517AbjCaDb7 (ORCPT ); Thu, 30 Mar 2023 23:31:59 -0400 Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0EEF618F80 for ; Thu, 30 Mar 2023 20:31:56 -0700 (PDT) Received: by mail-pj1-x1031.google.com with SMTP id gp15-20020a17090adf0f00b0023d1bbd9f9eso24118973pjb.0 for ; Thu, 30 Mar 2023 20:31:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680233515; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=w3buwZbi8UwI0JR//L3crQYz+6oBSYG/M98AK9MATFQ=; b=fuXLi9xSbUbEqkJl8uhdtJ/06Tw8zfHYGi67/DwMDHhe9fm0v2lm456H2gONn3NQUf noUEGMu+esSuCuuBBZ2RPOSk8SaM3R23iA8p9ufRQatKbY1MNQPD0c9hVGonNQZepGSE Ghmh9pxzTdl/m+bVPbVyCidDReb56ZQaxuZAYkNkYG2gT5QYGCsp3LbMVjgnoXEGh3+A 321W0QSzv/7KMFCJr7AFZdmJ0Teu3cSXPD9WGjAY44zJGh/ANQr3KI6hryKaB0e8z1kJ j0oyDP1TlfXnvSMjFr69dOF1pQ8ncx377nWzpC1BW940nw2SNwGWsu0gl+Bj3mNlGDCK aRFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680233515; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=w3buwZbi8UwI0JR//L3crQYz+6oBSYG/M98AK9MATFQ=; b=RI04nYJJl5TjR+feMlI5EKK9X2nTV3A2MgnGP2qgZezVJtH5kDxd2CcZJxdASBFKgh APVrcnwMXKWW2wyfrQIgCrK58vtu+t7YX0qtJ0jsPRe+OpI3Ty0xGXqB2JmygRKgqnfz 4rp2N7lWkrOwa9hxabcmv4JjKyuLKCiVsOQuuC3TCdpvsU8ocC4Stos6nTHwDgCDPWup z4TfX2Ga7polvAkciJrZGIQPNVu6kFFqIJwYKZ7U7AG1+g9Snxvkaawwenebm3d96dNZ nK2duAd5L0wSGUDBWZLyVWLVvh432uQiDO/qaWWsP+0VozTtnnN7o4axxZJzIvR9q452 5KJg== X-Gm-Message-State: AAQBX9foBfa97yqiV2lkK6VKBleWRPXFFhyztl/t15sx5oYNEDkLhAf1 P2gX4TUeATC3bhgrcVbrQHM= X-Received: by 2002:a17:90a:db97:b0:236:6a28:f784 with SMTP id h23-20020a17090adb9700b002366a28f784mr28430263pjv.3.1680233514896; Thu, 30 Mar 2023 20:31:54 -0700 (PDT) Received: from ubt.spreadtrum.com ([138.113.14.245]) by smtp.gmail.com with ESMTPSA id p4-20020a170902eac400b001a239325f1csm449128pld.100.2023.03.30.20.31.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Mar 2023 20:31:54 -0700 (PDT) From: Chunyan Zhang To: Joerg Roedel , Will Deacon , Robin Murphy Cc: iommu@lists.linux.dev, Baolin Wang , Orson Zhai , Chunyan Zhang , Chunyan Zhang , LKML Subject: [RESEND PATCH V3 1/2] iommu: sprd: release dma buffer to avoid memory leak Date: Fri, 31 Mar 2023 11:31:23 +0800 Message-Id: <20230331033124.864691-2-zhang.lyra@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230331033124.864691-1-zhang.lyra@gmail.com> References: <20230331033124.864691-1-zhang.lyra@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE, 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 lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Chunyan Zhang When attaching to a domain, the driver would alloc a DMA buffer which is used to store address mapping table, and it need to be released when the IOMMU domain is freed. Signed-off-by: Chunyan Zhang --- drivers/iommu/sprd-iommu.c | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/drivers/iommu/sprd-iommu.c b/drivers/iommu/sprd-iommu.c index ae94d74b73f4..7df1f730c778 100644 --- a/drivers/iommu/sprd-iommu.c +++ b/drivers/iommu/sprd-iommu.c @@ -151,13 +151,6 @@ static struct iommu_domain *sprd_iommu_domain_alloc(unsigned int domain_type) return &dom->domain; } -static void sprd_iommu_domain_free(struct iommu_domain *domain) -{ - struct sprd_iommu_domain *dom = to_sprd_domain(domain); - - kfree(dom); -} - static void sprd_iommu_first_vpn(struct sprd_iommu_domain *dom) { struct sprd_iommu_device *sdev = dom->sdev; @@ -230,6 +223,28 @@ static void sprd_iommu_hw_en(struct sprd_iommu_device *sdev, bool en) sprd_iommu_update_bits(sdev, reg_cfg, mask, 0, val); } +static void sprd_iommu_cleanup(struct sprd_iommu_domain *dom) +{ + size_t pgt_size; + + /* Nothing need to do if the domain hasn't been attached */ + if (!dom->sdev) + return; + + pgt_size = sprd_iommu_pgt_size(&dom->domain); + dma_free_coherent(dom->sdev->dev, pgt_size, dom->pgt_va, dom->pgt_pa); + dom->sdev = NULL; + sprd_iommu_hw_en(dom->sdev, false); +} + +static void sprd_iommu_domain_free(struct iommu_domain *domain) +{ + struct sprd_iommu_domain *dom = to_sprd_domain(domain); + + sprd_iommu_cleanup(dom); + kfree(dom); +} + static int sprd_iommu_attach_device(struct iommu_domain *domain, struct device *dev) { -- 2.25.1