Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Advanced Micro Devices, Inc. |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 | * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 18 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 20 | * OTHER DEALINGS IN THE SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #include <linux/mutex.h> |
| 24 | #include <linux/log2.h> |
| 25 | #include <linux/sched.h> |
| 26 | #include <linux/slab.h> |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 27 | #include <linux/amd-iommu.h> |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 28 | #include <linux/notifier.h> |
Alexey Skidanov | dd59239 | 2014-11-18 13:56:23 +0200 | [diff] [blame] | 29 | #include <linux/compat.h> |
| 30 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 31 | struct mm_struct; |
| 32 | |
| 33 | #include "kfd_priv.h" |
| 34 | |
| 35 | /* |
| 36 | * Initial size for the array of queues. |
| 37 | * The allocated size is doubled each time |
| 38 | * it is exceeded up to MAX_PROCESS_QUEUES. |
| 39 | */ |
| 40 | #define INITIAL_QUEUE_ARRAY_SIZE 16 |
| 41 | |
| 42 | /* |
| 43 | * List of struct kfd_process (field kfd_process). |
| 44 | * Unique/indexed by mm_struct* |
| 45 | */ |
| 46 | #define KFD_PROCESS_TABLE_SIZE 5 /* bits: 32 entries */ |
| 47 | static DEFINE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE); |
| 48 | static DEFINE_MUTEX(kfd_processes_mutex); |
| 49 | |
| 50 | DEFINE_STATIC_SRCU(kfd_processes_srcu); |
| 51 | |
| 52 | static struct workqueue_struct *kfd_process_wq; |
| 53 | |
| 54 | struct kfd_process_release_work { |
| 55 | struct work_struct kfd_work; |
| 56 | struct kfd_process *p; |
| 57 | }; |
| 58 | |
| 59 | static struct kfd_process *find_process(const struct task_struct *thread); |
| 60 | static struct kfd_process *create_process(const struct task_struct *thread); |
| 61 | |
| 62 | void kfd_process_create_wq(void) |
| 63 | { |
| 64 | if (!kfd_process_wq) |
| 65 | kfd_process_wq = create_workqueue("kfd_process_wq"); |
| 66 | } |
| 67 | |
| 68 | void kfd_process_destroy_wq(void) |
| 69 | { |
| 70 | if (kfd_process_wq) { |
| 71 | flush_workqueue(kfd_process_wq); |
| 72 | destroy_workqueue(kfd_process_wq); |
| 73 | kfd_process_wq = NULL; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | struct kfd_process *kfd_create_process(const struct task_struct *thread) |
| 78 | { |
| 79 | struct kfd_process *process; |
| 80 | |
| 81 | BUG_ON(!kfd_process_wq); |
| 82 | |
| 83 | if (thread->mm == NULL) |
| 84 | return ERR_PTR(-EINVAL); |
| 85 | |
| 86 | /* Only the pthreads threading model is supported. */ |
| 87 | if (thread->group_leader->mm != thread->mm) |
| 88 | return ERR_PTR(-EINVAL); |
| 89 | |
| 90 | /* Take mmap_sem because we call __mmu_notifier_register inside */ |
| 91 | down_write(&thread->mm->mmap_sem); |
| 92 | |
| 93 | /* |
| 94 | * take kfd processes mutex before starting of process creation |
| 95 | * so there won't be a case where two threads of the same process |
| 96 | * create two kfd_process structures |
| 97 | */ |
| 98 | mutex_lock(&kfd_processes_mutex); |
| 99 | |
| 100 | /* A prior open of /dev/kfd could have already created the process. */ |
| 101 | process = find_process(thread); |
| 102 | if (process) |
| 103 | pr_debug("kfd: process already found\n"); |
| 104 | |
| 105 | if (!process) |
| 106 | process = create_process(thread); |
| 107 | |
| 108 | mutex_unlock(&kfd_processes_mutex); |
| 109 | |
| 110 | up_write(&thread->mm->mmap_sem); |
| 111 | |
| 112 | return process; |
| 113 | } |
| 114 | |
| 115 | struct kfd_process *kfd_get_process(const struct task_struct *thread) |
| 116 | { |
| 117 | struct kfd_process *process; |
| 118 | |
| 119 | if (thread->mm == NULL) |
| 120 | return ERR_PTR(-EINVAL); |
| 121 | |
| 122 | /* Only the pthreads threading model is supported. */ |
| 123 | if (thread->group_leader->mm != thread->mm) |
| 124 | return ERR_PTR(-EINVAL); |
| 125 | |
| 126 | process = find_process(thread); |
| 127 | |
| 128 | return process; |
| 129 | } |
| 130 | |
| 131 | static struct kfd_process *find_process_by_mm(const struct mm_struct *mm) |
| 132 | { |
| 133 | struct kfd_process *process; |
| 134 | |
| 135 | hash_for_each_possible_rcu(kfd_processes_table, process, |
| 136 | kfd_processes, (uintptr_t)mm) |
| 137 | if (process->mm == mm) |
| 138 | return process; |
| 139 | |
| 140 | return NULL; |
| 141 | } |
| 142 | |
| 143 | static struct kfd_process *find_process(const struct task_struct *thread) |
| 144 | { |
| 145 | struct kfd_process *p; |
| 146 | int idx; |
| 147 | |
| 148 | idx = srcu_read_lock(&kfd_processes_srcu); |
| 149 | p = find_process_by_mm(thread->mm); |
| 150 | srcu_read_unlock(&kfd_processes_srcu, idx); |
| 151 | |
| 152 | return p; |
| 153 | } |
| 154 | |
| 155 | static void kfd_process_wq_release(struct work_struct *work) |
| 156 | { |
| 157 | struct kfd_process_release_work *my_work; |
| 158 | struct kfd_process_device *pdd, *temp; |
| 159 | struct kfd_process *p; |
| 160 | |
| 161 | my_work = (struct kfd_process_release_work *) work; |
| 162 | |
| 163 | p = my_work->p; |
| 164 | |
Oded Gabbay | 94a1ee0 | 2015-02-24 10:51:59 +0200 | [diff] [blame] | 165 | pr_debug("Releasing process (pasid %d) in workqueue\n", |
| 166 | p->pasid); |
| 167 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 168 | mutex_lock(&p->mutex); |
| 169 | |
| 170 | list_for_each_entry_safe(pdd, temp, &p->per_device_data, |
| 171 | per_device_list) { |
Oded Gabbay | 94a1ee0 | 2015-02-24 10:51:59 +0200 | [diff] [blame] | 172 | pr_debug("Releasing pdd (topology id %d) for process (pasid %d) in workqueue\n", |
| 173 | pdd->dev->id, p->pasid); |
| 174 | |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 175 | amd_iommu_unbind_pasid(pdd->dev->pdev, p->pasid); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 176 | list_del(&pdd->per_device_list); |
| 177 | |
| 178 | kfree(pdd); |
| 179 | } |
| 180 | |
| 181 | kfd_pasid_free(p->pasid); |
| 182 | |
| 183 | mutex_unlock(&p->mutex); |
| 184 | |
| 185 | mutex_destroy(&p->mutex); |
| 186 | |
| 187 | kfree(p->queues); |
| 188 | |
| 189 | kfree(p); |
| 190 | |
| 191 | kfree((void *)work); |
| 192 | } |
| 193 | |
| 194 | static void kfd_process_destroy_delayed(struct rcu_head *rcu) |
| 195 | { |
| 196 | struct kfd_process_release_work *work; |
| 197 | struct kfd_process *p; |
| 198 | |
| 199 | BUG_ON(!kfd_process_wq); |
| 200 | |
| 201 | p = container_of(rcu, struct kfd_process, rcu); |
| 202 | BUG_ON(atomic_read(&p->mm->mm_count) <= 0); |
| 203 | |
| 204 | mmdrop(p->mm); |
| 205 | |
Firo Yang | 1549fcd | 2015-04-23 17:58:05 +0800 | [diff] [blame^] | 206 | work = kmalloc(sizeof(struct kfd_process_release_work), GFP_ATOMIC); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 207 | |
| 208 | if (work) { |
| 209 | INIT_WORK((struct work_struct *) work, kfd_process_wq_release); |
| 210 | work->p = p; |
| 211 | queue_work(kfd_process_wq, (struct work_struct *) work); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | static void kfd_process_notifier_release(struct mmu_notifier *mn, |
| 216 | struct mm_struct *mm) |
| 217 | { |
| 218 | struct kfd_process *p; |
| 219 | |
| 220 | /* |
| 221 | * The kfd_process structure can not be free because the |
| 222 | * mmu_notifier srcu is read locked |
| 223 | */ |
| 224 | p = container_of(mn, struct kfd_process, mmu_notifier); |
| 225 | BUG_ON(p->mm != mm); |
| 226 | |
| 227 | mutex_lock(&kfd_processes_mutex); |
| 228 | hash_del_rcu(&p->kfd_processes); |
| 229 | mutex_unlock(&kfd_processes_mutex); |
| 230 | synchronize_srcu(&kfd_processes_srcu); |
| 231 | |
Ben Goz | 4510204 | 2014-07-17 01:04:10 +0300 | [diff] [blame] | 232 | mutex_lock(&p->mutex); |
| 233 | |
| 234 | /* In case our notifier is called before IOMMU notifier */ |
| 235 | pqm_uninit(&p->pqm); |
| 236 | |
| 237 | mutex_unlock(&p->mutex); |
| 238 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 239 | /* |
| 240 | * Because we drop mm_count inside kfd_process_destroy_delayed |
| 241 | * and because the mmu_notifier_unregister function also drop |
| 242 | * mm_count we need to take an extra count here. |
| 243 | */ |
| 244 | atomic_inc(&p->mm->mm_count); |
| 245 | mmu_notifier_unregister_no_release(&p->mmu_notifier, p->mm); |
| 246 | mmu_notifier_call_srcu(&p->rcu, &kfd_process_destroy_delayed); |
| 247 | } |
| 248 | |
| 249 | static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = { |
| 250 | .release = kfd_process_notifier_release, |
| 251 | }; |
| 252 | |
| 253 | static struct kfd_process *create_process(const struct task_struct *thread) |
| 254 | { |
| 255 | struct kfd_process *process; |
| 256 | int err = -ENOMEM; |
| 257 | |
| 258 | process = kzalloc(sizeof(*process), GFP_KERNEL); |
| 259 | |
| 260 | if (!process) |
| 261 | goto err_alloc_process; |
| 262 | |
| 263 | process->queues = kmalloc_array(INITIAL_QUEUE_ARRAY_SIZE, |
| 264 | sizeof(process->queues[0]), GFP_KERNEL); |
| 265 | if (!process->queues) |
| 266 | goto err_alloc_queues; |
| 267 | |
| 268 | process->pasid = kfd_pasid_alloc(); |
| 269 | if (process->pasid == 0) |
| 270 | goto err_alloc_pasid; |
| 271 | |
| 272 | mutex_init(&process->mutex); |
| 273 | |
| 274 | process->mm = thread->mm; |
| 275 | |
| 276 | /* register notifier */ |
| 277 | process->mmu_notifier.ops = &kfd_process_mmu_notifier_ops; |
| 278 | err = __mmu_notifier_register(&process->mmu_notifier, process->mm); |
| 279 | if (err) |
| 280 | goto err_mmu_notifier; |
| 281 | |
| 282 | hash_add_rcu(kfd_processes_table, &process->kfd_processes, |
| 283 | (uintptr_t)process->mm); |
| 284 | |
| 285 | process->lead_thread = thread->group_leader; |
| 286 | |
| 287 | process->queue_array_size = INITIAL_QUEUE_ARRAY_SIZE; |
| 288 | |
| 289 | INIT_LIST_HEAD(&process->per_device_data); |
| 290 | |
Ben Goz | 4510204 | 2014-07-17 01:04:10 +0300 | [diff] [blame] | 291 | err = pqm_init(&process->pqm, process); |
| 292 | if (err != 0) |
| 293 | goto err_process_pqm_init; |
| 294 | |
Alexey Skidanov | dd59239 | 2014-11-18 13:56:23 +0200 | [diff] [blame] | 295 | /* init process apertures*/ |
| 296 | process->is_32bit_user_mode = is_compat_task(); |
| 297 | if (kfd_init_apertures(process) != 0) |
| 298 | goto err_init_apretures; |
| 299 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 300 | return process; |
| 301 | |
Alexey Skidanov | dd59239 | 2014-11-18 13:56:23 +0200 | [diff] [blame] | 302 | err_init_apretures: |
| 303 | pqm_uninit(&process->pqm); |
Ben Goz | 4510204 | 2014-07-17 01:04:10 +0300 | [diff] [blame] | 304 | err_process_pqm_init: |
| 305 | hash_del_rcu(&process->kfd_processes); |
| 306 | synchronize_rcu(); |
| 307 | mmu_notifier_unregister_no_release(&process->mmu_notifier, process->mm); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 308 | err_mmu_notifier: |
| 309 | kfd_pasid_free(process->pasid); |
| 310 | err_alloc_pasid: |
| 311 | kfree(process->queues); |
| 312 | err_alloc_queues: |
| 313 | kfree(process); |
| 314 | err_alloc_process: |
| 315 | return ERR_PTR(err); |
| 316 | } |
| 317 | |
| 318 | struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev, |
Alexey Skidanov | 093c7d8 | 2014-11-18 14:00:04 +0200 | [diff] [blame] | 319 | struct kfd_process *p) |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 320 | { |
| 321 | struct kfd_process_device *pdd = NULL; |
| 322 | |
| 323 | list_for_each_entry(pdd, &p->per_device_data, per_device_list) |
| 324 | if (pdd->dev == dev) |
Alexey Skidanov | 093c7d8 | 2014-11-18 14:00:04 +0200 | [diff] [blame] | 325 | break; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 326 | |
Alexey Skidanov | 093c7d8 | 2014-11-18 14:00:04 +0200 | [diff] [blame] | 327 | return pdd; |
| 328 | } |
| 329 | |
| 330 | struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev, |
| 331 | struct kfd_process *p) |
| 332 | { |
| 333 | struct kfd_process_device *pdd = NULL; |
| 334 | |
| 335 | pdd = kzalloc(sizeof(*pdd), GFP_KERNEL); |
| 336 | if (pdd != NULL) { |
| 337 | pdd->dev = dev; |
| 338 | INIT_LIST_HEAD(&pdd->qpd.queues_list); |
| 339 | INIT_LIST_HEAD(&pdd->qpd.priv_queue_list); |
| 340 | pdd->qpd.dqm = dev->dqm; |
| 341 | list_add(&pdd->per_device_list, &p->per_device_data); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | return pdd; |
| 345 | } |
| 346 | |
| 347 | /* |
| 348 | * Direct the IOMMU to bind the process (specifically the pasid->mm) |
| 349 | * to the device. |
| 350 | * Unbinding occurs when the process dies or the device is removed. |
| 351 | * |
| 352 | * Assumes that the process lock is held. |
| 353 | */ |
| 354 | struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev, |
| 355 | struct kfd_process *p) |
| 356 | { |
Alexey Skidanov | 093c7d8 | 2014-11-18 14:00:04 +0200 | [diff] [blame] | 357 | struct kfd_process_device *pdd; |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 358 | int err; |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 359 | |
Alexey Skidanov | 093c7d8 | 2014-11-18 14:00:04 +0200 | [diff] [blame] | 360 | pdd = kfd_get_process_device_data(dev, p); |
| 361 | if (!pdd) { |
| 362 | pr_err("Process device data doesn't exist\n"); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 363 | return ERR_PTR(-ENOMEM); |
Alexey Skidanov | 093c7d8 | 2014-11-18 14:00:04 +0200 | [diff] [blame] | 364 | } |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 365 | |
| 366 | if (pdd->bound) |
| 367 | return pdd; |
| 368 | |
Oded Gabbay | b17f068 | 2014-07-17 00:06:27 +0300 | [diff] [blame] | 369 | err = amd_iommu_bind_pasid(dev->pdev, p->pasid, p->lead_thread); |
| 370 | if (err < 0) |
| 371 | return ERR_PTR(err); |
| 372 | |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 373 | pdd->bound = true; |
| 374 | |
| 375 | return pdd; |
| 376 | } |
| 377 | |
| 378 | void kfd_unbind_process_from_device(struct kfd_dev *dev, unsigned int pasid) |
| 379 | { |
| 380 | struct kfd_process *p; |
| 381 | struct kfd_process_device *pdd; |
| 382 | int idx, i; |
| 383 | |
| 384 | BUG_ON(dev == NULL); |
| 385 | |
| 386 | idx = srcu_read_lock(&kfd_processes_srcu); |
| 387 | |
| 388 | hash_for_each_rcu(kfd_processes_table, i, p, kfd_processes) |
| 389 | if (p->pasid == pasid) |
| 390 | break; |
| 391 | |
| 392 | srcu_read_unlock(&kfd_processes_srcu, idx); |
| 393 | |
| 394 | BUG_ON(p->pasid != pasid); |
| 395 | |
| 396 | mutex_lock(&p->mutex); |
| 397 | |
Ben Goz | 4510204 | 2014-07-17 01:04:10 +0300 | [diff] [blame] | 398 | pqm_uninit(&p->pqm); |
| 399 | |
Alexey Skidanov | 093c7d8 | 2014-11-18 14:00:04 +0200 | [diff] [blame] | 400 | pdd = kfd_get_process_device_data(dev, p); |
Oded Gabbay | 19f6d2a | 2014-07-16 23:25:31 +0300 | [diff] [blame] | 401 | |
| 402 | /* |
| 403 | * Just mark pdd as unbound, because we still need it to call |
| 404 | * amd_iommu_unbind_pasid() in when the process exits. |
| 405 | * We don't call amd_iommu_unbind_pasid() here |
| 406 | * because the IOMMU called us. |
| 407 | */ |
| 408 | if (pdd) |
| 409 | pdd->bound = false; |
| 410 | |
| 411 | mutex_unlock(&p->mutex); |
| 412 | } |
| 413 | |
| 414 | struct kfd_process_device *kfd_get_first_process_device_data(struct kfd_process *p) |
| 415 | { |
| 416 | return list_first_entry(&p->per_device_data, |
| 417 | struct kfd_process_device, |
| 418 | per_device_list); |
| 419 | } |
| 420 | |
| 421 | struct kfd_process_device *kfd_get_next_process_device_data(struct kfd_process *p, |
| 422 | struct kfd_process_device *pdd) |
| 423 | { |
| 424 | if (list_is_last(&pdd->per_device_list, &p->per_device_data)) |
| 425 | return NULL; |
| 426 | return list_next_entry(pdd, per_device_list); |
| 427 | } |
| 428 | |
| 429 | bool kfd_has_process_device_data(struct kfd_process *p) |
| 430 | { |
| 431 | return !(list_empty(&p->per_device_data)); |
| 432 | } |