Skip to content

Commit 2ba8336

Browse files
niklas88joergroedel
authored andcommitted
iommu/s390: Use RCU to allow concurrent domain_list iteration
The s390_domain->devices list is only added to when new devices are attached but is iterated through in read-only fashion for every mapping operation as well as for I/O TLB flushes and thus in performance critical code causing contention on the s390_domain->list_lock. Fortunately such a read-mostly linked list is a standard use case for RCU. This change closely follows the example fpr RCU protected list given in Documentation/RCU/listRCU.rst. Signed-off-by: Niklas Schnelle <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Joerg Roedel <[email protected]>
1 parent c228f5a commit 2ba8336

File tree

3 files changed

+29
-18
lines changed

3 files changed

+29
-18
lines changed

arch/s390/include/asm/pci.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ struct zpci_dev {
119119
struct list_head entry; /* list of all zpci_devices, needed for hotplug, etc. */
120120
struct list_head iommu_list;
121121
struct kref kref;
122+
struct rcu_head rcu;
122123
struct hotplug_slot hotplug_slot;
123124

124125
enum zpci_state state;

arch/s390/pci/pci.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ void zpci_release_device(struct kref *kref)
996996
break;
997997
}
998998
zpci_dbg(3, "rem fid:%x\n", zdev->fid);
999-
kfree(zdev);
999+
kfree_rcu(zdev, rcu);
10001000
}
10011001

10021002
int zpci_report_error(struct pci_dev *pdev,

drivers/iommu/s390-iommu.c

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <linux/iommu.h>
1111
#include <linux/iommu-helper.h>
1212
#include <linux/sizes.h>
13+
#include <linux/rculist.h>
14+
#include <linux/rcupdate.h>
1315
#include <asm/pci_dma.h>
1416

1517
static const struct iommu_ops s390_iommu_ops;
@@ -20,6 +22,7 @@ struct s390_domain {
2022
unsigned long *dma_table;
2123
spinlock_t dma_table_lock;
2224
spinlock_t list_lock;
25+
struct rcu_head rcu;
2326
};
2427

2528
static struct s390_domain *to_s390_domain(struct iommu_domain *dom)
@@ -61,18 +64,28 @@ static struct iommu_domain *s390_domain_alloc(unsigned domain_type)
6164

6265
spin_lock_init(&s390_domain->dma_table_lock);
6366
spin_lock_init(&s390_domain->list_lock);
64-
INIT_LIST_HEAD(&s390_domain->devices);
67+
INIT_LIST_HEAD_RCU(&s390_domain->devices);
6568

6669
return &s390_domain->domain;
6770
}
6871

72+
static void s390_iommu_rcu_free_domain(struct rcu_head *head)
73+
{
74+
struct s390_domain *s390_domain = container_of(head, struct s390_domain, rcu);
75+
76+
dma_cleanup_tables(s390_domain->dma_table);
77+
kfree(s390_domain);
78+
}
79+
6980
static void s390_domain_free(struct iommu_domain *domain)
7081
{
7182
struct s390_domain *s390_domain = to_s390_domain(domain);
7283

84+
rcu_read_lock();
7385
WARN_ON(!list_empty(&s390_domain->devices));
74-
dma_cleanup_tables(s390_domain->dma_table);
75-
kfree(s390_domain);
86+
rcu_read_unlock();
87+
88+
call_rcu(&s390_domain->rcu, s390_iommu_rcu_free_domain);
7689
}
7790

7891
static void __s390_iommu_detach_device(struct zpci_dev *zdev)
@@ -84,7 +97,7 @@ static void __s390_iommu_detach_device(struct zpci_dev *zdev)
8497
return;
8598

8699
spin_lock_irqsave(&s390_domain->list_lock, flags);
87-
list_del_init(&zdev->iommu_list);
100+
list_del_rcu(&zdev->iommu_list);
88101
spin_unlock_irqrestore(&s390_domain->list_lock, flags);
89102

90103
zpci_unregister_ioat(zdev, 0);
@@ -127,7 +140,7 @@ static int s390_iommu_attach_device(struct iommu_domain *domain,
127140
zdev->s390_domain = s390_domain;
128141

129142
spin_lock_irqsave(&s390_domain->list_lock, flags);
130-
list_add(&zdev->iommu_list, &s390_domain->devices);
143+
list_add_rcu(&zdev->iommu_list, &s390_domain->devices);
131144
spin_unlock_irqrestore(&s390_domain->list_lock, flags);
132145

133146
return 0;
@@ -203,14 +216,13 @@ static void s390_iommu_flush_iotlb_all(struct iommu_domain *domain)
203216
{
204217
struct s390_domain *s390_domain = to_s390_domain(domain);
205218
struct zpci_dev *zdev;
206-
unsigned long flags;
207219

208-
spin_lock_irqsave(&s390_domain->list_lock, flags);
209-
list_for_each_entry(zdev, &s390_domain->devices, iommu_list) {
220+
rcu_read_lock();
221+
list_for_each_entry_rcu(zdev, &s390_domain->devices, iommu_list) {
210222
zpci_refresh_trans((u64)zdev->fh << 32, zdev->start_dma,
211223
zdev->end_dma - zdev->start_dma + 1);
212224
}
213-
spin_unlock_irqrestore(&s390_domain->list_lock, flags);
225+
rcu_read_unlock();
214226
}
215227

216228
static void s390_iommu_iotlb_sync(struct iommu_domain *domain,
@@ -219,35 +231,33 @@ static void s390_iommu_iotlb_sync(struct iommu_domain *domain,
219231
struct s390_domain *s390_domain = to_s390_domain(domain);
220232
size_t size = gather->end - gather->start + 1;
221233
struct zpci_dev *zdev;
222-
unsigned long flags;
223234

224235
/* If gather was never added to there is nothing to flush */
225236
if (!gather->end)
226237
return;
227238

228-
spin_lock_irqsave(&s390_domain->list_lock, flags);
229-
list_for_each_entry(zdev, &s390_domain->devices, iommu_list) {
239+
rcu_read_lock();
240+
list_for_each_entry_rcu(zdev, &s390_domain->devices, iommu_list) {
230241
zpci_refresh_trans((u64)zdev->fh << 32, gather->start,
231242
size);
232243
}
233-
spin_unlock_irqrestore(&s390_domain->list_lock, flags);
244+
rcu_read_unlock();
234245
}
235246

236247
static void s390_iommu_iotlb_sync_map(struct iommu_domain *domain,
237248
unsigned long iova, size_t size)
238249
{
239250
struct s390_domain *s390_domain = to_s390_domain(domain);
240251
struct zpci_dev *zdev;
241-
unsigned long flags;
242252

243-
spin_lock_irqsave(&s390_domain->list_lock, flags);
244-
list_for_each_entry(zdev, &s390_domain->devices, iommu_list) {
253+
rcu_read_lock();
254+
list_for_each_entry_rcu(zdev, &s390_domain->devices, iommu_list) {
245255
if (!zdev->tlb_refresh)
246256
continue;
247257
zpci_refresh_trans((u64)zdev->fh << 32,
248258
iova, size);
249259
}
250-
spin_unlock_irqrestore(&s390_domain->list_lock, flags);
260+
rcu_read_unlock();
251261
}
252262

253263
static int s390_iommu_update_trans(struct s390_domain *s390_domain,

0 commit comments

Comments
 (0)