]> git.karo-electronics.de Git - linux-beck.git/blobdiff - fs/hugetlbfs/inode.c
[PATCH] hugetlb: overcommit accounting check
[linux-beck.git] / fs / hugetlbfs / inode.c
index ffdad4e64671f021ab5039a01c689c63e09f033f..e026c807e6b376fe57deb2dc2050d1d58d7c917a 100644 (file)
@@ -45,10 +45,58 @@ static struct backing_dev_info hugetlbfs_backing_dev_info = {
 
 int sysctl_hugetlb_shm_group;
 
+static void huge_pagevec_release(struct pagevec *pvec)
+{
+       int i;
+
+       for (i = 0; i < pagevec_count(pvec); ++i)
+               put_page(pvec->pages[i]);
+
+       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()
+ */
+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;
+       pgoff_t endpg = next + ((end - start) >> PAGE_SHIFT);
+
+       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;
 
@@ -67,6 +115,10 @@ static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)
        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);
 
        down(&inode->i_sem);
@@ -79,10 +131,8 @@ static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)
        if (!(vma->vm_flags & VM_WRITE) && len > inode->i_size)
                goto out;
 
-       ret = hugetlb_prefault(mapping, vma);
-       if (ret)
-               goto out;
-
+       ret = 0;
+       hugetlb_prefault_arch_hook(vma->vm_mm);
        if (inode->i_size < len)
                inode->i_size = len;
 out:
@@ -171,16 +221,6 @@ static int hugetlbfs_commit_write(struct file *file,
        return -EINVAL;
 }
 
-static void huge_pagevec_release(struct pagevec *pvec)
-{
-       int i;
-
-       for (i = 0; i < pagevec_count(pvec); ++i)
-               put_page(pvec->pages[i]);
-
-       pagevec_reinit(pvec);
-}
-
 static void truncate_huge_page(struct page *page)
 {
        clear_page_dirty(page);
@@ -231,25 +271,28 @@ static void hugetlbfs_delete_inode(struct inode *inode)
 
 static void hugetlbfs_forget_inode(struct inode *inode)
 {
-       struct super_block *super_block = inode->i_sb;
-
-       if (hlist_unhashed(&inode->i_hash))
-               goto out_truncate;
+       struct super_block *sb = inode->i_sb;
 
-       if (!(inode->i_state & (I_DIRTY|I_LOCK))) {
-               list_del(&inode->i_list);
-               list_add(&inode->i_list, &inode_unused);
-       }
-       inodes_stat.nr_unused++;
-       if (!super_block || (super_block->s_flags & MS_ACTIVE)) {
+       if (!hlist_unhashed(&inode->i_hash)) {
+               if (!(inode->i_state & (I_DIRTY|I_LOCK)))
+                       list_move(&inode->i_list, &inode_unused);
+               inodes_stat.nr_unused++;
+               if (!sb || (sb->s_flags & MS_ACTIVE)) {
+                       spin_unlock(&inode_lock);
+                       return;
+               }
+               inode->i_state |= I_WILL_FREE;
                spin_unlock(&inode_lock);
-               return;
+               /*
+                * write_inode_now is a noop as we set BDI_CAP_NO_WRITEBACK
+                * in our backing_dev_info.
+                */
+               write_inode_now(inode, 1);
+               spin_lock(&inode_lock);
+               inode->i_state &= ~I_WILL_FREE;
+               inodes_stat.nr_unused--;
+               hlist_del_init(&inode->i_hash);
        }
-
-       /* write_inode_now() ? */
-       inodes_stat.nr_unused--;
-       hlist_del_init(&inode->i_hash);
-out_truncate:
        list_del_init(&inode->i_list);
        list_del_init(&inode->i_sb_list);
        inode->i_state |= I_FREEING;
@@ -257,7 +300,6 @@ out_truncate:
        spin_unlock(&inode_lock);
        if (inode->i_data.nrpages)
                truncate_hugepages(&inode->i_data, 0);
-
        clear_inode(inode);
        destroy_inode(inode);
 }