This source file includes following definitions.
- drm_lease_owner
- _drm_find_lessee
- _drm_lease_held_master
- _drm_has_leased
- _drm_lease_held
- drm_lease_held
- drm_lease_filter_crtcs
- drm_lease_create
- drm_lease_destroy
- _drm_lease_revoke
- drm_lease_revoke
- validate_lease
- fill_object_idr
- drm_mode_create_lease_ioctl
- drm_mode_list_lessees_ioctl
- drm_mode_get_lease_ioctl
- drm_mode_revoke_lease_ioctl
1
2
3
4
5 #include <linux/file.h>
6 #include <linux/uaccess.h>
7
8 #include <drm/drm_auth.h>
9 #include <drm/drm_crtc_helper.h>
10 #include <drm/drm_drv.h>
11 #include <drm/drm_file.h>
12 #include <drm/drm_lease.h>
13 #include <drm/drm_print.h>
14
15 #include "drm_crtc_internal.h"
16 #include "drm_internal.h"
17 #include "drm_legacy.h"
18
19 #define drm_for_each_lessee(lessee, lessor) \
20 list_for_each_entry((lessee), &(lessor)->lessees, lessee_list)
21
22 static uint64_t drm_lease_idr_object;
23
24
25
26
27
28
29
30
31
32 struct drm_master *drm_lease_owner(struct drm_master *master)
33 {
34 while (master->lessor != NULL)
35 master = master->lessor;
36 return master;
37 }
38
39
40
41
42
43
44
45
46
47
48
49 static struct drm_master*
50 _drm_find_lessee(struct drm_master *master, int lessee_id)
51 {
52 lockdep_assert_held(&master->dev->mode_config.idr_mutex);
53 return idr_find(&drm_lease_owner(master)->lessee_idr, lessee_id);
54 }
55
56
57
58
59
60
61
62
63
64
65
66
67 static int _drm_lease_held_master(struct drm_master *master, int id)
68 {
69 lockdep_assert_held(&master->dev->mode_config.idr_mutex);
70 if (master->lessor)
71 return idr_find(&master->leases, id) != NULL;
72 return true;
73 }
74
75
76
77
78
79
80
81
82
83
84
85
86 static bool _drm_has_leased(struct drm_master *master, int id)
87 {
88 struct drm_master *lessee;
89
90 lockdep_assert_held(&master->dev->mode_config.idr_mutex);
91 drm_for_each_lessee(lessee, master)
92 if (_drm_lease_held_master(lessee, id))
93 return true;
94 return false;
95 }
96
97
98
99
100
101
102
103
104
105
106
107
108 bool _drm_lease_held(struct drm_file *file_priv, int id)
109 {
110 if (!file_priv || !file_priv->master)
111 return true;
112
113 return _drm_lease_held_master(file_priv->master, id);
114 }
115
116
117
118
119
120
121
122
123
124
125
126
127 bool drm_lease_held(struct drm_file *file_priv, int id)
128 {
129 struct drm_master *master;
130 bool ret;
131
132 if (!file_priv || !file_priv->master || !file_priv->master->lessor)
133 return true;
134
135 master = file_priv->master;
136 mutex_lock(&master->dev->mode_config.idr_mutex);
137 ret = _drm_lease_held_master(master, id);
138 mutex_unlock(&master->dev->mode_config.idr_mutex);
139 return ret;
140 }
141
142
143
144
145
146
147
148
149
150 uint32_t drm_lease_filter_crtcs(struct drm_file *file_priv, uint32_t crtcs_in)
151 {
152 struct drm_master *master;
153 struct drm_device *dev;
154 struct drm_crtc *crtc;
155 int count_in, count_out;
156 uint32_t crtcs_out = 0;
157
158 if (!file_priv || !file_priv->master || !file_priv->master->lessor)
159 return crtcs_in;
160
161 master = file_priv->master;
162 dev = master->dev;
163
164 count_in = count_out = 0;
165 mutex_lock(&master->dev->mode_config.idr_mutex);
166 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
167 if (_drm_lease_held_master(master, crtc->base.id)) {
168 uint32_t mask_in = 1ul << count_in;
169 if ((crtcs_in & mask_in) != 0) {
170 uint32_t mask_out = 1ul << count_out;
171 crtcs_out |= mask_out;
172 }
173 count_out++;
174 }
175 count_in++;
176 }
177 mutex_unlock(&master->dev->mode_config.idr_mutex);
178 return crtcs_out;
179 }
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196 static struct drm_master *drm_lease_create(struct drm_master *lessor, struct idr *leases)
197 {
198 struct drm_device *dev = lessor->dev;
199 int error;
200 struct drm_master *lessee;
201 int object;
202 int id;
203 void *entry;
204
205 DRM_DEBUG_LEASE("lessor %d\n", lessor->lessee_id);
206
207 lessee = drm_master_create(lessor->dev);
208 if (!lessee) {
209 DRM_DEBUG_LEASE("drm_master_create failed\n");
210 return ERR_PTR(-ENOMEM);
211 }
212
213 mutex_lock(&dev->mode_config.idr_mutex);
214
215 idr_for_each_entry(leases, entry, object) {
216 error = 0;
217 if (!idr_find(&dev->mode_config.object_idr, object))
218 error = -ENOENT;
219 else if (_drm_has_leased(lessor, object))
220 error = -EBUSY;
221
222 if (error != 0) {
223 DRM_DEBUG_LEASE("object %d failed %d\n", object, error);
224 goto out_lessee;
225 }
226 }
227
228
229 id = idr_alloc(&(drm_lease_owner(lessor)->lessee_idr), lessee, 1, 0, GFP_KERNEL);
230 if (id < 0) {
231 error = id;
232 goto out_lessee;
233 }
234
235 lessee->lessee_id = id;
236 lessee->lessor = drm_master_get(lessor);
237 list_add_tail(&lessee->lessee_list, &lessor->lessees);
238
239
240 lessee->leases = *leases;
241 DRM_DEBUG_LEASE("new lessee %d %p, lessor %d %p\n", lessee->lessee_id, lessee, lessor->lessee_id, lessor);
242
243 mutex_unlock(&dev->mode_config.idr_mutex);
244 return lessee;
245
246 out_lessee:
247 mutex_unlock(&dev->mode_config.idr_mutex);
248
249 drm_master_put(&lessee);
250
251 return ERR_PTR(error);
252 }
253
254
255
256
257
258
259
260
261
262
263 void drm_lease_destroy(struct drm_master *master)
264 {
265 struct drm_device *dev = master->dev;
266
267 mutex_lock(&dev->mode_config.idr_mutex);
268
269 DRM_DEBUG_LEASE("drm_lease_destroy %d\n", master->lessee_id);
270
271
272
273
274 WARN_ON(!list_empty(&master->lessees));
275
276
277 if (master->lessee_id != 0) {
278 DRM_DEBUG_LEASE("remove master %d from device list of lessees\n", master->lessee_id);
279 idr_remove(&(drm_lease_owner(master)->lessee_idr), master->lessee_id);
280 }
281
282
283 list_del(&master->lessee_list);
284
285 mutex_unlock(&dev->mode_config.idr_mutex);
286
287 if (master->lessor) {
288
289 drm_sysfs_lease_event(dev);
290 drm_master_put(&master->lessor);
291 }
292
293 DRM_DEBUG_LEASE("drm_lease_destroy done %d\n", master->lessee_id);
294 }
295
296
297
298
299
300 static void _drm_lease_revoke(struct drm_master *top)
301 {
302 int object;
303 void *entry;
304 struct drm_master *master = top;
305
306 lockdep_assert_held(&top->dev->mode_config.idr_mutex);
307
308
309
310
311
312 for (;;) {
313 DRM_DEBUG_LEASE("revoke leases for %p %d\n", master, master->lessee_id);
314
315
316 idr_for_each_entry(&master->leases, entry, object)
317 idr_remove(&master->leases, object);
318
319
320
321
322 if (!list_empty(&master->lessees)) {
323 master = list_first_entry(&master->lessees, struct drm_master, lessee_list);
324 } else {
325
326 while (master != top && master == list_last_entry(&master->lessor->lessees, struct drm_master, lessee_list))
327 master = master->lessor;
328
329 if (master == top)
330 break;
331
332
333 master = list_next_entry(master, lessee_list);
334 }
335 }
336 }
337
338
339
340
341
342 void drm_lease_revoke(struct drm_master *top)
343 {
344 mutex_lock(&top->dev->mode_config.idr_mutex);
345 _drm_lease_revoke(top);
346 mutex_unlock(&top->dev->mode_config.idr_mutex);
347 }
348
349 static int validate_lease(struct drm_device *dev,
350 int object_count,
351 struct drm_mode_object **objects,
352 bool universal_planes)
353 {
354 int o;
355 int has_crtc = -1;
356 int has_connector = -1;
357 int has_plane = -1;
358
359
360
361
362 for (o = 0; o < object_count; o++) {
363 if (objects[o]->type == DRM_MODE_OBJECT_CRTC && has_crtc == -1) {
364 has_crtc = o;
365 }
366 if (objects[o]->type == DRM_MODE_OBJECT_CONNECTOR && has_connector == -1)
367 has_connector = o;
368
369 if (universal_planes) {
370 if (objects[o]->type == DRM_MODE_OBJECT_PLANE && has_plane == -1)
371 has_plane = o;
372 }
373 }
374 if (has_crtc == -1 || has_connector == -1)
375 return -EINVAL;
376 if (universal_planes && has_plane == -1)
377 return -EINVAL;
378 return 0;
379 }
380
381 static int fill_object_idr(struct drm_device *dev,
382 struct drm_file *lessor_priv,
383 struct idr *leases,
384 int object_count,
385 u32 *object_ids)
386 {
387 struct drm_mode_object **objects;
388 u32 o;
389 int ret;
390 bool universal_planes = READ_ONCE(lessor_priv->universal_planes);
391
392 objects = kcalloc(object_count, sizeof(struct drm_mode_object *),
393 GFP_KERNEL);
394 if (!objects)
395 return -ENOMEM;
396
397
398
399 for (o = 0; o < object_count; o++) {
400 objects[o] = drm_mode_object_find(dev, lessor_priv,
401 object_ids[o],
402 DRM_MODE_OBJECT_ANY);
403 if (!objects[o]) {
404 ret = -ENOENT;
405 goto out_free_objects;
406 }
407
408 if (!drm_mode_object_lease_required(objects[o]->type)) {
409 DRM_DEBUG_KMS("invalid object for lease\n");
410 ret = -EINVAL;
411 goto out_free_objects;
412 }
413 }
414
415 ret = validate_lease(dev, object_count, objects, universal_planes);
416 if (ret) {
417 DRM_DEBUG_LEASE("lease validation failed\n");
418 goto out_free_objects;
419 }
420
421
422
423 for (o = 0; o < object_count; o++) {
424 struct drm_mode_object *obj = objects[o];
425 u32 object_id = objects[o]->id;
426 DRM_DEBUG_LEASE("Adding object %d to lease\n", object_id);
427
428
429
430
431
432
433
434
435
436 ret = idr_alloc(leases, &drm_lease_idr_object , object_id, object_id + 1, GFP_KERNEL);
437 if (ret < 0) {
438 DRM_DEBUG_LEASE("Object %d cannot be inserted into leases (%d)\n",
439 object_id, ret);
440 goto out_free_objects;
441 }
442 if (obj->type == DRM_MODE_OBJECT_CRTC && !universal_planes) {
443 struct drm_crtc *crtc = obj_to_crtc(obj);
444 ret = idr_alloc(leases, &drm_lease_idr_object, crtc->primary->base.id, crtc->primary->base.id + 1, GFP_KERNEL);
445 if (ret < 0) {
446 DRM_DEBUG_LEASE("Object primary plane %d cannot be inserted into leases (%d)\n",
447 object_id, ret);
448 goto out_free_objects;
449 }
450 if (crtc->cursor) {
451 ret = idr_alloc(leases, &drm_lease_idr_object, crtc->cursor->base.id, crtc->cursor->base.id + 1, GFP_KERNEL);
452 if (ret < 0) {
453 DRM_DEBUG_LEASE("Object cursor plane %d cannot be inserted into leases (%d)\n",
454 object_id, ret);
455 goto out_free_objects;
456 }
457 }
458 }
459 }
460
461 ret = 0;
462 out_free_objects:
463 for (o = 0; o < object_count; o++) {
464 if (objects[o])
465 drm_mode_object_put(objects[o]);
466 }
467 kfree(objects);
468 return ret;
469 }
470
471
472
473
474
475
476
477
478
479
480
481
482 int drm_mode_create_lease_ioctl(struct drm_device *dev,
483 void *data, struct drm_file *lessor_priv)
484 {
485 struct drm_mode_create_lease *cl = data;
486 size_t object_count;
487 int ret = 0;
488 struct idr leases;
489 struct drm_master *lessor = lessor_priv->master;
490 struct drm_master *lessee = NULL;
491 struct file *lessee_file = NULL;
492 struct file *lessor_file = lessor_priv->filp;
493 struct drm_file *lessee_priv;
494 int fd = -1;
495 uint32_t *object_ids;
496
497
498 if (!drm_core_check_feature(dev, DRIVER_MODESET))
499 return -EOPNOTSUPP;
500
501
502 if (lessor->lessor) {
503 DRM_DEBUG_LEASE("recursive leasing not allowed\n");
504 return -EINVAL;
505 }
506
507
508 if (cl->object_count == 0) {
509 DRM_DEBUG_LEASE("no objects in lease\n");
510 return -EINVAL;
511 }
512
513 if (cl->flags && (cl->flags & ~(O_CLOEXEC | O_NONBLOCK))) {
514 DRM_DEBUG_LEASE("invalid flags\n");
515 return -EINVAL;
516 }
517
518 object_count = cl->object_count;
519
520 object_ids = memdup_user(u64_to_user_ptr(cl->object_ids),
521 array_size(object_count, sizeof(__u32)));
522 if (IS_ERR(object_ids))
523 return PTR_ERR(object_ids);
524
525 idr_init(&leases);
526
527
528 ret = fill_object_idr(dev, lessor_priv, &leases,
529 object_count, object_ids);
530 kfree(object_ids);
531 if (ret) {
532 DRM_DEBUG_LEASE("lease object lookup failed: %i\n", ret);
533 idr_destroy(&leases);
534 return ret;
535 }
536
537
538 fd = get_unused_fd_flags(cl->flags & (O_CLOEXEC | O_NONBLOCK));
539 if (fd < 0) {
540 idr_destroy(&leases);
541 return fd;
542 }
543
544 DRM_DEBUG_LEASE("Creating lease\n");
545
546 lessee = drm_lease_create(lessor, &leases);
547
548 if (IS_ERR(lessee)) {
549 ret = PTR_ERR(lessee);
550 idr_destroy(&leases);
551 goto out_leases;
552 }
553
554
555 DRM_DEBUG_LEASE("Allocating lease file\n");
556 lessee_file = file_clone_open(lessor_file);
557 if (IS_ERR(lessee_file)) {
558 ret = PTR_ERR(lessee_file);
559 goto out_lessee;
560 }
561
562 lessee_priv = lessee_file->private_data;
563
564 drm_master_put(&lessee_priv->master);
565 lessee_priv->master = lessee;
566 lessee_priv->is_master = 1;
567 lessee_priv->authenticated = 1;
568
569
570 DRM_DEBUG_LEASE("Returning fd %d id %d\n", fd, lessee->lessee_id);
571 cl->fd = fd;
572 cl->lessee_id = lessee->lessee_id;
573
574
575 fd_install(fd, lessee_file);
576
577 DRM_DEBUG_LEASE("drm_mode_create_lease_ioctl succeeded\n");
578 return 0;
579
580 out_lessee:
581 drm_master_put(&lessee);
582
583 out_leases:
584 put_unused_fd(fd);
585
586 DRM_DEBUG_LEASE("drm_mode_create_lease_ioctl failed: %d\n", ret);
587 return ret;
588 }
589
590
591
592
593
594
595
596
597
598
599
600
601
602 int drm_mode_list_lessees_ioctl(struct drm_device *dev,
603 void *data, struct drm_file *lessor_priv)
604 {
605 struct drm_mode_list_lessees *arg = data;
606 __u32 __user *lessee_ids = (__u32 __user *) (uintptr_t) (arg->lessees_ptr);
607 __u32 count_lessees = arg->count_lessees;
608 struct drm_master *lessor = lessor_priv->master, *lessee;
609 int count;
610 int ret = 0;
611
612 if (arg->pad)
613 return -EINVAL;
614
615
616 if (!drm_core_check_feature(dev, DRIVER_MODESET))
617 return -EOPNOTSUPP;
618
619 DRM_DEBUG_LEASE("List lessees for %d\n", lessor->lessee_id);
620
621 mutex_lock(&dev->mode_config.idr_mutex);
622
623 count = 0;
624 drm_for_each_lessee(lessee, lessor) {
625
626 if (!idr_is_empty(&lessee->leases)) {
627 if (count_lessees > count) {
628 DRM_DEBUG_LEASE("Add lessee %d\n", lessee->lessee_id);
629 ret = put_user(lessee->lessee_id, lessee_ids + count);
630 if (ret)
631 break;
632 }
633 count++;
634 }
635 }
636
637 DRM_DEBUG_LEASE("Lessor leases to %d\n", count);
638 if (ret == 0)
639 arg->count_lessees = count;
640
641 mutex_unlock(&dev->mode_config.idr_mutex);
642
643 return ret;
644 }
645
646
647
648
649
650
651
652
653
654
655 int drm_mode_get_lease_ioctl(struct drm_device *dev,
656 void *data, struct drm_file *lessee_priv)
657 {
658 struct drm_mode_get_lease *arg = data;
659 __u32 __user *object_ids = (__u32 __user *) (uintptr_t) (arg->objects_ptr);
660 __u32 count_objects = arg->count_objects;
661 struct drm_master *lessee = lessee_priv->master;
662 struct idr *object_idr;
663 int count;
664 void *entry;
665 int object;
666 int ret = 0;
667
668 if (arg->pad)
669 return -EINVAL;
670
671
672 if (!drm_core_check_feature(dev, DRIVER_MODESET))
673 return -EOPNOTSUPP;
674
675 DRM_DEBUG_LEASE("get lease for %d\n", lessee->lessee_id);
676
677 mutex_lock(&dev->mode_config.idr_mutex);
678
679 if (lessee->lessor == NULL)
680
681 object_idr = &lessee->dev->mode_config.object_idr;
682 else
683
684 object_idr = &lessee->leases;
685
686 count = 0;
687 idr_for_each_entry(object_idr, entry, object) {
688 if (count_objects > count) {
689 DRM_DEBUG_LEASE("adding object %d\n", object);
690 ret = put_user(object, object_ids + count);
691 if (ret)
692 break;
693 }
694 count++;
695 }
696
697 DRM_DEBUG("lease holds %d objects\n", count);
698 if (ret == 0)
699 arg->count_objects = count;
700
701 mutex_unlock(&dev->mode_config.idr_mutex);
702
703 return ret;
704 }
705
706
707
708
709
710
711
712
713
714
715
716 int drm_mode_revoke_lease_ioctl(struct drm_device *dev,
717 void *data, struct drm_file *lessor_priv)
718 {
719 struct drm_mode_revoke_lease *arg = data;
720 struct drm_master *lessor = lessor_priv->master;
721 struct drm_master *lessee;
722 int ret = 0;
723
724 DRM_DEBUG_LEASE("revoke lease for %d\n", arg->lessee_id);
725
726
727 if (!drm_core_check_feature(dev, DRIVER_MODESET))
728 return -EOPNOTSUPP;
729
730 mutex_lock(&dev->mode_config.idr_mutex);
731
732 lessee = _drm_find_lessee(lessor, arg->lessee_id);
733
734
735 if (!lessee) {
736 ret = -ENOENT;
737 goto fail;
738 }
739
740
741 if (lessee->lessor != lessor) {
742 ret = -EACCES;
743 goto fail;
744 }
745
746 _drm_lease_revoke(lessee);
747
748 fail:
749 mutex_unlock(&dev->mode_config.idr_mutex);
750
751 return ret;
752 }