aboutsummaryrefslogblamecommitdiffstats
path: root/drivers/infiniband/core/uverbs_cmd.c
blob: 06ed32c8662facea14196c44349b2a0f0c757d05 (plain) (tree)
1
2
3
4
5

                                                                   
                                                                      
                                                           
                                                                  



























                                                                      

   
                       
                     
                       
                        
 
                          
 



                                  
                   
                      
 










                                                                              

                



                                                                     



                                                             






                                                                 
                        
                                       




                 























                                                                             











                                                                           





































                                                                             





                                                                               
                                                               



                         






                                                                    










                                                                            
                                               
                                                                     
 
                                                                          
                                                          






                                    

                                                                         
 

                                                                                
 
                                                                  
 
                                                   

                                               
                                                   
                                               
                                                
                                 
                
 


                                                       
 
                                         





                                                                    





                              



                                                                                


                                                            
                               
         
 

                                  
                                               
                                  
                                  

                                                              
 
                                  
                                            
 

                                                          
                                             



                              
                                                              

                                    
                            
         
 

                                                               

                                                          
                              
 


                                                                         
 
                                           
 

                                        





                                                                          
                                           
 
                 
 
         
                                              

                   


                                     
         
                        
 


                                                                        
    
                                           
                   

 
                                                               


                                                                           

                                                    
                                               
                                                    







                                                       
                                                                        
                                                                              


























                                                                          
                                                                

 
                                                                   


                                                
                                     
                
 
                                                 

                                         
 


                                                       
 
                                      
                                                                         
 
                                                           

 
                                                                 




                                              


                                     
                                                 


                                         
 


                                                       
 
                                                         



                                      
                                                                   
 
                                                           

 
                                                               


                                            


                                            
                                 
 


                                                       
 
                                                            

                                     
 


                                                


                         
                             
                           
                                 
                                   

                                        
                                                             

                               
 
                          

                                      
                                     
 

                                                          
                              
 
                                              
 
         
                                                                
                  

                  
    
                                      


                   
                                                                 

                                        
                
 


                                                       
 
                                                                            

 




















































































                                                                                   
                                                                
 
                                                               

                                             

                                                    
                                                      
                                                     
                                                
                                                     
                                 
 


                                                       
 
                                             


                                                

                                  



                                                   
                                           
                                                











                                                                  
                                                                             
                                                            

                                   


                                           
                    
                                                                            





                                            
                                       







                                                   





                                                               
                                                                     
                                
                                                      



                                          

                                                          
                              
 

                         
 
                                               
 
                                                       



                             
                                                         


                                          
                 
                                                               

    
                                               

                      

                         
 
                                               



                   
                                                                 

                                        
                
 


                                                       
 
                                                                                

 
                                                                            

                                                            

                            
                
                                                            


                                                         
                         
 
                                                          
 
                                                         
                                          



                           
                                              

                   

 
                                                             


                                          
                                          


                                         
                                 
 


                                                       
 


                                                                  


                                                   
 
                                                            

                                     
 
                                                                           

                              
                              
         
 
                                                     

                                                          





                                                                


                                                                               

                                  
                             



                                 
                                      
                           
                             
                           
                                
                                        
                                     
 
                          
 


                                      
                                  
 

                                                          
                              
 
                              
 
                                              
 
         
                                                              
 
        
                              
 
         
                                      


                   
                                                               


                                            





                                               


                                                       
 







                                                                    
                                                                      

                                     


                          




                               






                                                           
                                                                           
                                              





                                       
                        



                                                                     
                
                                 

                                         


                                            





                                       
                                                          


                                       
                                      

          
                             



                   
                                                               

                                      
                
 


                                                       
 
                                                                            

 
                                                               






                                            
                                 
 


                                                       
 
                                                            

                                     
 
                                                                           




                              




                                                                         
                                                                             










                                  




                                       

                                                          
                              
 
                              
                                              

         
                              
        
                              
         
                                      


                   
                                                                 

                                        
                
 


                                                       
 
                                                                            

 
                                                                          


                                                        

                                                           
                                 
                
 


                                                       
 
                                                                      

                                     
 


                                                                            
                                     
                                                       
 

                                                          
                                              
                           

         
                                              

 
                                                                        
                                                                          
 
                                            
                                                                  

                                            
                                                
                                         
                                 
 
                                                                       
                                        
 
                                                                         
                                                          

                           
 
                                     
                                                                               

                                               



                                 
                                                    



                                         
 

                                            
                                
 


                                                
                              
         
                                   
                                          

                                                       
                                                                

                                   



                                                                     
                                 
                                      

                                              
                                                                           
 
                                        
                                     
 
                                                          

                            
 
                                                      

                                    
                   
 
       
                                                                


                  
         
                    
                                                                  

    
                                               



                            
                                                                


                                               
                                            
                
 


                                                       
 





                                               
                                        
                                    

 
                                                                   
 

                                            
                
 


                                                       






                               
                                     
                                    

 
                                                                

                                            
                                                  


                                                      


                                                       
 
                                                                           

                               
 
                                                                           


                         

                           
                                                          
    
                              
 
                   

 

                                                                       







                                                 
                                                  



                                                 
                                                  
                                                                
            
                                                         










                                                     
                                                              

                                           


                                                  
                                          

                                           
 


                                                       
 
                                                                           

                               
 
                                                                   
                                         
                                            
 







                                             
                                                                 




                                                        

         
                                                           
                              

                             
                
 


                                                                         
        
                              
                   

 
                                                                    


                                           
                
 


                                                       
 
                                                                           
                
                               
 


                                                            
                              
 
                 

 
                                                                 
 

                                              
                                              
                                             
                
 


                                                       
 
                                                                        


                                     
                                                                
                                       

                                                                
 

                               
                                                           

 
                                                      
                                                        
 



                                                     
                                                                      


                                                                 
                                                  

                                                

                                                
                                 

                                                                       

                              
                                                                         
                                                          



                                                           
                                     
 
                                                                  

                                                                      
                                                                            







                                           






                                                                                
 
                                             
                                                                             
                                                 






                                                           




                                      
                


                                                     
                        
                                          
                                                                               
                                                                                
                                                                           



                                                      
 

                                                                                 

                                                                     
                                                                            



                                                              
                                 

                         
 
                           
                                                                     
                                                                            

                                         
                                                                            
                                              
                                              



                                      
                                    

         
                                                        
                                          

                                 
                                 
                                  


                                                                 
                               
 




                                                        
 


                                                
 
                                              


                                                                         
                                                           
                                                          
                                                              

                                                                      



                              








                                                          
                                           

                                             
                                                                           
                                                         
 

                                  
                             

         
                                             



                                                        



                                                 
                                            


                                                       
                                           
                                        
                             

                                                          



                                                          

                                                     


                                                                    
         
 
                                        
 
                                      






                                                             
                                                                           
 
                                                          

                            
 



                                                                            
                                         

         
               
                                      
                
                                       
                              
                                       
                
                                       
                    
                                           
 
                                                              
       
                                                                
 
        

                                         
               
                                      
                
                                       
                              
                                       
                
                                       
                    
                                           
 
                                                      


                   
                                                                


                                               
                
 


                                                       
 














                                                     
                                         

 
                                                                   
 

                                          
 


                                                       
 
                                                               




                               
                                      

 
                                                              


                                             





                                                                     
                                 
 


                                                       
 
                                                                         
                                                          

                                    
 
                                                                            



                                
 
                                                   

                              
                              


                                                        
                                          









                                                
                              

         
                                        
                                                          




                                                

                                                          
                                 
 

                                                                              

                                           
 
                                                              
 
            
                                                                

                                 
        
                                                      


                   





















                                                                            
                                                               







                                                  


                                                       







                                                           
                                                                           
                  
                              



                                                              
 
                              
















                                                            
                                                        









                                                               

                                                                   





                                                                     
                                                                                 
 
                                                          




                         
                   

 





                                                                                


                                                                          




                            






















                                                                          
                                                      
                                                        
 


                                
 
                                                  


                               

                                                                         
                  



                              

                                                                  



                                



















































                                                                               

         
                                                     
                                                                       

                                                                           



                                

                                                     

                                                 



                                










































                                                                          
 
                                           

                                                                    
 
                                                 

                                                                        
 


                                                                          
                                                            
 
           
                              
    




                    
                                                                
 
                                          
                
 


                                                                 




                                                           
                                      

 
                                                                   
 
                                          


                                                                              

                



                                                       





                                                              



                                                    




                                                           

 
                                                                 
 

                                              
                                              
                                             
                
 


                                                       
 
                                                                        


                                     
                                                                       
                                       
                                                           

                               
 
                                                           

 

                                                    



                                                                           

                                                                       
 
 
                                                                



                                                

                                                                

                                                  
                                              
                      
                                                  


                                         
 












                                                                           




                                                    
                                                                           

                              
                         
         
 
                                         


                                            
                                                                    

                                                   
                                     



                                                                
                                     

                 








                                                                     

                                                                   




                                              
                                                                        
                                                                             













                                                                          

                                                                     












                                                                           

                                                                       













                                                                             

                                                                     





                                              
                                     

                 







                                                                               










                                                       
 

                                                       
                                                                        


                                                                            
                                              
                                             






                                                
                                                                  






                                                          


                                                           
 
        
                              

                    
                                           
                                                         




                                
    

                       
                   

 


                                                                     





                                                          

                                         



                                                         










                                                                  






                                                
                                                                









                                                            






                                                                             




















                                                                            


                                                                            






















                                                
                                                                


                                             

                                                  
                                           
                      
                                    
 


                                                                    
 

                                                                         


                                   
                                                                           

                              
                         
         

                        
                                                                  
 

                              




                                                          
         
 


                                                           
    





                                
                   

 
                                                                    


                                                 

                                                      
                                                
                      
                                    
 


                                                                    
 

                                                                         


                                   
                                                                               

                              
                         
         

                        
                                                               
 
                               
 






                                                          


                                                           

    





                                
                   

 
                                                                





                                              
                                                  
                
                                 
 


                                                       
 
                                                            

                                     
 




                                                             
                                                                           
                  
                              
                         

         
                                                                 
                                           





                                                             
                                 




                                                                     
                
                                               
         
 
                                                                  

                                  
                             

         
                            
                                            
                          
 

                                  

                                                          
                              
 
                              
                                              
 
         

                                                              
 

                              
 
    
                                      


                   
                                                                 

                                        
                
 


                                                       
 
                                                                            

 
                                                                   


                                          
                                          
                                            
                                          
 


                                                       
 
                                                                           

                               
 
                                                                              
 
                                     
                                                          


                                                                              
                                     




                                                   
                             



                                                               
 
                                                         


                                                              

                             
        
                                       
                              
 
                   

 
                                                                   

                                          
                                          
                                         
                                            
                                          
                                                    
 


                                                       
 
                                                                           

                               
 
                                                                              
                                     
 
                                                          



                                                                              
                                     


                              






                                                                     
        
                                       
                              
                   
 
 
                                                                 


                                             
                                                            

                       
                            
 


                         

                                                                             


                                                                               

                                                           
 
    
                                   
                         
 
    
                                   
                         
 
                    
 
                                    




                                                                        


                       
                                                       

                                                              




                                                            

                         
                                             
 


                                                             


                                                  















                                                                      
 
                                  
 
                                                                        


                                                                             










                                                                                






                                                                             






                                                                                  
                                                               



                                                                  


                                                              

                                                       







                                                                        
                                                 








                                                                         





                               
                                                                             




                                                                         
                                                                                   
















                                                                              




                                                                      
 
                                 
                             
 



                                                            

                             

                                                                              
 
                                                      
                              




                                                                            
                                       


                                                                             

                               




                                                                             
                                       


                                                                              
                      
                               




                                                                             
                                       


                                                                              



                                                                        
                      

                              




                                                                                
                                       


                                                                                 
                      














                                                                                










                                                                             










                                                                             










                                                                              





                               






                                                                             











                                                                              
                                                                 


                                                                      




                                                       
                                                                             
                                                              



                                                                       
                                                                   
 
                                          






                                                     
                                 
 
                                                       





                                   
                                                                         
                                                          

                                    
 
                                                                           




                              
                                                                           







                                           
                                               

                                                                
                                                     

                                                
 
                                                                                
















                                                 





                                                
                                                                           
                                                          


                              

                              
                                                              

         
                                                           
           
                              
           
                              
         
                                                      



                   
                                                                    
 
                                            
                                                          

                                              

                                            
                                                       





                                   
                                                                           
                                                                        

                                     
 
                                                                       
                                                           

                               
 
                                                           

 
                                                                   
 
                                          

                                       

                
                                                       





                               
                                                                          

                               
                                                                           




                                                  



                                                    

                                                                    
                              


                   
                                                                              
 
                                                     

                                                                 
                






                                                         
                                    
                                 
 
                                                                    


                           






                                                                      




                                                                   





                                                                  










                                                                
                                                            
                                                                         







                                       
                                                                     

                                    


                             

                                                          
 

                                                                             















                                                                   

                                                    
                                                                           
 
                                                          





                                          
                                          
 
                                              

         

                                              
                                      

                                          
                                          





                           
                                                                               
 

                                                      
 
                                                       





                                   
                                                              
                                                               

 
                                                                     




                                                   
                                                          

                                                     
                                                     
                                                     

                                    

                      
                                 
 
                                                                    


                           


                               
                                  

                              







                                                                   
                                                                     

                               
                                
                                                                              

                               



                                        
                                         

                                                                                      


                                       
                                                

                                                                             
                        
                                           

                                                

         



                                           
                                                              

                                    

                                   
 
                                                                           




                              




                                                                           

                                                                         



                              




                                                                     







                                                               
                                               
                                
                                                  
                                                                   


                                                              
                                                                               
                                                    

                                      
 

                                                               

                                                                  

                                                                  


                                                                                      
                              

                              
 

                                                                          
 

                                       
                              
         

                                                              
 


                                       
                                                          
                
                              
 
                              


                                       
                                              
         
                                                   
                                        
         

                                                 

                         
                              
         
                                      





                                       
                                                                      
 
                                            

                                            
                                                       

                           
 


                               
                                                                                

 
                                                                 

                                                                  
 
                                              
                                             

                                             
                                                                      

                                              
                                 
 
                                                                           
                                                           

                                    
 


                                                                       
                                           
                                                                               
                                                 
                                        
                                      
                                 

                 





                                                                        

                                                                                      
         
 
                                           
                                                                     
                                                                       
                                   




                                          
                                                                            


                                

         
                                                          
                                           



                                             
 

                                                
 


                                                  
                             

         



                                                  

                                                
 



                                                            




                                                 
                                           
                                                      


                                                       


                                    
                                         
                                                           
 
                                      
                                                 

                                            

                                                 
 

                                                          
                              
 
                                         
                                         



                                               
                              
                                                              
 
         
                                                                  

                                                    

                   
        
                              

           

                                               
 


                                                
                                         
         
 
    
                                                      


                   
                                                                 


                                              
                
 


                                                       
 
                                       







                                           
                                                                        

 
                                                                  

                                             
                
 


                                                       
 
                                                                       

 
                                                                 





                                         


                                                       
 
                                                                               

                               

                                    

                                       

                                                                    
 
                               
 
                   

 
                                                                






                                             


                                                       
 
                                                                               

                               
 
                                       
 
                               

                
                           






                                        
                                                           

 
                                                                  
 

                                               
                                               
                                              
                
 


                                                       
 
                                                                          


                                     
                                                                   
                                       
                                                    


                               
                                                           
 
 
                                                                      
 
                                                        
                                              
                                         

                                     

                
                                                 



                                         
                                                       








                               
                                                                            


                           
                                                           
 






                                                                
                                                                          

                                                  
                                                  
                                                         




                                                                    
                                                  
                                                    




                                                                         



                                                          
                                            

                                                                           
                                                           
 
 
                                                                   
 
                                          
                         

                
                                                       








                                           
                                                                           


                               
                                                                                




                              
 








                                                                               





























                                                                                

                                                                                

                                                                                
                                                           
 
                                                        



                                                                 


                                                                              
                                                                          




                                                                      


                                           





                                                                             



                                                                 
                                                         


                                                                              
                                                                          
















                                                                            
                                                                 
                                                         


                                                                              
                                                                          












                                                                                



                                                                   























                                                                                 

                                                         


                                   












                                                                               




                                                                
                                                                                 
                                                                         











                                                                                


                                 










                                                                                


                                 










                                                                                


                                 





                                                                        
                                                                 
                                                         


                                                                              
                                                                          

















































                                                                                


                                          


                                                                




                                                                              



                                                                        

                                                                           
                                                                          


                                 





















                                                                                 



                                                                  
                                                          


                                                                               
                                                                           
                                                                   
                                                           


                                                                               
                                                                           






















                                                                               


                                   




                                                                      
                                                               



                                                                               

                                                                 


                                                                              
                                                                            


           
/*
 * Copyright (c) 2005 Topspin Communications.  All rights reserved.
 * Copyright (c) 2005, 2006, 2007 Cisco Systems.  All rights reserved.
 * Copyright (c) 2005 PathScale, Inc.  All rights reserved.
 * Copyright (c) 2006 Mellanox Technologies.  All rights reserved.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <linux/file.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/sched.h>

#include <linux/uaccess.h>

#include <rdma/uverbs_types.h>
#include <rdma/uverbs_std_types.h>
#include "rdma_core.h"

#include "uverbs.h"
#include "core_priv.h"

/*
 * Copy a response to userspace. If the provided 'resp' is larger than the
 * user buffer it is silently truncated. If the user provided a larger buffer
 * then the trailing portion is zero filled.
 *
 * These semantics are intended to support future extension of the output
 * structures.
 */
static int uverbs_response(struct uverbs_attr_bundle *attrs, const void *resp,
			   size_t resp_len)
{
	int ret;

	if (uverbs_attr_is_valid(attrs, UVERBS_ATTR_CORE_OUT))
		return uverbs_copy_to_struct_or_zero(
			attrs, UVERBS_ATTR_CORE_OUT, resp, resp_len);

	if (copy_to_user(attrs->ucore.outbuf, resp,
			 min(attrs->ucore.outlen, resp_len)))
		return -EFAULT;

	if (resp_len < attrs->ucore.outlen) {
		/*
		 * Zero fill any extra memory that user
		 * space might have provided.
		 */
		ret = clear_user(attrs->ucore.outbuf + resp_len,
				 attrs->ucore.outlen - resp_len);
		if (ret)
			return -EFAULT;
	}

	return 0;
}

/*
 * Copy a request from userspace. If the provided 'req' is larger than the
 * user buffer then the user buffer is zero extended into the 'req'. If 'req'
 * is smaller than the user buffer then the uncopied bytes in the user buffer
 * must be zero.
 */
static int uverbs_request(struct uverbs_attr_bundle *attrs, void *req,
			  size_t req_len)
{
	if (copy_from_user(req, attrs->ucore.inbuf,
			   min(attrs->ucore.inlen, req_len)))
		return -EFAULT;

	if (attrs->ucore.inlen < req_len) {
		memset(req + attrs->ucore.inlen, 0,
		       req_len - attrs->ucore.inlen);
	} else if (attrs->ucore.inlen > req_len) {
		if (!ib_is_buffer_cleared(attrs->ucore.inbuf + req_len,
					  attrs->ucore.inlen - req_len))
			return -EOPNOTSUPP;
	}
	return 0;
}

/*
 * Generate the value for the 'response_length' protocol used by write_ex.
 * This is the number of bytes the kernel actually wrote. Userspace can use
 * this to detect what structure members in the response the kernel
 * understood.
 */
static u32 uverbs_response_length(struct uverbs_attr_bundle *attrs,
				  size_t resp_len)
{
	return min_t(size_t, attrs->ucore.outlen, resp_len);
}

/*
 * The iterator version of the request interface is for handlers that need to
 * step over a flex array at the end of a command header.
 */
struct uverbs_req_iter {
	const void __user *cur;
	const void __user *end;
};

static int uverbs_request_start(struct uverbs_attr_bundle *attrs,
				struct uverbs_req_iter *iter,
				void *req,
				size_t req_len)
{
	if (attrs->ucore.inlen < req_len)
		return -ENOSPC;

	if (copy_from_user(req, attrs->ucore.inbuf, req_len))
		return -EFAULT;

	iter->cur = attrs->ucore.inbuf + req_len;
	iter->end = attrs->ucore.inbuf + attrs->ucore.inlen;
	return 0;
}

static int uverbs_request_next(struct uverbs_req_iter *iter, void *val,
			       size_t len)
{
	if (iter->cur + len > iter->end)
		return -ENOSPC;

	if (copy_from_user(val, iter->cur, len))
		return -EFAULT;

	iter->cur += len;
	return 0;
}

static const void __user *uverbs_request_next_ptr(struct uverbs_req_iter *iter,
						  size_t len)
{
	const void __user *res = iter->cur;

	if (iter->cur + len > iter->end)
		return (void __force __user