Skip to content

Commit 4dbd258

Browse files
eaugerjoergroedel
authored andcommitted
iommu: Revisit iommu_insert_resv_region() implementation
Current implementation is recursive and in case of allocation failure the existing @Regions list is altered. A non recursive version looks better for maintainability and simplifies the error handling. We use a separate stack for overlapping segment merging. The elements are sorted by start address and then by type, if their start address match. Note this new implementation may change the region order of appearance in /sys/kernel/iommu_groups/<n>/reserved_regions files but this order has never been documented, see commit bc7d12b ("iommu: Implement reserved_regions iommu-group sysfs file"). Signed-off-by: Eric Auger <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Signed-off-by: Joerg Roedel <[email protected]>
1 parent 0d87308 commit 4dbd258

File tree

1 file changed

+47
-49
lines changed

1 file changed

+47
-49
lines changed

drivers/iommu/iommu.c

Lines changed: 47 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -286,60 +286,58 @@ static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
286286
* @new: new region to insert
287287
* @regions: list of regions
288288
*
289-
* The new element is sorted by address with respect to the other
290-
* regions of the same type. In case it overlaps with another
291-
* region of the same type, regions are merged. In case it
292-
* overlaps with another region of different type, regions are
293-
* not merged.
289+
* Elements are sorted by start address and overlapping segments
290+
* of the same type are merged.
294291
*/
295-
static int iommu_insert_resv_region(struct iommu_resv_region *new,
296-
struct list_head *regions)
292+
int iommu_insert_resv_region(struct iommu_resv_region *new,
293+
struct list_head *regions)
297294
{
298-
struct iommu_resv_region *region;
299-
phys_addr_t start = new->start;
300-
phys_addr_t end = new->start + new->length - 1;
301-
struct list_head *pos = regions->next;
302-
303-
while (pos != regions) {
304-
struct iommu_resv_region *entry =
305-
list_entry(pos, struct iommu_resv_region, list);
306-
phys_addr_t a = entry->start;
307-
phys_addr_t b = entry->start + entry->length - 1;
308-
int type = entry->type;
309-
310-
if (end < a) {
311-
goto insert;
312-
} else if (start > b) {
313-
pos = pos->next;
314-
} else if ((start >= a) && (end <= b)) {
315-
if (new->type == type)
316-
return 0;
317-
else
318-
pos = pos->next;
295+
struct iommu_resv_region *iter, *tmp, *nr, *top;
296+
LIST_HEAD(stack);
297+
298+
nr = iommu_alloc_resv_region(new->start, new->length,
299+
new->prot, new->type);
300+
if (!nr)
301+
return -ENOMEM;
302+
303+
/* First add the new element based on start address sorting */
304+
list_for_each_entry(iter, regions, list) {
305+
if (nr->start < iter->start ||
306+
(nr->start == iter->start && nr->type <= iter->type))
307+
break;
308+
}
309+
list_add_tail(&nr->list, &iter->list);
310+
311+
/* Merge overlapping segments of type nr->type in @regions, if any */
312+
list_for_each_entry_safe(iter, tmp, regions, list) {
313+
phys_addr_t top_end, iter_end = iter->start + iter->length - 1;
314+
315+
/* no merge needed on elements of different types than @nr */
316+
if (iter->type != nr->type) {
317+
list_move_tail(&iter->list, &stack);
318+
continue;
319+
}
320+
321+
/* look for the last stack element of same type as @iter */
322+
list_for_each_entry_reverse(top, &stack, list)
323+
if (top->type == iter->type)
324+
goto check_overlap;
325+
326+
list_move_tail(&iter->list, &stack);
327+
continue;
328+
329+
check_overlap:
330+
top_end = top->start + top->length - 1;
331+
332+
if (iter->start > top_end + 1) {
333+
list_move_tail(&iter->list, &stack);
319334
} else {
320-
if (new->type == type) {
321-
phys_addr_t new_start = min(a, start);
322-
phys_addr_t new_end = max(b, end);
323-
int ret;
324-
325-
list_del(&entry->list);
326-
entry->start = new_start;
327-
entry->length = new_end - new_start + 1;
328-
ret = iommu_insert_resv_region(entry, regions);
329-
kfree(entry);
330-
return ret;
331-
} else {
332-
pos = pos->next;
333-
}
335+
top->length = max(top_end, iter_end) - top->start + 1;
336+
list_del(&iter->list);
337+
kfree(iter);
334338
}
335339
}
336-
insert:
337-
region = iommu_alloc_resv_region(new->start, new->length,
338-
new->prot, new->type);
339-
if (!region)
340-
return -ENOMEM;
341-
342-
list_add_tail(&region->list, pos);
340+
list_splice(&stack, regions);
343341
return 0;
344342
}
345343

0 commit comments

Comments
 (0)