From patchwork Mon Mar 6 02:58:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 64394 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp1629766wrd; Sun, 5 Mar 2023 19:00:44 -0800 (PST) X-Google-Smtp-Source: AK7set94tU7oxvm7QWH06zyVAnhhdrqtlpGrsOPPZ96VWLCEvcOzZvCkzMe7Z+IxbYKbFoU+DVI/ X-Received: by 2002:a05:6a20:8f09:b0:cb:cfb1:5009 with SMTP id b9-20020a056a208f0900b000cbcfb15009mr13017790pzk.34.1678071644649; Sun, 05 Mar 2023 19:00:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678071644; cv=none; d=google.com; s=arc-20160816; b=z4SYODx4m0C6yErU6jm/oOUhcsk9WLO8ggXfclJVUFx8udxpRA2OzSwgiNktCiKpc7 dk2bpl/WneQmZvYaEIOogAzXVuax2oP+y5PXQAdDmuwIInqQq3Tds9mS/ezvnSElOJiQ oKdrC11w+y4B512ZNLE8EYHD7QwGNUMVcd1u06ySQ+z2KnZG55mQ/8AhDJqBhAGV9wNh UE2bst8uHAdLaflW0Z07hGA2O6nz6xjgxRWoXm32bVUXWLmtnP7UPNh6PLcsevsmtN2Q 9XMSCeh+97OrkumM1Je35ASrVRPfaMrbzi92R7DSkacWTEt1oarrxHk8NydoGMT6B+ob sZ0g== 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=E2/CaviEcysBW/Jp4/aLR57bA/iZCyi+RXEmnBIQSqM=; b=wfbgVlIplGtp4lcO6A8c9dXqS7h3WnVt9bJoRvhhwsWLFs5xyMEyaL2IfvApoeFY5z EeUkOArzCvxLa/y6Qrt5FB+RWntmikX8rvSYm7jjs4R3H9FF+vV3HOCCBIScJN5e+kT4 nGasYfGX/1dQjB2fAE/54xt/5d67OQZLEODuoejOjGCwWNc2iC2J/UiYM8jYMSJvRZKa AxnRMrSuwHJqS5VFJuWMV6SH9EWO6AwirlrrQh94hDTGyd1dLSEGmaRweOzbsHowkair q33XmNwTgZc9LrdOb77KPPLeFRR7sx3G7cAzRtsLD5kKMFPg8wAfQffceGOL1NLgOr7c wJBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=J0+weIw6; 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=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h129-20020a636c87000000b004fbd23110d4si7694374pgc.773.2023.03.05.19.00.32; Sun, 05 Mar 2023 19:00:44 -0800 (PST) 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=@intel.com header.s=Intel header.b=J0+weIw6; 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=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229653AbjCFC72 (ORCPT + 99 others); Sun, 5 Mar 2023 21:59:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49178 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229591AbjCFC7X (ORCPT ); Sun, 5 Mar 2023 21:59:23 -0500 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D2F871114C for ; Sun, 5 Mar 2023 18:59:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678071562; x=1709607562; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=23OLppNDX/QUue37RTrJRL+8G3hSt1KtGJZMXHrLeEU=; b=J0+weIw6Mci7I/ufJQBR/7s3vJ+jZYM32NCgzjSl6TN2jE8mvQgZ8Qpj LHh6V6c8K+v1mVXZH8yb8VXofw/V8NKv/7virFc+avbZq50m8g3Q34awR o2xwiZkyaM90FTKipEr5xW33gt0h91yFLXK+GNs2DBsCXunNt9re710ub a+0DY+18bsXsivsI/AZQeLq0/nJWqqCQnpYI8thvO3ngeS0nCvV2gYsC8 xpxC91gFvThvZQyGyLKQeYDss8e8/55lAjIIRNVqbuZd9KQH3N7YQkUzF ya/M/u8qLUwPKPIJ05jKp1YO9jwggI8zJ5RL+wHNYifV3DoJ/2aCMOPbh g==; X-IronPort-AV: E=McAfee;i="6500,9779,10640"; a="363071513" X-IronPort-AV: E=Sophos;i="5.98,236,1673942400"; d="scan'208";a="363071513" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Mar 2023 18:59:22 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10640"; a="765072478" X-IronPort-AV: E=Sophos;i="5.98,236,1673942400"; d="scan'208";a="765072478" Received: from allen-box.sh.intel.com ([10.239.159.48]) by FMSMGA003.fm.intel.com with ESMTP; 05 Mar 2023 18:59:20 -0800 From: Lu Baolu To: iommu@lists.linux.dev Cc: Joerg Roedel , Jason Gunthorpe , Christoph Hellwig , Kevin Tian , Will Deacon , Robin Murphy , linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH v3 3/6] iommu: Same critical region for device release and removal Date: Mon, 6 Mar 2023 10:58:01 +0800 Message-Id: <20230306025804.13912-4-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230306025804.13912-1-baolu.lu@linux.intel.com> References: <20230306025804.13912-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE autolearn=ham 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?1759585652476580887?= X-GMAIL-MSGID: =?utf-8?q?1759585652476580887?= In a non-driver context, it is crucial to ensure the consistency of a device's iommu ops. Otherwise, it may result in a situation where a device is released but it's iommu ops are still used. Put the ops->release_device and __iommu_group_remove_device() in a some group->mutext critical region, so that, as long as group->mutex is held and the device is in its group's device list, its iommu ops are always consistent. Add check of group ownership if the released device is the last one. Signed-off-by: Jason Gunthorpe Signed-off-by: Lu Baolu Reviewed-by: Jason Gunthorpe --- drivers/iommu/iommu.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index bd9b293e07a8..0bcd9625090d 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -507,18 +507,44 @@ static void __iommu_group_release_device(struct iommu_group *group, void iommu_release_device(struct device *dev) { + struct iommu_group *group = dev->iommu_group; + struct group_device *device; const struct iommu_ops *ops; - if (!dev->iommu) + if (!dev->iommu || !group) return; iommu_device_unlink(dev->iommu->iommu_dev, dev); + mutex_lock(&group->mutex); + device = __iommu_group_remove_device(group, dev); + + /* + * If the group has become empty then ownership must have been released, + * and the current domain must be set back to NULL or the default + * domain. + */ + if (list_empty(&group->devices)) + WARN_ON(group->owner_cnt || + group->domain != group->default_domain); + + /* + * release_device() must stop using any attached domain on the device. + * If there are still other devices in the group they are not effected + * by this callback. + * + * The IOMMU driver must set the device to either an identity or + * blocking translation and stop using any domain pointer, as it is + * going to be freed. + */ ops = dev_iommu_ops(dev); if (ops->release_device) ops->release_device(dev); + mutex_unlock(&group->mutex); + + if (device) + __iommu_group_release_device(group, device); - iommu_group_remove_device(dev); module_put(ops->owner); dev_iommu_free(dev); }