Skip to contentSkip to conversation

fix judgment error in shmem_is_huge()

PATCH In review Liu Yuntao 3 participants 6 messages Sep 9, 2021 – Sep 26, 2021

Subsystems (2)
lore
Share & tools
lore

Liu Yuntao

author
#0 lore raw

PATCH v2 1/1 fix judgment error in shmem_is_huge()

In the case of SHMEM_HUGE_WITHIN_SIZE, the page index is not rounded up correctly. When the page index points to the first page in a huge page, round_up() cannot bring it to the end of the huge page, but to the end of the previous one.

an example:
HPAGE_PMD_NR on my machine is 512(2 MB huge page size). After allcoating a 3000 KB buffer, I access it at location 2050 KB. In shmem_is_huge(), the corresponding index happens to be 512. After rounded up by HPAGE_PMD_NR, it will still be 512 which is smaller than i_size, and shmem_is_huge() will return true. As a result, my buffer takes an additional huge page, and that shouldn't happen when shmem_enabled is set to within_size.

Fixes f3f0e1d2150b2b ("khugepaged: add support of collapse for tmpfs/shmem pages")

---
V1->V2:
add simplification of the condition after round_up()

---
 mm/shmem.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/shmem.c +2 -2
index 88742953532c..b5860f4a2738 100644
@@ -490,9 +490,9 @@ bool shmem_is_huge(struct vm_area_struct *vma,
 	case SHMEM_HUGE_ALWAYS:
 		return true;
 	case SHMEM_HUGE_WITHIN_SIZE:
-		index = round_up(index, HPAGE_PMD_NR);
+		index = round_up(index + 1, HPAGE_PMD_NR);
 		i_size = round_up(i_size_read(inode), PAGE_SIZE);
-		if (i_size >= HPAGE_PMD_SIZE && (i_size >> PAGE_SHIFT) >= index)
+		if (i_size >> PAGE_SHIFT >= index)
 			return true;
 		fallthrough;
 	case SHMEM_HUGE_ADVISE:
Signature · 1 line
2.23.0
Acked-by on v2 1/1
#1 lore raw
Quoted 14 lines · Liu Yuntao

In the case of SHMEM_HUGE_WITHIN_SIZE, the page index is not rounded up correctly. When the page index points to the first page in a huge page, round_up() cannot bring it to the end of the huge page, but to the end of the previous one.

an example:
HPAGE_PMD_NR on my machine is 512(2 MB huge page size). After allcoating a 3000 KB buffer, I access it at location 2050 KB. In shmem_is_huge(), the corresponding index happens to be 512. After rounded up by HPAGE_PMD_NR, it will still be 512 which is smaller than i_size, and shmem_is_huge() will return true. As a result, my buffer takes an additional huge page, and that shouldn't happen when shmem_enabled is set to within_size.

Quoted 2 lines
Fixes f3f0e1d2150b2b ("khugepaged: add support of collapse for tmpfs/shmem pages")
Signature · 1 line
 Kirill A. Shutemov
maintainer Acked-by on v2 1/1
#2 lore raw
In reply to Liu Yuntao
Quoted 8 lines · Liu Yuntao

In the case of SHMEM_HUGE_WITHIN_SIZE, the page index is not rounded up correctly. When the page index points to the first page in a huge page, round_up() cannot bring it to the end of the huge page, but to the end of the previous one.

an example:
HPAGE_PMD_NR on my machine is 512(2 MB huge page size). After allcoating a 3000 KB buffer, I access it at location 2050 KB.

Your example is certainly helpful, but weird! It's not impossible, but wouldn't it be easier to understand if you said "2048 KB" there?

Quoted 5 lines

In shmem_is_huge(), the corresponding index happens to be 512. After rounded up by HPAGE_PMD_NR, it will still be 512 which is smaller than i_size, and shmem_is_huge() will return true. As a result, my buffer takes an additional huge page, and that shouldn't happen when shmem_enabled is set to within_size.

A colleague very recently opened my eyes to within_size on shmem_enabled:
I've always been dubious of both, but they can work quite well together.

Quoted 3 lines
Fixes f3f0e1d2150b2b ("khugepaged: add support of collapse for tmpfs/shmem pages")

Thanks, with a nice simplification from Kirill.

Ignore the comment I've added below - it's not worth worrying about.

Quoted 12 lines

---
V1->V2:
add simplification of the condition after round_up()

---
 mm/shmem.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/shmem.c +0 -0
index 88742953532c..b5860f4a2738 100644

mm/shmem.c bool shmem_is_huge(struct vm_area_struct *vma, · line 493

Quoted 5 lines
mm/shmem.c +1 -1
@@ -490,9 +490,9 @@ bool shmem_is_huge(struct vm_area_struct *vma,
 	case SHMEM_HUGE_ALWAYS:
 		return true;
 	case SHMEM_HUGE_WITHIN_SIZE:
-		index = round_up(index, HPAGE_PMD_NR);
+		index = round_up(index + 1, HPAGE_PMD_NR);

Even without your change, I notice now that there's a possibility of index wrapping to 0 on 32-bit architecture here. But nothing goes terribly wrong in that case: it is not worth worrying about here.

Quoted 8 lines
mm/shmem.c +1 -1
 		i_size = round_up(i_size_read(inode), PAGE_SIZE);
-		if (i_size >= HPAGE_PMD_SIZE && (i_size >> PAGE_SHIFT) >= index)
+		if (i_size >> PAGE_SHIFT >= index)
 			return true;
 		fallthrough;
 	case SHMEM_HUGE_ADVISE:
Signature · 1 line
2.23.0
maintainer
#3 lore raw
Quoted 24 lines · Hugh Dickins
Quoted 8 lines · Liu Yuntao

In the case of SHMEM_HUGE_WITHIN_SIZE, the page index is not rounded up correctly. When the page index points to the first page in a huge page, round_up() cannot bring it to the end of the huge page, but to the end of the previous one.

an example:
HPAGE_PMD_NR on my machine is 512(2 MB huge page size). After allcoating a 3000 KB buffer, I access it at location 2050 KB.

Your example is certainly helpful, but weird! It's not impossible, but wouldn't it be easier to understand if you said "2048 KB" there?

Quoted 5 lines

In shmem_is_huge(), the corresponding index happens to be 512. After rounded up by HPAGE_PMD_NR, it will still be 512 which is smaller than i_size, and shmem_is_huge() will return true. As a result, my buffer takes an additional huge page, and that shouldn't happen when shmem_enabled is set to within_size.

A colleague very recently opened my eyes to within_size on shmem_enabled:
I've always been dubious of both, but they can work quite well together.

Quoted 1 line
Quoted 6 lines
Quoted 2 lines
Fixes f3f0e1d2150b2b ("khugepaged: add support of collapse for tmpfs/shmem pages")

Thanks, with a nice simplification from Kirill.

Andrew has just sent this on to Linus - thanks - and that's fine:
no need to get in the way of that.

But since replying, I have remembered more history, and there is something that we need to be aware of.

Whereas to you this is a straightforward off-by-one (or off-by-page) fix, it also results in a significant change in behaviour - I'd say usually for the better, but some might be surprised. This patch has Kirill's Ack and my Ack, and I hope and believe that we can get away with the change in behaviour: but let's be aware of it.

The change that concerns me is when, for example, copying a large file into a huge=within_size tmpfs (or more generally, just writing to the file by appending at EOF in the usual way).

With the old WITHIN_SIZE code, the first 2MB was allocated in small pages, then subsequent 2MB extents were allocated with huge pages; including the final extent, even if it only needed a single byte.

I always thought that was very clunky behaviour, the small pages coming at the wrong end of the file; and that's why I was dubious about it as a sensible filesystem mount option. But I was under the impression that it was the intended behaviour.

With your new WITHIN_SIZE code, all those appending allocations are outside i_size, and the whole file is allocated in small pages. (Then maybe later on khugepaged can assemble huge pages for it.)

Your patch makes within_size more sensible than it was for pre-sized files (and I think it's fair to say that the majority of files in shmem's internal mount, subject to thp/shmem_enabled, are likely to be fixed-size files); and better-defined than it used to be on growing files, but they won't get the huge pages they used to.

Hugh

author
#4 lore raw
Quoted 8 lines · Hugh Dickins
Quoted 7 lines · Hugh Dickins
Quoted 5 lines · Liu Yuntao

In the case of SHMEM_HUGE_WITHIN_SIZE, the page index is not rounded up correctly. When the page index points to the first page in a huge page, round_up() cannot bring it to the end of the huge page, but to the end of the previous one.

Quoted 6 lines
Quoted 6 lines
Quoted 3 lines

an example:
HPAGE_PMD_NR on my machine is 512(2 MB huge page size). After allcoating a 3000 KB buffer, I access it at location 2050 KB.

Your example is certainly helpful, but weird! It's not impossible, but wouldn't it be easier to understand if you said "2048 KB" there?

I wanted to emphasize that access to any bit in the first page will trigger this problem, so I didn't use "2048 KB".

Quoted 47 lines
Quoted 11 lines
Quoted 6 lines

In shmem_is_huge(), the corresponding index happens to be 512. After rounded up by HPAGE_PMD_NR, it will still be 512 which is smaller than i_size, and shmem_is_huge() will return true. As a result, my buffer takes an additional huge page, and that shouldn't happen when shmem_enabled is set to within_size.

A colleague very recently opened my eyes to within_size on shmem_enabled:
I've always been dubious of both, but they can work quite well together.

Quoted 1 line
Quoted 6 lines
Quoted 2 lines
Fixes f3f0e1d2150b2b ("khugepaged: add support of collapse for tmpfs/shmem pages")

Thanks, with a nice simplification from Kirill.

Andrew has just sent this on to Linus - thanks - and that's fine:
no need to get in the way of that.

But since replying, I have remembered more history, and there is something that we need to be aware of.

Whereas to you this is a straightforward off-by-one (or off-by-page) fix, it also results in a significant change in behaviour - I'd say usually for the better, but some might be surprised. This patch has Kirill's Ack and my Ack, and I hope and believe that we can get away with the change in behaviour: but let's be aware of it.

The change that concerns me is when, for example, copying a large file into a huge=within_size tmpfs (or more generally, just writing to the file by appending at EOF in the usual way).

With the old WITHIN_SIZE code, the first 2MB was allocated in small pages, then subsequent 2MB extents were allocated with huge pages; including the final extent, even if it only needed a single byte.

I always thought that was very clunky behaviour, the small pages coming at the wrong end of the file; and that's why I was dubious about it as a sensible filesystem mount option. But I was under the impression that it was the intended behaviour.

With your new WITHIN_SIZE code, all those appending allocations are outside i_size, and the whole file is allocated in small pages. (Then maybe later on khugepaged can assemble huge pages for it.)

Quoted 5 lines

Your patch makes within_size more sensible than it was for pre-sized files (and I think it's fair to say that the majority of files in shmem's internal mount, subject to thp/shmem_enabled, are likely to be fixed-size files); and better-defined than it used to be on growing files, but they won't get the huge pages they used to.

Although my patch changes shmem's behaviour, it makes shmem consistent with the documentation. I think with the new code, it will be easier for our users to understand.

Quoted 2 lines

Hugh

maintainer
#5 lore raw
Quoted 13 lines · liuyuntao
Quoted 12 lines · Hugh Dickins
Quoted 11 lines · Hugh Dickins
Quoted 8 lines · Liu Yuntao

In the case of SHMEM_HUGE_WITHIN_SIZE, the page index is not rounded up correctly. When the page index points to the first page in a huge page, round_up() cannot bring it to the end of the huge page, but to the end of the previous one.

an example:
HPAGE_PMD_NR on my machine is 512(2 MB huge page size). After allcoating a 3000 KB buffer, I access it at location 2050 KB.

Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

Your example is certainly helpful, but weird! It's not impossible, but wouldn't it be easier to understand if you said "2048 KB" there?

I wanted to emphasize that access to any bit in the first page will trigger this problem, so I didn't use "2048 KB".

Okay, thanks, I see your point now. (And I have to admit that, in my confusion, I had thought 2050 KB would be index 514 - of course not!)

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

In shmem_is_huge(), the corresponding index happens to be 512.

...

Quoted 9 lines
Quoted 5 lines

Your patch makes within_size more sensible than it was for pre-sized files (and I think it's fair to say that the majority of files in shmem's internal mount, subject to thp/shmem_enabled, are likely to be fixed-size files); and better-defined than it used to be on growing files, but they won't get the huge pages they used to.

Although my patch changes shmem's behaviour, it makes shmem consistent with the documentation. I think with the new code, it will be easier for our users to understand.

Yes, I do agree with you. But the change in behaviour when appending at EOF is significant, and needed to be called out - I think none of quite realized that effect at first.

Hugh