Skip to content

Commit 4965f56

Browse files
T Makphaibulchoketorvalds
authored andcommitted
kernel/resource.c: fix stack overflow in __reserve_region_with_split()
Using a recursive call add a non-conflicting region in __reserve_region_with_split() could result in a stack overflow in the case that the recursive calls are too deep. Convert the recursive calls to an iterative loop to avoid the problem. Tested on a machine containing 135 regions. The kernel no longer panicked with stack overflow. Also tested with code arbitrarily adding regions with no conflict, embedding two consecutive conflicts and embedding two non-consecutive conflicts. Signed-off-by: T Makphaibulchoke <tmac@hp.com> Reviewed-by: Ram Pai <linuxram@us.ibm.com> Cc: Paul Gortmaker <paul.gortmaker@gmail.com> Cc: Wei Yang <weiyang@linux.vnet.ibm.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent c99b684 commit 4965f56

1 file changed

Lines changed: 38 additions & 12 deletions

File tree

‎kernel/resource.c‎

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,7 @@ static void __init __reserve_region_with_split(struct resource *root,
763763
struct resource *parent = root;
764764
struct resource *conflict;
765765
struct resource *res = kzalloc(sizeof(*res), GFP_ATOMIC);
766+
struct resource *next_res = NULL;
766767

767768
if (!res)
768769
return;
@@ -772,21 +773,46 @@ static void __init __reserve_region_with_split(struct resource *root,
772773
res->end = end;
773774
res->flags = IORESOURCE_BUSY;
774775

775-
conflict = __request_resource(parent, res);
776-
if (!conflict)
777-
return;
776+
while (1) {
778777

779-
/* failed, split and try again */
780-
kfree(res);
778+
conflict = __request_resource(parent, res);
779+
if (!conflict) {
780+
if (!next_res)
781+
break;
782+
res = next_res;
783+
next_res = NULL;
784+
continue;
785+
}
781786

782-
/* conflict covered whole area */
783-
if (conflict->start <= start && conflict->end >= end)
784-
return;
787+
/* conflict covered whole area */
788+
if (conflict->start <= res->start &&
789+
conflict->end >= res->end) {
790+
kfree(res);
791+
WARN_ON(next_res);
792+
break;
793+
}
794+
795+
/* failed, split and try again */
796+
if (conflict->start > res->start) {
797+
end = res->end;
798+
res->end = conflict->start - 1;
799+
if (conflict->end < end) {
800+
next_res = kzalloc(sizeof(*next_res),
801+
GFP_ATOMIC);
802+
if (!next_res) {
803+
kfree(res);
804+
break;
805+
}
806+
next_res->name = name;
807+
next_res->start = conflict->end + 1;
808+
next_res->end = end;
809+
next_res->flags = IORESOURCE_BUSY;
810+
}
811+
} else {
812+
res->start = conflict->end + 1;
813+
}
814+
}
785815

786-
if (conflict->start > start)
787-
__reserve_region_with_split(root, start, conflict->start-1, name);
788-
if (conflict->end < end)
789-
__reserve_region_with_split(root, conflict->end+1, end, name);
790816
}
791817

792818
void __init reserve_region_with_split(struct resource *root,

0 commit comments

Comments
 (0)