Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp1215088iog; Thu, 30 Jun 2022 20:29:50 -0700 (PDT) X-Google-Smtp-Source: AGRyM1ssiGn9l+r7KxPTIvf4HIpCqtw3qbdnXuyXIHqlVnlghShBbaomqwauhF8hF4NIjFmtZS/T X-Received: by 2002:a17:903:1108:b0:168:d2ba:70fd with SMTP id n8-20020a170903110800b00168d2ba70fdmr17854314plh.136.1656646190418; Thu, 30 Jun 2022 20:29:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656646190; cv=none; d=google.com; s=arc-20160816; b=reZXdcpDZjv2C9hN4L9pCNu7rwa16gy+pAVcQegiy8AOzBasDIJo9oNfui77W726Bi q3T4zouQyCaKevuursZjESdtrFU21B25YmjeX0dFMk/DuvUSTJuPI7BszlXKDLZtIq2V LA7yas9qeCtnwRo8Lsgj5d4ldxUJCUzmsHnVxtsn/f6o/ybxyGneYFlpTjrVS3WCBLF/ LVHStA3o4doAzp3AuPdVWVgtvtmJWQkFn0gjUnxDdbCoyikfeWoqMsyEquP3CM6l8p1j Hg7n8FkQMIaC9+R7TW+am/YzQBHFy/whZ9uFX2RVjj4TFD/UyK0SmafRERQV9REA6kXl DHwg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:to:content-language:subject:cc:user-agent:mime-version :date:message-id:dkim-signature; bh=5tQWnFwE5pYLqH29kDN0zZ0LnUeD4S/FG3FZp/rfiyE=; b=VSLcmFm2C1yNv+9OPq/bLYWenFBu3M8Vr7xqR2acUUL4er2Q9WxcR1nn0EkH2QyO24 mkmURvI79RQYvgqDg/qK/NOhXMo6ZBEAn6zplARbDtH1xxrH+KY0xuwBEDBPdPAa48Mo YX4JNhlZAnYmGXRxt/MSJT+2rwCNoWP4S6xB7ZLh0Youj2KNCq3m6Wgfy0Dqv/OK2hzu K3lSINW4oX1EvvUV3tPK/XePb22lDj6sSlZSQ/YJKRW/rskS1oCLcBTb/KSDcbC8w+7G aHMw4hIsFHLrpLE2oxJSdUfc2VZwfOYq8OXThT4hvboBKBTm77iQM1e12cxV+N6ogwgY BH8Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=caRbIjGk; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f15-20020a63de0f000000b003fe34317ce2si26714276pgg.778.2022.06.30.20.29.35; Thu, 30 Jun 2022 20:29:50 -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=@intel.com header.s=Intel header.b=caRbIjGk; 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 S233604AbiGACkM (ORCPT + 99 others); Thu, 30 Jun 2022 22:40:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38864 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233336AbiGACkL (ORCPT ); Thu, 30 Jun 2022 22:40:11 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 784313879A for ; Thu, 30 Jun 2022 19:40:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1656643210; x=1688179210; h=message-id:date:mime-version:cc:subject:to:references: from:in-reply-to:content-transfer-encoding; bh=edwKdOeVps2DTKJzwrZSMgChaL5jo07778euBqV1TM8=; b=caRbIjGkd5t7pSm6fUCEoP7dQ6hFHn7wkIq5r0jVyoWcN6HwAERRgFTm H/lGt2uAIQdi+4xVl6w42MBjW0XD5UKv3ufQTvIkNGtCRzAqo7iy6Cf8Q ePkRcjIHlSMbj+EXize0F0TOVdMKoVJqpKOUcPCHfPTh+Bj4b7fngcMEw tO3rNlDwUvJD0L8GxtSkrIz74EEowf+bIsbaKqnZu9GderfcIKAkTVhWm 0SL4gvFKaaIGQ0NNtbRbfzVuLPmXZfhbLbuS8q2J9Nn6uoWJgSS8P1RGE wCXS6Fkzo6BVjNuyR1eDSDTAYEZShzfm5l79SjGIrcGV+t/EN9gAhD1Dr g==; X-IronPort-AV: E=McAfee;i="6400,9594,10394"; a="308044292" X-IronPort-AV: E=Sophos;i="5.92,236,1650956400"; d="scan'208";a="308044292" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Jun 2022 19:40:10 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.92,236,1650956400"; d="scan'208";a="589059098" Received: from allen-box.sh.intel.com (HELO [10.239.159.48]) ([10.239.159.48]) by orsmga007.jf.intel.com with ESMTP; 30 Jun 2022 19:40:07 -0700 Message-ID: <595c314b-2d16-aeef-d808-d1cb713ba175@linux.intel.com> Date: Fri, 1 Jul 2022 10:35:50 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Cc: baolu.lu@linux.intel.com, David Woodhouse , Jerry Snitselaar , Mike Travis , Dimitri Sivanich , "Anderson, Russ" , "iommu@lists.linux.dev" , "iommu@lists.linux-foundation.org" , "linux-kernel@vger.kernel.org" Subject: Re: [PATCH v1 3/6] iommu/vt-d: Refactor iommu information of each domain Content-Language: en-US To: "Tian, Kevin" , Joerg Roedel , Steve Wahl References: <20220625125204.2199437-1-baolu.lu@linux.intel.com> <20220625125204.2199437-4-baolu.lu@linux.intel.com> From: Lu Baolu In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-7.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_HI, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE 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 On 6/30/22 4:28 PM, Tian, Kevin wrote: >> From: Lu Baolu >> Sent: Saturday, June 25, 2022 8:52 PM >> >> +struct iommu_domain_info { >> + struct intel_iommu *iommu; >> + unsigned int refcnt; >> + u16 did; >> +}; >> + >> struct dmar_domain { >> int nid; /* node id */ >> - >> - unsigned int iommu_refcnt[DMAR_UNITS_SUPPORTED]; >> - /* Refcount of devices per iommu */ >> - >> - >> - u16 iommu_did[DMAR_UNITS_SUPPORTED]; >> - /* Domain ids per IOMMU. Use u16 >> since >> - * domain ids are 16 bit wide >> according >> - * to VT-d spec, section 9.3 */ > > It'd make sense to keep the comments when moving above fields. Sure. Updated. diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h index 56c3d1a9e155..fae45bbb0c7f 100644 --- a/drivers/iommu/intel/iommu.h +++ b/drivers/iommu/intel/iommu.h @@ -527,8 +527,10 @@ struct context_entry { struct iommu_domain_info { struct intel_iommu *iommu; - unsigned int refcnt; - u16 did; + unsigned int refcnt; /* Refcount of devices per iommu */ + u16 did; /* Domain ids per IOMMU. Use u16 since + * domain ids are 16 bit wide according + * to VT-d spec, section 9.3 */ }; > >> + spin_lock(&iommu->lock); >> + curr = xa_load(&domain->iommu_array, iommu->seq_id); >> + if (curr) { >> + curr->refcnt++; >> + kfree(info); >> + goto success; > > Not a fan of adding a label for success. Just putting two lines (unlock+ > return) here is clearer. Updated. > >> + ret = xa_err(xa_store(&domain->iommu_array, iommu->seq_id, >> + info, GFP_ATOMIC)); > > check xa_err in separate line. Sure. Updated as below: @@ -1778,13 +1780,14 @@ static int domain_attach_iommu(struct dmar_domain *domain, info->did = num; info->iommu = iommu; domain->nid = iommu->node; - ret = xa_err(xa_store(&domain->iommu_array, iommu->seq_id, - info, GFP_ATOMIC)); - if (ret) + curr = xa_cmpxchg(&domain->iommu_array, iommu->seq_id, + NULL, info, GFP_ATOMIC); + if (curr) { + ret = xa_err(curr) ? : -EBUSY; goto err_clear; + } domain_update_iommu_cap(domain); > >> >> static void domain_detach_iommu(struct dmar_domain *domain, >> struct intel_iommu *iommu) >> { >> - int num; >> + struct iommu_domain_info *info; >> >> spin_lock(&iommu->lock); >> - domain->iommu_refcnt[iommu->seq_id] -= 1; >> - if (domain->iommu_refcnt[iommu->seq_id] == 0) { >> - num = domain->iommu_did[iommu->seq_id]; >> - clear_bit(num, iommu->domain_ids); >> + info = xa_load(&domain->iommu_array, iommu->seq_id); >> + if (--info->refcnt == 0) { >> + clear_bit(info->did, iommu->domain_ids); >> + xa_erase(&domain->iommu_array, iommu->seq_id); >> domain_update_iommu_cap(domain); >> - domain->iommu_did[iommu->seq_id] = 0; >> + kfree(info); > > domain->nid may still point to the node of the removed iommu. Good catch! I should assign it to NUMA_NO_NODE, so that it could be updated in the next domain_update_iommu_cap(). Updated as below: @@ -1806,6 +1809,7 @@ static void domain_detach_iommu(struct dmar_domain *domain, if (--info->refcnt == 0) { clear_bit(info->did, iommu->domain_ids); xa_erase(&domain->iommu_array, iommu->seq_id); + domain->nid = NUMA_NO_NODE; domain_update_iommu_cap(domain); kfree(info); } Best regards, baolu