Skip to content

Commit c90af5f

Browse files
akmistrydisplaylink-emajewsk
authored andcommitted
CHROMIUM: drm/evdi: Implement pin/unpin GEM object functions
On GEM prime import by another DRM driver, the get_sg_table() callback is run. However, the backing pages for the object may not be allocated because they are only allocated by evdi on an mmap() operation. Instead, GEM provides the pin/unpin callback for the driver to take appropriate steps before calling get_sg_table(). If get_sg_table() is run without backing pacges allocated, a NULL pointer dereference, like below, occurs. [ 76.567556] ---[ end trace 3e7da4724f156b13 ]--- [ 76.576837] BUG: kernel NULL pointer dereference, address: 0000000000000000 [ 76.576848] #PF: supervisor read access in kernel mode [ 76.576852] #PF: error_code(0x0000) - not-present page [ 76.576856] PGD 0 P4D 0 [ 76.576863] Oops: 0000 [DisplayLink#1] PREEMPT SMP NOPTI [ 76.576871] CPU: 1 PID: 1447 Comm: chrome Tainted: G W 5.10.76-12479-gbbd208452b20 DisplayLink#1 <HASH:14e4 13> [ 76.576875] Hardware name: Google Grunt/Grunt, BIOS Google_Grunt.11031.115.0 11/15/2019 [ 76.576883] RIP: 0010:__sg_alloc_table_from_pages+0xd0/0x362 [ 76.576888] Code: 00 31 db 48 89 5d 90 83 fa 01 76 61 41 89 d7 41 bc 01 00 00 00 bb 01 00 00 00 31 f6 89 f6 48 81 c6 00 10 00 00 41 39 f1 76 1f <49> 8b 7c de f8 49 8b 0c de 48 29 c1 48 c1 f9 06 48 29 c7 48 c1 ff [ 76.576892] RSP: 0018:ffffbe93010efb90 EFLAGS: 00010286 [ 76.576897] RAX: ffffe0fc00000000 RBX: 0000000000000001 RCX: 0000000000000000 [ 76.576900] RDX: 0000000000000e10 RSI: 0000000000001000 RDI: ffffa3cd5764a950 [ 76.576903] RBP: ffffbe93010efc10 R08: 0000000000e10000 R09: 00000000fffff000 [ 76.576907] R10: 0000000000000000 R11: 0000000000000000 R12: 0000000000000001 [ 76.576910] R13: ffffa3ce0007f0b8 R14: 0000000000000000 R15: 0000000000000e10 [ 76.576914] FS: 00007a3bad2993c0(0000) GS:ffffa3ce2ad00000(0000) knlGS:0000000000000000 [ 76.576918] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 76.576921] CR2: 0000000000000000 CR3: 0000000123c66000 CR4: 00000000001506e0 [ 76.576925] Call Trace: [ 76.576937] ? _raw_spin_unlock+0xe/0x20 [ 76.576942] ? amdgpu_bo_create+0x26e/0x49c [ 76.576948] drm_prime_pages_to_sg+0x74/0xa3 [ 76.576953] drm_gem_map_dma_buf+0x38/0x94 [ 76.576959] dma_buf_dynamic_attach+0x14f/0x1ff [ 76.576964] amdgpu_gem_prime_import+0x113/0x1b8 [ 76.576969] drm_gem_prime_fd_to_handle+0x92/0x19c [ 76.576973] ? drm_prime_add_buf_handle+0xfd/0xfd [ 76.576977] drm_ioctl_kernel+0x8d/0xf5 [ 76.576981] drm_ioctl+0x281/0x3bb [ 76.576985] ? drm_prime_add_buf_handle+0xfd/0xfd [ 76.576991] amdgpu_drm_ioctl+0x49/0x7d [ 76.576997] __se_sys_ioctl+0x7c/0xb8 [ 76.577002] do_syscall_64+0x43/0x55 [ 76.577007] entry_SYSCALL_64_after_hwframe+0x44/0xa9 To resolve this, implement the pin/unpin operations using reference counting. This is the same approach taken by the drm_gem_shmem helpers. BUG=b:204816062 TEST=Deploy to grunt, plug in Displaylink adapter and use external display Change-Id: I7f8f5b9cac12742938e83fd2f75cb42b4ed6f190 Signed-off-by: Anand K Mistry <[email protected]> Style checks fix fix
1 parent 124284f commit c90af5f

4 files changed

Lines changed: 58 additions & 2 deletions

File tree

‎.travis.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ env:
2222
- KVER=5.9
2323
- KVER=5.11
2424
- KVER=5.12
25+
- KVER=5.13
26+
- KVER=5.14
2527
- KVER=master
2628

2729
jobs:

‎module/evdi_drm_drv.h‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515

1616
#include <linux/module.h>
1717
#include <linux/version.h>
18+
#include <linux/mutex.h>
1819
#include <linux/device.h>
19-
#if KERNEL_VERSION(5, 5, 0) <= LINUX_VERSION_CODE || defined(EL8)
20+
#if KERNEL_VERSION(5, 15, 0) < LINUX_VERSION_CODE
21+
#include <drm/drm_irq.h>
22+
#elif KERNEL_VERSION(5, 5, 0) <= LINUX_VERSION_CODE || defined(EL8)
2023
#include <drm/drm_drv.h>
2124
#include <drm/drm_fourcc.h>
2225
#include <drm/drm_ioctl.h>
@@ -62,6 +65,8 @@ struct evdi_device {
6265
struct evdi_gem_object {
6366
struct drm_gem_object base;
6467
struct page **pages;
68+
unsigned int pages_pin_count;
69+
struct mutex pages_lock;
6570
void *vmapping;
6671
#if KERNEL_VERSION(5, 11, 0) <= LINUX_VERSION_CODE
6772
bool vmap_is_iomem;

‎module/evdi_gem.c‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222

2323

2424
#if KERNEL_VERSION(5, 11, 0) <= LINUX_VERSION_CODE
25+
static int evdi_prime_pin(struct drm_gem_object *obj);
26+
static void evdi_prime_unpin(struct drm_gem_object *obj);
27+
2528
static const struct vm_operations_struct evdi_gem_vm_ops = {
2629
.fault = evdi_gem_fault,
2730
.open = drm_gem_vm_open,
@@ -30,6 +33,8 @@ static const struct vm_operations_struct evdi_gem_vm_ops = {
3033

3134
static struct drm_gem_object_funcs gem_obj_funcs = {
3235
.free = evdi_gem_free_object,
36+
.pin = evdi_prime_pin,
37+
.unpin = evdi_prime_unpin,
3338
.vm_ops = &evdi_gem_vm_ops,
3439
.export = drm_gem_prime_export,
3540
.get_sg_table = evdi_prime_get_sg_table,
@@ -80,6 +85,8 @@ struct evdi_gem_object *evdi_gem_alloc_object(struct drm_device *dev,
8085
obj->base.funcs = &gem_obj_funcs;
8186
#endif
8287

88+
mutex_init(&obj->pages_lock);
89+
8390
return obj;
8491
}
8592

@@ -226,6 +233,28 @@ static void evdi_gem_put_pages(struct evdi_gem_object *obj)
226233
obj->pages = NULL;
227234
}
228235

236+
static int evdi_pin_pages(struct evdi_gem_object *obj)
237+
{
238+
int ret = 0;
239+
240+
mutex_lock(&obj->pages_lock);
241+
if (obj->pages_pin_count++ == 0) {
242+
ret = evdi_gem_get_pages(obj, GFP_KERNEL);
243+
if (ret)
244+
obj->pages_pin_count--;
245+
}
246+
mutex_unlock(&obj->pages_lock);
247+
return ret;
248+
}
249+
250+
static void evdi_unpin_pages(struct evdi_gem_object *obj)
251+
{
252+
mutex_lock(&obj->pages_lock);
253+
if (--obj->pages_pin_count == 0)
254+
evdi_gem_put_pages(obj);
255+
mutex_unlock(&obj->pages_lock);
256+
}
257+
229258
int evdi_gem_vmap(struct evdi_gem_object *obj)
230259
{
231260
int page_count = obj->base.size / PAGE_SIZE;
@@ -248,6 +277,7 @@ int evdi_gem_vmap(struct evdi_gem_object *obj)
248277
return 0;
249278
}
250279

280+
ret = evdi_pin_pages(obj);
251281
ret = evdi_gem_get_pages(obj, GFP_KERNEL);
252282
if (ret)
253283
return ret;
@@ -283,6 +313,7 @@ void evdi_gem_vunmap(struct evdi_gem_object *obj)
283313
}
284314

285315
evdi_gem_put_pages(obj);
316+
evdi_unpin_pages(obj);
286317
}
287318

288319
void evdi_gem_free_object(struct drm_gem_object *gem_obj)
@@ -306,6 +337,7 @@ void evdi_gem_free_object(struct drm_gem_object *gem_obj)
306337
reservation_object_fini(&obj->_resv);
307338
#endif
308339
obj->resv = NULL;
340+
mutex_destroy(&obj->pages_lock);
309341
}
310342

311343
/*
@@ -328,6 +360,7 @@ int evdi_gem_mmap(struct drm_file *file,
328360
gobj = to_evdi_bo(obj);
329361

330362
ret = evdi_gem_get_pages(gobj, GFP_KERNEL);
363+
ret = evdi_pin_pages(gobj);
331364
if (ret)
332365
goto out;
333366

@@ -385,6 +418,22 @@ evdi_prime_import_sg_table(struct drm_device *dev,
385418
return &obj->base;
386419
}
387420

421+
#if KERNEL_VERSION(5, 11, 0) <= LINUX_VERSION_CODE
422+
static int evdi_prime_pin(struct drm_gem_object *obj)
423+
{
424+
struct evdi_gem_object *bo = to_evdi_bo(obj);
425+
426+
return evdi_pin_pages(bo);
427+
}
428+
429+
static void evdi_prime_unpin(struct drm_gem_object *obj)
430+
{
431+
struct evdi_gem_object *bo = to_evdi_bo(obj);
432+
433+
evdi_unpin_pages(bo);
434+
}
435+
#endif
436+
388437
struct sg_table *evdi_prime_get_sg_table(struct drm_gem_object *obj)
389438
{
390439
struct evdi_gem_object *bo = to_evdi_bo(obj);

‎module/evdi_painter.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ void evdi_painter_send_update_ready_if_needed(struct evdi_painter *painter)
686686
}
687687
}
688688

689-
static const char *dpms_str[] = { "on", "standby", "suspend", "off" };
689+
static const char * const dpms_str[] = { "on", "standby", "suspend", "off" };
690690

691691
void evdi_painter_dpms_notify(struct evdi_device *evdi, int mode)
692692
{

0 commit comments

Comments
 (0)