Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp3773200pxb; Mon, 1 Feb 2021 04:29:53 -0800 (PST) X-Google-Smtp-Source: ABdhPJzcUQp/HmX4J5aSo+xM2/BZm1gm3noI85VOCwdQ/hb1AUJdyXMMyWEWTw/ThcFs7pjrEPTL X-Received: by 2002:a05:6402:1809:: with SMTP id g9mr18385295edy.2.1612182593001; Mon, 01 Feb 2021 04:29:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612182592; cv=none; d=google.com; s=arc-20160816; b=HvkGyD4G+JX4DuhLZBTBV831DcB0lSGh+JFTu1Fpny96PGYuxYGzIIL3h+WR1gOf8F huOPbSYW+3Sevyk884phTqGps26v4myf3ms2QiSNdmEKXMxK692Kf1XfTJV7oDxQ78YZ K9dosKfYsLWoQ+1fpYkiVPOGfcarq+5ZLZYnT+PZgvQMUvNs3pzncwn/Xx7Pm4CDMy1o ejYsKI9agOWQwKm48l/abcurG/fEPd1xZ4T9EwXKg58NqaLPdDkmSdXvjT4Vx5CBg9S6 TUXjC4Rrz1KdAztfcr5uYVrMgTLXPcwDku4fsUFd0IbP8sDZfBYlwMvK+RmT/4yw3I9p EUdQ== 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 :mime-version:user-agent:date:message-id:from:cc:references:to :subject; bh=QHSMNn1cOe+7556aIqpi5qirXjHcWP7Zy5JPVVnqXuE=; b=CXHOUcCqHlLuoW1yN7vuAkqjHOCDQ0tdv2ZvWuzLkry0Jqh5Z7ZgJQUvThBW03HXxn N+QJf0s5dIZQ0F1zCcloDccWg9J09Hqk5YITBwms8smOMcyhhH9Zhcheol0973DwKbk6 RRkz98AKazP4G3tZEAyJpEYStz1ahwdxwvYmAtr25h8pSJ6R52nK+mgr4ptc5KWxrzZj LALwRE7gzYp8rIbAZ/dw0HtbopvromC/CSH0JLRKmBUPBz2derZIaUJEpzBbkj0GAKm0 YGN8uxx6OLb+SEYqJYuCXnNHetse1lrzsbmRhRSAfvxm2msPpryPeP+wd/CA2pGNY5Qf CX/Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c25si4671255edw.396.2021.02.01.04.29.26; Mon, 01 Feb 2021 04:29:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231356AbhBAM1n (ORCPT + 99 others); Mon, 1 Feb 2021 07:27:43 -0500 Received: from szxga04-in.huawei.com ([45.249.212.190]:11652 "EHLO szxga04-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229624AbhBAM11 (ORCPT ); Mon, 1 Feb 2021 07:27:27 -0500 Received: from DGGEMS402-HUB.china.huawei.com (unknown [172.30.72.60]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4DTnGG4rnkz162kv; Mon, 1 Feb 2021 20:25:26 +0800 (CST) Received: from [10.174.184.42] (10.174.184.42) by DGGEMS402-HUB.china.huawei.com (10.3.19.202) with Microsoft SMTP Server id 14.3.498.0; Mon, 1 Feb 2021 20:26:41 +0800 Subject: Re: [PATCH v13 03/15] iommu/arm-smmu-v3: Maintain a SID->device structure To: Eric Auger , , , , , , , , , , References: <20201118112151.25412-1-eric.auger@redhat.com> <20201118112151.25412-4-eric.auger@redhat.com> CC: , , , , , From: Keqian Zhu Message-ID: Date: Mon, 1 Feb 2021 20:26:41 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.7.1 MIME-Version: 1.0 In-Reply-To: <20201118112151.25412-4-eric.auger@redhat.com> Content-Type: text/plain; charset="windows-1252" Content-Transfer-Encoding: 7bit X-Originating-IP: [10.174.184.42] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Eric, On 2020/11/18 19:21, Eric Auger wrote: > From: Jean-Philippe Brucker > > When handling faults from the event or PRI queue, we need to find the > struct device associated to a SID. Add a rb_tree to keep track of SIDs. > > Signed-off-by: Jean-Philippe Brucker [...] > } > > +static int arm_smmu_insert_master(struct arm_smmu_device *smmu, > + struct arm_smmu_master *master) > +{ > + int i; > + int ret = 0; > + struct arm_smmu_stream *new_stream, *cur_stream; > + struct rb_node **new_node, *parent_node = NULL; > + struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(master->dev); > + > + master->streams = kcalloc(fwspec->num_ids, > + sizeof(struct arm_smmu_stream), GFP_KERNEL); > + if (!master->streams) > + return -ENOMEM; > + master->num_streams = fwspec->num_ids; This is not roll-backed when fail. > + > + mutex_lock(&smmu->streams_mutex); > + for (i = 0; i < fwspec->num_ids && !ret; i++) { Check ret at here, makes it hard to decide the start index of rollback. If we fail at here, then start index is (i-2). If we fail in the loop, then start index is (i-1). > + u32 sid = fwspec->ids[i]; > + > + new_stream = &master->streams[i]; > + new_stream->id = sid; > + new_stream->master = master; > + > + /* > + * Check the SIDs are in range of the SMMU and our stream table > + */ > + if (!arm_smmu_sid_in_range(smmu, sid)) { > + ret = -ERANGE; > + break; > + } > + > + /* Ensure l2 strtab is initialised */ > + if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) { > + ret = arm_smmu_init_l2_strtab(smmu, sid); > + if (ret) > + break; > + } > + > + /* Insert into SID tree */ > + new_node = &(smmu->streams.rb_node); > + while (*new_node) { > + cur_stream = rb_entry(*new_node, struct arm_smmu_stream, > + node); > + parent_node = *new_node; > + if (cur_stream->id > new_stream->id) { > + new_node = &((*new_node)->rb_left); > + } else if (cur_stream->id < new_stream->id) { > + new_node = &((*new_node)->rb_right); > + } else { > + dev_warn(master->dev, > + "stream %u already in tree\n", > + cur_stream->id); > + ret = -EINVAL; > + break; > + } > + } > + > + if (!ret) { > + rb_link_node(&new_stream->node, parent_node, new_node); > + rb_insert_color(&new_stream->node, &smmu->streams); > + } > + } > + > + if (ret) { > + for (; i > 0; i--) should be (i >= 0)? And the start index seems not correct. > + rb_erase(&master->streams[i].node, &smmu->streams); > + kfree(master->streams); > + } > + mutex_unlock(&smmu->streams_mutex); > + > + return ret; > +} > + > +static void arm_smmu_remove_master(struct arm_smmu_master *master) > +{ > + int i; > + struct arm_smmu_device *smmu = master->smmu; > + struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(master->dev); > + > + if (!smmu || !master->streams) > + return; > + > + mutex_lock(&smmu->streams_mutex); > + for (i = 0; i < fwspec->num_ids; i++) > + rb_erase(&master->streams[i].node, &smmu->streams); > + mutex_unlock(&smmu->streams_mutex); > + > + kfree(master->streams); > +} > + > static struct iommu_ops arm_smmu_ops; > > static struct iommu_device *arm_smmu_probe_device(struct device *dev) > { > - int i, ret; > + int ret; > struct arm_smmu_device *smmu; > struct arm_smmu_master *master; > struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); > @@ -2331,27 +2447,12 @@ static struct iommu_device *arm_smmu_probe_device(struct device *dev) > > master->dev = dev; > master->smmu = smmu; > - master->sids = fwspec->ids; > - master->num_sids = fwspec->num_ids; > INIT_LIST_HEAD(&master->bonds); > dev_iommu_priv_set(dev, master); > > - /* Check the SIDs are in range of the SMMU and our stream table */ > - for (i = 0; i < master->num_sids; i++) { > - u32 sid = master->sids[i]; > - > - if (!arm_smmu_sid_in_range(smmu, sid)) { > - ret = -ERANGE; > - goto err_free_master; > - } > - > - /* Ensure l2 strtab is initialised */ > - if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) { > - ret = arm_smmu_init_l2_strtab(smmu, sid); > - if (ret) > - goto err_free_master; > - } > - } > + ret = arm_smmu_insert_master(smmu, master); > + if (ret) > + goto err_free_master; > > master->ssid_bits = min(smmu->ssid_bits, fwspec->num_pasid_bits); > > @@ -2389,6 +2490,7 @@ static void arm_smmu_release_device(struct device *dev) > WARN_ON(arm_smmu_master_sva_enabled(master)); > arm_smmu_detach_dev(master); > arm_smmu_disable_pasid(master); > + arm_smmu_remove_master(master); > kfree(master); Thanks, Keqian