Documentation/driver-api/sync_file.rst GitHub 원문 ↗

Linux 6.18.37 · Driver API

Sync File API Guide

Explicit fencing의 sync_file 전달, in/out-fence 방향과 fd·dma_fence reference ownership을 설명하는 한국어 전문 번역입니다.

Source pathDocumentation/driver-api/sync_file.rst
Source versionLinux v6.18.37
TranslationDUJINLABS 전문 번역 + 해설

요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.

1. 요약·해설

원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.

요약과 해설

sync_file.rst:1-86

`sync_file`은 `dma_fence`를 file descriptor로 userspace에 전달하여 driver와 process 경계를 넘는 explicit buffer synchronization을 제공합니다. Out-fence는 driver가 완료를 signal하고, in-fence는 driver가 buffer 사용 전에 기다리며, fd와 fence reference의 ownership 규칙을 정확히 지켜야 합니다.

문서 구성
원문 줄핵심 내용
1-28Sync file과 explicit fencing cycle
29-43In-fence와 out-fence 방향
44-67Out-fence sync_file 생성과 fd 설치
68-82In-fence 획득과 reference 해제
83-86Kernel header reference

2. 영어 원문 전체

번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.

원문 전체 펼치기
1 ===================
2 Sync File API Guide
3 ===================
4
5 :Author: Gustavo Padovan <gustavo at padovan dot org>
6
7 This document serves as a guide for device drivers writers on what the
8 sync_file API is, and how drivers can support it. Sync file is the carrier of
9 the fences(struct dma_fence) that are needed to synchronize between drivers or
10 across process boundaries.
11
12 The sync_file API is meant to be used to send and receive fence information
13 to/from userspace. It enables userspace to do explicit fencing, where instead
14 of attaching a fence to the buffer a producer driver (such as a GPU or V4L
15 driver) sends the fence related to the buffer to userspace via a sync_file.
16
17 The sync_file then can be sent to the consumer (DRM driver for example), that
18 will not use the buffer for anything before the fence(s) signals, i.e., the
19 driver that issued the fence is not using/processing the buffer anymore, so it
20 signals that the buffer is ready to use. And vice-versa for the consumer ->
21 producer part of the cycle.
22
23 Sync files allows userspace awareness on buffer sharing synchronization between
24 drivers.
25
26 Sync file was originally added in the Android kernel but current Linux Desktop
27 can benefit a lot from it.
28
29 in-fences and out-fences
30 ------------------------
31
32 Sync files can go either to or from userspace. When a sync_file is sent from
33 the driver to userspace we call the fences it contains 'out-fences'. They are
34 related to a buffer that the driver is processing or is going to process, so
35 the driver creates an out-fence to be able to notify, through
36 dma_fence_signal(), when it has finished using (or processing) that buffer.
37 Out-fences are fences that the driver creates.
38
39 On the other hand if the driver receives fence(s) through a sync_file from
40 userspace we call these fence(s) 'in-fences'. Receiving in-fences means that
41 we need to wait for the fence(s) to signal before using any buffer related to
42 the in-fences.
43
44 Creating Sync Files
45 -------------------
46
47 When a driver needs to send an out-fence userspace it creates a sync_file.
48
49 Interface::
50
51 struct sync_file *sync_file_create(struct dma_fence *fence);
52
53 The caller pass the out-fence and gets back the sync_file. That is just the
54 first step, next it needs to install an fd on sync_file->file. So it gets an
55 fd::
56
57 fd = get_unused_fd_flags(O_CLOEXEC);
58
59 and installs it on sync_file->file::
60
61 fd_install(fd, sync_file->file);
62
63 The sync_file fd now can be sent to userspace.
64
65 If the creation process fail, or the sync_file needs to be released by any
66 other reason fput(sync_file->file) should be used.
67
68 Receiving Sync Files from Userspace
69 -----------------------------------
70
71 When userspace needs to send an in-fence to the driver it passes file descriptor
72 of the Sync File to the kernel. The kernel can then retrieve the fences
73 from it.
74
75 Interface::
76
77 struct dma_fence *sync_file_get_fence(int fd);
78
79
80 The returned reference is owned by the caller and must be disposed of
81 afterwards using dma_fence_put(). In case of error, a NULL is returned instead.
82
83 References:
84
85 1. struct sync_file in include/linux/sync_file.h
86 2. All interfaces mentioned above defined in include/linux/sync_file.h
87

3. 한국어 전문 번역

영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.

Sync File API 개요

1-28

이 문서는 Gustavo Padovan이 작성한 device driver writer용 `sync_file` API guide입니다.

Sync file은 driver 사이 또는 process 경계를 넘어 synchronization할 때 필요한 fence인 `struct dma_fence`의 carrier입니다.

`sync_file` API는 fence 정보를 userspace로 보내거나 userspace에서 받는 데 사용합니다. 이를 통해 userspace는 fence를 buffer 자체에 attach하지 않는 explicit fencing을 수행할 수 있습니다.

GPU나 V4L driver 같은 producer driver는 buffer와 연관된 fence를 sync_file로 userspace에 보냅니다. Userspace는 이를 DRM driver 같은 consumer에 전달할 수 있습니다.

Consumer는 fence가 signal되기 전에는 buffer를 사용하지 않습니다. Fence를 발행한 driver가 더 이상 buffer를 사용·처리하지 않을 때 signal하여 buffer를 사용할 준비가 됐음을 알립니다. Consumer에서 producer로 돌아가는 cycle도 같은 방식입니다.

따라서 sync file은 driver 사이 buffer sharing synchronization을 userspace가 인식하고 조정할 수 있게 합니다. 이 기능은 원래 Android kernel에 추가됐지만 현재 Linux Desktop도 크게 활용할 수 있습니다.

Explicit fencing cycle
Producer driver`dma_fence` for buffer`sync_file`Userspace
Userspace`sync_file` fdConsumer driverWait for fence signal
Producer finishes buffer`dma_fence_signal()`Consumer may use buffer
Consumer to producerSame cycle in reverse

Fence는 buffer와 분리된 sync_file fd로 userspace를 거쳐 producer와 consumer 사이를 이동합니다.

===================
Sync File API Guide
===================

:Author: Gustavo Padovan <gustavo at padovan dot org>

This document serves as a guide for device drivers writers on what the
sync_file API is, and how drivers can support it. Sync file is the carrier of
the fences(struct dma_fence) that are needed to synchronize between drivers or
across process boundaries.

The sync_file API is meant to be used to send and receive fence information
to/from userspace. It enables userspace to do explicit fencing, where instead
of attaching a fence to the buffer a producer driver (such as a GPU or V4L
driver) sends the fence related to the buffer to userspace via a sync_file.

The sync_file then can be sent to the consumer (DRM driver for example), that
will not use the buffer for anything before the fence(s) signals, i.e., the
driver that issued the fence is not using/processing the buffer anymore, so it
signals that the buffer is ready to use. And vice-versa for the consumer ->
producer part of the cycle.

Sync files allows userspace awareness on buffer sharing synchronization between
drivers.

Sync file was originally added in the Android kernel but current Linux Desktop
can benefit a lot from it.

In-fence와 out-fence

29-43

Sync file은 userspace로 나가거나 userspace에서 들어올 수 있습니다.

Driver가 userspace로 sync_file을 보낼 때 그 안의 fence를 out-fence라고 합니다. 이 fence는 driver가 처리 중이거나 처리할 buffer와 관련되며 driver가 직접 만듭니다.

Driver는 buffer 사용·처리를 마쳤을 때 `dma_fence_signal()`을 호출하여 out-fence를 통해 완료를 알립니다.

반대로 driver가 userspace의 sync_file을 통해 받는 fence는 in-fence입니다. In-fence와 연관된 buffer를 사용하기 전에 해당 fence가 signal될 때까지 기다려야 합니다.

Fence 방향과 driver 책임
종류방향생성·동작
Out-fenceDriver → userspaceDriver가 생성하고 완료 시 `dma_fence_signal()`
In-fenceUserspace → driver연관 buffer 사용 전에 signal 대기

in-fences and out-fences
------------------------

Sync files can go either to or from userspace. When a sync_file is sent from
the driver to userspace we call the fences it contains 'out-fences'. They are
related to a buffer that the driver is processing or is going to process, so
the driver creates an out-fence to be able to notify, through
dma_fence_signal(), when it has finished using (or processing) that buffer.
Out-fences are fences that the driver creates.

On the other hand if the driver receives fence(s) through a sync_file from
userspace we call these fence(s) 'in-fences'. Receiving in-fences means that
we need to wait for the fence(s) to signal before using any buffer related to
the in-fences.

Sync file 생성과 fd 설치

44-67

Driver가 userspace로 out-fence를 보내려면 `sync_file_create(struct dma_fence *fence)`로 sync_file을 만듭니다.

Caller는 out-fence를 전달하고 `struct sync_file *`을 돌려받습니다. 그다음 `sync_file->file`에 file descriptor를 설치해야 합니다.

먼저 `get_unused_fd_flags(O_CLOEXEC)`로 사용하지 않는 fd를 얻고 `fd_install(fd, sync_file->file)`로 file을 설치합니다. 이후 sync_file fd를 userspace로 보낼 수 있습니다.

생성 과정이 실패하거나 다른 이유로 sync_file을 release해야 하면 `fput(sync_file->file)`을 사용해야 합니다.

Out-fence를 sync_file fd로 내보내기
Driver out-fence`sync_file_create(fence)``struct sync_file *`
`get_unused_fd_flags(O_CLOEXEC)`Unused fd
Unused fd + `sync_file->file``fd_install()`Userspace-visible sync_file fd
Failure or release`fput(sync_file->file)`

Sync file 생성 뒤 fd allocation과 installation을 순서대로 수행합니다.

Creating Sync Files
-------------------

When a driver needs to send an out-fence userspace it creates a sync_file.

Interface::

        struct sync_file *sync_file_create(struct dma_fence *fence);

The caller pass the out-fence and gets back the sync_file. That is just the
first step, next it needs to install an fd on sync_file->file. So it gets an
fd::

        fd = get_unused_fd_flags(O_CLOEXEC);

and installs it on sync_file->file::

        fd_install(fd, sync_file->file);

The sync_file fd now can be sent to userspace.

If the creation process fail, or the sync_file needs to be released by any
other reason fput(sync_file->file) should be used.

Userspace sync file 수신

68-82

Userspace가 driver에 in-fence를 보내려면 Sync File의 file descriptor를 kernel에 전달합니다. Kernel은 이 fd에서 fence를 가져올 수 있습니다.

`sync_file_get_fence(int fd)`는 대응 `struct dma_fence *`를 반환합니다.

반환된 reference의 ownership은 caller에게 있으므로 사용 후 반드시 `dma_fence_put()`으로 해제해야 합니다. 오류이면 fence 대신 `NULL`을 반환합니다.

In-fence 획득과 reference ownership
Userspace sync_file fd`sync_file_get_fence(fd)``struct dma_fence *`
Fence referenceCaller waits or uses`dma_fence_put()`
Lookup error`NULL`

Fd lookup 성공 시 caller가 fence reference의 해제 책임을 갖습니다.

Receiving Sync Files from Userspace
-----------------------------------

When userspace needs to send an in-fence to the driver it passes file descriptor
of the Sync File to the kernel. The kernel can then retrieve the fences
from it.

Interface::

        struct dma_fence *sync_file_get_fence(int fd);


The returned reference is owned by the caller and must be disposed of
afterwards using dma_fence_put(). In case of error, a NULL is returned instead.

Header reference

83-86

`struct sync_file`은 `include/linux/sync_file.h`에 정의되어 있습니다.

이 문서에서 언급한 모든 interface 역시 `include/linux/sync_file.h`에 정의되어 있습니다.

Sync file source reference
대상Source path
`struct sync_file``include/linux/sync_file.h`
Documented interfaces`include/linux/sync_file.h`

References:

1. struct sync_file in include/linux/sync_file.h
2. All interfaces mentioned above defined in include/linux/sync_file.h