SlideShare a Scribd company logo
Outline
• spin_lock and semaphore in linux kernel
– Introduction and difference.
– Dead lock example of spin_lock.
• What is Context
– What is “context”.
– Control flow of procedure call, and interrupt handler.
• Log analysis
• Conclusion
– How to prevent dead lock of spin_lock.
0
Spin lock & Semaphore
• Semaphore:
– When init value is 1, it can be a mutex lock to prevent compromise of
critical section, just like spin lock.
– Different from spin lock, thread goes sleep for waiting lock when failed
to get the lock.
• Spin lock:
– Thread doesn’t go sleep for waiting lock when failed to get the lock, it
continue loop of trying to get lock.
1
Spin lock
• Spin lock usage for mutex lock :
2
Critical
Section
code
Spin_unlock(&mutex_lock)
Critical
Section
code
Spin_lock(&mutex_lock)
Spin_unlock(&mutex_lock)
1
Thread A start
execution.
Kernel code :
Thread ‘s time slice
is decreased to
zero. Thread’s
context will be
saved, then
processor is
assigned to
another thread
2
Timer interrupt
preempt thread A
Spin_lock(&mutex_lock)
3
Thread B failed to get
lock , and continue loop
for trying getting lock
forever
Kernel code :
Thread ‘s time slice
is decreased to
zero. Thread’s
context will be
saved, then
processor is
assigned to
another thread
4
Timer interrupt
preempt thread B
5 Thread A finish
critical section.
Thread A Thread B
What is context
• What does “context” means?
– A set of dedicated hardware resource that program will
use to meet the need of successful execution.
• Such as :
– general purpose register for computing.
– stack memory for support of procedure call.
– But from kernel’s point of view, “dedicated context of
process” actually is simulated, in fact resources are limited.
• kernel slices time and do context saving & restoring in purpose of
emulating a multi-processor environment.
• Program (process) will think just like that it have a dedicated context.
3
What is context
• What is user context and interrupt context
– user context: provided by kernel context-switch facility which is triggered by
timer interrupt, owner is call a user process, runs in user space code with user
mode or in kernel space code with svc mode.
– Interrupt context: part of registers (context?) save and restore by interrupt
handler by itself.
• Actually part of interrupt context(reg) will be the some context(reg) of
some user process.
4
Processor time
axis
Save every register which will be used later
into stack.
…
…
Restore those register which have been used.
And jump to return address (r14 register)
Pci bus interrupt
Timer interrupt
Timer interrupt
Thread A
Thread A
Thread B
Thread B
A’s subroutine
Int_handler()
What is context
• Compare Interrupt handler & procedure call.
– Interrupt handler run as a procedure call.
– The difference is that
• int_handler don’t receive any parameter and don’t return any value.
• Program is even unaware of execution of int_handler.
5
Processor time
axis
Pci bus interrupt
Timer interrupt
Timer interrupt
Thread A
Thread A
Thread B
Thread B
subroutine
Save every register which will be used later
into stack.
…
…
Restore those register which have been used,
and jump to return address(r14).
Save every register which will be used later
into stack.
Read parameter in param register
…
Put return value in param register
Restore those register which have been used,
and jump to return address(r14).
Void Foo(void) : user space
Int_handler(): kernel space
double-acquire deadlock(1/2)
• Spin_lock convention
– Unlike spin lock implementation in other
operating system, linux kernel’s spin lock is not
recursive.
– Double-acquire deadlock example as followed:
6
Spin_lock(&mutex_lock);
fooB();
Spin_unlock(&mutex_lock);
Thread A
Save every register which will be used later into stack.
Read parameter in param register
…
Spin_lock(&mutex_lock);
…
Put return value in param register
Restore those register which have been used,
and jump to return address(r14).
Void fooB(void)
double-acquire deadlock(2/2)• Spin_lock synchronization between user context and interrupt context
– Double-acquire deadlock example(2) as followed:
– Example that won’t have Double-acquire deadlock as followed:
7
Spin_lock(&mutex_lock);
Spin_unlock(&mutex_lock);
Thread A
Save every register which will be used later into stack.
…
Spin_lock(&mutex_lock);
…
Restore those register which have been used,
and jump to return address(r14).
Sdio_int_handler()
Interrupt
happens just
after thread A
get spin lock
Sdio_int handler
will be busy-
waiting
mutex_lock
Spin_lock(&mutex_lock);
Spin_unlock(&mutex_lock);
Thread A
Save every register which will be used later
into stack.
…
Spin_lock(&mutex_lock);
…
Restore those register which have been used,
and jump to return address(r14).
Sdio_int_handler()
Timer Interrupt
happens just
after thread A
get spin lock
Kernel code :
Thread ‘s time slice is
decreased to zero.
Thread’s context will be
saved, then processor is
assigned to another thread
Thread B’s user code
execution
Sdio Interrupt
happens just
after thread A
get spin lock
Sdio_int handler
and thread B will
be busy-waiting
mutex_lock
Log Analysis(1)
• In our case, CheckCallbackTimeout() might just
interrupt WiMAXQueryImformation() in user
context(CM_Query thread)
8
Spin_lock(&mutex_lock);
Spin_unlock(&mutex_lock);
Thread A
Timer Interrupt
happens just
after thread A
get spin lock
Kernel code :
…
If (timer has to be exucuted){
CheckCallbackTimeout();
}
…
…
Return;
CheckCallbackTimeout
{
LDDB_spin_lock();
…
}
Log Analysis(2)
• Timer callback function is called in __irq_svc.
• __irq_svc is a subroutine which is only called by irq
handler.
9
Conclusion – Immediate Solution
• Use spin_lock_irqsave and
spin_lock_irqrestore.
– Turn off interrupt before acquire spin lock.
10
Conclusion – what action we have to take right
now
• What should we do before implementation - Identify those
context which open the same lock to do synchronization.
– Prevent double-acquire deadlock scenario with interrupt disable API,
when lock is shared in interrupt and user context.
– Prevent using semaphore in interrupt context.
– Leave interrupt as soon as possible, and postpone task into other user
context, such as work queue.
• Turn on CONFIG_PROVE_LOCKING,
CONFIG_DEBUG_LOCK_ALLOC, CONFIG_DEBUG_SPINLOCK
– That will help debugging.
11
Reference
• Linux.Kernel.Development.3rd.Edition, Robert Love.
• Linux device driver programming 驅動程式設計,
平田 豐.
12
Appendix-context switch• Context-switch code
– Restore and jump should be combined to a atomic operation.
Copyright 2009 FUJITSU LIMITED 13
Timer interrupt code :
…
If thread ‘s time slice is decreased to zero.
{
save r0~r15 into current ’s TCB;
restore B’s r0~r14 registers;
jump r15 <- B’s TCB[15] + 3
}
return from interrupt;
Spin_lock(&mutex_lock);
…
…
Spin_unlock(&mutex_lock);
…
…
Sleep(2000ms);
…
…
Sema_get(&mutex_lock)
Sleep function (kernel code ):
…
…
save r0~r1 into current’s TCB;
restore A’s r0~r14 registers;
jump r15 <- A’s TCB[15] + 3
return ;
semaphore function (kernel code ):
….
if lsemaphore is zero {
save r0~r14 into current’s TCB;
restore A’s r0~r14 registers;
jump r15 <- B’s TCB[15] + 3
}
return ;
Thread A
Thread B
1
2
3
4
5

More Related Content

What's hot

Cgroups in android
Cgroups in androidCgroups in android
Cgroups in android
ramalinga prasad tadepalli
 
LISA2019 Linux Systems Performance
LISA2019 Linux Systems PerformanceLISA2019 Linux Systems Performance
LISA2019 Linux Systems Performance
Brendan Gregg
 
Improve Android System Component Performance
Improve Android System Component PerformanceImprove Android System Component Performance
Improve Android System Component Performance
National Cheng Kung University
 
Understanding a kernel oops and a kernel panic
Understanding a kernel oops and a kernel panicUnderstanding a kernel oops and a kernel panic
Understanding a kernel oops and a kernel panic
Joseph Lu
 
Introduction To Linux Kernel Modules
Introduction To Linux Kernel ModulesIntroduction To Linux Kernel Modules
Introduction To Linux Kernel Modules
dibyajyotig
 
Embedded Linux BSP Training (Intro)
Embedded Linux BSP Training (Intro)Embedded Linux BSP Training (Intro)
Embedded Linux BSP Training (Intro)
RuggedBoardGroup
 
Broken Linux Performance Tools 2016
Broken Linux Performance Tools 2016Broken Linux Performance Tools 2016
Broken Linux Performance Tools 2016
Brendan Gregg
 
Linux Memory Management with CMA (Contiguous Memory Allocator)
Linux Memory Management with CMA (Contiguous Memory Allocator)Linux Memory Management with CMA (Contiguous Memory Allocator)
Linux Memory Management with CMA (Contiguous Memory Allocator)
Pankaj Suryawanshi
 
Linux Internals - Part III
Linux Internals - Part IIILinux Internals - Part III
Linux Internals - Part III
Emertxe Information Technologies Pvt Ltd
 
Pci express transaction
Pci express transactionPci express transaction
Pci express transaction
y38y38
 
Linux 4.x Tracing: Performance Analysis with bcc/BPF
Linux 4.x Tracing: Performance Analysis with bcc/BPFLinux 4.x Tracing: Performance Analysis with bcc/BPF
Linux 4.x Tracing: Performance Analysis with bcc/BPF
Brendan Gregg
 
SFO15-200: Linux kernel generic TEE driver
SFO15-200: Linux kernel generic TEE driverSFO15-200: Linux kernel generic TEE driver
SFO15-200: Linux kernel generic TEE driver
Linaro
 
Yet another introduction to Linux RCU
Yet another introduction to Linux RCUYet another introduction to Linux RCU
Yet another introduction to Linux RCU
Viller Hsiao
 
YOW2021 Computing Performance
YOW2021 Computing PerformanceYOW2021 Computing Performance
YOW2021 Computing Performance
Brendan Gregg
 
Linux Internals - Interview essentials - 1.0
Linux Internals - Interview essentials - 1.0Linux Internals - Interview essentials - 1.0
Linux Internals - Interview essentials - 1.0
Emertxe Information Technologies Pvt Ltd
 
Interrupt Affinityについて
Interrupt AffinityについてInterrupt Affinityについて
Interrupt AffinityについてTakuya ASADA
 
Power optimization for Android apps
Power optimization for Android appsPower optimization for Android apps
Power optimization for Android apps
Xavier Hallade
 
HKG15-505: Power Management interactions with OP-TEE and Trusted Firmware
HKG15-505: Power Management interactions with OP-TEE and Trusted FirmwareHKG15-505: Power Management interactions with OP-TEE and Trusted Firmware
HKG15-505: Power Management interactions with OP-TEE and Trusted Firmware
Linaro
 
Linux kernel
Linux kernelLinux kernel
eBPF Workshop
eBPF WorkshopeBPF Workshop
eBPF Workshop
Michael Kehoe
 

What's hot (20)

Cgroups in android
Cgroups in androidCgroups in android
Cgroups in android
 
LISA2019 Linux Systems Performance
LISA2019 Linux Systems PerformanceLISA2019 Linux Systems Performance
LISA2019 Linux Systems Performance
 
Improve Android System Component Performance
Improve Android System Component PerformanceImprove Android System Component Performance
Improve Android System Component Performance
 
Understanding a kernel oops and a kernel panic
Understanding a kernel oops and a kernel panicUnderstanding a kernel oops and a kernel panic
Understanding a kernel oops and a kernel panic
 
Introduction To Linux Kernel Modules
Introduction To Linux Kernel ModulesIntroduction To Linux Kernel Modules
Introduction To Linux Kernel Modules
 
Embedded Linux BSP Training (Intro)
Embedded Linux BSP Training (Intro)Embedded Linux BSP Training (Intro)
Embedded Linux BSP Training (Intro)
 
Broken Linux Performance Tools 2016
Broken Linux Performance Tools 2016Broken Linux Performance Tools 2016
Broken Linux Performance Tools 2016
 
Linux Memory Management with CMA (Contiguous Memory Allocator)
Linux Memory Management with CMA (Contiguous Memory Allocator)Linux Memory Management with CMA (Contiguous Memory Allocator)
Linux Memory Management with CMA (Contiguous Memory Allocator)
 
Linux Internals - Part III
Linux Internals - Part IIILinux Internals - Part III
Linux Internals - Part III
 
Pci express transaction
Pci express transactionPci express transaction
Pci express transaction
 
Linux 4.x Tracing: Performance Analysis with bcc/BPF
Linux 4.x Tracing: Performance Analysis with bcc/BPFLinux 4.x Tracing: Performance Analysis with bcc/BPF
Linux 4.x Tracing: Performance Analysis with bcc/BPF
 
SFO15-200: Linux kernel generic TEE driver
SFO15-200: Linux kernel generic TEE driverSFO15-200: Linux kernel generic TEE driver
SFO15-200: Linux kernel generic TEE driver
 
Yet another introduction to Linux RCU
Yet another introduction to Linux RCUYet another introduction to Linux RCU
Yet another introduction to Linux RCU
 
YOW2021 Computing Performance
YOW2021 Computing PerformanceYOW2021 Computing Performance
YOW2021 Computing Performance
 
Linux Internals - Interview essentials - 1.0
Linux Internals - Interview essentials - 1.0Linux Internals - Interview essentials - 1.0
Linux Internals - Interview essentials - 1.0
 
Interrupt Affinityについて
Interrupt AffinityについてInterrupt Affinityについて
Interrupt Affinityについて
 
Power optimization for Android apps
Power optimization for Android appsPower optimization for Android apps
Power optimization for Android apps
 
HKG15-505: Power Management interactions with OP-TEE and Trusted Firmware
HKG15-505: Power Management interactions with OP-TEE and Trusted FirmwareHKG15-505: Power Management interactions with OP-TEE and Trusted Firmware
HKG15-505: Power Management interactions with OP-TEE and Trusted Firmware
 
Linux kernel
Linux kernelLinux kernel
Linux kernel
 
eBPF Workshop
eBPF WorkshopeBPF Workshop
eBPF Workshop
 

Similar to Dead Lock Analysis of spin_lock() in Linux Kernel (english)

Linux synchronization tools
Linux synchronization toolsLinux synchronization tools
Linux synchronization toolsmukul bhardwaj
 
Synchronization linux
Synchronization linuxSynchronization linux
Synchronization linuxSusant Sahani
 
Linux kernel development_ch9-10_20120410
Linux kernel development_ch9-10_20120410Linux kernel development_ch9-10_20120410
Linux kernel development_ch9-10_20120410huangachou
 
Linux kernel development chapter 10
Linux kernel development chapter 10Linux kernel development chapter 10
Linux kernel development chapter 10huangachou
 
An Introduction to Locks in Go
An Introduction to Locks in GoAn Introduction to Locks in Go
An Introduction to Locks in Go
Yu-Shuan Hsieh
 
Multithreaded Programming Part- III.pdf
Multithreaded Programming Part- III.pdfMultithreaded Programming Part- III.pdf
Multithreaded Programming Part- III.pdf
Harika Pudugosula
 
Let's Talk Locks!
Let's Talk Locks!Let's Talk Locks!
Let's Talk Locks!
C4Media
 
13 superscalar
13 superscalar13 superscalar
13 superscalar
Hammad Farooq
 
Concurrency 2010
Concurrency 2010Concurrency 2010
Concurrency 2010
敬倫 林
 
13_Superscalar.ppt
13_Superscalar.ppt13_Superscalar.ppt
13_Superscalar.ppt
LavleshkumarBais
 
Memory model
Memory modelMemory model
Memory model
Yi-Hsiu Hsu
 
Preempt_rt realtime patch
Preempt_rt realtime patchPreempt_rt realtime patch
Preempt_rt realtime patch
Emre Can Kucukoglu
 
semaphore & mutex.pdf
semaphore & mutex.pdfsemaphore & mutex.pdf
semaphore & mutex.pdf
Adrian Huang
 
Beneath the Linux Interrupt handling
Beneath the Linux Interrupt handlingBeneath the Linux Interrupt handling
Beneath the Linux Interrupt handling
Bhoomil Chavda
 
Linux Locking Mechanisms
Linux Locking MechanismsLinux Locking Mechanisms
Linux Locking Mechanisms
Kernel TLV
 
Highly Scalable Java Programming for Multi-Core System
Highly Scalable Java Programming for Multi-Core SystemHighly Scalable Java Programming for Multi-Core System
Highly Scalable Java Programming for Multi-Core System
James Gan
 
cache2k, Java Caching, Turbo Charged, FOSDEM 2015
cache2k, Java Caching, Turbo Charged, FOSDEM 2015cache2k, Java Caching, Turbo Charged, FOSDEM 2015
cache2k, Java Caching, Turbo Charged, FOSDEM 2015
cruftex
 
Lec13 Computer Architecture by Hsien-Hsin Sean Lee Georgia Tech -- Multicore
Lec13 Computer Architecture by Hsien-Hsin Sean Lee Georgia Tech -- MulticoreLec13 Computer Architecture by Hsien-Hsin Sean Lee Georgia Tech -- Multicore
Lec13 Computer Architecture by Hsien-Hsin Sean Lee Georgia Tech -- Multicore
Hsien-Hsin Sean Lee, Ph.D.
 
AOS Lab 6: Scheduling
AOS Lab 6: SchedulingAOS Lab 6: Scheduling
AOS Lab 6: SchedulingZubair Nabi
 

Similar to Dead Lock Analysis of spin_lock() in Linux Kernel (english) (20)

Linux synchronization tools
Linux synchronization toolsLinux synchronization tools
Linux synchronization tools
 
Synchronization linux
Synchronization linuxSynchronization linux
Synchronization linux
 
Linux kernel development_ch9-10_20120410
Linux kernel development_ch9-10_20120410Linux kernel development_ch9-10_20120410
Linux kernel development_ch9-10_20120410
 
Linux kernel development chapter 10
Linux kernel development chapter 10Linux kernel development chapter 10
Linux kernel development chapter 10
 
An Introduction to Locks in Go
An Introduction to Locks in GoAn Introduction to Locks in Go
An Introduction to Locks in Go
 
Multithreaded Programming Part- III.pdf
Multithreaded Programming Part- III.pdfMultithreaded Programming Part- III.pdf
Multithreaded Programming Part- III.pdf
 
Kernel
KernelKernel
Kernel
 
Let's Talk Locks!
Let's Talk Locks!Let's Talk Locks!
Let's Talk Locks!
 
13 superscalar
13 superscalar13 superscalar
13 superscalar
 
Concurrency 2010
Concurrency 2010Concurrency 2010
Concurrency 2010
 
13_Superscalar.ppt
13_Superscalar.ppt13_Superscalar.ppt
13_Superscalar.ppt
 
Memory model
Memory modelMemory model
Memory model
 
Preempt_rt realtime patch
Preempt_rt realtime patchPreempt_rt realtime patch
Preempt_rt realtime patch
 
semaphore & mutex.pdf
semaphore & mutex.pdfsemaphore & mutex.pdf
semaphore & mutex.pdf
 
Beneath the Linux Interrupt handling
Beneath the Linux Interrupt handlingBeneath the Linux Interrupt handling
Beneath the Linux Interrupt handling
 
Linux Locking Mechanisms
Linux Locking MechanismsLinux Locking Mechanisms
Linux Locking Mechanisms
 
Highly Scalable Java Programming for Multi-Core System
Highly Scalable Java Programming for Multi-Core SystemHighly Scalable Java Programming for Multi-Core System
Highly Scalable Java Programming for Multi-Core System
 
cache2k, Java Caching, Turbo Charged, FOSDEM 2015
cache2k, Java Caching, Turbo Charged, FOSDEM 2015cache2k, Java Caching, Turbo Charged, FOSDEM 2015
cache2k, Java Caching, Turbo Charged, FOSDEM 2015
 
Lec13 Computer Architecture by Hsien-Hsin Sean Lee Georgia Tech -- Multicore
Lec13 Computer Architecture by Hsien-Hsin Sean Lee Georgia Tech -- MulticoreLec13 Computer Architecture by Hsien-Hsin Sean Lee Georgia Tech -- Multicore
Lec13 Computer Architecture by Hsien-Hsin Sean Lee Georgia Tech -- Multicore
 
AOS Lab 6: Scheduling
AOS Lab 6: SchedulingAOS Lab 6: Scheduling
AOS Lab 6: Scheduling
 

More from Sneeker Yeh

Basic Concept of Pixel and MPEG data structure (english)
Basic Concept of Pixel and MPEG data structure (english)Basic Concept of Pixel and MPEG data structure (english)
Basic Concept of Pixel and MPEG data structure (english)
Sneeker Yeh
 
Concurrency bug identification through kernel panic log (english)
Concurrency bug identification through kernel panic log (english)Concurrency bug identification through kernel panic log (english)
Concurrency bug identification through kernel panic log (english)
Sneeker Yeh
 
Introduction to netlink in linux kernel (english)
Introduction to netlink in linux kernel (english)Introduction to netlink in linux kernel (english)
Introduction to netlink in linux kernel (english)
Sneeker Yeh
 
Introduction to synchronous display controller (chinese)
Introduction to synchronous display controller (chinese)Introduction to synchronous display controller (chinese)
Introduction to synchronous display controller (chinese)
Sneeker Yeh
 
Introduction to SPI and PMIC with SPI interface (chinese)
Introduction to SPI and PMIC with SPI interface (chinese)Introduction to SPI and PMIC with SPI interface (chinese)
Introduction to SPI and PMIC with SPI interface (chinese)
Sneeker Yeh
 
Introduction to Nand Flash interface (chinese)
Introduction to Nand Flash interface (chinese)Introduction to Nand Flash interface (chinese)
Introduction to Nand Flash interface (chinese)
Sneeker Yeh
 
FAT file system implementation from scratch in boot-loader (chinese)
FAT file system implementation from scratch in boot-loader (chinese)FAT file system implementation from scratch in boot-loader (chinese)
FAT file system implementation from scratch in boot-loader (chinese)
Sneeker Yeh
 
Bootloader and MMU (english)
Bootloader and MMU (english)Bootloader and MMU (english)
Bootloader and MMU (english)
Sneeker Yeh
 

More from Sneeker Yeh (8)

Basic Concept of Pixel and MPEG data structure (english)
Basic Concept of Pixel and MPEG data structure (english)Basic Concept of Pixel and MPEG data structure (english)
Basic Concept of Pixel and MPEG data structure (english)
 
Concurrency bug identification through kernel panic log (english)
Concurrency bug identification through kernel panic log (english)Concurrency bug identification through kernel panic log (english)
Concurrency bug identification through kernel panic log (english)
 
Introduction to netlink in linux kernel (english)
Introduction to netlink in linux kernel (english)Introduction to netlink in linux kernel (english)
Introduction to netlink in linux kernel (english)
 
Introduction to synchronous display controller (chinese)
Introduction to synchronous display controller (chinese)Introduction to synchronous display controller (chinese)
Introduction to synchronous display controller (chinese)
 
Introduction to SPI and PMIC with SPI interface (chinese)
Introduction to SPI and PMIC with SPI interface (chinese)Introduction to SPI and PMIC with SPI interface (chinese)
Introduction to SPI and PMIC with SPI interface (chinese)
 
Introduction to Nand Flash interface (chinese)
Introduction to Nand Flash interface (chinese)Introduction to Nand Flash interface (chinese)
Introduction to Nand Flash interface (chinese)
 
FAT file system implementation from scratch in boot-loader (chinese)
FAT file system implementation from scratch in boot-loader (chinese)FAT file system implementation from scratch in boot-loader (chinese)
FAT file system implementation from scratch in boot-loader (chinese)
 
Bootloader and MMU (english)
Bootloader and MMU (english)Bootloader and MMU (english)
Bootloader and MMU (english)
 

Recently uploaded

Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
Peter Caitens
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
Tier1 app
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
vrstrong314
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Natan Silnitsky
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
IES VE
 

Recently uploaded (20)

Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 

Dead Lock Analysis of spin_lock() in Linux Kernel (english)

  • 1. Outline • spin_lock and semaphore in linux kernel – Introduction and difference. – Dead lock example of spin_lock. • What is Context – What is “context”. – Control flow of procedure call, and interrupt handler. • Log analysis • Conclusion – How to prevent dead lock of spin_lock. 0
  • 2. Spin lock & Semaphore • Semaphore: – When init value is 1, it can be a mutex lock to prevent compromise of critical section, just like spin lock. – Different from spin lock, thread goes sleep for waiting lock when failed to get the lock. • Spin lock: – Thread doesn’t go sleep for waiting lock when failed to get the lock, it continue loop of trying to get lock. 1
  • 3. Spin lock • Spin lock usage for mutex lock : 2 Critical Section code Spin_unlock(&mutex_lock) Critical Section code Spin_lock(&mutex_lock) Spin_unlock(&mutex_lock) 1 Thread A start execution. Kernel code : Thread ‘s time slice is decreased to zero. Thread’s context will be saved, then processor is assigned to another thread 2 Timer interrupt preempt thread A Spin_lock(&mutex_lock) 3 Thread B failed to get lock , and continue loop for trying getting lock forever Kernel code : Thread ‘s time slice is decreased to zero. Thread’s context will be saved, then processor is assigned to another thread 4 Timer interrupt preempt thread B 5 Thread A finish critical section. Thread A Thread B
  • 4. What is context • What does “context” means? – A set of dedicated hardware resource that program will use to meet the need of successful execution. • Such as : – general purpose register for computing. – stack memory for support of procedure call. – But from kernel’s point of view, “dedicated context of process” actually is simulated, in fact resources are limited. • kernel slices time and do context saving & restoring in purpose of emulating a multi-processor environment. • Program (process) will think just like that it have a dedicated context. 3
  • 5. What is context • What is user context and interrupt context – user context: provided by kernel context-switch facility which is triggered by timer interrupt, owner is call a user process, runs in user space code with user mode or in kernel space code with svc mode. – Interrupt context: part of registers (context?) save and restore by interrupt handler by itself. • Actually part of interrupt context(reg) will be the some context(reg) of some user process. 4 Processor time axis Save every register which will be used later into stack. … … Restore those register which have been used. And jump to return address (r14 register) Pci bus interrupt Timer interrupt Timer interrupt Thread A Thread A Thread B Thread B A’s subroutine Int_handler()
  • 6. What is context • Compare Interrupt handler & procedure call. – Interrupt handler run as a procedure call. – The difference is that • int_handler don’t receive any parameter and don’t return any value. • Program is even unaware of execution of int_handler. 5 Processor time axis Pci bus interrupt Timer interrupt Timer interrupt Thread A Thread A Thread B Thread B subroutine Save every register which will be used later into stack. … … Restore those register which have been used, and jump to return address(r14). Save every register which will be used later into stack. Read parameter in param register … Put return value in param register Restore those register which have been used, and jump to return address(r14). Void Foo(void) : user space Int_handler(): kernel space
  • 7. double-acquire deadlock(1/2) • Spin_lock convention – Unlike spin lock implementation in other operating system, linux kernel’s spin lock is not recursive. – Double-acquire deadlock example as followed: 6 Spin_lock(&mutex_lock); fooB(); Spin_unlock(&mutex_lock); Thread A Save every register which will be used later into stack. Read parameter in param register … Spin_lock(&mutex_lock); … Put return value in param register Restore those register which have been used, and jump to return address(r14). Void fooB(void)
  • 8. double-acquire deadlock(2/2)• Spin_lock synchronization between user context and interrupt context – Double-acquire deadlock example(2) as followed: – Example that won’t have Double-acquire deadlock as followed: 7 Spin_lock(&mutex_lock); Spin_unlock(&mutex_lock); Thread A Save every register which will be used later into stack. … Spin_lock(&mutex_lock); … Restore those register which have been used, and jump to return address(r14). Sdio_int_handler() Interrupt happens just after thread A get spin lock Sdio_int handler will be busy- waiting mutex_lock Spin_lock(&mutex_lock); Spin_unlock(&mutex_lock); Thread A Save every register which will be used later into stack. … Spin_lock(&mutex_lock); … Restore those register which have been used, and jump to return address(r14). Sdio_int_handler() Timer Interrupt happens just after thread A get spin lock Kernel code : Thread ‘s time slice is decreased to zero. Thread’s context will be saved, then processor is assigned to another thread Thread B’s user code execution Sdio Interrupt happens just after thread A get spin lock Sdio_int handler and thread B will be busy-waiting mutex_lock
  • 9. Log Analysis(1) • In our case, CheckCallbackTimeout() might just interrupt WiMAXQueryImformation() in user context(CM_Query thread) 8 Spin_lock(&mutex_lock); Spin_unlock(&mutex_lock); Thread A Timer Interrupt happens just after thread A get spin lock Kernel code : … If (timer has to be exucuted){ CheckCallbackTimeout(); } … … Return; CheckCallbackTimeout { LDDB_spin_lock(); … }
  • 10. Log Analysis(2) • Timer callback function is called in __irq_svc. • __irq_svc is a subroutine which is only called by irq handler. 9
  • 11. Conclusion – Immediate Solution • Use spin_lock_irqsave and spin_lock_irqrestore. – Turn off interrupt before acquire spin lock. 10
  • 12. Conclusion – what action we have to take right now • What should we do before implementation - Identify those context which open the same lock to do synchronization. – Prevent double-acquire deadlock scenario with interrupt disable API, when lock is shared in interrupt and user context. – Prevent using semaphore in interrupt context. – Leave interrupt as soon as possible, and postpone task into other user context, such as work queue. • Turn on CONFIG_PROVE_LOCKING, CONFIG_DEBUG_LOCK_ALLOC, CONFIG_DEBUG_SPINLOCK – That will help debugging. 11
  • 13. Reference • Linux.Kernel.Development.3rd.Edition, Robert Love. • Linux device driver programming 驅動程式設計, 平田 豐. 12
  • 14. Appendix-context switch• Context-switch code – Restore and jump should be combined to a atomic operation. Copyright 2009 FUJITSU LIMITED 13 Timer interrupt code : … If thread ‘s time slice is decreased to zero. { save r0~r15 into current ’s TCB; restore B’s r0~r14 registers; jump r15 <- B’s TCB[15] + 3 } return from interrupt; Spin_lock(&mutex_lock); … … Spin_unlock(&mutex_lock); … … Sleep(2000ms); … … Sema_get(&mutex_lock) Sleep function (kernel code ): … … save r0~r1 into current’s TCB; restore A’s r0~r14 registers; jump r15 <- A’s TCB[15] + 3 return ; semaphore function (kernel code ): …. if lsemaphore is zero { save r0~r14 into current’s TCB; restore A’s r0~r14 registers; jump r15 <- B’s TCB[15] + 3 } return ; Thread A Thread B 1 2 3 4 5