SlideShare a Scribd company logo
1 of 14
Download to read offline
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

0章 Linuxカーネルを読む前に最低限知っておくべきこと
0章 Linuxカーネルを読む前に最低限知っておくべきこと0章 Linuxカーネルを読む前に最低限知っておくべきこと
0章 Linuxカーネルを読む前に最低限知っておくべきことmao999
 
Qt on Real Time Operating Systems
Qt on Real Time Operating SystemsQt on Real Time Operating Systems
Qt on Real Time Operating Systemsaccount inactive
 
Linux packet-forwarding
Linux packet-forwardingLinux packet-forwarding
Linux packet-forwardingMasakazu Asama
 
Ethernetの受信処理
Ethernetの受信処理Ethernetの受信処理
Ethernetの受信処理Takuya ASADA
 
JEP280: Java 9 で文字列結合の処理が変わるぞ!準備はいいか!? #jjug_ccc
JEP280: Java 9 で文字列結合の処理が変わるぞ!準備はいいか!? #jjug_cccJEP280: Java 9 で文字列結合の処理が変わるぞ!準備はいいか!? #jjug_ccc
JEP280: Java 9 で文字列結合の処理が変わるぞ!準備はいいか!? #jjug_cccYujiSoftware
 
BPF - in-kernel virtual machine
BPF - in-kernel virtual machineBPF - in-kernel virtual machine
BPF - in-kernel virtual machineAlexei Starovoitov
 
Practical Windows Kernel Exploitation
Practical Windows Kernel ExploitationPractical Windows Kernel Exploitation
Practical Windows Kernel ExploitationzeroSteiner
 
SpectreとMeltdown:最近のCPUの深い話
SpectreとMeltdown:最近のCPUの深い話SpectreとMeltdown:最近のCPUの深い話
SpectreとMeltdown:最近のCPUの深い話LINE Corporation
 
Reverse eningeering
Reverse eningeeringReverse eningeering
Reverse eningeeringKent Huang
 
Linux Kernel - Virtual File System
Linux Kernel - Virtual File SystemLinux Kernel - Virtual File System
Linux Kernel - Virtual File SystemAdrian Huang
 
Velocity 2015 linux perf tools
Velocity 2015 linux perf toolsVelocity 2015 linux perf tools
Velocity 2015 linux perf toolsBrendan Gregg
 
twlkh-linux-vsyscall-and-vdso
twlkh-linux-vsyscall-and-vdsotwlkh-linux-vsyscall-and-vdso
twlkh-linux-vsyscall-and-vdsoViller Hsiao
 
Interrupt Affinityについて
Interrupt AffinityについてInterrupt Affinityについて
Interrupt AffinityについてTakuya ASADA
 
Linux Interrupts
Linux InterruptsLinux Interrupts
Linux InterruptsKernel TLV
 
Understanding of linux kernel memory model
Understanding of linux kernel memory modelUnderstanding of linux kernel memory model
Understanding of linux kernel memory modelSeongJae Park
 
【学習メモ#1st】12ステップで作る組込みOS自作入門
【学習メモ#1st】12ステップで作る組込みOS自作入門【学習メモ#1st】12ステップで作る組込みOS自作入門
【学習メモ#1st】12ステップで作る組込みOS自作入門sandai
 
30分で分かる!OSの作り方 ver.2
30分で分かる!OSの作り方 ver.230分で分かる!OSの作り方 ver.2
30分で分かる!OSの作り方 ver.2uchan_nos
 

What's hot (20)

0章 Linuxカーネルを読む前に最低限知っておくべきこと
0章 Linuxカーネルを読む前に最低限知っておくべきこと0章 Linuxカーネルを読む前に最低限知っておくべきこと
0章 Linuxカーネルを読む前に最低限知っておくべきこと
 
Qt on Real Time Operating Systems
Qt on Real Time Operating SystemsQt on Real Time Operating Systems
Qt on Real Time Operating Systems
 
Linux packet-forwarding
Linux packet-forwardingLinux packet-forwarding
Linux packet-forwarding
 
Ethernetの受信処理
Ethernetの受信処理Ethernetの受信処理
Ethernetの受信処理
 
JEP280: Java 9 で文字列結合の処理が変わるぞ!準備はいいか!? #jjug_ccc
JEP280: Java 9 で文字列結合の処理が変わるぞ!準備はいいか!? #jjug_cccJEP280: Java 9 で文字列結合の処理が変わるぞ!準備はいいか!? #jjug_ccc
JEP280: Java 9 で文字列結合の処理が変わるぞ!準備はいいか!? #jjug_ccc
 
BPF - in-kernel virtual machine
BPF - in-kernel virtual machineBPF - in-kernel virtual machine
BPF - in-kernel virtual machine
 
Practical Windows Kernel Exploitation
Practical Windows Kernel ExploitationPractical Windows Kernel Exploitation
Practical Windows Kernel Exploitation
 
SpectreとMeltdown:最近のCPUの深い話
SpectreとMeltdown:最近のCPUの深い話SpectreとMeltdown:最近のCPUの深い話
SpectreとMeltdown:最近のCPUの深い話
 
Making Linux do Hard Real-time
Making Linux do Hard Real-timeMaking Linux do Hard Real-time
Making Linux do Hard Real-time
 
Reverse eningeering
Reverse eningeeringReverse eningeering
Reverse eningeering
 
Linux Kernel - Virtual File System
Linux Kernel - Virtual File SystemLinux Kernel - Virtual File System
Linux Kernel - Virtual File System
 
Velocity 2015 linux perf tools
Velocity 2015 linux perf toolsVelocity 2015 linux perf tools
Velocity 2015 linux perf tools
 
twlkh-linux-vsyscall-and-vdso
twlkh-linux-vsyscall-and-vdsotwlkh-linux-vsyscall-and-vdso
twlkh-linux-vsyscall-and-vdso
 
Interrupt Affinityについて
Interrupt AffinityについてInterrupt Affinityについて
Interrupt Affinityについて
 
Linux Interrupts
Linux InterruptsLinux Interrupts
Linux Interrupts
 
Understanding of linux kernel memory model
Understanding of linux kernel memory modelUnderstanding of linux kernel memory model
Understanding of linux kernel memory model
 
【学習メモ#1st】12ステップで作る組込みOS自作入門
【学習メモ#1st】12ステップで作る組込みOS自作入門【学習メモ#1st】12ステップで作る組込みOS自作入門
【学習メモ#1st】12ステップで作る組込みOS自作入門
 
入門 シェル実装
入門 シェル実装入門 シェル実装
入門 シェル実装
 
perfを使ったPostgreSQLの解析(前編)
perfを使ったPostgreSQLの解析(前編)perfを使ったPostgreSQLの解析(前編)
perfを使ったPostgreSQLの解析(前編)
 
30分で分かる!OSの作り方 ver.2
30分で分かる!OSの作り方 ver.230分で分かる!OSの作り方 ver.2
30分で分かる!OSの作り方 ver.2
 

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 GoYu-Shuan Hsieh
 
Multithreaded Programming Part- III.pdf
Multithreaded Programming Part- III.pdfMultithreaded Programming Part- III.pdf
Multithreaded Programming Part- III.pdfHarika Pudugosula
 
Let's Talk Locks!
Let's Talk Locks!Let's Talk Locks!
Let's Talk Locks!C4Media
 
Concurrency 2010
Concurrency 2010Concurrency 2010
Concurrency 2010敬倫 林
 
semaphore & mutex.pdf
semaphore & mutex.pdfsemaphore & mutex.pdf
semaphore & mutex.pdfAdrian Huang
 
Beneath the Linux Interrupt handling
Beneath the Linux Interrupt handlingBeneath the Linux Interrupt handling
Beneath the Linux Interrupt handlingBhoomil Chavda
 
Linux Locking Mechanisms
Linux Locking MechanismsLinux Locking Mechanisms
Linux Locking MechanismsKernel 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 SystemJames 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 2015cruftex
 
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 -- MulticoreHsien-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

Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...OnePlan Solutions
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...OnePlan Solutions
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecturerahul_net
 
Pros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdf
Pros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdfPros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdf
Pros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdfkalichargn70th171
 
Osi security architecture in network.pptx
Osi security architecture in network.pptxOsi security architecture in network.pptx
Osi security architecture in network.pptxVinzoCenzo
 
Advantages of Cargo Cloud Solutions.pptx
Advantages of Cargo Cloud Solutions.pptxAdvantages of Cargo Cloud Solutions.pptx
Advantages of Cargo Cloud Solutions.pptxRTS corp
 
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4jGraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4jNeo4j
 
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdfAndrey Devyatkin
 
Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLionel Briand
 
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...kalichargn70th171
 
Mastering Project Planning with Microsoft Project 2016.pptx
Mastering Project Planning with Microsoft Project 2016.pptxMastering Project Planning with Microsoft Project 2016.pptx
Mastering Project Planning with Microsoft Project 2016.pptxAS Design & AST.
 
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonLeveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonApplitools
 
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxThe Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxRTS corp
 
Zer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfZer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfmaor17
 
Introduction to Firebase Workshop Slides
Introduction to Firebase Workshop SlidesIntroduction to Firebase Workshop Slides
Introduction to Firebase Workshop Slidesvaideheekore1
 
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics
 
Effectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorEffectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorTier1 app
 
2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shards2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shardsChristopher Curtin
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldRoberto Pérez Alcolea
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfRTS corp
 

Recently uploaded (20)

Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecture
 
Pros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdf
Pros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdfPros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdf
Pros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdf
 
Osi security architecture in network.pptx
Osi security architecture in network.pptxOsi security architecture in network.pptx
Osi security architecture in network.pptx
 
Advantages of Cargo Cloud Solutions.pptx
Advantages of Cargo Cloud Solutions.pptxAdvantages of Cargo Cloud Solutions.pptx
Advantages of Cargo Cloud Solutions.pptx
 
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4jGraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
 
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
 
Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and Repair
 
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...
 
Mastering Project Planning with Microsoft Project 2016.pptx
Mastering Project Planning with Microsoft Project 2016.pptxMastering Project Planning with Microsoft Project 2016.pptx
Mastering Project Planning with Microsoft Project 2016.pptx
 
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonLeveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
 
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxThe Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
 
Zer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfZer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdf
 
Introduction to Firebase Workshop Slides
Introduction to Firebase Workshop SlidesIntroduction to Firebase Workshop Slides
Introduction to Firebase Workshop Slides
 
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
 
Effectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorEffectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryError
 
2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shards2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shards
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository world
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
 

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