Skip to content

Commit da6043f

Browse files
Andy Whitcroftrafaeljw
authored andcommitted
PM / hibernate: memory_bm_find_bit(): Tighten node optimisation
When looking for a bit by number we make use of the cached result from the preceding lookup to speed up operation. Firstly we check if the requested pfn is within the cached zone and if not lookup the new zone. We then check if the offset for that pfn falls within the existing cached node. This happens regardless of whether the node is within the zone we are now scanning. With certain memory layouts it is possible for this to false trigger creating a temporary alias for the pfn to a different bit. This leads the hibernation code to free memory which it was never allocated with the expected fallout. Ensure the zone we are scanning matches the cached zone before considering the cached node. Deep thanks go to Andrea for many, many, many hours of hacking and testing that went into cornering this bug. Reported-by: Andrea Righi <[email protected]> Tested-by: Andrea Righi <[email protected]> Signed-off-by: Andy Whitcroft <[email protected]> Signed-off-by: Rafael J. Wysocki <[email protected]>
1 parent 7d194c2 commit da6043f

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

kernel/power/snapshot.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,8 +734,15 @@ static int memory_bm_find_bit(struct memory_bitmap *bm, unsigned long pfn,
734734
* We have found the zone. Now walk the radix tree to find the leaf node
735735
* for our PFN.
736736
*/
737+
738+
/*
739+
* If the zone we wish to scan is the the current zone and the
740+
* pfn falls into the current node then we do not need to walk
741+
* the tree.
742+
*/
737743
node = bm->cur.node;
738-
if (((pfn - zone->start_pfn) & ~BM_BLOCK_MASK) == bm->cur.node_pfn)
744+
if (zone == bm->cur.zone &&
745+
((pfn - zone->start_pfn) & ~BM_BLOCK_MASK) == bm->cur.node_pfn)
739746
goto node_found;
740747

741748
node = zone->rtree;

0 commit comments

Comments
 (0)