linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] hmm-tests: Fix migrate_dirty_page test
@ 2022-09-13  5:22 Alistair Popple
  2022-09-13  7:21 ` Mika Penttilä
  2022-09-13  8:06 ` David Hildenbrand
  0 siblings, 2 replies; 5+ messages in thread
From: Alistair Popple @ 2022-09-13  5:22 UTC (permalink / raw)
  To: linux-mm, akpm
  Cc: John Hubbard, Peter Xu, Nadav Amit, huang ying, LKML,
	Sierra Guiza, Alejandro (Alex),
	Felix Kuehling, Jason Gunthorpe, David Hildenbrand,
	Ralph Campbell, Matthew Wilcox, Karol Herbst, Lyude Paul,
	Ben Skeggs, Logan Gunthorpe, paulus, linuxppc-dev,
	Alistair Popple

As noted by John Hubbard the original test relied on side effects of the
implementation of migrate_vma_setup() to detect if pages had been
swapped to disk or not. This is subject to change in future so
explicitly check for swap entries via pagemap instead. Fix a spelling
mistake while we're at it.

Signed-off-by: Alistair Popple <apopple@nvidia.com>
Fixes: 5cc88e844e87 ("selftests/hmm-tests: add test for dirty bits")
---
 tools/testing/selftests/vm/hmm-tests.c | 50 +++++++++++++++++++++++---
 1 file changed, 46 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/vm/hmm-tests.c b/tools/testing/selftests/vm/hmm-tests.c
index 70fdb49b59ed..b5f6a7dc1f12 100644
--- a/tools/testing/selftests/vm/hmm-tests.c
+++ b/tools/testing/selftests/vm/hmm-tests.c
@@ -1261,9 +1261,47 @@ static int destroy_cgroup(void)
 	return 0;
 }
 
+static uint64_t get_pfn(int fd, uint64_t ptr)
+{
+	uint64_t pfn;
+	int ret;
+
+	ret = pread(fd, &pfn, sizeof(ptr),
+		(uint64_t) ptr / getpagesize() * sizeof(ptr));
+	if (ret != sizeof(ptr))
+		return 0;
+
+	return pfn;
+}
+
+#define PAGEMAP_SWAPPED (1ULL << 62)
+
+/* Returns true if at least one page in the range is on swap */
+static bool pages_swapped(void *ptr, unsigned long pages)
+{
+	uint64_t pfn;
+	int fd = open("/proc/self/pagemap", O_RDONLY);
+	unsigned long i;
+
+	if (fd < 0)
+		return false;
+
+	for (i = 0; i < pages; i++) {
+		pfn = get_pfn(fd, (uint64_t) ptr + i * getpagesize());
+
+		if (pfn & PAGEMAP_SWAPPED) {
+			close(fd);
+			return true;
+		}
+	}
+
+	close(fd);
+	return false;
+}
+
 /*
  * Try and migrate a dirty page that has previously been swapped to disk. This
- * checks that we don't loose dirty bits.
+ * checks that we don't lose dirty bits.
  */
 TEST_F(hmm, migrate_dirty_page)
 {
@@ -1300,6 +1338,10 @@ TEST_F(hmm, migrate_dirty_page)
 
 	ASSERT_FALSE(write_cgroup_param(cgroup, "memory.reclaim", 1UL<<30));
 
+	/* Make sure at least some pages got paged to disk. */
+	if (!pages_swapped(buffer->ptr, npages))
+		SKIP(return, "Pages weren't swapped when they should have been");
+
 	/* Fault pages back in from swap as clean pages */
 	for (i = 0, ptr = buffer->ptr; i < size / sizeof(*ptr); ++i)
 		tmp += ptr[i];
@@ -1309,10 +1351,10 @@ TEST_F(hmm, migrate_dirty_page)
 		ptr[i] = i;
 
 	/*
-	 * Attempt to migrate memory to device, which should fail because
-	 * hopefully some pages are backed by swap storage.
+	 * Attempt to migrate memory to device. This might fail if some pages
+	 * are/were backed by swap but that's ok.
 	 */
-	ASSERT_TRUE(hmm_migrate_sys_to_dev(self->fd, buffer, npages));
+	hmm_migrate_sys_to_dev(self->fd, buffer, npages);
 
 	ASSERT_FALSE(write_cgroup_param(cgroup, "memory.reclaim", 1UL<<30));
 
-- 
2.35.1



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] hmm-tests: Fix migrate_dirty_page test
  2022-09-13  5:22 [PATCH] hmm-tests: Fix migrate_dirty_page test Alistair Popple
@ 2022-09-13  7:21 ` Mika Penttilä
  2022-09-13  8:06 ` David Hildenbrand
  1 sibling, 0 replies; 5+ messages in thread
From: Mika Penttilä @ 2022-09-13  7:21 UTC (permalink / raw)
  To: Alistair Popple, linux-mm, akpm
  Cc: John Hubbard, Peter Xu, Nadav Amit, huang ying, LKML,
	Sierra Guiza, Alejandro (Alex),
	Felix Kuehling, Jason Gunthorpe, David Hildenbrand,
	Ralph Campbell, Matthew Wilcox, Karol Herbst, Lyude Paul,
	Ben Skeggs, Logan Gunthorpe, paulus, linuxppc-dev



On 13.9.2022 8.22, Alistair Popple wrote:
> As noted by John Hubbard the original test relied on side effects of the
> implementation of migrate_vma_setup() to detect if pages had been
> swapped to disk or not. This is subject to change in future so
> explicitly check for swap entries via pagemap instead. Fix a spelling
> mistake while we're at it.
> 
> Signed-off-by: Alistair Popple <apopple@nvidia.com>
> Fixes: 5cc88e844e87 ("selftests/hmm-tests: add test for dirty bits")
> ---
>   tools/testing/selftests/vm/hmm-tests.c | 50 +++++++++++++++++++++++---
>   1 file changed, 46 insertions(+), 4 deletions(-)
> 
> diff --git a/tools/testing/selftests/vm/hmm-tests.c b/tools/testing/selftests/vm/hmm-tests.c
> index 70fdb49b59ed..b5f6a7dc1f12 100644
> --- a/tools/testing/selftests/vm/hmm-tests.c
> +++ b/tools/testing/selftests/vm/hmm-tests.c
> @@ -1261,9 +1261,47 @@ static int destroy_cgroup(void)
>   	return 0;
>   }
>   
> +static uint64_t get_pfn(int fd, uint64_t ptr)
> +{
> +	uint64_t pfn;
> +	int ret;
> +
> +	ret = pread(fd, &pfn, sizeof(ptr),
> +		(uint64_t) ptr / getpagesize() * sizeof(ptr));
> +	if (ret != sizeof(ptr))
> +		return 0;
> +
> +	return pfn;
> +}
> +
> +#define PAGEMAP_SWAPPED (1ULL << 62)
> +
> +/* Returns true if at least one page in the range is on swap */
> +static bool pages_swapped(void *ptr, unsigned long pages)
> +{
> +	uint64_t pfn;
> +	int fd = open("/proc/self/pagemap", O_RDONLY);
> +	unsigned long i;
> +
> +	if (fd < 0)
> +		return false;
> +
> +	for (i = 0; i < pages; i++) {
> +		pfn = get_pfn(fd, (uint64_t) ptr + i * getpagesize());
> +
> +		if (pfn & PAGEMAP_SWAPPED) {
> +			close(fd);
> +			return true;
> +		}
> +	}
> +
> +	close(fd);
> +	return false;
> +}
> +
>   /*
>    * Try and migrate a dirty page that has previously been swapped to disk. This
> - * checks that we don't loose dirty bits.
> + * checks that we don't lose dirty bits.
>    */
>   TEST_F(hmm, migrate_dirty_page)
>   {
> @@ -1300,6 +1338,10 @@ TEST_F(hmm, migrate_dirty_page)
>   
>   	ASSERT_FALSE(write_cgroup_param(cgroup, "memory.reclaim", 1UL<<30));
>   
> +	/* Make sure at least some pages got paged to disk. */
> +	if (!pages_swapped(buffer->ptr, npages))
> +		SKIP(return, "Pages weren't swapped when they should have been");
> +
>   	/* Fault pages back in from swap as clean pages */
>   	for (i = 0, ptr = buffer->ptr; i < size / sizeof(*ptr); ++i)
>   		tmp += ptr[i];
> @@ -1309,10 +1351,10 @@ TEST_F(hmm, migrate_dirty_page)
>   		ptr[i] = i;
>   
>   	/*
> -	 * Attempt to migrate memory to device, which should fail because
> -	 * hopefully some pages are backed by swap storage.
> +	 * Attempt to migrate memory to device. This might fail if some pages
> +	 * are/were backed by swap but that's ok.
>   	 */
> -	ASSERT_TRUE(hmm_migrate_sys_to_dev(self->fd, buffer, npages));
> +	hmm_migrate_sys_to_dev(self->fd, buffer, npages);
>   
>   	ASSERT_FALSE(write_cgroup_param(cgroup, "memory.reclaim", 1UL<<30));
>   


Reviewed-by: Mika Penttilä <mpenttil@redhat.com>



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] hmm-tests: Fix migrate_dirty_page test
  2022-09-13  5:22 [PATCH] hmm-tests: Fix migrate_dirty_page test Alistair Popple
  2022-09-13  7:21 ` Mika Penttilä
@ 2022-09-13  8:06 ` David Hildenbrand
  2022-09-13  8:20   ` Alistair Popple
  1 sibling, 1 reply; 5+ messages in thread
From: David Hildenbrand @ 2022-09-13  8:06 UTC (permalink / raw)
  To: Alistair Popple, linux-mm, akpm
  Cc: John Hubbard, Peter Xu, Nadav Amit, huang ying, LKML,
	Sierra Guiza, Alejandro (Alex),
	Felix Kuehling, Jason Gunthorpe, Ralph Campbell, Matthew Wilcox,
	Karol Herbst, Lyude Paul, Ben Skeggs, Logan Gunthorpe, paulus,
	linuxppc-dev

On 13.09.22 07:22, Alistair Popple wrote:
> As noted by John Hubbard the original test relied on side effects of the
> implementation of migrate_vma_setup() to detect if pages had been
> swapped to disk or not. This is subject to change in future so
> explicitly check for swap entries via pagemap instead. Fix a spelling
> mistake while we're at it.
> 
> Signed-off-by: Alistair Popple <apopple@nvidia.com>
> Fixes: 5cc88e844e87 ("selftests/hmm-tests: add test for dirty bits")
> ---
>   tools/testing/selftests/vm/hmm-tests.c | 50 +++++++++++++++++++++++---
>   1 file changed, 46 insertions(+), 4 deletions(-)
> 
> diff --git a/tools/testing/selftests/vm/hmm-tests.c b/tools/testing/selftests/vm/hmm-tests.c
> index 70fdb49b59ed..b5f6a7dc1f12 100644
> --- a/tools/testing/selftests/vm/hmm-tests.c
> +++ b/tools/testing/selftests/vm/hmm-tests.c
> @@ -1261,9 +1261,47 @@ static int destroy_cgroup(void)
>   	return 0;
>   }
>   
> +static uint64_t get_pfn(int fd, uint64_t ptr)
> +{
> +	uint64_t pfn;
> +	int ret;
> +
> +	ret = pread(fd, &pfn, sizeof(ptr),
> +		(uint64_t) ptr / getpagesize() * sizeof(ptr));
> +	if (ret != sizeof(ptr))
> +		return 0;
> +
> +	return pfn;
> +}
> +
> +#define PAGEMAP_SWAPPED (1ULL << 62)
> +
> +/* Returns true if at least one page in the range is on swap */
> +static bool pages_swapped(void *ptr, unsigned long pages)
> +{
> +	uint64_t pfn;
> +	int fd = open("/proc/self/pagemap", O_RDONLY);
> +	unsigned long i;
> +
> +	if (fd < 0)
> +		return false;
> +
> +	for (i = 0; i < pages; i++) {
> +		pfn = get_pfn(fd, (uint64_t) ptr + i * getpagesize());
> +
> +		if (pfn & PAGEMAP_SWAPPED) {
> +			close(fd);
> +			return true;
> +		}

We do have pagemap_get_entry() in vm_util.c to query the pagemap entry.

Can you further, add pagemap_is_swapped() to vm_util.c?

I'll be also needing that (including a variant for testing a range) in 
anon COW tests.


-- 
Thanks,

David / dhildenb



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] hmm-tests: Fix migrate_dirty_page test
  2022-09-13  8:06 ` David Hildenbrand
@ 2022-09-13  8:20   ` Alistair Popple
  2022-09-14 10:09     ` David Hildenbrand
  0 siblings, 1 reply; 5+ messages in thread
From: Alistair Popple @ 2022-09-13  8:20 UTC (permalink / raw)
  To: David Hildenbrand
  Cc: linux-mm, akpm, John Hubbard, Peter Xu, Nadav Amit, huang ying,
	LKML, Sierra Guiza, Alejandro (Alex),
	Felix Kuehling, Jason Gunthorpe, Ralph Campbell, Matthew Wilcox,
	Karol Herbst, Lyude Paul, Ben Skeggs, Logan Gunthorpe, paulus,
	linuxppc-dev


David Hildenbrand <david@redhat.com> writes:

> On 13.09.22 07:22, Alistair Popple wrote:
>> As noted by John Hubbard the original test relied on side effects of the
>> implementation of migrate_vma_setup() to detect if pages had been
>> swapped to disk or not. This is subject to change in future so
>> explicitly check for swap entries via pagemap instead. Fix a spelling
>> mistake while we're at it.
>> Signed-off-by: Alistair Popple <apopple@nvidia.com>
>> Fixes: 5cc88e844e87 ("selftests/hmm-tests: add test for dirty bits")
>> ---
>>   tools/testing/selftests/vm/hmm-tests.c | 50 +++++++++++++++++++++++---
>>   1 file changed, 46 insertions(+), 4 deletions(-)
>> diff --git a/tools/testing/selftests/vm/hmm-tests.c
>> b/tools/testing/selftests/vm/hmm-tests.c
>> index 70fdb49b59ed..b5f6a7dc1f12 100644
>> --- a/tools/testing/selftests/vm/hmm-tests.c
>> +++ b/tools/testing/selftests/vm/hmm-tests.c
>> @@ -1261,9 +1261,47 @@ static int destroy_cgroup(void)
>>   	return 0;
>>   }
>>   +static uint64_t get_pfn(int fd, uint64_t ptr)
>> +{
>> +	uint64_t pfn;
>> +	int ret;
>> +
>> +	ret = pread(fd, &pfn, sizeof(ptr),
>> +		(uint64_t) ptr / getpagesize() * sizeof(ptr));
>> +	if (ret != sizeof(ptr))
>> +		return 0;
>> +
>> +	return pfn;
>> +}
>> +
>> +#define PAGEMAP_SWAPPED (1ULL << 62)
>> +
>> +/* Returns true if at least one page in the range is on swap */
>> +static bool pages_swapped(void *ptr, unsigned long pages)
>> +{
>> +	uint64_t pfn;
>> +	int fd = open("/proc/self/pagemap", O_RDONLY);
>> +	unsigned long i;
>> +
>> +	if (fd < 0)
>> +		return false;
>> +
>> +	for (i = 0; i < pages; i++) {
>> +		pfn = get_pfn(fd, (uint64_t) ptr + i * getpagesize());
>> +
>> +		if (pfn & PAGEMAP_SWAPPED) {
>> +			close(fd);
>> +			return true;
>> +		}
>
> We do have pagemap_get_entry() in vm_util.c to query the pagemap entry.

Thanks. I'd missed that, although `grep pagemap
tools/testing/selftests/vm` suggests I'm not the first to follow a
tradition of open-coding this :-)

But there's no need to perpetuate that tradition, so will redo this to
use vm_util.c instead.

> Can you further, add pagemap_is_swapped() to vm_util.c?
>
> I'll be also needing that (including a variant for testing a range) in anon COW
> tests.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] hmm-tests: Fix migrate_dirty_page test
  2022-09-13  8:20   ` Alistair Popple
@ 2022-09-14 10:09     ` David Hildenbrand
  0 siblings, 0 replies; 5+ messages in thread
From: David Hildenbrand @ 2022-09-14 10:09 UTC (permalink / raw)
  To: Alistair Popple
  Cc: linux-mm, akpm, John Hubbard, Peter Xu, Nadav Amit, huang ying,
	LKML, Sierra Guiza, Alejandro (Alex),
	Felix Kuehling, Jason Gunthorpe, Ralph Campbell, Matthew Wilcox,
	Karol Herbst, Lyude Paul, Ben Skeggs, Logan Gunthorpe, paulus,
	linuxppc-dev

On 13.09.22 10:20, Alistair Popple wrote:
> 
> David Hildenbrand <david@redhat.com> writes:
> 
>> On 13.09.22 07:22, Alistair Popple wrote:
>>> As noted by John Hubbard the original test relied on side effects of the
>>> implementation of migrate_vma_setup() to detect if pages had been
>>> swapped to disk or not. This is subject to change in future so
>>> explicitly check for swap entries via pagemap instead. Fix a spelling
>>> mistake while we're at it.
>>> Signed-off-by: Alistair Popple <apopple@nvidia.com>
>>> Fixes: 5cc88e844e87 ("selftests/hmm-tests: add test for dirty bits")
>>> ---
>>>    tools/testing/selftests/vm/hmm-tests.c | 50 +++++++++++++++++++++++---
>>>    1 file changed, 46 insertions(+), 4 deletions(-)
>>> diff --git a/tools/testing/selftests/vm/hmm-tests.c
>>> b/tools/testing/selftests/vm/hmm-tests.c
>>> index 70fdb49b59ed..b5f6a7dc1f12 100644
>>> --- a/tools/testing/selftests/vm/hmm-tests.c
>>> +++ b/tools/testing/selftests/vm/hmm-tests.c
>>> @@ -1261,9 +1261,47 @@ static int destroy_cgroup(void)
>>>    	return 0;
>>>    }
>>>    +static uint64_t get_pfn(int fd, uint64_t ptr)
>>> +{
>>> +	uint64_t pfn;
>>> +	int ret;
>>> +
>>> +	ret = pread(fd, &pfn, sizeof(ptr),
>>> +		(uint64_t) ptr / getpagesize() * sizeof(ptr));
>>> +	if (ret != sizeof(ptr))
>>> +		return 0;
>>> +
>>> +	return pfn;
>>> +}
>>> +
>>> +#define PAGEMAP_SWAPPED (1ULL << 62)
>>> +
>>> +/* Returns true if at least one page in the range is on swap */
>>> +static bool pages_swapped(void *ptr, unsigned long pages)
>>> +{
>>> +	uint64_t pfn;
>>> +	int fd = open("/proc/self/pagemap", O_RDONLY);
>>> +	unsigned long i;
>>> +
>>> +	if (fd < 0)
>>> +		return false;
>>> +
>>> +	for (i = 0; i < pages; i++) {
>>> +		pfn = get_pfn(fd, (uint64_t) ptr + i * getpagesize());
>>> +
>>> +		if (pfn & PAGEMAP_SWAPPED) {
>>> +			close(fd);
>>> +			return true;
>>> +		}
>>
>> We do have pagemap_get_entry() in vm_util.c to query the pagemap entry.
> 
> Thanks. I'd missed that, although `grep pagemap
> tools/testing/selftests/vm` suggests I'm not the first to follow a
> tradition of open-coding this :-)
> 
> But there's no need to perpetuate that tradition, so will redo this to
> use vm_util.c instead.

Yeah, we just recently factored stuff out into there. I'll be factoring 
out more in my upcoming tests from the madv_populate tests.

-- 
Thanks,

David / dhildenb



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-09-14 10:09 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-13  5:22 [PATCH] hmm-tests: Fix migrate_dirty_page test Alistair Popple
2022-09-13  7:21 ` Mika Penttilä
2022-09-13  8:06 ` David Hildenbrand
2022-09-13  8:20   ` Alistair Popple
2022-09-14 10:09     ` David Hildenbrand

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox