Skip to content

Commit fc2c226

Browse files
urezkiakpm00
authored andcommitted
mm: vmalloc: fix lockdep warning
A lockdep reports a possible deadlock in the find_vmap_area_exceed_addr_lock() function: ============================================ WARNING: possible recursive locking detected 6.9.0-rc1-00060-ged3ccc57b108-dirty #6140 Not tainted -------------------------------------------- drgn/455 is trying to acquire lock: ffff0000c00131d0 (&vn->busy.lock/1){+.+.}-{2:2}, at: find_vmap_area_exceed_addr_lock+0x64/0x124 but task is already holding lock: ffff0000c0011878 (&vn->busy.lock/1){+.+.}-{2:2}, at: find_vmap_area_exceed_addr_lock+0x64/0x124 other info that might help us debug this: Possible unsafe locking scenario: CPU0 ---- lock(&vn->busy.lock/1); lock(&vn->busy.lock/1); *** DEADLOCK *** indeed it can happen if the find_vmap_area_exceed_addr_lock() gets called concurrently because it tries to acquire two nodes locks. It was done to prevent removing a lowest VA found on a previous step. To address this a lowest VA is found first without holding a node lock where it resides. As a last step we check if a VA still there because it can go away, if removed, proceed with next lowest. [[email protected]: fix comment typos, per Baoquan] Link: https://lkml.kernel.org/r/[email protected] Fixes: 53becf3 ("mm: vmalloc: support multiple nodes in vread_iter") Signed-off-by: Uladzislau Rezki (Sony) <[email protected]> Tested-by: Jens Axboe <[email protected]> Tested-by: Omar Sandoval <[email protected]> Reported-by: Jens Axboe <[email protected]> Cc: Baoquan He <[email protected]> Cc: Christoph Hellwig <[email protected]> Cc: Dave Chinner <[email protected]> Cc: Lorenzo Stoakes <[email protected]> Cc: Matthew Wilcox (Oracle) <[email protected]> Cc: Oleksiy Avramchenko <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 4ed91fa commit fc2c226

File tree

1 file changed

+43
-30
lines changed

1 file changed

+43
-30
lines changed

mm/vmalloc.c

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,27 @@ unsigned long vmalloc_nr_pages(void)
989989
return atomic_long_read(&nr_vmalloc_pages);
990990
}
991991

992+
static struct vmap_area *__find_vmap_area(unsigned long addr, struct rb_root *root)
993+
{
994+
struct rb_node *n = root->rb_node;
995+
996+
addr = (unsigned long)kasan_reset_tag((void *)addr);
997+
998+
while (n) {
999+
struct vmap_area *va;
1000+
1001+
va = rb_entry(n, struct vmap_area, rb_node);
1002+
if (addr < va->va_start)
1003+
n = n->rb_left;
1004+
else if (addr >= va->va_end)
1005+
n = n->rb_right;
1006+
else
1007+
return va;
1008+
}
1009+
1010+
return NULL;
1011+
}
1012+
9921013
/* Look up the first VA which satisfies addr < va_end, NULL if none. */
9931014
static struct vmap_area *
9941015
__find_vmap_area_exceed_addr(unsigned long addr, struct rb_root *root)
@@ -1025,47 +1046,39 @@ __find_vmap_area_exceed_addr(unsigned long addr, struct rb_root *root)
10251046
static struct vmap_node *
10261047
find_vmap_area_exceed_addr_lock(unsigned long addr, struct vmap_area **va)
10271048
{
1028-
struct vmap_node *vn, *va_node = NULL;
1029-
struct vmap_area *va_lowest;
1049+
unsigned long va_start_lowest;
1050+
struct vmap_node *vn;
10301051
int i;
10311052

1032-
for (i = 0; i < nr_vmap_nodes; i++) {
1053+
repeat:
1054+
for (i = 0, va_start_lowest = 0; i < nr_vmap_nodes; i++) {
10331055
vn = &vmap_nodes[i];
10341056

10351057
spin_lock(&vn->busy.lock);
1036-
va_lowest = __find_vmap_area_exceed_addr(addr, &vn->busy.root);
1037-
if (va_lowest) {
1038-
if (!va_node || va_lowest->va_start < (*va)->va_start) {
1039-
if (va_node)
1040-
spin_unlock(&va_node->busy.lock);
1041-
1042-
*va = va_lowest;
1043-
va_node = vn;
1044-
continue;
1045-
}
1046-
}
1058+
*va = __find_vmap_area_exceed_addr(addr, &vn->busy.root);
1059+
1060+
if (*va)
1061+
if (!va_start_lowest || (*va)->va_start < va_start_lowest)
1062+
va_start_lowest = (*va)->va_start;
10471063
spin_unlock(&vn->busy.lock);
10481064
}
10491065

1050-
return va_node;
1051-
}
1052-
1053-
static struct vmap_area *__find_vmap_area(unsigned long addr, struct rb_root *root)
1054-
{
1055-
struct rb_node *n = root->rb_node;
1066+
/*
1067+
* Check if found VA exists, it might have gone away. In this case we
1068+
* repeat the search because a VA has been removed concurrently and we
1069+
* need to proceed to the next one, which is a rare case.
1070+
*/
1071+
if (va_start_lowest) {
1072+
vn = addr_to_node(va_start_lowest);
10561073

1057-
addr = (unsigned long)kasan_reset_tag((void *)addr);
1074+
spin_lock(&vn->busy.lock);
1075+
*va = __find_vmap_area(va_start_lowest, &vn->busy.root);
10581076

1059-
while (n) {
1060-
struct vmap_area *va;
1077+
if (*va)
1078+
return vn;
10611079

1062-
va = rb_entry(n, struct vmap_area, rb_node);
1063-
if (addr < va->va_start)
1064-
n = n->rb_left;
1065-
else if (addr >= va->va_end)
1066-
n = n->rb_right;
1067-
else
1068-
return va;
1080+
spin_unlock(&vn->busy.lock);
1081+
goto repeat;
10691082
}
10701083

10711084
return NULL;

0 commit comments

Comments
 (0)