Skip to content

Commit 890a4cd

Browse files
committed
gfx: same dma-buf, same handle
Signed-off-by: Luc Ma <[email protected]>
1 parent 8585d8e commit 890a4cd

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

Diff for: source/_posts/gfx/piglit.md

+32-11
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,44 @@ flowchart TB
1212
subgraph p0 ["piglit_drm_create_dma_buf()"]
1313
p.a["drv->create(w, h, fourcc, src_data, drm_buf)"]
1414
p.b["drv->export(drm_buf)"]
15+
subgraph p1 ["create_and_destroy_texture()"]
16+
p.c["egl_image_for_dma_buf_fd<br>(buf, fd, fourcc, img)"]
17+
p.d["texture_for_egl_image(img, texture)"]
18+
p.e["eglDestroyImageKHR(display, img)"]
19+
p.f["glDeleteTextures(1, &texture)"]
20+
p.g["glFinish()"]
21+
end
1522
end
1623
end
1724
subgraph t1 [Thread 1]
1825
subgraph lo11 [Loop 1]
1926
t1.a["eglCreateContext()"]
2027
t1.b["eglMakeCurrent()"]
21-
t1.c["egl_image_for_dma_buf_fd<br>(buf, fd, fourcc, img)"]
22-
t1.d["texture_for_egl_image(img, texture)"]
23-
t1.e["eglDestroyImageKHR(display, img)"]
24-
t1.f["glDeleteTextures(1, &texture)"]
25-
t1.g["glFinish()"]
28+
subgraph lo111 ["create_and_destroy_texture()"]
29+
t1.c["egl_image_for_dma_buf_fd<br>(buf, fd, fourcc, img)"]
30+
t1.d["texture_for_egl_image(img, texture)"]
31+
t1.e["eglDestroyImageKHR(display, img)"]
32+
t1.f["glDeleteTextures(1, &texture)"]
33+
t1.g["glFinish()"]
34+
end
2635
end
2736
end
2837
2938
subgraph t2 [Thread 2]
3039
subgraph lo21 [Loop 1]
3140
t2.a["eglCreateContext()"]
3241
t2.b["eglMakeCurrent()"]
33-
t2.c["egl_image_for_dma_buf_fd<br>(buf, fd, fourcc, img)"]
34-
t2.d["texture_for_egl_image(img, texture)"]
35-
t2.e["eglDestroyImageKHR(display, img)"]
36-
t2.f["glDeleteTextures(1, &texture)"]
37-
t2.g["glFinish()"]
42+
subgraph lo211 ["create_and_destroy_texture()"]
43+
t2.c["egl_image_for_dma_buf_fd<br>(buf, fd, fourcc, img)"]
44+
t2.d["texture_for_egl_image(img, texture)"]
45+
t2.e["eglDestroyImageKHR(display, img)"]
46+
t2.f["glDeleteTextures(1, &texture)"]
47+
t2.g["glFinish()"]
48+
end
3849
end
3950
end
4051
41-
p.a --> p.b
52+
p.a --> p.b --> p.c --> p.d --> p.e --> p.f --> p.g
4253
4354
t1.a --> t1.b --> t1.c --> t1.d --> t1.e --> t1.f --> t1.g
4455
t2.a --> t2.b --> t2.c --> t2.d --> t2.e --> t2.f --> t2.g
@@ -68,6 +79,16 @@ flowchart TB
6879

6980
FD 是文件描述符,而 bo handles 在内核态和用户态各自维护有一份,分别是 **GEM bo handles****Userspace bo handles**, 在 mesa 的实现里,这两个集合是 1:1 映射关系, 当一个 gem bo handle 被导入, 如果对应的 userspace bo handle(实际上就是 drmPrimeFDToHandle() 返回的整数)已经存在,mesa 驱动仅仅是将这个 userspace bo handle 的引用计数加 1. 这就有可能导致 bo_destroy 函数和 bo_import() 函数发生竞争 (race), 从而导致 UAF, 这个 Piglit 用例就是专门测试这种场景的。
7081

82+
## FAQ
83+
84+
### Q: 两个线程先后对同一个 FD 调用 `drmPrimeFDToHandle()`,返回给各自线程的 handle ,是同一个值吗?
85+
86+
A: 因为是同一个 FD, 所以它底层的或者说内核的 drm_gem_object 也是同一个,返回的 handle 是同一个值,这也正是 ext_image_dma_buf_import/refcount-multithread 这个用例要构建的场景。
87+
88+
### Q: `drmPrimeFDToHandle()`/`drmPrimeHandleToFD()` 会影响 drm_gem_object 的引用计数吗?
89+
90+
A: 不会
91+
7192
# [Waffle](https://gitlab.freedesktop.org/mesa/waffle)
7293

7394
[Waffle /wa:fl/ 动听而无意义的话](https://gitlab.freedesktop.org/mesa/waffle) 是一个跨平台的在运行时动态地选择一个 OpenGL API.piglit 用它来创建 EGLContext, 最终会创建 Galliumm pipe_context. 此外 apitrace, Dante (open source DOOM 3) 也使用 waffle

0 commit comments

Comments
 (0)