e i.MX6 processor family.
aboutsummaryrefslogblamecommitdiffstats
path: root/mm/mempolicy.c
blob: 36f42573a3352fa6ba452eea2d76ed2021c8605c (plain) (tree)
1
2
3
4
5



                                                  
                                                               














                                                                             
  

                                                                  



                                                                              




                                                                          
  































                                                                              







                            





                           
                          


                            
                       

                           
                          
                       
                           
                           
                        
 


                         

                     
                    
                                                                                             
                                                                                       
                                                                               
 

                                       
 

                                                                    
                               
 


                                                          
                                   
                                                     
                               
                              

  




                                                                       
                                                                  
                                                        
 
                  
 









                                                               
                 
         

                 

 

                                                                       








                                                                           

 










                                                                              
                                                                      














                                                                        
                         

                                                                            

                                 
                                        
                
 

                                                                  
 

                                                  
                                                
                                                                       
         















                                                                             



                                                            
                            
                              
 



                                                                         














                                                                             
                                                                      



                                                      
                      

 




                                             


                                         

















                                                                               
 














                                                                    


                                                            
                                               
                                                     


                                                    


                                                                           
                                                  




                                                                              

 



                                                         

                       


                                                              
                                                 



























                                                                     

















                                                               
                                                               

                                                                           
 
                                                                     
                                                                  

                                                             
                              
 

                        
                        
 
                                                                          
            
                                  
                        

                                       
                                 

                                                       
                                 










                                                                          

                                       
                                        


                                                                          
                                          
                                                                     
                                                                   
                                                               

                              
                                                        
                                        


                           
                                                                         

                                                             
                              








                                               
                                                                
                                                    




                                                  
                                                                         

                                                             
                              








                                               
                                                                
                                                    




                                                  
                                                             

                                                             
                              



                           
                                           



                                               
                                                                
                                                    


                                                  

 




                                                          

                                                                         
                                                                            



                                                  
                                                        
 


                                            
         
 




                                                                           









                                                                    
                                                           
 



                                                      
                                                                        
                                                                















                                                                        
                                                                        








                                                               
                              

























                                                                       





























                                                                   
                                   

                                                                       
 
                              
                                           
 
                                           

                                    








                                                                
                                     
                                 
                                    
                                                  
                                       
                                                            


                                        


                 



                                                                       
 
                            


                                 
                          

                                  
                             
                                    

                            
                                               
                                                              
                                                                      


















                                                                               
                          

                                                                     
 
                



                                                   
                                          

                                                                              
                               








                                                              
                                  




                                                            













                                                                 
                                                                        





                                                    
                                      
                                                       
                                                               
                                                   



                                      


                                                                 




                                                                              
         





                                                
                
                  
                                                

     
                           




                                                
                       
  

                 

                                                                           

          
                                                             
           




                                                                     
 
 
                                                                                 
 
                                                               

 



                                                     

                                                                      






                                
 


                                                                 
                                   

                                                                    
                   


  

                                                                     





                                                                            


                       
 
                                 
 



                                                            





























                                                                      
 


























                                                                  
         
    
                               


                           


 






                                                                      
                                                                                   

                                                                      
                                                 
 










                                                                           
 




                                                                           

 




                                                                            
 
                                                                                   


                    

      
                                                            

                                                                    







                                           

                                                                      
                               
                                                                 















                                                
                                                









                                                            


                                                              









                                                             
 
                                           

                                                                          



                                                                  
 
                                
                      


                   
  



                                                                  
                                                                          









                                           
                                              
                               























































                                                                                    
                                  
 

                                            

                               


                                                 


                                                
                                                                     







                                                                        
                             
 


                                           
                               

                                                                             


                                                
                                                     

 




















                                                                   
                                                      












                                                                      
                                                         



                                                                            
                                     





                                                             
                                                                       



                             
                                                             



                              



                                             
                                              
                                                                         





                   





                                                                        

                                    


















                                                                 



























































                                                                                
                      




                                                                
                                                                        
                                                         
                                                                    









                                                                 







                                                              






                                                                             
   
                                                                 
                                                               
 
                                                

                  
                                                             



                                                                              
                                          






                                             




                                                                       

                                                                    
                                                  






                                                                             

                                                                            
 
                                
 
                               
                            

                                                      

                       
                  



                                                                               
                   



                                                                           
                                                     

                      

                      
                                      








                                                          
                                               
                                 
                                                   

                                   


                   


                                                                           



                                                                         


                                            
                                                    



                                      



                                                
 


                                                
                         



                                                                          







                                                                         
         
 
                
                      


         



                                                              
                                                     
                        


                     


                                            

              
                                                   

                              


                   






                                                                           








                                                                       





                                                       
                       




                                                                         

                                                                       
  



                                                                         
   
                                                                              

                                                                         
 
                            
 
                                                   
                                                       
 

                                                                   
                                                                              



                                                       

                  
 
      
 

                                                         

                                                                    



                            
                                     
                                             
                                                                        
                                                               

























                                                                              
                                                                         
 
                                                                   
                            
 
                                          
 
                                                     
                             

                                                                 
                                   

                                                          

                                                 
                  
                                                       
                   
                                                                   
                                                                               
                                




                                             
                                                                             
















                                                                             
                                                           


                                                       
                                                           



                                                   
                                                  
                                                             
                                      




                                                              
                                         
                                                                                
                                                 
                                                                              


                                   
  
                                                                      




                                                                    
 

                                                   




                                                                           



                                                               

                                    


                   





















                                                                             









                                                                   




                                                          
                               
                         
                                                                
                         
                          

                                  
                             
                                                           
                            

                                                                    





                         
  



























































                                                                           
                                                                 
                                                      






















                                                                      
                                                          
                                    
                            


                                     

                                                                       







                                                                   
                                                       













































                                                                                
                                       




                                                




























                                                                                


         






                                                                           
                                                            
                               
                                            
                                         
                                                           
























                                                                                
                                           
                                    


                                             




                                  



                                    

                                                                  
                                                              


                                                            
                                                          
 





                                                                        
                                                 















                                                                    
 
                                                                    


                                                                  
                                                

                              
                                                
 
 
  



                                             
                                                                   
                                                   
   
                                        
                                          
                                                               
 




                                              

                                                                    



                                     







                                                                         
   
                                                                      
 



                                                     







                                                        
                                                    
                                 
                                                                     
                                 


                                   


                                                                   
                                           
                                                    
                                 


                              
                           

                         
                       
                            


                                                        







                                              




                                                                     
                                                           
                        
                      
                        
                  
                                                                   
                   
                             
                                 
                                      
                      




                                                                                

         
                       





                                                                    
                                                          
                                                    
                                                            


                                






                                                                                





                                              

                            



                         






                                                                           



                                                           
                                                                                



                         
                            
                                                    
 




                                                                           




                                           






                                   
                                         

                                                                   
                                                               


                       
                                  
                             



                                                     



                      


                                       

                                        



                                      
                                      



                                            


                                                                           
                                                


                                                                          

         


                                            
                           







                                                                       

                                
                                   

                                


                                         
                                                                         



                                        


                                         
 

                                
 
                                                      



                                



                           


                                         
                                      

 
                          





                                                        

                                             
 


                                                                          















                                                         






                                                               
 


                                                                

                                              
                                                    

                                       

                                          
                              


                        
                





                                                           
                                                             
                                                    
                           




                                                         
                                                     










                                                                              
                                                                
                                                                        



                         
 

                                                   
 

                                                     
 

                                                            
 

                                                               
 








                                                               
                                             



                                                                  


                               
                                                                         

                 
/*
 * Simple NUMA memory policy for the Linux kernel.
 *
 * Copyright 2003,2004 Andi Kleen, SuSE Labs.
 * (C) Copyright 2005 Christoph Lameter, Silicon Graphics, Inc.
 * Subject to the GNU Public License, version 2.
 *
 * NUMA policy allows the user to give hints in which node(s) memory should
 * be allocated.
 *
 * Support four policies per VMA and per process:
 *
 * The VMA policy has priority over the process policy for a page fault.
 *
 * interleave     Allocate memory interleaved over a set of nodes,
 *                with normal fallback if it fails.
 *                For VMA based allocations this interleaves based on the
 *                offset into the backing object or offset into the mapping
 *                for anonymous memory. For process policy an process counter
 *                is used.
 *
 * bind           Only allocate memory on a specific set of nodes,
 *                no fallback.
 *                FIXME: memory is allocated starting with the first node
 *                to the last. It would be better if bind would truly restrict
 *                the allocation to memory nodes instead
 *
 * preferred       Try a specific node first before normal fallback.
 *                As a special case node -1 here means do the allocation
 *                on the local CPU. This is normally identical to default,
 *                but useful to set in a VMA when you have a non default
 *                process policy.
 *
 * default        Allocate on the local node first, or when on a VMA
 *                use the process policy. This is what Linux always did
 *		  in a NUMA aware kernel and still does by, ahem, default.
 *
 * The process policy is applied for most non interrupt memory allocations
 * in that process' context. Interrupts ignore the policies and always
 * try to allocate on the local CPU. The VMA policy is only applied for memory
 * allocations for a VMA in the VM.
 *
 * Currently there are a few corner cases in swapping where the policy
 * is not applied, but the majority should be handled. When process policy
 * is used it is not remembered over swap outs/swap ins.
 *
 * Only the highest zone in the zone hierarchy gets policied. Allocations
 * requesting a lower zone just use default policy. This implies that
 * on systems with highmem kernel lowmem allocation don't get policied.
 * Same with GFP_DMA allocations.
 *
 * For shmfs/tmpfs/hugetlbfs shared memory the policy is shared between
 * all users and remembered even when nobody has memory mapped.
 */

/* Notebook:
   fix mmap readahead to honour policy and enable policy for any page cache
   object
   statistics for bigpages
   global policy for page cache? currently it uses process policy. Requires
   first item above.
   handle mremap for shared memory (currently ignored for the policy)
   grows down?
   make bind policy root only? It can trigger oom much faster and the
   kernel is not always grateful with that.
*/

#include <linux/mempolicy.h>
#include <linux/mm.h>
#include <linux/highmem.h>
#include <linux/hugetlb.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/nodemask.h>
#include <linux/cpuset.h>
#include <linux/gfp.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/module.h>
#include <linux/nsproxy.h>
#include <linux/interrupt.h>
#include <linux/init.h>
#include <linux/compat.h>
#include <linux/swap.h>
#include <linux/seq_file.h>
#include <linux/proc_fs.h>
#include <linux/migrate.h>
#include <linux/rmap.h>
#include <linux/security.h>
#include <linux/syscalls.h>
#include <linux/ctype.h>

#include <asm/tlbflush.h>
#include <asm/uaccess.h>

#include "internal.h"

/* Internal flags */
#define MPOL_MF_DISCONTIG_OK (MPOL_MF_INTERNAL << 0)	/* Skip checks for continuous vmas */
#define MPOL_MF_INVERT (MPOL_MF_INTERNAL << 1)		/* Invert check for nodemask */
#define MPOL_MF_STATS (MPOL_MF_INTERNAL << 2)		/* Gather statistics */

static struct kmem_cache *policy_cache;
static struct kmem_cache *sn_cache;

/* Highest zone. An specific allocation for a zone below that is not
   policied. */
enum zone_type policy_zone = 0;

/*
 * run-time system-wide default policy => local allocation
 */
struct mempolicy default_policy = {
	.refcnt = ATOMIC_INIT(1), /* never free it */
	.mode = MPOL_PREFERRED,
	.flags = MPOL_F_LOCAL,
};

static const struct mempolicy_operations {
	int (*create)(struct mempolicy *pol, const nodemask_t *nodes);
	void (*rebind)(struct mempolicy *pol, const nodemask_t *nodes);
} mpol_ops[MPOL_MAX];

/* Check that the nodemask contains at least one populated zone */
static int is_valid_nodemask(const nodemask_t *nodemask)
{
	int nd, k;

	/* Check that there is something useful in this mask */
	k = policy_zone;

	for_each_node_mask(nd, *nodemask) {
		struct zone *z;

		for (k = 0; k <= policy_zone; k++) {
			z = &NODE_DATA(nd)->node_zones[k];
			if (z->present_pages > 0)
				return 1;
		}
	}

	return 0;
}

static inline int mpol_store_user_nodemask(const struct mempolicy *pol)
{
	return pol->flags & (MPOL_F_STATIC_NODES | MPOL_F_RELATIVE_NODES);
}

static void mpol_relative_nodemask(nodemask_t *ret, const nodemask_t *orig,
				   const nodemask_t *rel)
{
	nodemask_t tmp;
	nodes_fold(tmp, *orig, nodes_weight(*rel));
	nodes_onto(*ret, tmp, *rel);
}

static int mpol_new_interleave(struct mempolicy *pol, const nodemask_t *nodes)
{
	if (nodes_empty(*nodes))
		return -EINVAL;
	pol->v.nodes = *nodes;
	return 0;
}

static int mpol_new_preferred(struct mempolicy *pol, const nodemask_t *nodes)
{
	if (!nodes)
		pol->flags |= MPOL_F_LOCAL;	/* local allocation */
	else if (nodes_empty(*nodes))
		return -EINVAL;			/*  no allowed nodes */
	else
		pol->v.preferred_node = first_node(*nodes);
	return 0;
}

static int mpol_new_bind(struct mempolicy *pol, const nodemask_t *nodes)
{
	if (!is_valid_nodemask(nodes))
		return -EINVAL;
	pol->v.nodes = *nodes;
	return 0;
}

/* Create a new policy */
static struct mempolicy *mpol_new(unsigned short mode, unsigned short flags,
				  nodemask_t *nodes)
{
	struct mempolicy *policy;
	nodemask_t cpuset_context_nmask;
	int ret;

	pr_debug("setting mode %d flags %d nodes[0] %lx\n",
		 mode, flags, nodes ? nodes_addr(*nodes)[0] : -1);

	if (mode == MPOL_DEFAULT) {
		if (nodes && !nodes_empty(*nodes))
			return ERR_PTR(-EINVAL);
		return NULL;	/* simply delete any existing policy */
	}
	VM_BUG_ON(!nodes);

	/*
	 * MPOL_PREFERRED cannot be used with MPOL_F_STATIC_NODES or
	 * MPOL_F_RELATIVE_NODES if the nodemask is empty (local allocation).
	 * All other modes require a valid pointer to a non-empty nodemask.
	 */
	if (mode == MPOL_PREFERRED) {
		if (nodes_empty(*nodes)) {
			if (((flags & MPOL_F_STATIC_NODES) ||
			     (flags & MPOL_F_RELATIVE_NODES)))
				return ERR_PTR(-EINVAL);
			nodes = NULL;	/* flag local alloc */
		}
	} else if (nodes_empty(*nodes))
		return ERR_PTR(-EINVAL);
	policy = kmem_cache_alloc(policy_cache, GFP_KERNEL);
	if (!policy)
		return ERR_PTR(-ENOMEM);
	atomic_set(&policy->refcnt, 1);
	policy->mode = mode;
	policy->flags = flags;

	if (nodes) {
		/*
		 * cpuset related setup doesn't apply to local allocation
		 */
		cpuset_update_task_memory_state();
		if (flags & MPOL_F_RELATIVE_NODES)
			mpol_relative_nodemask(&cpuset_context_nmask, nodes,
					       &cpuset_current_mems_allowed);
		else
			nodes_and(cpuset_context_nmask, *nodes,
				  cpuset_current_mems_allowed);
		if (mpol_store_user_nodemask(policy))
			policy->w.user_nodemask = *nodes;
		else
			policy->w.cpuset_mems_allowed =
						cpuset_mems_allowed(current);
	}

	ret = mpol_ops[mode].create(policy,
				nodes ? &cpuset_context_nmask : NULL);
	if (ret < 0) {
		kmem_cache_free(policy_cache, policy);
		return ERR_PTR(ret);
	}
	return policy;
}

/* Slow path of a mpol destructor. */
void __mpol_put(struct mempolicy *p)
{
	if (!atomic_dec_and_test(&p->refcnt))
		return;
	kmem_cache_free(policy_cache, p);
}

static void mpol_rebind_default(struct mempolicy *pol, const nodemask_t *nodes)
{
}

static void mpol_rebind_nodemask(struct mempolicy *pol,
				 const nodemask_t *nodes)
{
	nodemask_t tmp;

	if (pol->flags & MPOL_F_STATIC_NODES)
		nodes_and(tmp, pol->w.user_nodemask, *nodes);
	else if (pol->flags & MPOL_F_RELATIVE_NODES)
		mpol_relative_nodemask(&tmp, &pol->w.user_nodemask, nodes);
	else {
		nodes_remap(tmp, pol->v.nodes, pol->w.cpuset_mems_allowed,
			    *nodes);
		pol->w.cpuset_mems_allowed = *nodes;
	}

	pol->v.nodes = tmp;
	if (!node_isset(current->il_next, tmp)) {
		current->il_next = next_node(current->il_next, tmp);
		if (current->il_next >= MAX_NUMNODES)
			current->il_next = first_node(tmp);
		if (current->il_next >= MAX_NUMNODES)
			current->il_next = numa_node_id();
	}
}

static void mpol_rebind_preferred(struct mempolicy *pol,
				  const nodemask_t *nodes)
{
	nodemask_t tmp;

	if (pol->flags & MPOL_F_STATIC_NODES) {
		int node = first_node(pol->w.user_nodemask);

		if (node_isset(node, *nodes)) {
			pol->v.preferred_node = node;
			pol->flags &= ~MPOL_F_LOCAL;
		} else
			pol->flags |= MPOL_F_LOCAL;
	} else if (pol->flags & MPOL_F_RELATIVE_NODES) {
		mpol_relative_nodemask(&tmp, &pol->w.user_nodemask, nodes);
		pol->v.preferred_node = first_node(tmp);
	} else if (!(pol->flags & MPOL_F_LOCAL)) {
		pol->v.preferred_node = node_remap(pol->v.preferred_node,
						   pol->w.cpuset_mems_allowed,
						   *nodes);
		pol->w.cpuset_mems_allowed = *nodes;
	}
}

/* Migrate a policy to a different set of nodes */