@@ -48,7 +48,8 @@ struct amdgpu_mn {
4848 /* protected by adev->mn_lock */
4949 struct hlist_node node ;
5050
51- /* objects protected by mm->mmap_sem */
51+ /* objects protected by lock */
52+ struct mutex lock ;
5253 struct rb_root objects ;
5354};
5455
@@ -72,7 +73,7 @@ static void amdgpu_mn_destroy(struct work_struct *work)
7273 struct amdgpu_bo * bo , * next_bo ;
7374
7475 mutex_lock (& adev -> mn_lock );
75- down_write (& rmn -> mm -> mmap_sem );
76+ mutex_lock (& rmn -> lock );
7677 hash_del (& rmn -> node );
7778 rbtree_postorder_for_each_entry_safe (node , next_node , & rmn -> objects ,
7879 it .rb ) {
@@ -82,7 +83,7 @@ static void amdgpu_mn_destroy(struct work_struct *work)
8283 }
8384 kfree (node );
8485 }
85- up_write (& rmn -> mm -> mmap_sem );
86+ mutex_unlock (& rmn -> lock );
8687 mutex_unlock (& adev -> mn_lock );
8788 mmu_notifier_unregister_no_release (& rmn -> mn , rmn -> mm );
8889 kfree (rmn );
@@ -104,6 +105,76 @@ static void amdgpu_mn_release(struct mmu_notifier *mn,
104105 schedule_work (& rmn -> work );
105106}
106107
108+ /**
109+ * amdgpu_mn_invalidate_node - unmap all BOs of a node
110+ *
111+ * @node: the node with the BOs to unmap
112+ *
113+ * We block for all BOs and unmap them by move them
114+ * into system domain again.
115+ */
116+ static void amdgpu_mn_invalidate_node (struct amdgpu_mn_node * node ,
117+ unsigned long start ,
118+ unsigned long end )
119+ {
120+ struct amdgpu_bo * bo ;
121+ long r ;
122+
123+ list_for_each_entry (bo , & node -> bos , mn_list ) {
124+
125+ if (!amdgpu_ttm_tt_affect_userptr (bo -> tbo .ttm , start , end ))
126+ continue ;
127+
128+ r = amdgpu_bo_reserve (bo , true);
129+ if (r ) {
130+ DRM_ERROR ("(%ld) failed to reserve user bo\n" , r );
131+ continue ;
132+ }
133+
134+ r = reservation_object_wait_timeout_rcu (bo -> tbo .resv ,
135+ true, false, MAX_SCHEDULE_TIMEOUT );
136+ if (r <= 0 )
137+ DRM_ERROR ("(%ld) failed to wait for user bo\n" , r );
138+
139+ amdgpu_ttm_placement_from_domain (bo , AMDGPU_GEM_DOMAIN_CPU );
140+ r = ttm_bo_validate (& bo -> tbo , & bo -> placement , false, false);
141+ if (r )
142+ DRM_ERROR ("(%ld) failed to validate user bo\n" , r );
143+
144+ amdgpu_bo_unreserve (bo );
145+ }
146+ }
147+
148+ /**
149+ * amdgpu_mn_invalidate_page - callback to notify about mm change
150+ *
151+ * @mn: our notifier
152+ * @mn: the mm this callback is about
153+ * @address: address of invalidate page
154+ *
155+ * Invalidation of a single page. Blocks for all BOs mapping it
156+ * and unmap them by move them into system domain again.
157+ */
158+ static void amdgpu_mn_invalidate_page (struct mmu_notifier * mn ,
159+ struct mm_struct * mm ,
160+ unsigned long address )
161+ {
162+ struct amdgpu_mn * rmn = container_of (mn , struct amdgpu_mn , mn );
163+ struct interval_tree_node * it ;
164+
165+ mutex_lock (& rmn -> lock );
166+
167+ it = interval_tree_iter_first (& rmn -> objects , address , address );
168+ if (it ) {
169+ struct amdgpu_mn_node * node ;
170+
171+ node = container_of (it , struct amdgpu_mn_node , it );
172+ amdgpu_mn_invalidate_node (node , address , address );
173+ }
174+
175+ mutex_unlock (& rmn -> lock );
176+ }
177+
107178/**
108179 * amdgpu_mn_invalidate_range_start - callback to notify about mm change
109180 *
@@ -126,44 +197,24 @@ static void amdgpu_mn_invalidate_range_start(struct mmu_notifier *mn,
126197 /* notification is exclusive, but interval is inclusive */
127198 end -= 1 ;
128199
200+ mutex_lock (& rmn -> lock );
201+
129202 it = interval_tree_iter_first (& rmn -> objects , start , end );
130203 while (it ) {
131204 struct amdgpu_mn_node * node ;
132- struct amdgpu_bo * bo ;
133- long r ;
134205
135206 node = container_of (it , struct amdgpu_mn_node , it );
136207 it = interval_tree_iter_next (it , start , end );
137208
138- list_for_each_entry (bo , & node -> bos , mn_list ) {
139-
140- if (!amdgpu_ttm_tt_affect_userptr (bo -> tbo .ttm , start ,
141- end ))
142- continue ;
143-
144- r = amdgpu_bo_reserve (bo , true);
145- if (r ) {
146- DRM_ERROR ("(%ld) failed to reserve user bo\n" , r );
147- continue ;
148- }
149-
150- r = reservation_object_wait_timeout_rcu (bo -> tbo .resv ,
151- true, false, MAX_SCHEDULE_TIMEOUT );
152- if (r <= 0 )
153- DRM_ERROR ("(%ld) failed to wait for user bo\n" , r );
154-
155- amdgpu_ttm_placement_from_domain (bo , AMDGPU_GEM_DOMAIN_CPU );
156- r = ttm_bo_validate (& bo -> tbo , & bo -> placement , false, false);
157- if (r )
158- DRM_ERROR ("(%ld) failed to validate user bo\n" , r );
159-
160- amdgpu_bo_unreserve (bo );
161- }
209+ amdgpu_mn_invalidate_node (node , start , end );
162210 }
211+
212+ mutex_unlock (& rmn -> lock );
163213}
164214
165215static const struct mmu_notifier_ops amdgpu_mn_ops = {
166216 .release = amdgpu_mn_release ,
217+ .invalidate_page = amdgpu_mn_invalidate_page ,
167218 .invalidate_range_start = amdgpu_mn_invalidate_range_start ,
168219};
169220
@@ -196,6 +247,7 @@ static struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev)
196247 rmn -> adev = adev ;
197248 rmn -> mm = mm ;
198249 rmn -> mn .ops = & amdgpu_mn_ops ;
250+ mutex_init (& rmn -> lock );
199251 rmn -> objects = RB_ROOT ;
200252
201253 r = __mmu_notifier_register (& rmn -> mn , mm );
@@ -242,7 +294,7 @@ int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)
242294
243295 INIT_LIST_HEAD (& bos );
244296
245- down_write (& rmn -> mm -> mmap_sem );
297+ mutex_lock (& rmn -> lock );
246298
247299 while ((it = interval_tree_iter_first (& rmn -> objects , addr , end ))) {
248300 kfree (node );
@@ -256,7 +308,7 @@ int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)
256308 if (!node ) {
257309 node = kmalloc (sizeof (struct amdgpu_mn_node ), GFP_KERNEL );
258310 if (!node ) {
259- up_write (& rmn -> mm -> mmap_sem );
311+ mutex_unlock (& rmn -> lock );
260312 return - ENOMEM ;
261313 }
262314 }
@@ -271,7 +323,7 @@ int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)
271323
272324 interval_tree_insert (& node -> it , & rmn -> objects );
273325
274- up_write (& rmn -> mm -> mmap_sem );
326+ mutex_unlock (& rmn -> lock );
275327
276328 return 0 ;
277329}
@@ -297,7 +349,7 @@ void amdgpu_mn_unregister(struct amdgpu_bo *bo)
297349 return ;
298350 }
299351
300- down_write (& rmn -> mm -> mmap_sem );
352+ mutex_lock (& rmn -> lock );
301353
302354 /* save the next list entry for later */
303355 head = bo -> mn_list .next ;
@@ -312,6 +364,6 @@ void amdgpu_mn_unregister(struct amdgpu_bo *bo)
312364 kfree (node );
313365 }
314366
315- up_write (& rmn -> mm -> mmap_sem );
367+ mutex_unlock (& rmn -> lock );
316368 mutex_unlock (& adev -> mn_lock );
317369}
0 commit comments