Skip to content

Commit

Permalink
linux: [gpu-sched]describle state transition more specifically
Browse files Browse the repository at this point in the history
Signed-off-by: Luc Ma <[email protected]>
  • Loading branch information
lucmann committed Jan 14, 2025
1 parent 6e46f13 commit 2bd7ccf
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
55 changes: 52 additions & 3 deletions source/_drafts/kernel-sync.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,60 @@ categories: linux

信号量的实现基于一个变量,根据这个变量的取值范围可以把信号量分为

- binary semaphore (0/1)
- normal semaphore (non-negative integer)
- binary semaphore (= 1, mutex)
- normal semaphore (> 1, counting semaphore)

信号量的操作叫 **Acquire****Release**, 对应的实现是 `void down(struct semaphore *)``void up(struct semaphore *)`

- down

```c
static inline int __sched ___down_common(struct semaphore *sem, long state,
long timeout)
{
struct semaphore_waiter waiter;

list_add_tail(&waiter.list, &sem->wait_list);
waiter.task = current;
waiter.up = false;

for (;;) {
if (signal_pending_state(state, current))
goto interrupted;
if (unlikely(timeout <= 0))
goto timed_out;
__set_current_state(state);
raw_spin_unlock_irq(&sem->lock);
timeout = schedule_timeout(timeout);
raw_spin_lock_irq(&sem->lock);
if (waiter.up)
return 0;
}

timed_out:
list_del(&waiter.list);
return -ETIME;

interrupted:
list_del(&waiter.list);
return -EINTR;
}
```
- up
```c
static noinline void __sched __up(struct semaphore *sem)
{
struct semaphore_waiter *waiter = list_first_entry(&sem->wait_list,
struct semaphore_waiter, list);
list_del(&waiter->list);
waiter->up = true;
wake_up_process(waiter->task);
}
```

# References

- [Synchronization primitives in Linux kernel](https://0xax.gitbooks.io/linux-insides/content/SyncPrim/)
- [Synchronization primitives in Linux kernel](https://0xax.gitbooks.io/linux-insides/content/SyncPrim/)
- [https://yarchive.net/comp/linux/semaphores.html](https://yarchive.net/comp/linux/semaphores.html)
4 changes: 2 additions & 2 deletions source/_posts/lnx/gpu-sched.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ stateDiagram-v2
P: parked
I: idle
R --> S: Wait for Sth
R --> S: schedule_timeout()
R --> D: Wait for Disk I/O
R --> T: SIGTSTP
R --> t: gdb/strace
S --> R: Sth Available
S --> R: wake_up_process()
D --> R: I/O Completed
T --> R: SIGCONT
T --> t: gdb/strace
Expand Down

0 comments on commit 2bd7ccf

Please sign in to comment.