Skip to contentSkip to conversation

hugetlb strict commit accounting - v3

PATCH In review Chen, Kenneth W 4 participants 7 messages Mar 10, 2006 – Mar 20, 2006

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hugetlb strict commit accounting - v3

hugetlb strict commit accounting for shared mapping - v3

The a region reservation list is implementation as a linked list hanging off address_space i_data->private_list. It turns out that clear_inode() was also looking at inode->i_data->private_list and if not empty, it think inode has dirty buffers and start clearing. Except it won't go very far before oops-ing. That could happen if a reservation is made but no actual faulting. hugetlbfs_delete_inode and hugetlbfs_forget_inode doesn't call truncate_hugepages if there are no actual page in the page cache, leading to clear_inode to do bad thing. Change that to always call truncate_hugepages even if there are no pages in page cache and to let the unreserve code to clear out the reservation linked list.

Changes since v2:
* fix none empty i_data->private_list before calling clear_inode
* delete VMACCTPG macro

Changes since v1:

* change resv_huge_pages to normal unsigned long
* add proper lock around update/access resv_huge_pages
* resv_huge_pages record future needs of hugetlb pages
* strict commit accounting for shared mapping
* don't allow free_huge_pages to dip below reserved page in sysctl path

fs/hugetlbfs/inode.c renamed from fs/hugetlbfs/inode.c.orig +10 -48
@@ -56,48 +56,9 @@ static void huge_pagevec_release(struct 
 	pagevec_reinit(pvec);
 }
 
-/*
- * huge_pages_needed tries to determine the number of new huge pages that
- * will be required to fully populate this VMA.  This will be equal to
- * the size of the VMA in huge pages minus the number of huge pages
- * (covered by this VMA) that are found in the page cache.
- *
- * Result is in bytes to be compatible with is_hugepage_mem_enough()
- */
-static unsigned long
-huge_pages_needed(struct address_space *mapping, struct vm_area_struct *vma)
-{
-	int i;
-	struct pagevec pvec;
-	unsigned long start = vma->vm_start;
-	unsigned long end = vma->vm_end;
-	unsigned long hugepages = (end - start) >> HPAGE_SHIFT;
-	pgoff_t next = vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT);
-	pgoff_t endpg = next + hugepages;
-
-	pagevec_init(&pvec, 0);
-	while (next < endpg) {
-		if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE))
-			break;
-		for (i = 0; i < pagevec_count(&pvec); i++) {
-			struct page *page = pvec.pages[i];
-			if (page->index > next)
-				next = page->index;
-			if (page->index >= endpg)
-				break;
-			next++;
-			hugepages--;
-		}
-		huge_pagevec_release(&pvec);
-	}
-	return hugepages << HPAGE_SHIFT;
-}
-
 static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)
 {
 	struct inode *inode = file->f_dentry->d_inode;
-	struct address_space *mapping = inode->i_mapping;
-	unsigned long bytes;
 	loff_t len, vma_len;
 	int ret;
 
@@ -113,10 +74,6 @@ static int hugetlbfs_file_mmap(struct fi
 	if (vma->vm_end - vma->vm_start < HPAGE_SIZE)
 		return -EINVAL;
 
-	bytes = huge_pages_needed(mapping, vma);
-	if (!is_hugepage_mem_enough(bytes))
-		return -ENOMEM;
-
 	vma_len = (loff_t)(vma->vm_end - vma->vm_start);
 
 	mutex_lock(&inode->i_mutex);
@@ -129,6 +86,10 @@ static int hugetlbfs_file_mmap(struct fi
 	if (!(vma->vm_flags & VM_WRITE) && len > inode->i_size)
 		goto out;
 
+	if (vma->vm_flags & VM_MAYSHARE)
+		if (hugetlb_reserve_pages(inode, vma))
+			goto out;
+
 	ret = 0;
 	hugetlb_prefault_arch_hook(vma->vm_mm);
 	if (inode->i_size < len)
@@ -232,7 +193,7 @@ static void truncate_hugepages(struct ad
 	const pgoff_t start = lstart >> HPAGE_SHIFT;
 	struct pagevec pvec;
 	pgoff_t next;
-	int i;
+	int i, freed = 0;
 
 	pagevec_init(&pvec, 0);
 	next = start;
@@ -254,16 +215,17 @@ static void truncate_hugepages(struct ad
 			truncate_huge_page(page);
 			unlock_page(page);
 			hugetlb_put_quota(mapping);
+			freed++;
 		}
 		huge_pagevec_release(&pvec);
 	}
 	BUG_ON(!lstart && mapping->nrpages);
+	hugetlb_unreserve_pages(mapping->host, start, freed);
 }
 
 static void hugetlbfs_delete_inode(struct inode *inode)
 {
-	if (inode->i_data.nrpages)
-		truncate_hugepages(&inode->i_data, 0);
+	truncate_hugepages(&inode->i_data, 0);
 	clear_inode(inode);
 }
 
@@ -296,8 +258,7 @@ static void hugetlbfs_forget_inode(struc
 	inode->i_state |= I_FREEING;
 	inodes_stat.nr_inodes--;
 	spin_unlock(&inode_lock);
-	if (inode->i_data.nrpages)
-		truncate_hugepages(&inode->i_data, 0);
+	truncate_hugepages(&inode->i_data, 0);
 	clear_inode(inode);
 	destroy_inode(inode);
 }
@@ -401,6 +362,7 @@ static struct inode *hugetlbfs_get_inode
 		inode->i_mapping->a_ops = &hugetlbfs_aops;
 		inode->i_mapping->backing_dev_info =&hugetlbfs_backing_dev_info;
 		inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
+		INIT_LIST_HEAD(&inode->i_mapping->private_list);
 		info = HUGETLBFS_I(inode);
 		mpol_shared_policy_init(&info->policy, MPOL_DEFAULT, NULL);
 		switch (mode & S_IFMT) {
include/linux/hugetlb.h renamed from include/linux/hugetlb.h.orig +2 -0
@@ -26,6 +26,8 @@ struct page *alloc_huge_page(struct vm_a
 void free_huge_page(struct page *);
 int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
 			unsigned long address, int write_access);
+int hugetlb_reserve_pages(struct inode *inode, struct vm_area_struct *vma);
+void hugetlb_unreserve_pages(struct inode *inode, pgoff_t offset, int freed);
 
 extern unsigned long max_huge_pages;
 extern const unsigned long hugetlb_zero, hugetlb_infinity;
mm/hugetlb.c renamed from mm/hugetlb.c.orig +172 -1

203 lines not on this page · raw message

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#1 lore raw
Quoted 13 lines · Kenneth W

hugetlb strict commit accounting for shared mapping - v3

The a region reservation list is implementation as a linked list hanging off address_space i_data->private_list. It turns out that clear_inode() was also looking at inode->i_data->private_list and if not empty, it think inode has dirty buffers and start clearing. Except it won't go very far before oops-ing. That could happen if a reservation is made but no actual faulting. hugetlbfs_delete_inode and hugetlbfs_forget_inode doesn't call truncate_hugepages if there are no actual page in the page cache, leading to clear_inode to do bad thing. Change that to always call truncate_hugepages even if there are no pages in page cache and to let the unreserve code to clear out the reservation linked list.

Hrm.. overloading the private_list in this manner sounds fragile. Maybe we should move the list into the hugetlbfs specific inode data.

Signature · 10 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

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#2 lore raw
Quoted 16 lines · David Gibson
Quoted 13 lines · Kenneth W

hugetlb strict commit accounting for shared mapping - v3

The a region reservation list is implementation as a linked list hanging off address_space i_data->private_list. It turns out that clear_inode() was also looking at inode->i_data->private_list and if not empty, it think inode has dirty buffers and start clearing. Except it won't go very far before oops-ing. That could happen if a reservation is made but no actual faulting. hugetlbfs_delete_inode and hugetlbfs_forget_inode doesn't call truncate_hugepages if there are no actual page in the page cache, leading to clear_inode to do bad thing. Change that to always call truncate_hugepages even if there are no pages in page cache and to let the unreserve code to clear out the reservation linked list.

Quoted 2 lines

Hrm.. overloading the private_list in this manner sounds fragile. Maybe we should move the list into the hugetlbfs specific inode data.

private_list and private_lock are available for use by the subsystem which owns this mapping's address_space_operations. ie: hugetlbfs.

It's been this way for several years but afaik this is the first time that's actually been taken advantage of.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#3 lore raw
Quoted 17 lines · Andrew Morton
Quoted 16 lines · David Gibson
Quoted 13 lines · Kenneth W

hugetlb strict commit accounting for shared mapping - v3

The a region reservation list is implementation as a linked list hanging off address_space i_data->private_list. It turns out that clear_inode() was also looking at inode->i_data->private_list and if not empty, it think inode has dirty buffers and start clearing. Except it won't go very far before oops-ing. That could happen if a reservation is made but no actual faulting. hugetlbfs_delete_inode and hugetlbfs_forget_inode doesn't call truncate_hugepages if there are no actual page in the page cache, leading to clear_inode to do bad thing. Change that to always call truncate_hugepages even if there are no pages in page cache and to let the unreserve code to clear out the reservation linked list.

Quoted 5 lines
Quoted 2 lines

Hrm.. overloading the private_list in this manner sounds fragile. Maybe we should move the list into the hugetlbfs specific inode data.

private_list and private_lock are available for use by the subsystem which owns this mapping's address_space_operations. ie: hugetlbfs.

If that's so, why is clear_inode messing with it?

Quoted 2 lines

It's been this way for several years but afaik this is the first time that's actually been taken advantage of.

Signature · 10 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

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#4 lore raw
Quoted 19 lines · David Gibson
Quoted 17 lines · Andrew Morton
Quoted 16 lines · David Gibson
Quoted 13 lines · Kenneth W

hugetlb strict commit accounting for shared mapping - v3

The a region reservation list is implementation as a linked list hanging off address_space i_data->private_list. It turns out that clear_inode() was also looking at inode->i_data->private_list and if not empty, it think inode has dirty buffers and start clearing. Except it won't go very far before oops-ing. That could happen if a reservation is made but no actual faulting. hugetlbfs_delete_inode and hugetlbfs_forget_inode doesn't call truncate_hugepages if there are no actual page in the page cache, leading to clear_inode to do bad thing. Change that to always call truncate_hugepages even if there are no pages in page cache and to let the unreserve code to clear out the reservation linked list.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Hrm.. overloading the private_list in this manner sounds fragile. Maybe we should move the list into the hugetlbfs specific inode data.

private_list and private_lock are available for use by the subsystem which owns this mapping's address_space_operations. ie: hugetlbfs.

If that's so, why is clear_inode messing with it?

Oh. It's being bad.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#5 lore raw
Quoted 8 lines · Andrew Morton
Quoted 6 lines
Quoted 3 lines

private_list and private_lock are available for use by the subsystem which owns this mapping's address_space_operations. ie: hugetlbfs.

If that's so, why is clear_inode messing with it?

Oh. It's being bad.

That doesn't rule out reuse. It just means that only buffer_head users are allowed to run clear_inode() with a non-empty list.

So it's bad, but not fatally so.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#6 lore raw
In reply to Chen, Kenneth W

mm/hugetlb.c struct page *alloc_huge_page(struct vm_a · line 109

Quoted 13 lines · Kenneth W
mm/hugetlb.c renamed from mm/hugetlb.c.orig +6 -0
@@ -98,6 +98,12 @@ struct page *alloc_huge_page(struct vm_a
 	int i;
 
 	spin_lock(&hugetlb_lock);
+	if (vma->vm_flags & VM_MAYSHARE)
+		resv_huge_pages--;
+	else if (free_huge_pages <= resv_huge_pages) {
+		spin_unlock(&hugetlb_lock);
+		return NULL;
+	}
 	page = dequeue_huge_page(vma, addr);
 	if (!page) {
 		spin_unlock(&hugetlb_lock);

Unfortunately this will break down when two or more threads race to allocate the same page. You end up with a double-decrement of resv_huge_pages even though only one thread will win the race.

Signature · 8 lines
Adam Litke - (agl at us.ibm.com)
IBM Linux Technology Center

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>