3535#define AMDGPU_BO_LIST_MAX_PRIORITY 32u
3636#define AMDGPU_BO_LIST_NUM_BUCKETS (AMDGPU_BO_LIST_MAX_PRIORITY + 1)
3737
38- static int amdgpu_bo_list_set (struct amdgpu_device * adev ,
39- struct drm_file * filp ,
40- struct amdgpu_bo_list * list ,
41- struct drm_amdgpu_bo_list_entry * info ,
42- unsigned num_entries );
38+ static void amdgpu_bo_list_free_rcu (struct rcu_head * rcu )
39+ {
40+ struct amdgpu_bo_list * list = container_of (rcu , struct amdgpu_bo_list ,
41+ rhead );
42+
43+ kvfree (list );
44+ }
4345
44- static void amdgpu_bo_list_release_rcu (struct kref * ref )
46+ static void amdgpu_bo_list_free (struct kref * ref )
4547{
46- unsigned i ;
4748 struct amdgpu_bo_list * list = container_of (ref , struct amdgpu_bo_list ,
4849 refcount );
50+ struct amdgpu_bo_list_entry * e ;
4951
50- for ( i = 0 ; i < list -> num_entries ; ++ i )
51- amdgpu_bo_unref (& list -> array [ i ]. robj );
52+ amdgpu_bo_list_for_each_entry ( e , list )
53+ amdgpu_bo_unref (& e -> robj );
5254
53- mutex_destroy (& list -> lock );
54- kvfree (list -> array );
55- kfree_rcu (list , rhead );
55+ call_rcu (& list -> rhead , amdgpu_bo_list_free_rcu );
5656}
5757
58- int amdgpu_bo_list_create (struct amdgpu_device * adev ,
59- struct drm_file * filp ,
60- struct drm_amdgpu_bo_list_entry * info ,
61- unsigned num_entries ,
62- struct amdgpu_bo_list * * list_out )
58+ int amdgpu_bo_list_create (struct amdgpu_device * adev , struct drm_file * filp ,
59+ struct drm_amdgpu_bo_list_entry * info ,
60+ unsigned num_entries , struct amdgpu_bo_list * * result )
6361{
62+ unsigned last_entry = 0 , first_userptr = num_entries ;
63+ struct amdgpu_bo_list_entry * array ;
6464 struct amdgpu_bo_list * list ;
65+ uint64_t total_size = 0 ;
66+ size_t size ;
67+ unsigned i ;
6568 int r ;
6669
70+ if (num_entries > SIZE_MAX / sizeof (struct amdgpu_bo_list_entry ))
71+ return - EINVAL ;
6772
68- list = kzalloc (sizeof (struct amdgpu_bo_list ), GFP_KERNEL );
73+ size = sizeof (struct amdgpu_bo_list );
74+ size += num_entries * sizeof (struct amdgpu_bo_list_entry );
75+ list = kvmalloc (size , GFP_KERNEL );
6976 if (!list )
7077 return - ENOMEM ;
7178
72- /* initialize bo list*/
73- mutex_init (& list -> lock );
7479 kref_init (& list -> refcount );
75- r = amdgpu_bo_list_set (adev , filp , list , info , num_entries );
76- if (r ) {
77- kfree (list );
78- return r ;
79- }
80-
81- * list_out = list ;
82- return 0 ;
83- }
84-
85- static void amdgpu_bo_list_destroy (struct amdgpu_fpriv * fpriv , int id )
86- {
87- struct amdgpu_bo_list * list ;
80+ list -> gds_obj = adev -> gds .gds_gfx_bo ;
81+ list -> gws_obj = adev -> gds .gws_gfx_bo ;
82+ list -> oa_obj = adev -> gds .oa_gfx_bo ;
8883
89- mutex_lock (& fpriv -> bo_list_lock );
90- list = idr_remove (& fpriv -> bo_list_handles , id );
91- mutex_unlock (& fpriv -> bo_list_lock );
92- if (list )
93- kref_put (& list -> refcount , amdgpu_bo_list_release_rcu );
94- }
95-
96- static int amdgpu_bo_list_set (struct amdgpu_device * adev ,
97- struct drm_file * filp ,
98- struct amdgpu_bo_list * list ,
99- struct drm_amdgpu_bo_list_entry * info ,
100- unsigned num_entries )
101- {
102- struct amdgpu_bo_list_entry * array ;
103- struct amdgpu_bo * gds_obj = adev -> gds .gds_gfx_bo ;
104- struct amdgpu_bo * gws_obj = adev -> gds .gws_gfx_bo ;
105- struct amdgpu_bo * oa_obj = adev -> gds .oa_gfx_bo ;
106-
107- unsigned last_entry = 0 , first_userptr = num_entries ;
108- unsigned i ;
109- int r ;
110- unsigned long total_size = 0 ;
111-
112- array = kvmalloc_array (num_entries , sizeof (struct amdgpu_bo_list_entry ), GFP_KERNEL );
113- if (!array )
114- return - ENOMEM ;
84+ array = amdgpu_bo_list_array_entry (list , 0 );
11585 memset (array , 0 , num_entries * sizeof (struct amdgpu_bo_list_entry ));
11686
11787 for (i = 0 ; i < num_entries ; ++ i ) {
@@ -148,59 +118,56 @@ static int amdgpu_bo_list_set(struct amdgpu_device *adev,
148118 entry -> tv .shared = !entry -> robj -> prime_shared_count ;
149119
150120 if (entry -> robj -> preferred_domains == AMDGPU_GEM_DOMAIN_GDS )
151- gds_obj = entry -> robj ;
121+ list -> gds_obj = entry -> robj ;
152122 if (entry -> robj -> preferred_domains == AMDGPU_GEM_DOMAIN_GWS )
153- gws_obj = entry -> robj ;
123+ list -> gws_obj = entry -> robj ;
154124 if (entry -> robj -> preferred_domains == AMDGPU_GEM_DOMAIN_OA )
155- oa_obj = entry -> robj ;
125+ list -> oa_obj = entry -> robj ;
156126
157127 total_size += amdgpu_bo_size (entry -> robj );
158128 trace_amdgpu_bo_list_set (list , entry -> robj );
159129 }
160130
161- for (i = 0 ; i < list -> num_entries ; ++ i )
162- amdgpu_bo_unref (& list -> array [i ].robj );
163-
164- kvfree (list -> array );
165-
166- list -> gds_obj = gds_obj ;
167- list -> gws_obj = gws_obj ;
168- list -> oa_obj = oa_obj ;
169131 list -> first_userptr = first_userptr ;
170- list -> array = array ;
171132 list -> num_entries = num_entries ;
172133
173134 trace_amdgpu_cs_bo_status (list -> num_entries , total_size );
135+
136+ * result = list ;
174137 return 0 ;
175138
176139error_free :
177140 while (i -- )
178141 amdgpu_bo_unref (& array [i ].robj );
179- kvfree (array );
142+ kvfree (list );
180143 return r ;
144+
181145}
182146
183- struct amdgpu_bo_list *
184- amdgpu_bo_list_get (struct amdgpu_fpriv * fpriv , int id )
147+ static void amdgpu_bo_list_destroy (struct amdgpu_fpriv * fpriv , int id )
185148{
186- struct amdgpu_bo_list * result ;
149+ struct amdgpu_bo_list * list ;
187150
151+ mutex_lock (& fpriv -> bo_list_lock );
152+ list = idr_remove (& fpriv -> bo_list_handles , id );
153+ mutex_unlock (& fpriv -> bo_list_lock );
154+ if (list )
155+ kref_put (& list -> refcount , amdgpu_bo_list_free );
156+ }
157+
158+ int amdgpu_bo_list_get (struct amdgpu_fpriv * fpriv , int id ,
159+ struct amdgpu_bo_list * * result )
160+ {
188161 rcu_read_lock ();
189- result = idr_find (& fpriv -> bo_list_handles , id );
162+ * result = idr_find (& fpriv -> bo_list_handles , id );
190163
191- if (result ) {
192- if (kref_get_unless_zero (& result -> refcount )) {
193- rcu_read_unlock ();
194- mutex_lock (& result -> lock );
195- } else {
196- rcu_read_unlock ();
197- result = NULL ;
198- }
199- } else {
164+ if (* result && kref_get_unless_zero (& (* result )-> refcount )) {
200165 rcu_read_unlock ();
166+ return 0 ;
201167 }
202168
203- return result ;
169+ rcu_read_unlock ();
170+ return - ENOENT ;
204171}
205172
206173void amdgpu_bo_list_get_list (struct amdgpu_bo_list * list ,
@@ -211,6 +178,7 @@ void amdgpu_bo_list_get_list(struct amdgpu_bo_list *list,
211178 * concatenated in descending order.
212179 */
213180 struct list_head bucket [AMDGPU_BO_LIST_NUM_BUCKETS ];
181+ struct amdgpu_bo_list_entry * e ;
214182 unsigned i ;
215183
216184 for (i = 0 ; i < AMDGPU_BO_LIST_NUM_BUCKETS ; i ++ )
@@ -221,14 +189,13 @@ void amdgpu_bo_list_get_list(struct amdgpu_bo_list *list,
221189 * in the list, the sort mustn't change the ordering of buffers
222190 * with the same priority, i.e. it must be stable.
223191 */
224- for ( i = 0 ; i < list -> num_entries ; i ++ ) {
225- unsigned priority = list -> array [ i ]. priority ;
192+ amdgpu_bo_list_for_each_entry ( e , list ) {
193+ unsigned priority = e -> priority ;
226194
227- if (!list -> array [i ].robj -> parent )
228- list_add_tail (& list -> array [i ].tv .head ,
229- & bucket [priority ]);
195+ if (!e -> robj -> parent )
196+ list_add_tail (& e -> tv .head , & bucket [priority ]);
230197
231- list -> array [ i ]. user_pages = NULL ;
198+ e -> user_pages = NULL ;
232199 }
233200
234201 /* Connect the sorted buckets in the output list. */
@@ -238,20 +205,7 @@ void amdgpu_bo_list_get_list(struct amdgpu_bo_list *list,
238205
239206void amdgpu_bo_list_put (struct amdgpu_bo_list * list )
240207{
241- mutex_unlock (& list -> lock );
242- kref_put (& list -> refcount , amdgpu_bo_list_release_rcu );
243- }
244-
245- void amdgpu_bo_list_free (struct amdgpu_bo_list * list )
246- {
247- unsigned i ;
248-
249- for (i = 0 ; i < list -> num_entries ; ++ i )
250- amdgpu_bo_unref (& list -> array [i ].robj );
251-
252- mutex_destroy (& list -> lock );
253- kvfree (list -> array );
254- kfree (list );
208+ kref_put (& list -> refcount , amdgpu_bo_list_free );
255209}
256210
257211int amdgpu_bo_create_list_entry_array (struct drm_amdgpu_bo_list_in * in ,
@@ -304,7 +258,7 @@ int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
304258 union drm_amdgpu_bo_list * args = data ;
305259 uint32_t handle = args -> in .list_handle ;
306260 struct drm_amdgpu_bo_list_entry * info = NULL ;
307- struct amdgpu_bo_list * list ;
261+ struct amdgpu_bo_list * list , * old ;
308262 int r ;
309263
310264 r = amdgpu_bo_create_list_entry_array (& args -> in , & info );
@@ -322,7 +276,7 @@ int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
322276 r = idr_alloc (& fpriv -> bo_list_handles , list , 1 , 0 , GFP_KERNEL );
323277 mutex_unlock (& fpriv -> bo_list_lock );
324278 if (r < 0 ) {
325- amdgpu_bo_list_free (list );
279+ amdgpu_bo_list_put (list );
326280 return r ;
327281 }
328282
@@ -335,17 +289,22 @@ int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
335289 break ;
336290
337291 case AMDGPU_BO_LIST_OP_UPDATE :
338- r = - ENOENT ;
339- list = amdgpu_bo_list_get ( fpriv , handle );
340- if (! list )
292+ r = amdgpu_bo_list_create ( adev , filp , info , args -> in . bo_number ,
293+ & list );
294+ if (r )
341295 goto error_free ;
342296
343- r = amdgpu_bo_list_set (adev , filp , list , info ,
344- args -> in .bo_number );
345- amdgpu_bo_list_put (list );
346- if (r )
297+ mutex_lock (& fpriv -> bo_list_lock );
298+ old = idr_replace (& fpriv -> bo_list_handles , list , handle );
299+ mutex_unlock (& fpriv -> bo_list_lock );
300+
301+ if (IS_ERR (old )) {
302+ amdgpu_bo_list_put (list );
303+ r = PTR_ERR (old );
347304 goto error_free ;
305+ }
348306
307+ amdgpu_bo_list_put (old );
349308 break ;
350309
351310 default :
0 commit comments