Skip to content

Commit 361d744

Browse files
committed
iommufd: Add iopt_area_alloc()
We never initialize the two interval tree nodes, and zero fill is not the same as RB_CLEAR_NODE. This can hide issues where we missed adding the area to the trees. Factor out the allocation and clear the two nodes. Fixes: 51fe614 ("iommufd: Data structure to provide IOVA to PFN mapping") Link: https://lore.kernel.org/r/20231030145035.GG691768@ziepe.ca Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
1 parent e7250ab commit 361d744

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

drivers/iommu/iommufd/io_pagetable.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,18 @@ static int iopt_insert_area(struct io_pagetable *iopt, struct iopt_area *area,
222222
return 0;
223223
}
224224

225+
static struct iopt_area *iopt_area_alloc(void)
226+
{
227+
struct iopt_area *area;
228+
229+
area = kzalloc(sizeof(*area), GFP_KERNEL_ACCOUNT);
230+
if (!area)
231+
return NULL;
232+
RB_CLEAR_NODE(&area->node.rb);
233+
RB_CLEAR_NODE(&area->pages_node.rb);
234+
return area;
235+
}
236+
225237
static int iopt_alloc_area_pages(struct io_pagetable *iopt,
226238
struct list_head *pages_list,
227239
unsigned long length, unsigned long *dst_iova,
@@ -232,7 +244,7 @@ static int iopt_alloc_area_pages(struct io_pagetable *iopt,
232244
int rc = 0;
233245

234246
list_for_each_entry(elm, pages_list, next) {
235-
elm->area = kzalloc(sizeof(*elm->area), GFP_KERNEL_ACCOUNT);
247+
elm->area = iopt_area_alloc();
236248
if (!elm->area)
237249
return -ENOMEM;
238250
}
@@ -1177,11 +1189,11 @@ static int iopt_area_split(struct iopt_area *area, unsigned long iova)
11771189
iopt_area_start_byte(area, new_start) & (alignment - 1))
11781190
return -EINVAL;
11791191

1180-
lhs = kzalloc(sizeof(*area), GFP_KERNEL_ACCOUNT);
1192+
lhs = iopt_area_alloc();
11811193
if (!lhs)
11821194
return -ENOMEM;
11831195

1184-
rhs = kzalloc(sizeof(*area), GFP_KERNEL_ACCOUNT);
1196+
rhs = iopt_area_alloc();
11851197
if (!rhs) {
11861198
rc = -ENOMEM;
11871199
goto err_free_lhs;

drivers/iommu/iommufd/pages.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,6 +1507,8 @@ void iopt_area_unfill_domains(struct iopt_area *area, struct iopt_pages *pages)
15071507
area, domain, iopt_area_index(area),
15081508
iopt_area_last_index(area));
15091509

1510+
if (IS_ENABLED(CONFIG_IOMMUFD_TEST))
1511+
WARN_ON(RB_EMPTY_NODE(&area->pages_node.rb));
15101512
interval_tree_remove(&area->pages_node, &pages->domains_itree);
15111513
iopt_area_unfill_domain(area, pages, area->storage_domain);
15121514
area->storage_domain = NULL;

0 commit comments

Comments
 (0)