diff options
Diffstat (limited to 'mm/hugetlb.c')
| -rw-r--r-- | mm/hugetlb.c | 977 | 
1 files changed, 622 insertions, 355 deletions
diff --git a/mm/hugetlb.c b/mm/hugetlb.c index b49579c7f2a..7a0a73d2fcf 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -13,6 +13,7 @@  #include <linux/nodemask.h>  #include <linux/pagemap.h>  #include <linux/mempolicy.h> +#include <linux/compiler.h>  #include <linux/cpuset.h>  #include <linux/mutex.h>  #include <linux/bootmem.h> @@ -22,6 +23,7 @@  #include <linux/swap.h>  #include <linux/swapops.h>  #include <linux/page-isolation.h> +#include <linux/jhash.h>  #include <asm/page.h>  #include <asm/pgtable.h> @@ -53,6 +55,13 @@ static unsigned long __initdata default_hstate_size;   */  DEFINE_SPINLOCK(hugetlb_lock); +/* + * Serializes faults on the same logical page.  This is used to + * prevent spurious OOMs when the hugepage pool is fully utilized. + */ +static int num_fault_mutexes; +static struct mutex *htlb_fault_mutex_table ____cacheline_aligned_in_smp; +  static inline void unlock_or_release_subpool(struct hugepage_subpool *spool)  {  	bool free = (spool->count == 0) && (spool->used_hpages == 0); @@ -135,15 +144,8 @@ static inline struct hugepage_subpool *subpool_vma(struct vm_area_struct *vma)   * Region tracking -- allows tracking of reservations and instantiated pages   *                    across the pages in a mapping.   * - * The region data structures are protected by a combination of the mmap_sem - * and the hugetlb_instantiation_mutex.  To access or modify a region the caller - * must either hold the mmap_sem for write, or the mmap_sem for read and - * the hugetlb_instantiation_mutex: - * - *	down_write(&mm->mmap_sem); - * or - *	down_read(&mm->mmap_sem); - *	mutex_lock(&hugetlb_instantiation_mutex); + * The region data structures are embedded into a resv_map and + * protected by a resv_map's lock   */  struct file_region {  	struct list_head link; @@ -151,10 +153,12 @@ struct file_region {  	long to;  }; -static long region_add(struct list_head *head, long f, long t) +static long region_add(struct resv_map *resv, long f, long t)  { +	struct list_head *head = &resv->regions;  	struct file_region *rg, *nrg, *trg; +	spin_lock(&resv->lock);  	/* Locate the region we are either in or before. */  	list_for_each_entry(rg, head, link)  		if (f <= rg->to) @@ -184,14 +188,18 @@ static long region_add(struct list_head *head, long f, long t)  	}  	nrg->from = f;  	nrg->to = t; +	spin_unlock(&resv->lock);  	return 0;  } -static long region_chg(struct list_head *head, long f, long t) +static long region_chg(struct resv_map *resv, long f, long t)  { -	struct file_region *rg, *nrg; +	struct list_head *head = &resv->regions; +	struct file_region *rg, *nrg = NULL;  	long chg = 0; +retry: +	spin_lock(&resv->lock);  	/* Locate the region we are before or in. */  	list_for_each_entry(rg, head, link)  		if (f <= rg->to) @@ -201,15 +209,21 @@ static long region_chg(struct list_head *head, long f, long t)  	 * Subtle, allocate a new region at the position but make it zero  	 * size such that we can guarantee to record the reservation. */  	if (&rg->link == head || t < rg->from) { -		nrg = kmalloc(sizeof(*nrg), GFP_KERNEL); -		if (!nrg) -			return -ENOMEM; -		nrg->from = f; -		nrg->to   = f; -		INIT_LIST_HEAD(&nrg->link); -		list_add(&nrg->link, rg->link.prev); +		if (!nrg) { +			spin_unlock(&resv->lock); +			nrg = kmalloc(sizeof(*nrg), GFP_KERNEL); +			if (!nrg) +				return -ENOMEM; + +			nrg->from = f; +			nrg->to   = f; +			INIT_LIST_HEAD(&nrg->link); +			goto retry; +		} -		return t - f; +		list_add(&nrg->link, rg->link.prev); +		chg = t - f; +		goto out_nrg;  	}  	/* Round our left edge to the current segment if it encloses us. */ @@ -222,7 +236,7 @@ static long region_chg(struct list_head *head, long f, long t)  		if (&rg->link == head)  			break;  		if (rg->from > t) -			return chg; +			goto out;  		/* We overlap with this area, if it extends further than  		 * us then we must extend ourselves.  Account for its @@ -233,20 +247,30 @@ static long region_chg(struct list_head *head, long f, long t)  		}  		chg -= rg->to - rg->from;  	} + +out: +	spin_unlock(&resv->lock); +	/*  We already know we raced and no longer need the new region */ +	kfree(nrg); +	return chg; +out_nrg: +	spin_unlock(&resv->lock);  	return chg;  } -static long region_truncate(struct list_head *head, long end) +static long region_truncate(struct resv_map *resv, long end)  { +	struct list_head *head = &resv->regions;  	struct file_region *rg, *trg;  	long chg = 0; +	spin_lock(&resv->lock);  	/* Locate the region we are either in or before. */  	list_for_each_entry(rg, head, link)  		if (end <= rg->to)  			break;  	if (&rg->link == head) -		return 0; +		goto out;  	/* If we are in the middle of a region then adjust it. */  	if (end > rg->from) { @@ -263,14 +287,19 @@ static long region_truncate(struct list_head *head, long end)  		list_del(&rg->link);  		kfree(rg);  	} + +out: +	spin_unlock(&resv->lock);  	return chg;  } -static long region_count(struct list_head *head, long f, long t) +static long region_count(struct resv_map *resv, long f, long t)  { +	struct list_head *head = &resv->regions;  	struct file_region *rg;  	long chg = 0; +	spin_lock(&resv->lock);  	/* Locate each segment we overlap with, and count that overlap. */  	list_for_each_entry(rg, head, link) {  		long seg_from; @@ -286,6 +315,7 @@ static long region_count(struct list_head *head, long f, long t)  		chg += seg_to - seg_from;  	} +	spin_unlock(&resv->lock);  	return chg;  } @@ -376,39 +406,46 @@ static void set_vma_private_data(struct vm_area_struct *vma,  	vma->vm_private_data = (void *)value;  } -struct resv_map { -	struct kref refs; -	struct list_head regions; -}; - -static struct resv_map *resv_map_alloc(void) +struct resv_map *resv_map_alloc(void)  {  	struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL);  	if (!resv_map)  		return NULL;  	kref_init(&resv_map->refs); +	spin_lock_init(&resv_map->lock);  	INIT_LIST_HEAD(&resv_map->regions);  	return resv_map;  } -static void resv_map_release(struct kref *ref) +void resv_map_release(struct kref *ref)  {  	struct resv_map *resv_map = container_of(ref, struct resv_map, refs);  	/* Clear out any active regions before we release the map. */ -	region_truncate(&resv_map->regions, 0); +	region_truncate(resv_map, 0);  	kfree(resv_map);  } +static inline struct resv_map *inode_resv_map(struct inode *inode) +{ +	return inode->i_mapping->private_data; +} +  static struct resv_map *vma_resv_map(struct vm_area_struct *vma)  {  	VM_BUG_ON(!is_vm_hugetlb_page(vma)); -	if (!(vma->vm_flags & VM_MAYSHARE)) +	if (vma->vm_flags & VM_MAYSHARE) { +		struct address_space *mapping = vma->vm_file->f_mapping; +		struct inode *inode = mapping->host; + +		return inode_resv_map(inode); + +	} else {  		return (struct resv_map *)(get_vma_private_data(vma) &  							~HPAGE_RESV_MASK); -	return NULL; +	}  }  static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map) @@ -476,40 +513,6 @@ static int vma_has_reserves(struct vm_area_struct *vma, long chg)  	return 0;  } -static void copy_gigantic_page(struct page *dst, struct page *src) -{ -	int i; -	struct hstate *h = page_hstate(src); -	struct page *dst_base = dst; -	struct page *src_base = src; - -	for (i = 0; i < pages_per_huge_page(h); ) { -		cond_resched(); -		copy_highpage(dst, src); - -		i++; -		dst = mem_map_next(dst, dst_base, i); -		src = mem_map_next(src, src_base, i); -	} -} - -void copy_huge_page(struct page *dst, struct page *src) -{ -	int i; -	struct hstate *h = page_hstate(src); - -	if (unlikely(pages_per_huge_page(h) > MAX_ORDER_NR_PAGES)) { -		copy_gigantic_page(dst, src); -		return; -	} - -	might_sleep(); -	for (i = 0; i < pages_per_huge_page(h); i++) { -		cond_resched(); -		copy_highpage(dst + i, src + i); -	} -} -  static void enqueue_huge_page(struct hstate *h, struct page *page)  {  	int nid = page_to_nid(page); @@ -541,7 +544,7 @@ static struct page *dequeue_huge_page_node(struct hstate *h, int nid)  /* Movability of hugepages depends on migration support. */  static inline gfp_t htlb_alloc_mask(struct hstate *h)  { -	if (hugepages_treat_as_movable || hugepage_migration_support(h)) +	if (hugepages_treat_as_movable || hugepage_migration_supported(h))  		return GFP_HIGHUSER_MOVABLE;  	else  		return GFP_HIGHUSER; @@ -574,7 +577,7 @@ static struct page *dequeue_huge_page_vma(struct hstate *h,  		goto err;  retry_cpuset: -	cpuset_mems_cookie = get_mems_allowed(); +	cpuset_mems_cookie = read_mems_allowed_begin();  	zonelist = huge_zonelist(vma, address,  					htlb_alloc_mask(h), &mpol, &nodemask); @@ -596,7 +599,7 @@ retry_cpuset:  	}  	mpol_cond_put(mpol); -	if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !page)) +	if (unlikely(!page && read_mems_allowed_retry(cpuset_mems_cookie)))  		goto retry_cpuset;  	return page; @@ -604,25 +607,242 @@ err:  	return NULL;  } +/* + * common helper functions for hstate_next_node_to_{alloc|free}. + * We may have allocated or freed a huge page based on a different + * nodes_allowed previously, so h->next_node_to_{alloc|free} might + * be outside of *nodes_allowed.  Ensure that we use an allowed + * node for alloc or free. + */ +static int next_node_allowed(int nid, nodemask_t *nodes_allowed) +{ +	nid = next_node(nid, *nodes_allowed); +	if (nid == MAX_NUMNODES) +		nid = first_node(*nodes_allowed); +	VM_BUG_ON(nid >= MAX_NUMNODES); + +	return nid; +} + +static int get_valid_node_allowed(int nid, nodemask_t *nodes_allowed) +{ +	if (!node_isset(nid, *nodes_allowed)) +		nid = next_node_allowed(nid, nodes_allowed); +	return nid; +} + +/* + * returns the previously saved node ["this node"] from which to + * allocate a persistent huge page for the pool and advance the + * next node from which to allocate, handling wrap at end of node + * mask. + */ +static int hstate_next_node_to_alloc(struct hstate *h, +					nodemask_t *nodes_allowed) +{ +	int nid; + +	VM_BUG_ON(!nodes_allowed); + +	nid = get_valid_node_allowed(h->next_nid_to_alloc, nodes_allowed); +	h->next_nid_to_alloc = next_node_allowed(nid, nodes_allowed); + +	return nid; +} + +/* + * helper for free_pool_huge_page() - return the previously saved + * node ["this node"] from which to free a huge page.  Advance the + * next node id whether or not we find a free huge page to free so + * that the next attempt to free addresses the next node. + */ +static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed) +{ +	int nid; + +	VM_BUG_ON(!nodes_allowed); + +	nid = get_valid_node_allowed(h->next_nid_to_free, nodes_allowed); +	h->next_nid_to_free = next_node_allowed(nid, nodes_allowed); + +	return nid; +} + +#define for_each_node_mask_to_alloc(hs, nr_nodes, node, mask)		\ +	for (nr_nodes = nodes_weight(*mask);				\ +		nr_nodes > 0 &&						\ +		((node = hstate_next_node_to_alloc(hs, mask)) || 1);	\ +		nr_nodes--) + +#define for_each_node_mask_to_free(hs, nr_nodes, node, mask)		\ +	for (nr_nodes = nodes_weight(*mask);				\ +		nr_nodes > 0 &&						\ +		((node = hstate_next_node_to_free(hs, mask)) || 1);	\ +		nr_nodes--) + +#if defined(CONFIG_CMA) && defined(CONFIG_X86_64) +static void destroy_compound_gigantic_page(struct page *page, +					unsigned long order) +{ +	int i; +	int nr_pages = 1 << order; +	struct page *p = page + 1; + +	for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) { +		__ClearPageTail(p); +		set_page_refcounted(p); +		p->first_page = NULL; +	} + +	set_compound_order(page, 0); +	__ClearPageHead(page); +} + +static void free_gigantic_page(struct page *page, unsigned order) +{ +	free_contig_range(page_to_pfn(page), 1 << order); +} + +static int __alloc_gigantic_page(unsigned long start_pfn, +				unsigned long nr_pages) +{ +	unsigned long end_pfn = start_pfn + nr_pages; +	return alloc_contig_range(start_pfn, end_pfn, MIGRATE_MOVABLE); +} + +static bool pfn_range_valid_gigantic(unsigned long start_pfn, +				unsigned long nr_pages) +{ +	unsigned long i, end_pfn = start_pfn + nr_pages; +	struct page *page; + +	for (i = start_pfn; i < end_pfn; i++) { +		if (!pfn_valid(i)) +			return false; + +		page = pfn_to_page(i); + +		if (PageReserved(page)) +			return false; + +		if (page_count(page) > 0) +			return false; + +		if (PageHuge(page)) +			return false; +	} + +	return true; +} + +static bool zone_spans_last_pfn(const struct zone *zone, +			unsigned long start_pfn, unsigned long nr_pages) +{ +	unsigned long last_pfn = start_pfn + nr_pages - 1; +	return zone_spans_pfn(zone, last_pfn); +} + +static struct page *alloc_gigantic_page(int nid, unsigned order) +{ +	unsigned long nr_pages = 1 << order; +	unsigned long ret, pfn, flags; +	struct zone *z; + +	z = NODE_DATA(nid)->node_zones; +	for (; z - NODE_DATA(nid)->node_zones < MAX_NR_ZONES; z++) { +		spin_lock_irqsave(&z->lock, flags); + +		pfn = ALIGN(z->zone_start_pfn, nr_pages); +		while (zone_spans_last_pfn(z, pfn, nr_pages)) { +			if (pfn_range_valid_gigantic(pfn, nr_pages)) { +				/* +				 * We release the zone lock here because +				 * alloc_contig_range() will also lock the zone +				 * at some point. If there's an allocation +				 * spinning on this lock, it may win the race +				 * and cause alloc_contig_range() to fail... +				 */ +				spin_unlock_irqrestore(&z->lock, flags); +				ret = __alloc_gigantic_page(pfn, nr_pages); +				if (!ret) +					return pfn_to_page(pfn); +				spin_lock_irqsave(&z->lock, flags); +			} +			pfn += nr_pages; +		} + +		spin_unlock_irqrestore(&z->lock, flags); +	} + +	return NULL; +} + +static void prep_new_huge_page(struct hstate *h, struct page *page, int nid); +static void prep_compound_gigantic_page(struct page *page, unsigned long order); + +static struct page *alloc_fresh_gigantic_page_node(struct hstate *h, int nid) +{ +	struct page *page; + +	page = alloc_gigantic_page(nid, huge_page_order(h)); +	if (page) { +		prep_compound_gigantic_page(page, huge_page_order(h)); +		prep_new_huge_page(h, page, nid); +	} + +	return page; +} + +static int alloc_fresh_gigantic_page(struct hstate *h, +				nodemask_t *nodes_allowed) +{ +	struct page *page = NULL; +	int nr_nodes, node; + +	for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) { +		page = alloc_fresh_gigantic_page_node(h, node); +		if (page) +			return 1; +	} + +	return 0; +} + +static inline bool gigantic_page_supported(void) { return true; } +#else +static inline bool gigantic_page_supported(void) { return false; } +static inline void free_gigantic_page(struct page *page, unsigned order) { } +static inline void destroy_compound_gigantic_page(struct page *page, +						unsigned long order) { } +static inline int alloc_fresh_gigantic_page(struct hstate *h, +					nodemask_t *nodes_allowed) { return 0; } +#endif +  static void update_and_free_page(struct hstate *h, struct page *page)  {  	int i; -	VM_BUG_ON(h->order >= MAX_ORDER); +	if (hstate_is_gigantic(h) && !gigantic_page_supported()) +		return;  	h->nr_huge_pages--;  	h->nr_huge_pages_node[page_to_nid(page)]--;  	for (i = 0; i < pages_per_huge_page(h); i++) {  		page[i].flags &= ~(1 << PG_locked | 1 << PG_error |  				1 << PG_referenced | 1 << PG_dirty | -				1 << PG_active | 1 << PG_reserved | -				1 << PG_private | 1 << PG_writeback); +				1 << PG_active | 1 << PG_private | +				1 << PG_writeback);  	} -	VM_BUG_ON(hugetlb_cgroup_from_page(page)); +	VM_BUG_ON_PAGE(hugetlb_cgroup_from_page(page), page);  	set_compound_page_dtor(page, NULL);  	set_page_refcounted(page); -	arch_release_hugepage(page); -	__free_pages(page, huge_page_order(h)); +	if (hstate_is_gigantic(h)) { +		destroy_compound_gigantic_page(page, huge_page_order(h)); +		free_gigantic_page(page, huge_page_order(h)); +	} else { +		arch_release_hugepage(page); +		__free_pages(page, huge_page_order(h)); +	}  }  struct hstate *size_to_hstate(unsigned long size) @@ -636,7 +856,7 @@ struct hstate *size_to_hstate(unsigned long size)  	return NULL;  } -static void free_huge_page(struct page *page) +void free_huge_page(struct page *page)  {  	/*  	 * Can't pass hstate in here because it is called from the @@ -653,6 +873,7 @@ static void free_huge_page(struct page *page)  	BUG_ON(page_count(page));  	BUG_ON(page_mapcount(page));  	restore_reserve = PagePrivate(page); +	ClearPagePrivate(page);  	spin_lock(&hugetlb_lock);  	hugetlb_cgroup_uncharge_page(hstate_index(h), @@ -660,7 +881,7 @@ static void free_huge_page(struct page *page)  	if (restore_reserve)  		h->resv_huge_pages++; -	if (h->surplus_huge_pages_node[nid] && huge_page_order(h) < MAX_ORDER) { +	if (h->surplus_huge_pages_node[nid]) {  		/* remove the page from active list */  		list_del(&page->lru);  		update_and_free_page(h, page); @@ -695,8 +916,22 @@ static void prep_compound_gigantic_page(struct page *page, unsigned long order)  	/* we rely on prep_new_huge_page to set the destructor */  	set_compound_order(page, order);  	__SetPageHead(page); +	__ClearPageReserved(page);  	for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) {  		__SetPageTail(p); +		/* +		 * For gigantic hugepages allocated through bootmem at +		 * boot, it's safer to be consistent with the not-gigantic +		 * hugepages and clear the PG_reserved bit from all tail pages +		 * too.  Otherwse drivers using get_user_pages() to access tail +		 * pages may get the reference counting wrong if they see +		 * PG_reserved set on a tail page (despite the head page not +		 * having PG_reserved set).  Enforcing this consistency between +		 * head and tail pages allows drivers to optimize away a check +		 * on the head page when they need know if put_page() is needed +		 * after get_user_pages(). +		 */ +		__ClearPageReserved(p);  		set_page_count(p, 0);  		p->first_page = page;  	} @@ -709,18 +944,26 @@ static void prep_compound_gigantic_page(struct page *page, unsigned long order)   */  int PageHuge(struct page *page)  { -	compound_page_dtor *dtor; -  	if (!PageCompound(page))  		return 0;  	page = compound_head(page); -	dtor = get_compound_page_dtor(page); - -	return dtor == free_huge_page; +	return get_compound_page_dtor(page) == free_huge_page;  }  EXPORT_SYMBOL_GPL(PageHuge); +/* + * PageHeadHuge() only returns true for hugetlbfs head page, but not for + * normal or transparent huge pages. + */ +int PageHeadHuge(struct page *page_head) +{ +	if (!PageHead(page_head)) +		return 0; + +	return get_compound_page_dtor(page_head) == free_huge_page; +} +  pgoff_t __basepage_index(struct page *page)  {  	struct page *page_head = compound_head(page); @@ -742,9 +985,6 @@ static struct page *alloc_fresh_huge_page_node(struct hstate *h, int nid)  {  	struct page *page; -	if (h->order >= MAX_ORDER) -		return NULL; -  	page = alloc_pages_exact_node(nid,  		htlb_alloc_mask(h)|__GFP_COMP|__GFP_THISNODE|  						__GFP_REPEAT|__GFP_NOWARN, @@ -760,79 +1000,6 @@ static struct page *alloc_fresh_huge_page_node(struct hstate *h, int nid)  	return page;  } -/* - * common helper functions for hstate_next_node_to_{alloc|free}. - * We may have allocated or freed a huge page based on a different - * nodes_allowed previously, so h->next_node_to_{alloc|free} might - * be outside of *nodes_allowed.  Ensure that we use an allowed - * node for alloc or free. - */ -static int next_node_allowed(int nid, nodemask_t *nodes_allowed) -{ -	nid = next_node(nid, *nodes_allowed); -	if (nid == MAX_NUMNODES) -		nid = first_node(*nodes_allowed); -	VM_BUG_ON(nid >= MAX_NUMNODES); - -	return nid; -} - -static int get_valid_node_allowed(int nid, nodemask_t *nodes_allowed) -{ -	if (!node_isset(nid, *nodes_allowed)) -		nid = next_node_allowed(nid, nodes_allowed); -	return nid; -} - -/* - * returns the previously saved node ["this node"] from which to - * allocate a persistent huge page for the pool and advance the - * next node from which to allocate, handling wrap at end of node - * mask. - */ -static int hstate_next_node_to_alloc(struct hstate *h, -					nodemask_t *nodes_allowed) -{ -	int nid; - -	VM_BUG_ON(!nodes_allowed); - -	nid = get_valid_node_allowed(h->next_nid_to_alloc, nodes_allowed); -	h->next_nid_to_alloc = next_node_allowed(nid, nodes_allowed); - -	return nid; -} - -/* - * helper for free_pool_huge_page() - return the previously saved - * node ["this node"] from which to free a huge page.  Advance the - * next node id whether or not we find a free huge page to free so - * that the next attempt to free addresses the next node. - */ -static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed) -{ -	int nid; - -	VM_BUG_ON(!nodes_allowed); - -	nid = get_valid_node_allowed(h->next_nid_to_free, nodes_allowed); -	h->next_nid_to_free = next_node_allowed(nid, nodes_allowed); - -	return nid; -} - -#define for_each_node_mask_to_alloc(hs, nr_nodes, node, mask)		\ -	for (nr_nodes = nodes_weight(*mask);				\ -		nr_nodes > 0 &&						\ -		((node = hstate_next_node_to_alloc(hs, mask)) || 1);	\ -		nr_nodes--) - -#define for_each_node_mask_to_free(hs, nr_nodes, node, mask)		\ -	for (nr_nodes = nodes_weight(*mask);				\ -		nr_nodes > 0 &&						\ -		((node = hstate_next_node_to_free(hs, mask)) || 1);	\ -		nr_nodes--) -  static int alloc_fresh_huge_page(struct hstate *h, nodemask_t *nodes_allowed)  {  	struct page *page; @@ -936,7 +1103,7 @@ static struct page *alloc_buddy_huge_page(struct hstate *h, int nid)  	struct page *page;  	unsigned int r_nid; -	if (h->order >= MAX_ORDER) +	if (hstate_is_gigantic(h))  		return NULL;  	/* @@ -1100,7 +1267,7 @@ retry:  		 * no users -- drop the buddy allocator's reference.  		 */  		put_page_testzero(page); -		VM_BUG_ON(page_count(page)); +		VM_BUG_ON_PAGE(page_count(page), page);  		enqueue_huge_page(h, page);  	}  free: @@ -1129,7 +1296,7 @@ static void return_unused_surplus_pages(struct hstate *h,  	h->resv_huge_pages -= unused_resv_pages;  	/* Cannot return gigantic pages currently */ -	if (h->order >= MAX_ORDER) +	if (hstate_is_gigantic(h))  		return;  	nr_pages = min(unused_resv_pages, h->surplus_huge_pages); @@ -1145,6 +1312,7 @@ static void return_unused_surplus_pages(struct hstate *h,  	while (nr_pages--) {  		if (!free_pool_huge_page(h, &node_states[N_MEMORY], 1))  			break; +		cond_resched_lock(&hugetlb_lock);  	}  } @@ -1161,45 +1329,34 @@ static void return_unused_surplus_pages(struct hstate *h,  static long vma_needs_reservation(struct hstate *h,  			struct vm_area_struct *vma, unsigned long addr)  { -	struct address_space *mapping = vma->vm_file->f_mapping; -	struct inode *inode = mapping->host; - -	if (vma->vm_flags & VM_MAYSHARE) { -		pgoff_t idx = vma_hugecache_offset(h, vma, addr); -		return region_chg(&inode->i_mapping->private_list, -							idx, idx + 1); +	struct resv_map *resv; +	pgoff_t idx; +	long chg; -	} else if (!is_vma_resv_set(vma, HPAGE_RESV_OWNER)) { +	resv = vma_resv_map(vma); +	if (!resv)  		return 1; -	} else  { -		long err; -		pgoff_t idx = vma_hugecache_offset(h, vma, addr); -		struct resv_map *resv = vma_resv_map(vma); +	idx = vma_hugecache_offset(h, vma, addr); +	chg = region_chg(resv, idx, idx + 1); -		err = region_chg(&resv->regions, idx, idx + 1); -		if (err < 0) -			return err; -		return 0; -	} +	if (vma->vm_flags & VM_MAYSHARE) +		return chg; +	else +		return chg < 0 ? chg : 0;  }  static void vma_commit_reservation(struct hstate *h,  			struct vm_area_struct *vma, unsigned long addr)  { -	struct address_space *mapping = vma->vm_file->f_mapping; -	struct inode *inode = mapping->host; - -	if (vma->vm_flags & VM_MAYSHARE) { -		pgoff_t idx = vma_hugecache_offset(h, vma, addr); -		region_add(&inode->i_mapping->private_list, idx, idx + 1); +	struct resv_map *resv; +	pgoff_t idx; -	} else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) { -		pgoff_t idx = vma_hugecache_offset(h, vma, addr); -		struct resv_map *resv = vma_resv_map(vma); +	resv = vma_resv_map(vma); +	if (!resv) +		return; -		/* Mark this page used in the map. */ -		region_add(&resv->regions, idx, idx + 1); -	} +	idx = vma_hugecache_offset(h, vma, addr); +	region_add(resv, idx, idx + 1);  }  static struct page *alloc_huge_page(struct vm_area_struct *vma, @@ -1229,24 +1386,17 @@ static struct page *alloc_huge_page(struct vm_area_struct *vma,  			return ERR_PTR(-ENOSPC);  	ret = hugetlb_cgroup_charge_cgroup(idx, pages_per_huge_page(h), &h_cg); -	if (ret) { -		if (chg || avoid_reserve) -			hugepage_subpool_put_pages(spool, 1); -		return ERR_PTR(-ENOSPC); -	} +	if (ret) +		goto out_subpool_put; +  	spin_lock(&hugetlb_lock);  	page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve, chg);  	if (!page) {  		spin_unlock(&hugetlb_lock);  		page = alloc_buddy_huge_page(h, NUMA_NO_NODE); -		if (!page) { -			hugetlb_cgroup_uncharge_cgroup(idx, -						       pages_per_huge_page(h), -						       h_cg); -			if (chg || avoid_reserve) -				hugepage_subpool_put_pages(spool, 1); -			return ERR_PTR(-ENOSPC); -		} +		if (!page) +			goto out_uncharge_cgroup; +  		spin_lock(&hugetlb_lock);  		list_move(&page->lru, &h->hugepage_activelist);  		/* Fall through */ @@ -1258,6 +1408,13 @@ static struct page *alloc_huge_page(struct vm_area_struct *vma,  	vma_commit_reservation(h, vma, addr);  	return page; + +out_uncharge_cgroup: +	hugetlb_cgroup_uncharge_cgroup(idx, pages_per_huge_page(h), h_cg); +out_subpool_put: +	if (chg || avoid_reserve) +		hugepage_subpool_put_pages(spool, 1); +	return ERR_PTR(-ENOSPC);  }  /* @@ -1282,9 +1439,9 @@ int __weak alloc_bootmem_huge_page(struct hstate *h)  	for_each_node_mask_to_alloc(h, nr_nodes, node, &node_states[N_MEMORY]) {  		void *addr; -		addr = __alloc_bootmem_node_nopanic(NODE_DATA(node), -				huge_page_size(h), huge_page_size(h), 0); - +		addr = memblock_virt_alloc_try_nid_nopanic( +				huge_page_size(h), huge_page_size(h), +				0, BOOTMEM_ALLOC_ACCESSIBLE, node);  		if (addr) {  			/*  			 * Use the beginning of the huge page to store the @@ -1305,7 +1462,7 @@ found:  	return 1;  } -static void prep_compound_huge_page(struct page *page, int order) +static void __init prep_compound_huge_page(struct page *page, int order)  {  	if (unlikely(order > (MAX_ORDER - 1)))  		prep_compound_gigantic_page(page, order); @@ -1324,14 +1481,14 @@ static void __init gather_bootmem_prealloc(void)  #ifdef CONFIG_HIGHMEM  		page = pfn_to_page(m->phys >> PAGE_SHIFT); -		free_bootmem_late((unsigned long)m, -				  sizeof(struct huge_bootmem_page)); +		memblock_free_late(__pa(m), +				   sizeof(struct huge_bootmem_page));  #else  		page = virt_to_page(m);  #endif -		__ClearPageReserved(page);  		WARN_ON(page_count(page) != 1);  		prep_compound_huge_page(page, h->order); +		WARN_ON(PageReserved(page));  		prep_new_huge_page(h, page, page_to_nid(page));  		/*  		 * If we had gigantic hugepages allocated at boot time, we need @@ -1339,7 +1496,7 @@ static void __init gather_bootmem_prealloc(void)  		 * fix confusing memory reports from free(1) and another  		 * side-effects, like CommitLimit going negative.  		 */ -		if (h->order > (MAX_ORDER - 1)) +		if (hstate_is_gigantic(h))  			adjust_managed_page_count(page, 1 << h->order);  	}  } @@ -1349,7 +1506,7 @@ static void __init hugetlb_hstate_alloc_pages(struct hstate *h)  	unsigned long i;  	for (i = 0; i < h->max_huge_pages; ++i) { -		if (h->order >= MAX_ORDER) { +		if (hstate_is_gigantic(h)) {  			if (!alloc_bootmem_huge_page(h))  				break;  		} else if (!alloc_fresh_huge_page(h, @@ -1365,7 +1522,7 @@ static void __init hugetlb_init_hstates(void)  	for_each_hstate(h) {  		/* oversize hugepages were init'ed in early boot */ -		if (h->order < MAX_ORDER) +		if (!hstate_is_gigantic(h))  			hugetlb_hstate_alloc_pages(h);  	}  } @@ -1399,7 +1556,7 @@ static void try_to_free_low(struct hstate *h, unsigned long count,  {  	int i; -	if (h->order >= MAX_ORDER) +	if (hstate_is_gigantic(h))  		return;  	for_each_node_mask(i, *nodes_allowed) { @@ -1462,7 +1619,7 @@ static unsigned long set_max_huge_pages(struct hstate *h, unsigned long count,  {  	unsigned long min_count, ret; -	if (h->order >= MAX_ORDER) +	if (hstate_is_gigantic(h) && !gigantic_page_supported())  		return h->max_huge_pages;  	/* @@ -1489,7 +1646,10 @@ static unsigned long set_max_huge_pages(struct hstate *h, unsigned long count,  		 * and reducing the surplus.  		 */  		spin_unlock(&hugetlb_lock); -		ret = alloc_fresh_huge_page(h, nodes_allowed); +		if (hstate_is_gigantic(h)) +			ret = alloc_fresh_gigantic_page(h, nodes_allowed); +		else +			ret = alloc_fresh_huge_page(h, nodes_allowed);  		spin_lock(&hugetlb_lock);  		if (!ret)  			goto out; @@ -1520,6 +1680,7 @@ static unsigned long set_max_huge_pages(struct hstate *h, unsigned long count,  	while (min_count < persistent_huge_pages(h)) {  		if (!free_pool_huge_page(h, nodes_allowed, 0))  			break; +		cond_resched_lock(&hugetlb_lock);  	}  	while (count < persistent_huge_pages(h)) {  		if (!adjust_pool_surplus(h, nodes_allowed, 1)) @@ -1588,7 +1749,7 @@ static ssize_t nr_hugepages_store_common(bool obey_mempolicy,  		goto out;  	h = kobj_to_hstate(kobj, &nid); -	if (h->order >= MAX_ORDER) { +	if (hstate_is_gigantic(h) && !gigantic_page_supported()) {  		err = -EINVAL;  		goto out;  	} @@ -1671,7 +1832,7 @@ static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj,  	unsigned long input;  	struct hstate *h = kobj_to_hstate(kobj, NULL); -	if (h->order >= MAX_ORDER) +	if (hstate_is_gigantic(h))  		return -EINVAL;  	err = kstrtoul(buf, 10, &input); @@ -1955,16 +2116,15 @@ static void __exit hugetlb_exit(void)  	}  	kobject_put(hugepages_kobj); +	kfree(htlb_fault_mutex_table);  }  module_exit(hugetlb_exit);  static int __init hugetlb_init(void)  { -	/* Some platform decide whether they support huge pages at boot -	 * time. On these, such as powerpc, HPAGE_SHIFT is set to 0 when -	 * there is no such support -	 */ -	if (HPAGE_SHIFT == 0) +	int i; + +	if (!hugepages_supported())  		return 0;  	if (!size_to_hstate(default_hstate_size)) { @@ -1984,6 +2144,17 @@ static int __init hugetlb_init(void)  	hugetlb_register_all_nodes();  	hugetlb_cgroup_file_init(); +#ifdef CONFIG_SMP +	num_fault_mutexes = roundup_pow_of_two(8 * num_possible_cpus()); +#else +	num_fault_mutexes = 1; +#endif +	htlb_fault_mutex_table = +		kmalloc(sizeof(struct mutex) * num_fault_mutexes, GFP_KERNEL); +	BUG_ON(!htlb_fault_mutex_table); + +	for (i = 0; i < num_fault_mutexes; i++) +		mutex_init(&htlb_fault_mutex_table[i]);  	return 0;  }  module_init(hugetlb_init); @@ -2080,9 +2251,12 @@ static int hugetlb_sysctl_handler_common(bool obey_mempolicy,  	unsigned long tmp;  	int ret; +	if (!hugepages_supported()) +		return -ENOTSUPP; +  	tmp = h->max_huge_pages; -	if (write && h->order >= MAX_ORDER) +	if (write && hstate_is_gigantic(h) && !gigantic_page_supported())  		return -EINVAL;  	table->data = &tmp; @@ -2133,9 +2307,12 @@ int hugetlb_overcommit_handler(struct ctl_table *table, int write,  	unsigned long tmp;  	int ret; +	if (!hugepages_supported()) +		return -ENOTSUPP; +  	tmp = h->nr_overcommit_huge_pages; -	if (write && h->order >= MAX_ORDER) +	if (write && hstate_is_gigantic(h))  		return -EINVAL;  	table->data = &tmp; @@ -2158,6 +2335,8 @@ out:  void hugetlb_report_meminfo(struct seq_file *m)  {  	struct hstate *h = &default_hstate; +	if (!hugepages_supported()) +		return;  	seq_printf(m,  			"HugePages_Total:   %5lu\n"  			"HugePages_Free:    %5lu\n" @@ -2174,6 +2353,8 @@ void hugetlb_report_meminfo(struct seq_file *m)  int hugetlb_report_node_meminfo(int nid, char *buf)  {  	struct hstate *h = &default_hstate; +	if (!hugepages_supported()) +		return 0;  	return sprintf(buf,  		"Node %d HugePages_Total: %5u\n"  		"Node %d HugePages_Free:  %5u\n" @@ -2188,6 +2369,9 @@ void hugetlb_show_meminfo(void)  	struct hstate *h;  	int nid; +	if (!hugepages_supported()) +		return; +  	for_each_node_state(nid, N_MEMORY)  		for_each_hstate(h)  			pr_info("Node %d hugepages_total=%u hugepages_free=%u hugepages_surp=%u hugepages_size=%lukB\n", @@ -2262,41 +2446,30 @@ static void hugetlb_vm_op_open(struct vm_area_struct *vma)  	 * after this open call completes.  It is therefore safe to take a  	 * new reference here without additional locking.  	 */ -	if (resv) +	if (resv && is_vma_resv_set(vma, HPAGE_RESV_OWNER))  		kref_get(&resv->refs);  } -static void resv_map_put(struct vm_area_struct *vma) -{ -	struct resv_map *resv = vma_resv_map(vma); - -	if (!resv) -		return; -	kref_put(&resv->refs, resv_map_release); -} -  static void hugetlb_vm_op_close(struct vm_area_struct *vma)  {  	struct hstate *h = hstate_vma(vma);  	struct resv_map *resv = vma_resv_map(vma);  	struct hugepage_subpool *spool = subpool_vma(vma); -	unsigned long reserve; -	unsigned long start; -	unsigned long end; +	unsigned long reserve, start, end; -	if (resv) { -		start = vma_hugecache_offset(h, vma, vma->vm_start); -		end = vma_hugecache_offset(h, vma, vma->vm_end); +	if (!resv || !is_vma_resv_set(vma, HPAGE_RESV_OWNER)) +		return; -		reserve = (end - start) - -			region_count(&resv->regions, start, end); +	start = vma_hugecache_offset(h, vma, vma->vm_start); +	end = vma_hugecache_offset(h, vma, vma->vm_end); -		resv_map_put(vma); +	reserve = (end - start) - region_count(resv, start, end); -		if (reserve) { -			hugetlb_acct_memory(h, -reserve); -			hugepage_subpool_put_pages(spool, reserve); -		} +	kref_put(&resv->refs, resv_map_release); + +	if (reserve) { +		hugetlb_acct_memory(h, -reserve); +		hugepage_subpool_put_pages(spool, reserve);  	}  } @@ -2347,6 +2520,31 @@ static void set_huge_ptep_writable(struct vm_area_struct *vma,  		update_mmu_cache(vma, address, ptep);  } +static int is_hugetlb_entry_migration(pte_t pte) +{ +	swp_entry_t swp; + +	if (huge_pte_none(pte) || pte_present(pte)) +		return 0; +	swp = pte_to_swp_entry(pte); +	if (non_swap_entry(swp) && is_migration_entry(swp)) +		return 1; +	else +		return 0; +} + +static int is_hugetlb_entry_hwpoisoned(pte_t pte) +{ +	swp_entry_t swp; + +	if (huge_pte_none(pte) || pte_present(pte)) +		return 0; +	swp = pte_to_swp_entry(pte); +	if (non_swap_entry(swp) && is_hwpoison_entry(swp)) +		return 1; +	else +		return 0; +}  int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,  			    struct vm_area_struct *vma) @@ -2357,24 +2555,53 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,  	int cow;  	struct hstate *h = hstate_vma(vma);  	unsigned long sz = huge_page_size(h); +	unsigned long mmun_start;	/* For mmu_notifiers */ +	unsigned long mmun_end;		/* For mmu_notifiers */ +	int ret = 0;  	cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; +	mmun_start = vma->vm_start; +	mmun_end = vma->vm_end; +	if (cow) +		mmu_notifier_invalidate_range_start(src, mmun_start, mmun_end); +  	for (addr = vma->vm_start; addr < vma->vm_end; addr += sz) { +		spinlock_t *src_ptl, *dst_ptl;  		src_pte = huge_pte_offset(src, addr);  		if (!src_pte)  			continue;  		dst_pte = huge_pte_alloc(dst, addr, sz); -		if (!dst_pte) -			goto nomem; +		if (!dst_pte) { +			ret = -ENOMEM; +			break; +		}  		/* If the pagetables are shared don't copy or take references */  		if (dst_pte == src_pte)  			continue; -		spin_lock(&dst->page_table_lock); -		spin_lock_nested(&src->page_table_lock, SINGLE_DEPTH_NESTING); -		if (!huge_pte_none(huge_ptep_get(src_pte))) { +		dst_ptl = huge_pte_lock(h, dst, dst_pte); +		src_ptl = huge_pte_lockptr(h, src, src_pte); +		spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); +		entry = huge_ptep_get(src_pte); +		if (huge_pte_none(entry)) { /* skip none entry */ +			; +		} else if (unlikely(is_hugetlb_entry_migration(entry) || +				    is_hugetlb_entry_hwpoisoned(entry))) { +			swp_entry_t swp_entry = pte_to_swp_entry(entry); + +			if (is_write_migration_entry(swp_entry) && cow) { +				/* +				 * COW mappings require pages in both +				 * parent and child to be set to read. +				 */ +				make_migration_entry_read(&swp_entry); +				entry = swp_entry_to_pte(swp_entry); +				set_huge_pte_at(src, addr, src_pte, entry); +			} +			set_huge_pte_at(dst, addr, dst_pte, entry); +		} else {  			if (cow)  				huge_ptep_set_wrprotect(src, addr, src_pte);  			entry = huge_ptep_get(src_pte); @@ -2383,39 +2610,14 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,  			page_dup_rmap(ptepage);  			set_huge_pte_at(dst, addr, dst_pte, entry);  		} -		spin_unlock(&src->page_table_lock); -		spin_unlock(&dst->page_table_lock); +		spin_unlock(src_ptl); +		spin_unlock(dst_ptl);  	} -	return 0; -nomem: -	return -ENOMEM; -} +	if (cow) +		mmu_notifier_invalidate_range_end(src, mmun_start, mmun_end); -static int is_hugetlb_entry_migration(pte_t pte) -{ -	swp_entry_t swp; - -	if (huge_pte_none(pte) || pte_present(pte)) -		return 0; -	swp = pte_to_swp_entry(pte); -	if (non_swap_entry(swp) && is_migration_entry(swp)) -		return 1; -	else -		return 0; -} - -static int is_hugetlb_entry_hwpoisoned(pte_t pte) -{ -	swp_entry_t swp; - -	if (huge_pte_none(pte) || pte_present(pte)) -		return 0; -	swp = pte_to_swp_entry(pte); -	if (non_swap_entry(swp) && is_hwpoison_entry(swp)) -		return 1; -	else -		return 0; +	return ret;  }  void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, @@ -2427,6 +2629,7 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,  	unsigned long address;  	pte_t *ptep;  	pte_t pte; +	spinlock_t *ptl;  	struct page *page;  	struct hstate *h = hstate_vma(vma);  	unsigned long sz = huge_page_size(h); @@ -2440,25 +2643,25 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,  	tlb_start_vma(tlb, vma);  	mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);  again: -	spin_lock(&mm->page_table_lock);  	for (address = start; address < end; address += sz) {  		ptep = huge_pte_offset(mm, address);  		if (!ptep)  			continue; +		ptl = huge_pte_lock(h, mm, ptep);  		if (huge_pmd_unshare(mm, &address, ptep)) -			continue; +			goto unlock;  		pte = huge_ptep_get(ptep);  		if (huge_pte_none(pte)) -			continue; +			goto unlock;  		/*  		 * HWPoisoned hugepage is already unmapped and dropped reference  		 */  		if (unlikely(is_hugetlb_entry_hwpoisoned(pte))) {  			huge_pte_clear(mm, address, ptep); -			continue; +			goto unlock;  		}  		page = pte_page(pte); @@ -2469,7 +2672,7 @@ again:  		 */  		if (ref_page) {  			if (page != ref_page) -				continue; +				goto unlock;  			/*  			 * Mark the VMA as having unmapped its page so that @@ -2486,13 +2689,18 @@ again:  		page_remove_rmap(page);  		force_flush = !__tlb_remove_page(tlb, page); -		if (force_flush) +		if (force_flush) { +			spin_unlock(ptl);  			break; +		}  		/* Bail out after unmapping reference page if supplied */ -		if (ref_page) +		if (ref_page) { +			spin_unlock(ptl);  			break; +		} +unlock: +		spin_unlock(ptl);  	} -	spin_unlock(&mm->page_table_lock);  	/*  	 * mmu_gather ran out of room to batch pages, we break out of  	 * the PTE lock to avoid doing the potential expensive TLB invalidate @@ -2598,7 +2806,7 @@ static int unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,   */  static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,  			unsigned long address, pte_t *ptep, pte_t pte, -			struct page *pagecache_page) +			struct page *pagecache_page, spinlock_t *ptl)  {  	struct hstate *h = hstate_vma(vma);  	struct page *old_page, *new_page; @@ -2632,8 +2840,8 @@ retry_avoidcopy:  	page_cache_get(old_page); -	/* Drop page_table_lock as buddy allocator may be called */ -	spin_unlock(&mm->page_table_lock); +	/* Drop page table lock as buddy allocator may be called */ +	spin_unlock(ptl);  	new_page = alloc_huge_page(vma, address, outside_reserve);  	if (IS_ERR(new_page)) { @@ -2651,13 +2859,14 @@ retry_avoidcopy:  			BUG_ON(huge_pte_none(pte));  			if (unmap_ref_private(mm, vma, old_page, address)) {  				BUG_ON(huge_pte_none(pte)); -				spin_lock(&mm->page_table_lock); +				spin_lock(ptl);  				ptep = huge_pte_offset(mm, address & huge_page_mask(h)); -				if (likely(pte_same(huge_ptep_get(ptep), pte))) +				if (likely(ptep && +					   pte_same(huge_ptep_get(ptep), pte)))  					goto retry_avoidcopy;  				/* -				 * race occurs while re-acquiring page_table_lock, and -				 * our job is done. +				 * race occurs while re-acquiring page table +				 * lock, and our job is done.  				 */  				return 0;  			} @@ -2665,7 +2874,7 @@ retry_avoidcopy:  		}  		/* Caller expects lock to be held */ -		spin_lock(&mm->page_table_lock); +		spin_lock(ptl);  		if (err == -ENOMEM)  			return VM_FAULT_OOM;  		else @@ -2680,7 +2889,7 @@ retry_avoidcopy:  		page_cache_release(new_page);  		page_cache_release(old_page);  		/* Caller expects lock to be held */ -		spin_lock(&mm->page_table_lock); +		spin_lock(ptl);  		return VM_FAULT_OOM;  	} @@ -2692,12 +2901,12 @@ retry_avoidcopy:  	mmun_end = mmun_start + huge_page_size(h);  	mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);  	/* -	 * Retake the page_table_lock to check for racing updates +	 * Retake the page table lock to check for racing updates  	 * before the page tables are altered  	 */ -	spin_lock(&mm->page_table_lock); +	spin_lock(ptl);  	ptep = huge_pte_offset(mm, address & huge_page_mask(h)); -	if (likely(pte_same(huge_ptep_get(ptep), pte))) { +	if (likely(ptep && pte_same(huge_ptep_get(ptep), pte))) {  		ClearPagePrivate(new_page);  		/* Break COW */ @@ -2709,13 +2918,13 @@ retry_avoidcopy:  		/* Make the old page be freed below */  		new_page = old_page;  	} -	spin_unlock(&mm->page_table_lock); +	spin_unlock(ptl);  	mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);  	page_cache_release(new_page);  	page_cache_release(old_page);  	/* Caller expects lock to be held */ -	spin_lock(&mm->page_table_lock); +	spin_lock(ptl);  	return 0;  } @@ -2753,16 +2962,16 @@ static bool hugetlbfs_pagecache_present(struct hstate *h,  }  static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma, -			unsigned long address, pte_t *ptep, unsigned int flags) +			   struct address_space *mapping, pgoff_t idx, +			   unsigned long address, pte_t *ptep, unsigned int flags)  {  	struct hstate *h = hstate_vma(vma);  	int ret = VM_FAULT_SIGBUS;  	int anon_rmap = 0; -	pgoff_t idx;  	unsigned long size;  	struct page *page; -	struct address_space *mapping;  	pte_t new_pte; +	spinlock_t *ptl;  	/*  	 * Currently, we are forced to kill the process in the event the @@ -2775,9 +2984,6 @@ static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,  		return ret;  	} -	mapping = vma->vm_file->f_mapping; -	idx = vma_hugecache_offset(h, vma, address); -  	/*  	 * Use page lock to guard against racing truncation  	 * before we get page_table_lock. @@ -2849,7 +3055,8 @@ retry:  			goto backout_unlocked;  		} -	spin_lock(&mm->page_table_lock); +	ptl = huge_pte_lockptr(h, mm, ptep); +	spin_lock(ptl);  	size = i_size_read(mapping->host) >> huge_page_shift(h);  	if (idx >= size)  		goto backout; @@ -2861,8 +3068,7 @@ retry:  	if (anon_rmap) {  		ClearPagePrivate(page);  		hugepage_add_new_anon_rmap(page, vma, address); -	} -	else +	} else  		page_dup_rmap(page);  	new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)  				&& (vma->vm_flags & VM_SHARED))); @@ -2870,32 +3076,69 @@ retry:  	if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {  		/* Optimization, do the COW without a second fault */ -		ret = hugetlb_cow(mm, vma, address, ptep, new_pte, page); +		ret = hugetlb_cow(mm, vma, address, ptep, new_pte, page, ptl);  	} -	spin_unlock(&mm->page_table_lock); +	spin_unlock(ptl);  	unlock_page(page);  out:  	return ret;  backout: -	spin_unlock(&mm->page_table_lock); +	spin_unlock(ptl);  backout_unlocked:  	unlock_page(page);  	put_page(page);  	goto out;  } +#ifdef CONFIG_SMP +static u32 fault_mutex_hash(struct hstate *h, struct mm_struct *mm, +			    struct vm_area_struct *vma, +			    struct address_space *mapping, +			    pgoff_t idx, unsigned long address) +{ +	unsigned long key[2]; +	u32 hash; + +	if (vma->vm_flags & VM_SHARED) { +		key[0] = (unsigned long) mapping; +		key[1] = idx; +	} else { +		key[0] = (unsigned long) mm; +		key[1] = address >> huge_page_shift(h); +	} + +	hash = jhash2((u32 *)&key, sizeof(key)/sizeof(u32), 0); + +	return hash & (num_fault_mutexes - 1); +} +#else +/* + * For uniprocesor systems we always use a single mutex, so just + * return 0 and avoid the hashing overhead. + */ +static u32 fault_mutex_hash(struct hstate *h, struct mm_struct *mm, +			    struct vm_area_struct *vma, +			    struct address_space *mapping, +			    pgoff_t idx, unsigned long address) +{ +	return 0; +} +#endif +  int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,  			unsigned long address, unsigned int flags)  { -	pte_t *ptep; -	pte_t entry; +	pte_t *ptep, entry; +	spinlock_t *ptl;  	int ret; +	u32 hash; +	pgoff_t idx;  	struct page *page = NULL;  	struct page *pagecache_page = NULL; -	static DEFINE_MUTEX(hugetlb_instantiation_mutex);  	struct hstate *h = hstate_vma(vma); +	struct address_space *mapping;  	address &= huge_page_mask(h); @@ -2903,7 +3146,7 @@ int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,  	if (ptep) {  		entry = huge_ptep_get(ptep);  		if (unlikely(is_hugetlb_entry_migration(entry))) { -			migration_entry_wait_huge(mm, ptep); +			migration_entry_wait_huge(vma, mm, ptep);  			return 0;  		} else if (unlikely(is_hugetlb_entry_hwpoisoned(entry)))  			return VM_FAULT_HWPOISON_LARGE | @@ -2914,15 +3157,20 @@ int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,  	if (!ptep)  		return VM_FAULT_OOM; +	mapping = vma->vm_file->f_mapping; +	idx = vma_hugecache_offset(h, vma, address); +  	/*  	 * Serialize hugepage allocation and instantiation, so that we don't  	 * get spurious allocation failures if two CPUs race to instantiate  	 * the same page in the page cache.  	 */ -	mutex_lock(&hugetlb_instantiation_mutex); +	hash = fault_mutex_hash(h, mm, vma, mapping, idx, address); +	mutex_lock(&htlb_fault_mutex_table[hash]); +  	entry = huge_ptep_get(ptep);  	if (huge_pte_none(entry)) { -		ret = hugetlb_no_page(mm, vma, address, ptep, flags); +		ret = hugetlb_no_page(mm, vma, mapping, idx, address, ptep, flags);  		goto out_mutex;  	} @@ -2959,17 +3207,18 @@ int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,  	if (page != pagecache_page)  		lock_page(page); -	spin_lock(&mm->page_table_lock); +	ptl = huge_pte_lockptr(h, mm, ptep); +	spin_lock(ptl);  	/* Check for a racing update before calling hugetlb_cow */  	if (unlikely(!pte_same(entry, huge_ptep_get(ptep)))) -		goto out_page_table_lock; +		goto out_ptl;  	if (flags & FAULT_FLAG_WRITE) {  		if (!huge_pte_write(entry)) {  			ret = hugetlb_cow(mm, vma, address, ptep, entry, -							pagecache_page); -			goto out_page_table_lock; +					pagecache_page, ptl); +			goto out_ptl;  		}  		entry = huge_pte_mkdirty(entry);  	} @@ -2978,8 +3227,8 @@ int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,  						flags & FAULT_FLAG_WRITE))  		update_mmu_cache(vma, address, ptep); -out_page_table_lock: -	spin_unlock(&mm->page_table_lock); +out_ptl: +	spin_unlock(ptl);  	if (pagecache_page) {  		unlock_page(pagecache_page); @@ -2990,8 +3239,7 @@ out_page_table_lock:  	put_page(page);  out_mutex: -	mutex_unlock(&hugetlb_instantiation_mutex); - +	mutex_unlock(&htlb_fault_mutex_table[hash]);  	return ret;  } @@ -3005,9 +3253,9 @@ long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,  	unsigned long remainder = *nr_pages;  	struct hstate *h = hstate_vma(vma); -	spin_lock(&mm->page_table_lock);  	while (vaddr < vma->vm_end && remainder) {  		pte_t *pte; +		spinlock_t *ptl = NULL;  		int absent;  		struct page *page; @@ -3015,8 +3263,12 @@ long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,  		 * Some archs (sparc64, sh*) have multiple pte_ts to  		 * each hugepage.  We have to make sure we get the  		 * first, for the page indexing below to work. +		 * +		 * Note that page table lock is not held when pte is null.  		 */  		pte = huge_pte_offset(mm, vaddr & huge_page_mask(h)); +		if (pte) +			ptl = huge_pte_lock(h, mm, pte);  		absent = !pte || huge_pte_none(huge_ptep_get(pte));  		/* @@ -3028,6 +3280,8 @@ long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,  		 */  		if (absent && (flags & FOLL_DUMP) &&  		    !hugetlbfs_pagecache_present(h, vma, vaddr)) { +			if (pte) +				spin_unlock(ptl);  			remainder = 0;  			break;  		} @@ -3047,10 +3301,10 @@ long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,  		      !huge_pte_write(huge_ptep_get(pte)))) {  			int ret; -			spin_unlock(&mm->page_table_lock); +			if (pte) +				spin_unlock(ptl);  			ret = hugetlb_fault(mm, vma, vaddr,  				(flags & FOLL_WRITE) ? FAULT_FLAG_WRITE : 0); -			spin_lock(&mm->page_table_lock);  			if (!(ret & VM_FAULT_ERROR))  				continue; @@ -3063,7 +3317,7 @@ long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,  same_page:  		if (pages) {  			pages[i] = mem_map_offset(page, pfn_offset); -			get_page(pages[i]); +			get_page_foll(pages[i]);  		}  		if (vmas) @@ -3081,8 +3335,8 @@ same_page:  			 */  			goto same_page;  		} +		spin_unlock(ptl);  	} -	spin_unlock(&mm->page_table_lock);  	*nr_pages = remainder;  	*position = vaddr; @@ -3102,14 +3356,17 @@ unsigned long hugetlb_change_protection(struct vm_area_struct *vma,  	BUG_ON(address >= end);  	flush_cache_range(vma, address, end); +	mmu_notifier_invalidate_range_start(mm, start, end);  	mutex_lock(&vma->vm_file->f_mapping->i_mmap_mutex); -	spin_lock(&mm->page_table_lock);  	for (; address < end; address += huge_page_size(h)) { +		spinlock_t *ptl;  		ptep = huge_pte_offset(mm, address);  		if (!ptep)  			continue; +		ptl = huge_pte_lock(h, mm, ptep);  		if (huge_pmd_unshare(mm, &address, ptep)) {  			pages++; +			spin_unlock(ptl);  			continue;  		}  		if (!huge_pte_none(huge_ptep_get(ptep))) { @@ -3119,8 +3376,8 @@ unsigned long hugetlb_change_protection(struct vm_area_struct *vma,  			set_huge_pte_at(mm, address, ptep, pte);  			pages++;  		} +		spin_unlock(ptl);  	} -	spin_unlock(&mm->page_table_lock);  	/*  	 * Must flush TLB before releasing i_mmap_mutex: x86's huge_pmd_unshare  	 * may have cleared our pud entry and done put_page on the page table: @@ -3129,6 +3386,7 @@ unsigned long hugetlb_change_protection(struct vm_area_struct *vma,  	 */  	flush_tlb_range(vma, start, end);  	mutex_unlock(&vma->vm_file->f_mapping->i_mmap_mutex); +	mmu_notifier_invalidate_range_end(mm, start, end);  	return pages << h->order;  } @@ -3141,6 +3399,7 @@ int hugetlb_reserve_pages(struct inode *inode,  	long ret, chg;  	struct hstate *h = hstate_inode(inode);  	struct hugepage_subpool *spool = subpool_inode(inode); +	struct resv_map *resv_map;  	/*  	 * Only apply hugepage reservation if asked. At fault time, an @@ -3156,10 +3415,13 @@ int hugetlb_reserve_pages(struct inode *inode,  	 * to reserve the full area even if read-only as mprotect() may be  	 * called to make the mapping read-write. Assume !vma is a shm mapping  	 */ -	if (!vma || vma->vm_flags & VM_MAYSHARE) -		chg = region_chg(&inode->i_mapping->private_list, from, to); -	else { -		struct resv_map *resv_map = resv_map_alloc(); +	if (!vma || vma->vm_flags & VM_MAYSHARE) { +		resv_map = inode_resv_map(inode); + +		chg = region_chg(resv_map, from, to); + +	} else { +		resv_map = resv_map_alloc();  		if (!resv_map)  			return -ENOMEM; @@ -3202,20 +3464,23 @@ int hugetlb_reserve_pages(struct inode *inode,  	 * else has to be done for private mappings here  	 */  	if (!vma || vma->vm_flags & VM_MAYSHARE) -		region_add(&inode->i_mapping->private_list, from, to); +		region_add(resv_map, from, to);  	return 0;  out_err: -	if (vma) -		resv_map_put(vma); +	if (vma && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) +		kref_put(&resv_map->refs, resv_map_release);  	return ret;  }  void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)  {  	struct hstate *h = hstate_inode(inode); -	long chg = region_truncate(&inode->i_mapping->private_list, offset); +	struct resv_map *resv_map = inode_resv_map(inode); +	long chg = 0;  	struct hugepage_subpool *spool = subpool_inode(inode); +	if (resv_map) +		chg = region_truncate(resv_map, offset);  	spin_lock(&inode->i_lock);  	inode->i_blocks -= (blocks_per_huge_page(h) * freed);  	spin_unlock(&inode->i_lock); @@ -3283,6 +3548,7 @@ pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)  	unsigned long saddr;  	pte_t *spte = NULL;  	pte_t *pte; +	spinlock_t *ptl;  	if (!vma_shareable(vma, addr))  		return (pte_t *)pmd_alloc(mm, pud, addr); @@ -3305,13 +3571,14 @@ pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)  	if (!spte)  		goto out; -	spin_lock(&mm->page_table_lock); +	ptl = huge_pte_lockptr(hstate_vma(vma), mm, spte); +	spin_lock(ptl);  	if (pud_none(*pud))  		pud_populate(mm, pud,  				(pmd_t *)((unsigned long)spte & PAGE_MASK));  	else  		put_page(virt_to_page(spte)); -	spin_unlock(&mm->page_table_lock); +	spin_unlock(ptl);  out:  	pte = (pte_t *)pmd_alloc(mm, pud, addr);  	mutex_unlock(&mapping->i_mmap_mutex); @@ -3325,7 +3592,7 @@ out:   * indicated by page_count > 1, unmap is achieved by clearing pud and   * decrementing the ref count. If count == 1, the pte page is not shared.   * - * called with vma->vm_mm->page_table_lock held. + * called with page table lock held.   *   * returns: 1 successfully unmapped a shared pte page   *	    0 the underlying pte page is not shared, or it is the last user @@ -3424,7 +3691,7 @@ follow_huge_pud(struct mm_struct *mm, unsigned long address,  #else /* !CONFIG_ARCH_WANT_GENERAL_HUGETLB */  /* Can be overriden by architectures */ -__attribute__((weak)) struct page * +struct page * __weak  follow_huge_pud(struct mm_struct *mm, unsigned long address,  	       pud_t *pud, int write)  { @@ -3481,7 +3748,7 @@ int dequeue_hwpoisoned_huge_page(struct page *hpage)  bool isolate_huge_page(struct page *page, struct list_head *list)  { -	VM_BUG_ON(!PageHead(page)); +	VM_BUG_ON_PAGE(!PageHead(page), page);  	if (!get_page_unless_zero(page))  		return false;  	spin_lock(&hugetlb_lock); @@ -3492,7 +3759,7 @@ bool isolate_huge_page(struct page *page, struct list_head *list)  void putback_active_hugepage(struct page *page)  { -	VM_BUG_ON(!PageHead(page)); +	VM_BUG_ON_PAGE(!PageHead(page), page);  	spin_lock(&hugetlb_lock);  	list_move_tail(&page->lru, &(page_hstate(page))->hugepage_activelist);  	spin_unlock(&hugetlb_lock); @@ -3501,7 +3768,7 @@ void putback_active_hugepage(struct page *page)  bool is_hugepage_active(struct page *page)  { -	VM_BUG_ON(!PageHuge(page)); +	VM_BUG_ON_PAGE(!PageHuge(page), page);  	/*  	 * This function can be called for a tail page because the caller,  	 * scan_movable_pages, scans through a given pfn-range which typically  | 
