From patchwork Fri Mar 31 03:31:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chunyan Zhang X-Patchwork-Id: 77522 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp297205vqo; Thu, 30 Mar 2023 20:44:52 -0700 (PDT) X-Google-Smtp-Source: AKy350b7v7VFzZ0v47kamSNJCCjl0W8HessryGkETGQdIMMS3oPw57xxwhLwTcxAl/bTSxl7TWzi X-Received: by 2002:a17:906:9397:b0:93f:fbe:ed17 with SMTP id l23-20020a170906939700b0093f0fbeed17mr21934181ejx.62.1680234291766; Thu, 30 Mar 2023 20:44:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680234291; cv=none; d=google.com; s=arc-20160816; b=zc8/XbFF6jMW0ut4ntQMViYgbq59Mpxly05/Cn85V7hTtA7MZY/1jXt7LWJtP4T2rX 7WKLb2wWinWv6cbCw3jK0fMQ+izcURzMGNiJHM7Qw55LygFsRXBSWEQXf7d67k9aosF4 3EsgKJrV8Ew3d8JSzHIYO7DFM2ZKfijabgbFrdY3XjVQi8ZTPgcqPkk9O4cNfSMpYJ0V 8anNCByu5rC0uV0rpd0aC9qFXtRJKdwIAFptY9seALBCz6v0cgmHiS25XLtcQyO3csK9 2WFYk+v40bdRD/7iFlEBTjN3wUwb8NyaB8B9O8qRylBLYkT3nXTmFhLge5McNEDeL9qx Vy7Q== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y12-20020a170906914c00b00946872f37c6si1092951ejw.450.2023.03.30.20.44.28; Thu, 30 Mar 2023 20:44:51 -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 S229756AbjCaDcC (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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761853353060141871?= X-GMAIL-MSGID: =?utf-8?q?1761853353060141871?= 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) { From patchwork Fri Mar 31 03:31:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chunyan Zhang X-Patchwork-Id: 77521 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp295567vqo; Thu, 30 Mar 2023 20:38:59 -0700 (PDT) X-Google-Smtp-Source: AKy350YV55WzsdDv5wxHW+u3FWaP3/5VdaqhQrrWOVPGn/JUWZTyY4Ml7/I+Bnr3nitMMEQj5PkN X-Received: by 2002:a17:906:1501:b0:93e:9362:7607 with SMTP id b1-20020a170906150100b0093e93627607mr22728918ejd.20.1680233938982; Thu, 30 Mar 2023 20:38:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680233938; cv=none; d=google.com; s=arc-20160816; b=xPwuqlYSL77AphKQuaumlbDrOijbEqIVDfU6z4gdL28b/rUdRa1eMrrsbwSpEj/0Z2 SbGhqBm8F2At24TbLT/MbUdlpXXLiLyDTvTBpP7ek0wvtn9N02UYZL4QMY0r2BF+Oe3z PiSoQ7LM1EcZp1B3P8GVFMA/kUvQGExdpwrSN1v7b8bgebhC3Z4I8pCJFnT4P9P34n+2 JRbfj/0IRl9B5JBPuEs6YBgOcgr9Ogp2FQPw0PmI5uMnVPzAbExvZ/TnZZvIqzow/cAe uKbN9T5hyll0nNn8iTJMcOp+OnU1ST64iwXgZjnoVk2mGAhW1ZPHigMh7KW0+6rOtysw 93Mw== 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=Z+9ZPtRrjL6zYn8USHZp7DIBp0J7SJ4SjTl0hBaFZwU=; b=qtOn0DhjOxthDfL0ZsZgFYX1mwPfvsE7T9xws5Nc+F43apsyEH4CNyR9GUMIeFFkzk 6aCjvj3fywKo0Cy/jg5TRKGECR7yNu56YQIXmTmM1/b8vsTD84jqLIA0KflLYCVXApQj JDLn2kOyzwAat/uBTgW+X8UoVmKEdyuZ7jkeRpn3bzfKq0v83fiwCjHNinAV3IG+BL0L uuhx4N38xGHt4T1qQat65an76Q6flTBWgh0WlKXmWujRvBWP2YUuTaAvWWGI+NH5sZbA EUKUe6hVQLYkypC5sciYrF/MdyMkrkqd5apnn0Qn217XyKCOMXVeRac2Jyb33zy/9w6W rcKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=DCa6KQxG; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bh1-20020a170906a0c100b00947bac8a3cbsi106417ejb.546.2023.03.30.20.38.34; Thu, 30 Mar 2023 20:38:58 -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=DCa6KQxG; 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 S229806AbjCaDcE (ORCPT + 99 others); Thu, 30 Mar 2023 23:32:04 -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 S229749AbjCaDcB (ORCPT ); Thu, 30 Mar 2023 23:32:01 -0400 Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0CC37CA19 for ; Thu, 30 Mar 2023 20:31:58 -0700 (PDT) Received: by mail-pl1-x62e.google.com with SMTP id o2so20046945plg.4 for ; Thu, 30 Mar 2023 20:31:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680233517; 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=Z+9ZPtRrjL6zYn8USHZp7DIBp0J7SJ4SjTl0hBaFZwU=; b=DCa6KQxGG6AzMQVouFVFhw+hOgFCcmaF1zjXTSBVZFn3cRWwVJMldaTcDG/bcHarIn wKN527uYemylzJGjuDKerFk0a1H1DxfYo0pWhoAM2XeqMrO/zWP7A4j2eKcsklyP5nYO 6iLv41misbE4Ld549/Mpg0xZ0xvDmWDPHdldwhJ0tKWt8eZteEbQaixaP83kHwVdThhX FNYYiN/01zrYghz6igEjQwEA47ScJQf/uEjLsppkLDXuoWrkLBkMYRStm7guSPHSL0Ve WKhcgrWCVPP4yZOltI4ItQJGBVwY39oGinS+lAAVzuVw6H1dj8B8hbE1qmAghoPRhWgZ aFCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680233517; 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=Z+9ZPtRrjL6zYn8USHZp7DIBp0J7SJ4SjTl0hBaFZwU=; b=zn1kbVSVgFN78jhwNNRxNX66gcmEUayTlUcCKuUPLtMyJqTaFXvgv9uhzE4Iim95vJ fs8/FonSi3GYoryqtF4n7Z+4hp2OmnoCwYzXoj225Tq6mHQCAMZzdm2vjojHGw1oDUea 331qB03VS3dFfxe+XttyA5yq4RyKMDXeeZoatCGJlFHSA80LGXOpwm8b6oVk+UoT/63K ghvgkYT8zmfdMEgnZuLrI1DH0/ZLJAc/CNHSHR5O1zwq9uKKJme3a7ooQv86xeU+fCC1 pjbH42T0GD7S2/FpLyoUZnxKVbKKYrCiwiIX5e0VzfKkHoXzjUrCb7ODhBW2JNrSatOv cSAg== X-Gm-Message-State: AAQBX9fS94BML7gKlXO5N6zENCyheNlKkkw+KRNmJjrwh4VwbXGw/vAP FCxXtb3VJT5o/tT8uNdajfE= X-Received: by 2002:a17:90b:33cc:b0:237:40a5:7cb9 with SMTP id lk12-20020a17090b33cc00b0023740a57cb9mr28369711pjb.5.1680233517522; Thu, 30 Mar 2023 20:31:57 -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.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Mar 2023 20:31:57 -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 2/2] iommu: sprd: Add support for reattaching an existing domain Date: Fri, 31 Mar 2023 11:31:24 +0800 Message-Id: <20230331033124.864691-3-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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761852983291462456?= X-GMAIL-MSGID: =?utf-8?q?1761852983291462456?= From: Chunyan Zhang This IOMMU driver should allow a domain to be attached more than once. If IOMMU is reattaching to the same domain which is attached, there's nothing to be done. If reattching to a previously-used domain, do not alloc DMA buffer again which stores address mapping table to avoid memory leak. Signed-off-by: Chunyan Zhang --- drivers/iommu/sprd-iommu.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/drivers/iommu/sprd-iommu.c b/drivers/iommu/sprd-iommu.c index 7df1f730c778..3513b2b108bf 100644 --- a/drivers/iommu/sprd-iommu.c +++ b/drivers/iommu/sprd-iommu.c @@ -62,6 +62,7 @@ enum sprd_iommu_version { * @eb: gate clock which controls IOMMU access */ struct sprd_iommu_device { + struct sprd_iommu_domain *dom; enum sprd_iommu_version ver; u32 *prot_page_va; dma_addr_t prot_page_pa; @@ -252,15 +253,27 @@ static int sprd_iommu_attach_device(struct iommu_domain *domain, struct sprd_iommu_domain *dom = to_sprd_domain(domain); size_t pgt_size = sprd_iommu_pgt_size(domain); - if (dom->sdev) - return -EINVAL; + /* The device is attached to this domain */ + if (sdev->dom == dom) + return 0; - dom->pgt_va = dma_alloc_coherent(sdev->dev, pgt_size, &dom->pgt_pa, GFP_KERNEL); - if (!dom->pgt_va) - return -ENOMEM; + /* The first time that domain is attaching to a device */ + if (!dom->pgt_va) { + dom->pgt_va = dma_alloc_coherent(sdev->dev, pgt_size, &dom->pgt_pa, GFP_KERNEL); + if (!dom->pgt_va) + return -ENOMEM; + + dom->sdev = sdev; + } - dom->sdev = sdev; + sdev->dom = dom; + /* + * One sprd IOMMU serves one client device only, disabled it before + * configure mapping table to avoid access conflict in case other + * mapping table is stored in. + */ + sprd_iommu_hw_en(sdev, false); sprd_iommu_first_ppn(dom); sprd_iommu_first_vpn(dom); sprd_iommu_vpn_range(dom);