Skip to content

Commit 07a3382

Browse files
joergroedelrafaeljw
authored andcommitted
PM / Hibernate: Add memory_rtree_find_bit function
Add a function to find a bit in the radix tree for a given pfn. Also add code to the memory bitmap wrapper functions to use the radix tree together with the existing memory bitmap implementation. On read accesses compare the results of both bitmaps to make sure the radix tree behaves the same way. Signed-off-by: Joerg Roedel <jroedel@suse.de> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1 parent f469f02 commit 07a3382

1 file changed

Lines changed: 81 additions & 3 deletions

File tree

‎kernel/power/snapshot.c‎

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,56 @@ static int memory_bm_find_bit(struct memory_bitmap *bm, unsigned long pfn,
720720
return 0;
721721
}
722722

723+
/*
724+
* memory_rtree_find_bit - Find the bit for pfn in the memory
725+
* bitmap
726+
*
727+
* Walks the radix tree to find the page which contains the bit for
728+
* pfn and returns the bit position in **addr and *bit_nr.
729+
*/
730+
static int memory_rtree_find_bit(struct memory_bitmap *bm, unsigned long pfn,
731+
void **addr, unsigned int *bit_nr)
732+
{
733+
struct mem_zone_bm_rtree *curr, *zone;
734+
struct rtree_node *node;
735+
int i, block_nr;
736+
737+
zone = NULL;
738+
739+
/* Find the right zone */
740+
list_for_each_entry(curr, &bm->zones, list) {
741+
if (pfn >= curr->start_pfn && pfn < curr->end_pfn) {
742+
zone = curr;
743+
break;
744+
}
745+
}
746+
747+
if (!zone)
748+
return -EFAULT;
749+
750+
/*
751+
* We have a zone. Now walk the radix tree to find the leave
752+
* node for our pfn.
753+
*/
754+
node = zone->rtree;
755+
block_nr = (pfn - zone->start_pfn) >> BM_BLOCK_SHIFT;
756+
757+
for (i = zone->levels; i > 0; i--) {
758+
int index;
759+
760+
index = block_nr >> ((i - 1) * BM_RTREE_LEVEL_SHIFT);
761+
index &= BM_RTREE_LEVEL_MASK;
762+
BUG_ON(node->data[index] == 0);
763+
node = (struct rtree_node *)node->data[index];
764+
}
765+
766+
/* Set return values */
767+
*addr = node->data;
768+
*bit_nr = (pfn - zone->start_pfn) & BM_BLOCK_MASK;
769+
770+
return 0;
771+
}
772+
723773
static void memory_bm_set_bit(struct memory_bitmap *bm, unsigned long pfn)
724774
{
725775
void *addr;
@@ -729,6 +779,10 @@ static void memory_bm_set_bit(struct memory_bitmap *bm, unsigned long pfn)
729779
error = memory_bm_find_bit(bm, pfn, &addr, &bit);
730780
BUG_ON(error);
731781
set_bit(bit, addr);
782+
783+
error = memory_rtree_find_bit(bm, pfn, &addr, &bit);
784+
BUG_ON(error);
785+
set_bit(bit, addr);
732786
}
733787

734788
static int mem_bm_set_bit_check(struct memory_bitmap *bm, unsigned long pfn)
@@ -740,6 +794,13 @@ static int mem_bm_set_bit_check(struct memory_bitmap *bm, unsigned long pfn)
740794
error = memory_bm_find_bit(bm, pfn, &addr, &bit);
741795
if (!error)
742796
set_bit(bit, addr);
797+
else
798+
return error;
799+
800+
error = memory_rtree_find_bit(bm, pfn, &addr, &bit);
801+
if (!error)
802+
set_bit(bit, addr);
803+
743804
return error;
744805
}
745806

@@ -752,25 +813,42 @@ static void memory_bm_clear_bit(struct memory_bitmap *bm, unsigned long pfn)
752813
error = memory_bm_find_bit(bm, pfn, &addr, &bit);
753814
BUG_ON(error);
754815
clear_bit(bit, addr);
816+
817+
error = memory_rtree_find_bit(bm, pfn, &addr, &bit);
818+
BUG_ON(error);
819+
clear_bit(bit, addr);
755820
}
756821

757822
static int memory_bm_test_bit(struct memory_bitmap *bm, unsigned long pfn)
758823
{
759824
void *addr;
760825
unsigned int bit;
761-
int error;
826+
int error, error2;
827+
int v;
762828

763829
error = memory_bm_find_bit(bm, pfn, &addr, &bit);
764830
BUG_ON(error);
765-
return test_bit(bit, addr);
831+
v = test_bit(bit, addr);
832+
833+
error2 = memory_rtree_find_bit(bm, pfn, &addr, &bit);
834+
BUG_ON(error2);
835+
836+
WARN_ON_ONCE(v != test_bit(bit, addr));
837+
838+
return v;
766839
}
767840

768841
static bool memory_bm_pfn_present(struct memory_bitmap *bm, unsigned long pfn)
769842
{
770843
void *addr;
771844
unsigned int bit;
845+
int present;
846+
847+
present = !memory_bm_find_bit(bm, pfn, &addr, &bit);
848+
849+
WARN_ON_ONCE(present != !memory_rtree_find_bit(bm, pfn, &addr, &bit));
772850

773-
return !memory_bm_find_bit(bm, pfn, &addr, &bit);
851+
return present;
774852
}
775853

776854
/**

0 commit comments

Comments
 (0)