hugetlb_no_page might break hugetlb quota
Subsystems (2)
PATCH 1/1 hugetlb_no_page might break hugetlb quota
From: Zhang Yanmin <[email protected]>
In function hugetlb_no_page, backout path always calls hugetlb_put_quota. It's incorrect when find_lock_page gets the page or the new page is added into page cache.
In addition, if the vma->vm_flags doesn't include VM_SHARED, the quota shouldn't be decreased.
My patch against 2.6.16-rc5-mm2 fixes it.
---
mm/hugetlb.c +3 -3
@@ -562,11 +562,12 @@ int hugetlb_no_page(struct mm_struct *mm retry: page =find_lock_page (mapping, idx);if (!page) { -if (hugetlb_get_quota (mapping)) +if (vma->vm_flags &VM_SHARED &&hugetlb_get_quota (mapping)) goto out; page =alloc_huge_page (vma, address);if (!page) { -hugetlb_put_quota (mapping); +if (vma->vm_flags &VM_SHARED ) +hugetlb_put_quota (mapping); ret =VM_FAULT_OOM ; goto out; }
@@ -613,7 +614,6 @@ out: backout:spin_unlock (&mm->page_table_lock); -hugetlb_put_quota (mapping);unlock_page (page);put_page (page); goto out;
Zhang, Yanmin wrote on Sunday, March 05, 2006 10:22 PM
Quoted 3 lines
In function
hugetlb_no_page, backout path always callshugetlb_put_quota. It's incorrect whenfind_lock_pagegets the page or the new page is added into page cache.
While I acknowledge the bug, this patch is not complete. It makes file system quota consistent with respect to page cache state. But such quota (more severely, the page cache state) is still buggy, for example under ftruncate case: if one ftrucate hugetlb file and then tries to fault a page outside ftruncate area, a new hugetlb page is allocated and then added into page cache along with file system quota; and at the end returning VM_FAULT_SIGBUS. In this case, kernel traps an unreachable page until possibly next mmap that extends it. That need to be fixed. Which means we will be adding back conditional call to hugetlb_put_quota(mapping) in the backout path.
Quoted 2 lines
In addition, if the
vma->vm_flagsdoesn't includeVM_SHARED, the quota shouldn't be decreased.
Why? Private hugetlb page should be charged against the quota. Or is there a better reason not to do so?
- Ken
Quoted 5 lines · Kenneth W
Zhang, Yanmin wrote on Sunday, March 05, 2006 10:22 PM
Quoted 3 lines
In function
hugetlb_no_page, backout path always callshugetlb_put_quota. It's incorrect whenfind_lock_pagegets the page or the new page is added into page cache.
Quoted 8 lines
While I acknowledge the bug, this patch is not complete. It makes file system quota consistent with respect to page cache state. But such quota (more severely, the page cache state) is still buggy, for example under ftruncate case: if one ftrucate hugetlb file and then tries to fault a page outside ftruncate area, a new hugetlb page is allocated and then added into page cache along with file system quota; and at the end returning
VM_FAULT_SIGBUS. In this case, kernel traps an unreachable page until possibly next mmap that extends it. That need to be fixed.
I have another patch to fix it. The second patch is to delete checking (!(vma->vm_flags & VM_WRITE) && len > inode->i_size) in function hugetlbfs_file_mmap, and add a checking in hugetlb_no_page,
to implement a capability for application to mmap
an zero-length huge page area. It's useful for process to protect one area. As a side effect, the second patch also fixes the problem you said.
Quoted 9 lines
Which means we will be adding back conditional call to
hugetlb_put_quota(mapping)in the backout path.Quoted 2 lines
In addition, if the
vma->vm_flagsdoesn't includeVM_SHARED, the quota shouldn't be decreased.Why? Private hugetlb page should be charged against the quota. Or is there a better reason not to do so?
I checked tmpfs and other fs. Most of them don't charge private page against the quota.
Quoted 14 lines · Yanmin
Quoted 5 lines · Kenneth W
Zhang, Yanmin wrote on Sunday, March 05, 2006 10:22 PM
Quoted 3 lines
In function
hugetlb_no_page, backout path always callshugetlb_put_quota. It's incorrect whenfind_lock_pagegets the page or the new page is added into page cache.Quoted 8 lines
While I acknowledge the bug, this patch is not complete. It makes file system quota consistent with respect to page cache state. But such quota (more severely, the page cache state) is still buggy, for example under ftruncate case: if one ftrucate hugetlb file and then tries to fault a page outside ftruncate area, a new hugetlb page is allocated and then added into page cache along with file system quota; and at the end returning
VM_FAULT_SIGBUS. In this case, kernel traps an unreachable page until possibly next mmap that extends it. That need to be fixed.
Quoted 7 lines
I have another patch to fix it. The second patch is to delete checking (!(vma->vm_flags &
VM_WRITE) && len >inode->i_size) in functionhugetlbfs_file_mmap, and add a checking inhugetlb_no_page,
to implement a capability for application to mmap
an zero-length huge page area. It's useful for process to protect one area. As a side effect, the second patch also fixes the problem you said.
Here is the second patch.
From: Zhang Yanmin <[email protected]>
Sometimes, applications need below call to be successful although "/mnt/hugepages/file1" doesn't exist.
fd = open("/mnt/hugepages/file1", O_CREAT|O_RDWR, 0755); *addr = mmap(NULL, 0x1024*1024*256, PROT_NONE, MAP_SHARED, fd, 0);
As for regular pages (or files), above call does work, but as for huge pages, above call would fail because hugetlbfs_file_mmap would fail if (!(vma->vm_flags & VM_WRITE) && len > inode->i_size).
This capability on huge page is useful on ia64 when the process wants to protect one area on region 4, so other threads couldn't read/write this area.
My patch against 2.6.16-rc5-mm3 implements it.
In addition, my patch fixes a bug caught by Ken. Consider below scenario:
Process A mmaps a hugetlb file with 5 huge pages, then process B truncates the file with length of 2 huge pages. Later on, process A still could fault on huge page 3-5, and these huge pages would be added into page cache. Then, no processes could access these huge pages. My patch fixes it by moving checking (idx >= size) to the begining of function hugetlb_no_page.
Thanks for Ken's good suggestions.
---
fs/hugetlbfs/inode.c +0 -2
@@ -84,8 +84,6 @@ static int hugetlbfs_file_mmap(struct fi ret = -ENOMEM ; len = vma_len + ((loff_t )vma->vm_pgoff <<PAGE_SHIFT ); -if (!(vma->vm_flags &VM_WRITE ) && len > inode->i_size) - goto out;if (vma->vm_flags &VM_MAYSHARE )if (hugetlb_extend_reservation (info, len >>HPAGE_SHIFT ) !=0 )
mm/hugetlb.c +4 -3
@@ -555,6 +555,10 @@ int hugetlb_no_page(struct mm_struct *mm idx = ((address - vma->vm_start) >>HPAGE_SHIFT ) + (vma->vm_pgoff >> (HPAGE_SHIFT -PAGE_SHIFT )); + size =i_size_read (mapping->host) >>HPAGE_SHIFT ; +if (idx >= size) +return ret; +/* * Use page lock to guard against racing truncation * before we get page_table_lock.
@@ -588,9 +592,6 @@ retry: }spin_lock (&mm->page_table_lock); - size =i_size_read (mapping->host) >>HPAGE_SHIFT ; -if (idx >= size) - goto backout; ret =VM_FAULT_MINOR ;if (!pte_none (*ptep))
PATCH 1/1 ftruncate on huge page couldn't extend hugetlb file
From: Zhang Yanmin <[email protected]>
Currently, ftruncate on hugetlb files couldn't extend them. My patch enables it.
This patch is against 2.6.16-rc5-mm3 and on the top of the patch which implements mmap on zero-length hugetlb files with PROT_NONE.
Thanks for Ken's good idea.
---
fs/hugetlbfs/inode.c +14 -10
@@ -308,18 +308,22 @@ static int hugetlb_vmtruncate(struct inounsigned long pgoff;struct address_space *mapping = inode->i_mapping; -if (offset > inode->i_size) -return -EINVAL ; -BUG_ON (offset & ~HPAGE_MASK ); pgoff = offset >>HPAGE_SHIFT ; - - inode->i_size = offset; -spin_lock (&mapping->i_mmap_lock); -if (!prio_tree_empty (&mapping->i_mmap)) -hugetlb_vmtruncate_list (&mapping->i_mmap, pgoff); -spin_unlock (&mapping->i_mmap_lock); -truncate_hugepages (inode, offset); +if (offset > inode->i_size) { +if (hugetlb_extend_reservation (HUGETLBFS_I (inode), pgoff) !=0 ) +return -ENOMEM ; + inode->i_size = offset; + } +else { + + inode->i_size = offset; +spin_lock (&mapping->i_mmap_lock); +if (!prio_tree_empty (&mapping->i_mmap)) +hugetlb_vmtruncate_list (&mapping->i_mmap, pgoff); +spin_unlock (&mapping->i_mmap_lock); +truncate_hugepages (inode, offset); + }return 0 ; }
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [PATCH] ftruncate on huge page couldn't extend hugetlb file
Quoted 9 lines · Andrew Morton
Review
Quoted 6 lines · Yanmin
(quoted diff) +0
-1- if (!prio_tree_empty(&mapping->i_mmap)) - hugetlb_vmtruncate_list(&mapping->i_mmap, pgoff); - spin_unlock(&mapping->i_mmap_lock); - truncate_hugepages(inode, offset); + if (offset > inode->i_size) {whitespace is broken here.
Sorry for the stupid problem.
Quoted 6 lines
} else {Quoted 2 lines
+
+inode->i_size= offset;
i_size_write(), unlessi_semis held. I guess it is, so OK.
Yes.
Quoted 8 lines
Quoted 8 lines
+
spin_lock(&mapping->i_mmap_lock);
+ if (!prio_tree_empty(&mapping->i_mmap))
+hugetlb_vmtruncate_list(&mapping->i_mmap, pgoff);
+spin_unlock(&mapping->i_mmap_lock);
+truncate_hugepages(inode, offset);
+ }
return 0;
Here is the new patch.
From: Zhang Yanmin <[email protected]>
Currently, ftruncate on hugetlb files couldn't extend them. My patch enables it.
This patch is against 2.6.16-rc5-mm3 and on the top of the patch which implements mmap on zero-length hugetlb files with PROT_NONE.
Thanks for Ken's good idea.
---
fs/hugetlbfs/inode.c +12 -10
@@ -308,18 +308,20 @@ static int hugetlb_vmtruncate(struct inounsigned long pgoff;struct address_space *mapping = inode->i_mapping; -if (offset > inode->i_size) -return -EINVAL ; -BUG_ON (offset & ~HPAGE_MASK ); pgoff = offset >>HPAGE_SHIFT ; - - inode->i_size = offset; -spin_lock (&mapping->i_mmap_lock); -if (!prio_tree_empty (&mapping->i_mmap)) -hugetlb_vmtruncate_list (&mapping->i_mmap, pgoff); -spin_unlock (&mapping->i_mmap_lock); -truncate_hugepages (inode, offset); +if (offset > inode->i_size) { +if (hugetlb_extend_reservation (HUGETLBFS_I (inode), pgoff) !=0 ) +return -ENOMEM ; + inode->i_size = offset; + }else { + inode->i_size = offset; +spin_lock (&mapping->i_mmap_lock); +if (!prio_tree_empty (&mapping->i_mmap)) +hugetlb_vmtruncate_list (&mapping->i_mmap, pgoff); +spin_unlock (&mapping->i_mmap_lock); +truncate_hugepages (inode, offset); + }return 0 ; }
RE: [PATCH] ftruncate on huge page couldn't extend hugetlb file
In reply to Zhang, YanminZhang, Yanmin wrote on Tuesday, March 07, 2006 7:25 PM
Quoted 18 lines
Currently, ftruncate on hugetlb files couldn't extend them. My patch enables it.
This patch is against 2.6.16-rc5-mm3 and on the top of the patch which implements mmap on zero-length hugetlb files with
PROT_NONE.fs/hugetlbfs/inode.c +6
-7- - inode->i_size = offset; -spin_lock (&mapping->i_mmap_lock); -if (!prio_tree_empty (&mapping->i_mmap)) -hugetlb_vmtruncate_list (&mapping->i_mmap, pgoff); -spin_unlock (&mapping->i_mmap_lock); -truncate_hugepages (inode, offset); +if (offset > inode->i_size) { +if (hugetlb_extend_reservation (HUGETLBFS_I (inode), pgoff) !=0 ) +return -ENOMEM ; + inode->i_size = offset; + } +else {
fs/hugetlbfs/inode.c static int hugetlb_vmtruncate(struct ino · line 328
Quoted 10 lines
fs/hugetlbfs/inode.c +8
-0+ + inode->i_size = offset; +spin_lock (&mapping->i_mmap_lock); +if (!prio_tree_empty (&mapping->i_mmap)) +hugetlb_vmtruncate_list (&mapping->i_mmap, pgoff); +spin_unlock (&mapping->i_mmap_lock); +truncate_hugepages (inode, offset); + }return 0 ; }
Hmm?? I don't think you need to extend the reservation when extending hugetlb file via ftruncate. You don't have any vma that pass beyond current size. So making a reservation is a wrong thing to do here.
- Ken
Re: [PATCH] ftruncate on huge page couldn't extend hugetlb file
Quoted 25 lines · Kenneth W
Zhang, Yanmin wrote on Tuesday, March 07, 2006 7:25 PM
Quoted 24 lines
Currently, ftruncate on hugetlb files couldn't extend them. My patch enables it.
This patch is against 2.6.16-rc5-mm3 and on the top of the patch which implements mmap on zero-length hugetlb files with
PROT_NONE.fs/hugetlbfs/inode.c +12
-7- - inode->i_size = offset; -spin_lock (&mapping->i_mmap_lock); -if (!prio_tree_empty (&mapping->i_mmap)) -hugetlb_vmtruncate_list (&mapping->i_mmap, pgoff); -spin_unlock (&mapping->i_mmap_lock); -truncate_hugepages (inode, offset); +if (offset > inode->i_size) { +if (hugetlb_extend_reservation (HUGETLBFS_I (inode), pgoff) !=0 ) +return -ENOMEM ; + inode->i_size = offset; + } +else { + + inode->i_size = offset; +spin_lock (&mapping->i_mmap_lock); +if (!prio_tree_empty (&mapping->i_mmap)) +hugetlb_vmtruncate_list (&mapping->i_mmap, pgoff); +spin_unlock (&mapping->i_mmap_lock);
Quoted 8 lines
fs/hugetlbfs/inode.c static int hugetlb_vmtruncate(struct ino · line 328
Quoted 4 lines
fs/hugetlbfs/inode.c +2
-0+truncate_hugepages (inode, offset); + }return 0 ; }Hmm?? I don't think you need to extend the reservation when extending hugetlb file via ftruncate. You don't have any vma that pass beyond current size. So making a reservation is a wrong thing to do here.
Fwiw, I think truncate *should* extend the reservation. We have a separate thread arguing about whether we should be reserving by inode length, as I've implemented, or by which ranges are actually mapped (as apw's old path implemented). As long as it *is* by inode length - so it's conceptually all about the logical file in hugetlbfs, not about any of its mappings - I think it makes sense for an extending truncate() to extend the reservation. It's not reserving them for any particular mapping, it's reserving them for page cache pages.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
RE: [PATCH] ftruncate on huge page couldn't extend hugetlb file
David Gibson wrote on Wednesday, March 08, 2006 3:58 PM
Quoted 4 lines
Quoted 3 lines
Hmm?? I don't think you need to extend the reservation when extending hugetlb file via ftruncate. You don't have any vma that pass beyond current size. So making a reservation is a wrong thing to do here.
Quoted 8 lines
Fwiw, I think truncate *should* extend the reservation. We have a separate thread arguing about whether we should be reserving by inode length, as I've implemented, or by which ranges are actually mapped (as apw's old path implemented). As long as it *is* by inode length - so it's conceptually all about the logical file in hugetlbfs, not about any of its mappings - I think it makes sense for an extending
truncate()to extend the reservation. It's not reserving them for any particular mapping, it's reserving them for page cache pages.
But you already make reservation at mmap time. If you reserve it again when extending the file, won't you double count?
- Ken
Re: [PATCH] ftruncate on huge page couldn't extend hugetlb file
Quoted 14 lines · Kenneth W
David Gibson wrote on Wednesday, March 08, 2006 3:58 PM
Quoted 4 lines
Quoted 3 lines
Hmm?? I don't think you need to extend the reservation when extending hugetlb file via ftruncate. You don't have any vma that pass beyond current size. So making a reservation is a wrong thing to do here.
Quoted 8 lines
Fwiw, I think truncate *should* extend the reservation. We have a separate thread arguing about whether we should be reserving by inode length, as I've implemented, or by which ranges are actually mapped (as apw's old path implemented). As long as it *is* by inode length - so it's conceptually all about the logical file in hugetlbfs, not about any of its mappings - I think it makes sense for an extending
truncate()to extend the reservation. It's not reserving them for any particular mapping, it's reserving them for page cache pages.
Quoted 2 lines
But you already make reservation at mmap time. If you reserve it again when extending the file, won't you double count?
Well, I'd generally expect extending truncate() to come before mmap(), but in any case hugetlb_extend_reservation() is safe against double counting (it's idempotent if called twice with the same number of pages). The semantics are "ensure the this many pages total are guaranteed available, that is, either reserved or already instantiated".
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
RE: [PATCH] ftruncate on huge page couldn't extend hugetlb file
David Gibson wrote on Wednesday, March 08, 2006 4:23 PM
Quoted 9 lines
Quoted 2 lines
But you already make reservation at mmap time. If you reserve it again when extending the file, won't you double count?
Well, I'd generally expect extending
truncate()to come beforemmap(), but in any casehugetlb_extend_reservation()is safe against double counting (it's idempotent if called twice with the same number of pages). The semantics are "ensure the this many pages total are guaranteed available, that is, either reserved or already instantiated".
It's kind of peculiar that kernel reserve hugetlb page at the time of extending truncate. Maybe there is a close correlation between mmap size to the file size. But these two aren't the same and and shouldn't be mixed.
- Ken
Re: [PATCH] ftruncate on huge page couldn't extend hugetlb file
Quoted 11 lines · Kenneth W
David Gibson wrote on Wednesday, March 08, 2006 4:23 PM
Quoted 9 lines
Quoted 2 lines
But you already make reservation at mmap time. If you reserve it again when extending the file, won't you double count?
Well, I'd generally expect extending
truncate()to come beforemmap(), but in any casehugetlb_extend_reservation()is safe against double counting (it's idempotent if called twice with the same number of pages). The semantics are "ensure the this many pages total are guaranteed available, that is, either reserved or already instantiated".
Quoted 4 lines
It's kind of peculiar that kernel reserve hugetlb page at the time of extending truncate. Maybe there is a close correlation between mmap size to the file size. But these two aren't the same and and shouldn't be mixed.
Indeed. The fundamental basis of my approach as opposed to that in apw's old accounting patches is that reservation is related to filesize, not mmap() size. The only connection to mmap() is that (shared writable) mmap() on hugetlbfs also extends filesize and reservation size as a side effect.
There's a slight wrinkle in the different between i_size and reserved pages. i_size can be extended beyond the "reserved portion" of the file. We need that behaviour because i_size also acts as a hard offset limit for MAP_PRIVATE mappings, but privately instantiated pages don't come from the file's reservation.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson