SlideShare a Scribd company logo
1 of 24
Download to read offline
printk() considered harmful
KS/LPC tech topic
November 2016
Sergey Senozhatsky
Samsung Electronics
Table of contents 1. Deadlocks and recursion in printk()
2. Async printk()
3. pr_cont() *
4. Console semaphore *
* bonus topics
1. Deadlocks in printk()
● There are several scenarios that can cause deadlocks in printk()
● For instance:
○ Re-entrant printk() [resolved]
■ Per-cpu buffers for safe printk() from NMI context
○ Recursions in printk() [unresolved]
■ A proposal [RFC]
1. Deadlocks in printk()
● An extension of NMI-printk() idea [1]
○ Use additional per-cpu buffers to handle printk() recursion
○ Mark unsafe regions with printk_safe_enter()/exit() calls, that
set special per-cpu flags
○ printk()->vprintk_funct() then checks those per-cpu flags
○ And “redirects” unsafe printk() call to one of the safe
callbacks:
■ vprintk_safe_nmi()
■ vprintk_safe()
1. Deadlocks in printk()
● This handles printk() recursion only:
○ printk() -> /* anything that calls printk() again */ -> printk()
● But what about printk() deadlocks...
1. Deadlocks in printk()
SyS_ioctl
tty_ioctl
tty_mode_ioctl
uart_set_termios
uart_change_speed
FOO_serial_set_termios
spin_lock_irqsave(&port->lock) // lock the output port
/* WARN_ON() / BUG_ON() / printk() */
vprintk_emit()
console_unlock()
call_console_drivers()
FOO_write()
spin_lock_irqsave(&port->lock) // deadlock
1. Deadlocks in printk()
● The fundamental issue is that printk() depends
on two different lock groups - internal and external locks
● We can handle internal locks with printk_safe
● External locks are out of control: scheduler locks, clock locks, etc.
● printk_deferred(), basically, tells printk() to acquire ONLY
internal locks (logbuf spin lock), avoiding any external locks
1. Deadlocks in printk()
● A proposal:
○ Make printk() behave like printk_deferred()
○ IOW, avoid any external locks in printk() if we are in atomic,
queueing irq_work for printing
○ So we can eventually remove printk_deferred(), which is
always soooo hard
1. Deadlocks in printk()
● Thoughts/questions?
2. Async printk()
● Printing CPU does an “endless” loop in console_unlock()
● console_unlock() is not always preemptible
● Things get worse once you have a slow serial console
attached
2. Async printk()
● You never know how long will it take to printk() a message
● printk() can spin lockup, soft/hard lockup the system, etc.
● It’s not always safe to call printk() under spin_lock, RCU lock, etc.
○ It, thus, can provoke memory pressure, etc.
● It’s not always safe to call printk() from IRQ
● It’s not always safe to call printk_deferred()
2. Async printk()
● A proposal (started by Jan Kara) [2]:
○ Offload printing to a special printk kthread
○ printk() stores the message and queue irq_work
/* or wake_up() the printk kthread if the proposal from #1 won’t fly */
○ wake_up_klogd_work_func() wakes up the printk
kthread instead of doing console_unlock() from IRQ
2. Async printk()
● A proposal (continue):
○ So there is no time-unbound console_unlock() possibly
standing behind printk() and deferred_printk()
○ The printk kthread is always preemptible
○ We switch back to sync printk mode when in panic.
● printk() already depends on scheduler (wake_up() in semaphore)
● With printk_safe we also minimize the impact of wake_up()
○ Recursive printk() from wake_up()
2. Async printk()
● Thoughts/questions?
3. pr_cont()
● pr_cont() is NOT SMP safe
● “That said, we should strive to avoid the need for KERN_CONT. It does result in
real problems for logging, and should generally not be seen as a good feature.
If we someday can get rid of the feature entirely, because nobody does any
fragmented printk calls, that would be lovely.”
Linus Torvalds
3. pr_cont()
● pr_cont() is not going to die
version git grep “pr_cont(“ git grep “KERN_CONT”
tags/v3.8 761 547
tags/v4.5 1123 515
tags/v4.9-rc2 1194 501
3. pr_cont()
● Proposals:
○ Make pr_cont buffer per-thread [hardly possible]
○ Use fake pr_cont buffers [Petr Mladek posted a PoC patch set [3]]
○ Add a new cont API that will use a temp kmalloc()-d or per-CPU
LOG_LINE_MAX buffer [an extended version of [4]]
■ kmalloc() might not always work. OOM pr_cont(), so
per-CPU buffers look a bit better
■ Stop caring about pr_cont() in the meantime
■ Probably add pr_cont() usage WARN to checkpatch.pl
3. pr_cont()
● Thoughts/questions?
4. Console semaphore
● Console driver list is protected by a single console_sem semaphore
● Not every console_lock() caller:
○ Modifies the consoles list (read-only access)
○ Is a kernel thread (user-space can access that list in syscalls)
○ Want to print the messages from console_unlock()
○ Want to wait in TASK_UNINTERRUPTIBLE for current console_sem owner
■ Which possibly can be in console_unlock() printing loop
● Example:
○ cat /proc/consoles does console_lock()/console_unlock()
4. Console semaphore
● Do we want to replace console sem with a READ/WRITE lock?
○ So processes that don’t modify the list can acquire console_sem
in read mode
● Direct console_unlock() callers will still do that “endless”
printing loop
● [flashback]
○ In async printk() we wake_up() printk kthread from wake_up_klogd_work_func()
4. Console semaphore
● Do we want to split console_unlock() function into
○ async_flush_console_unlock() function
■ Basically, just unlock console_sem and wake_up()
printk kthread
○ sync_flush_console_unlock() function
■ Flush the logbuf and then unlock console_sem
■ There are paths that probably want to flush logbuf
from console_unlock()
4. Console semaphore
● Thoughts/questions?
Thank you.
Links
1. https://marc.info/?l=linux-kernel&m=147758348104960
2. https://marc.info/?l=linux-kernel&m=146314209118602
3. https://marc.info/?l=linux-kernel&m=146860197621876
4. https://marc.info/?l=linux-kernel&m=147188048515796

More Related Content

What's hot

grsecurity and PaX
grsecurity and PaXgrsecurity and PaX
grsecurity and PaXKernel TLV
 
Semtex.c [CVE-2013-2094] - A Linux Privelege Escalation
Semtex.c [CVE-2013-2094] - A Linux Privelege EscalationSemtex.c [CVE-2013-2094] - A Linux Privelege Escalation
Semtex.c [CVE-2013-2094] - A Linux Privelege EscalationKernel TLV
 
App secforum2014 andrivet-cplusplus11-metaprogramming_applied_to_software_obf...
App secforum2014 andrivet-cplusplus11-metaprogramming_applied_to_software_obf...App secforum2014 andrivet-cplusplus11-metaprogramming_applied_to_software_obf...
App secforum2014 andrivet-cplusplus11-metaprogramming_applied_to_software_obf...Cyber Security Alliance
 
Kernel Recipes 2016 - Speeding up development by setting up a kernel build farm
Kernel Recipes 2016 - Speeding up development by setting up a kernel build farmKernel Recipes 2016 - Speeding up development by setting up a kernel build farm
Kernel Recipes 2016 - Speeding up development by setting up a kernel build farmAnne Nicolas
 
Kernel Recipes 2016 - entry_*.S: A carefree stroll through kernel entry code
Kernel Recipes 2016 - entry_*.S: A carefree stroll through kernel entry codeKernel Recipes 2016 - entry_*.S: A carefree stroll through kernel entry code
Kernel Recipes 2016 - entry_*.S: A carefree stroll through kernel entry codeAnne Nicolas
 
QEMU - Binary Translation
QEMU - Binary Translation QEMU - Binary Translation
QEMU - Binary Translation Jiann-Fuh Liaw
 
Specializing the Data Path - Hooking into the Linux Network Stack
Specializing the Data Path - Hooking into the Linux Network StackSpecializing the Data Path - Hooking into the Linux Network Stack
Specializing the Data Path - Hooking into the Linux Network StackKernel TLV
 
Zn task - defcon russia 20
Zn task  - defcon russia 20Zn task  - defcon russia 20
Zn task - defcon russia 20DefconRussia
 
Transactional Memory
Transactional MemoryTransactional Memory
Transactional MemoryYuuki Takano
 
Killing any security product … using a Mimikatz undocumented feature
Killing any security product … using a Mimikatz undocumented featureKilling any security product … using a Mimikatz undocumented feature
Killing any security product … using a Mimikatz undocumented featureCyber Security Alliance
 
Kernel Recipes 2019 - RCU in 2019 - Joel Fernandes
Kernel Recipes 2019 - RCU in 2019 - Joel FernandesKernel Recipes 2019 - RCU in 2019 - Joel Fernandes
Kernel Recipes 2019 - RCU in 2019 - Joel FernandesAnne Nicolas
 
Translation Cache Policies for Dynamic Binary Translation
Translation Cache Policies for Dynamic Binary TranslationTranslation Cache Policies for Dynamic Binary Translation
Translation Cache Policies for Dynamic Binary TranslationSaber Ferjani
 
protothread and its usage in contiki OS
protothread and its usage in contiki OSprotothread and its usage in contiki OS
protothread and its usage in contiki OSSalah Amean
 
Linux kernel debugging
Linux kernel debuggingLinux kernel debugging
Linux kernel debugginglibfetion
 
XPDDS17: The dm_op hypercall and libxendevicemodel - Paul Durrant, Citrix
XPDDS17: The dm_op hypercall and libxendevicemodel - Paul Durrant, CitrixXPDDS17: The dm_op hypercall and libxendevicemodel - Paul Durrant, Citrix
XPDDS17: The dm_op hypercall and libxendevicemodel - Paul Durrant, CitrixThe Linux Foundation
 
Migrating KSM page causes the VM lock up as the KSM page merging list is too ...
Migrating KSM page causes the VM lock up as the KSM page merging list is too ...Migrating KSM page causes the VM lock up as the KSM page merging list is too ...
Migrating KSM page causes the VM lock up as the KSM page merging list is too ...Gavin Guo
 

What's hot (20)

grsecurity and PaX
grsecurity and PaXgrsecurity and PaX
grsecurity and PaX
 
Semtex.c [CVE-2013-2094] - A Linux Privelege Escalation
Semtex.c [CVE-2013-2094] - A Linux Privelege EscalationSemtex.c [CVE-2013-2094] - A Linux Privelege Escalation
Semtex.c [CVE-2013-2094] - A Linux Privelege Escalation
 
App secforum2014 andrivet-cplusplus11-metaprogramming_applied_to_software_obf...
App secforum2014 andrivet-cplusplus11-metaprogramming_applied_to_software_obf...App secforum2014 andrivet-cplusplus11-metaprogramming_applied_to_software_obf...
App secforum2014 andrivet-cplusplus11-metaprogramming_applied_to_software_obf...
 
FlexSC
FlexSCFlexSC
FlexSC
 
Kernel Recipes 2016 - Speeding up development by setting up a kernel build farm
Kernel Recipes 2016 - Speeding up development by setting up a kernel build farmKernel Recipes 2016 - Speeding up development by setting up a kernel build farm
Kernel Recipes 2016 - Speeding up development by setting up a kernel build farm
 
Kernel Recipes 2016 - entry_*.S: A carefree stroll through kernel entry code
Kernel Recipes 2016 - entry_*.S: A carefree stroll through kernel entry codeKernel Recipes 2016 - entry_*.S: A carefree stroll through kernel entry code
Kernel Recipes 2016 - entry_*.S: A carefree stroll through kernel entry code
 
QEMU - Binary Translation
QEMU - Binary Translation QEMU - Binary Translation
QEMU - Binary Translation
 
FIFODC
FIFODCFIFODC
FIFODC
 
Specializing the Data Path - Hooking into the Linux Network Stack
Specializing the Data Path - Hooking into the Linux Network StackSpecializing the Data Path - Hooking into the Linux Network Stack
Specializing the Data Path - Hooking into the Linux Network Stack
 
Zn task - defcon russia 20
Zn task  - defcon russia 20Zn task  - defcon russia 20
Zn task - defcon russia 20
 
Transactional Memory
Transactional MemoryTransactional Memory
Transactional Memory
 
Killing any security product … using a Mimikatz undocumented feature
Killing any security product … using a Mimikatz undocumented featureKilling any security product … using a Mimikatz undocumented feature
Killing any security product … using a Mimikatz undocumented feature
 
Timers
TimersTimers
Timers
 
Kernel Recipes 2019 - RCU in 2019 - Joel Fernandes
Kernel Recipes 2019 - RCU in 2019 - Joel FernandesKernel Recipes 2019 - RCU in 2019 - Joel Fernandes
Kernel Recipes 2019 - RCU in 2019 - Joel Fernandes
 
Translation Cache Policies for Dynamic Binary Translation
Translation Cache Policies for Dynamic Binary TranslationTranslation Cache Policies for Dynamic Binary Translation
Translation Cache Policies for Dynamic Binary Translation
 
protothread and its usage in contiki OS
protothread and its usage in contiki OSprotothread and its usage in contiki OS
protothread and its usage in contiki OS
 
Barcamp presentation
Barcamp presentationBarcamp presentation
Barcamp presentation
 
Linux kernel debugging
Linux kernel debuggingLinux kernel debugging
Linux kernel debugging
 
XPDDS17: The dm_op hypercall and libxendevicemodel - Paul Durrant, Citrix
XPDDS17: The dm_op hypercall and libxendevicemodel - Paul Durrant, CitrixXPDDS17: The dm_op hypercall and libxendevicemodel - Paul Durrant, Citrix
XPDDS17: The dm_op hypercall and libxendevicemodel - Paul Durrant, Citrix
 
Migrating KSM page causes the VM lock up as the KSM page merging list is too ...
Migrating KSM page causes the VM lock up as the KSM page merging list is too ...Migrating KSM page causes the VM lock up as the KSM page merging list is too ...
Migrating KSM page causes the VM lock up as the KSM page merging list is too ...
 

Viewers also liked

151209 SMU Outback Quad Redev Pkg-EmailSpreads (1)
151209 SMU Outback Quad Redev Pkg-EmailSpreads (1)151209 SMU Outback Quad Redev Pkg-EmailSpreads (1)
151209 SMU Outback Quad Redev Pkg-EmailSpreads (1)Ann Abel
 
Whats hot,not 2011 webcast
Whats hot,not 2011 webcastWhats hot,not 2011 webcast
Whats hot,not 2011 webcastJan K McCorkle
 
Generator Presentation to Dealer.com
Generator Presentation to Dealer.comGenerator Presentation to Dealer.com
Generator Presentation to Dealer.comL Torres
 
NCBR - lista projektów rekomendowanych do dofinansowania
NCBR - lista projektów rekomendowanych do dofinansowaniaNCBR - lista projektów rekomendowanych do dofinansowania
NCBR - lista projektów rekomendowanych do dofinansowaniaBusiness Insider Polska
 
The Secret Sauce of Startup Marketing - PayPal
The Secret Sauce of Startup Marketing - PayPalThe Secret Sauce of Startup Marketing - PayPal
The Secret Sauce of Startup Marketing - PayPalBC Tech Association
 
Our Favourite Teacher
Our Favourite TeacherOur Favourite Teacher
Our Favourite Teacherprosvsports
 
Informatica y-convergencia-diseno-grafico
Informatica y-convergencia-diseno-graficoInformatica y-convergencia-diseno-grafico
Informatica y-convergencia-diseno-graficoKevin Rincon Murillo
 
Tqm training
Tqm trainingTqm training
Tqm trainingsrvichare
 
What You Need to Know about Google My Business & Possum in 2016
What You Need to Know about Google My Business & Possum in 2016What You Need to Know about Google My Business & Possum in 2016
What You Need to Know about Google My Business & Possum in 2016Joy Hawkins
 
Design Patterns for working with Fast Data in Kafka
Design Patterns for working with Fast Data in KafkaDesign Patterns for working with Fast Data in Kafka
Design Patterns for working with Fast Data in KafkaIan Downard
 
La fruta: Comunica con los 5 sentidos
La fruta: Comunica con los 5 sentidosLa fruta: Comunica con los 5 sentidos
La fruta: Comunica con los 5 sentidosFashion Fruit
 

Viewers also liked (16)

Nbvtalkstaffmotivationataitam
NbvtalkstaffmotivationataitamNbvtalkstaffmotivationataitam
Nbvtalkstaffmotivationataitam
 
151209 SMU Outback Quad Redev Pkg-EmailSpreads (1)
151209 SMU Outback Quad Redev Pkg-EmailSpreads (1)151209 SMU Outback Quad Redev Pkg-EmailSpreads (1)
151209 SMU Outback Quad Redev Pkg-EmailSpreads (1)
 
Whats hot,not 2011 webcast
Whats hot,not 2011 webcastWhats hot,not 2011 webcast
Whats hot,not 2011 webcast
 
Estudio apetitoso 2 continuación
Estudio apetitoso 2 continuaciónEstudio apetitoso 2 continuación
Estudio apetitoso 2 continuación
 
Amitha_resume_2015
Amitha_resume_2015Amitha_resume_2015
Amitha_resume_2015
 
Generator Presentation to Dealer.com
Generator Presentation to Dealer.comGenerator Presentation to Dealer.com
Generator Presentation to Dealer.com
 
Ly thuyet kano
Ly thuyet kanoLy thuyet kano
Ly thuyet kano
 
NCBR - lista projektów rekomendowanych do dofinansowania
NCBR - lista projektów rekomendowanych do dofinansowaniaNCBR - lista projektów rekomendowanych do dofinansowania
NCBR - lista projektów rekomendowanych do dofinansowania
 
The Secret Sauce of Startup Marketing - PayPal
The Secret Sauce of Startup Marketing - PayPalThe Secret Sauce of Startup Marketing - PayPal
The Secret Sauce of Startup Marketing - PayPal
 
Our Favourite Teacher
Our Favourite TeacherOur Favourite Teacher
Our Favourite Teacher
 
Informatica y-convergencia-diseno-grafico
Informatica y-convergencia-diseno-graficoInformatica y-convergencia-diseno-grafico
Informatica y-convergencia-diseno-grafico
 
Tqm training
Tqm trainingTqm training
Tqm training
 
What You Need to Know about Google My Business & Possum in 2016
What You Need to Know about Google My Business & Possum in 2016What You Need to Know about Google My Business & Possum in 2016
What You Need to Know about Google My Business & Possum in 2016
 
Nanomedicina final
Nanomedicina finalNanomedicina final
Nanomedicina final
 
Design Patterns for working with Fast Data in Kafka
Design Patterns for working with Fast Data in KafkaDesign Patterns for working with Fast Data in Kafka
Design Patterns for working with Fast Data in Kafka
 
La fruta: Comunica con los 5 sentidos
La fruta: Comunica con los 5 sentidosLa fruta: Comunica con los 5 sentidos
La fruta: Comunica con los 5 sentidos
 

Similar to printk() considered harmful

HKG15-409: ARM Hibernation enablement on SoCs - a case study
HKG15-409: ARM Hibernation enablement on SoCs - a case studyHKG15-409: ARM Hibernation enablement on SoCs - a case study
HKG15-409: ARM Hibernation enablement on SoCs - a case studyLinaro
 
Designing Tracing Tools
Designing Tracing ToolsDesigning Tracing Tools
Designing Tracing ToolsSysdig
 
Linux Capabilities - eng - v2.1.5, compact
Linux Capabilities - eng - v2.1.5, compactLinux Capabilities - eng - v2.1.5, compact
Linux Capabilities - eng - v2.1.5, compactAlessandro Selli
 
Linux kernel debugging
Linux kernel debuggingLinux kernel debugging
Linux kernel debuggingHao-Ran Liu
 
bcc/BPF tools - Strategy, current tools, future challenges
bcc/BPF tools - Strategy, current tools, future challengesbcc/BPF tools - Strategy, current tools, future challenges
bcc/BPF tools - Strategy, current tools, future challengesIO Visor Project
 
Designing Tracing Tools
Designing Tracing ToolsDesigning Tracing Tools
Designing Tracing ToolsBrendan Gregg
 
Let's trace Linux Lernel with KGDB @ COSCUP 2021
Let's trace Linux Lernel with KGDB @ COSCUP 2021Let's trace Linux Lernel with KGDB @ COSCUP 2021
Let's trace Linux Lernel with KGDB @ COSCUP 2021Jian-Hong Pan
 
Linux: the first second
Linux: the first secondLinux: the first second
Linux: the first secondAlison Chaiken
 
Linux Kernel Platform Development: Challenges and Insights
 Linux Kernel Platform Development: Challenges and Insights Linux Kernel Platform Development: Challenges and Insights
Linux Kernel Platform Development: Challenges and InsightsGlobalLogic Ukraine
 
When kdump is way too much
When kdump is way too muchWhen kdump is way too much
When kdump is way too muchIgalia
 
Kernel debug log and console on openSUSE
Kernel debug log and console on openSUSEKernel debug log and console on openSUSE
Kernel debug log and console on openSUSESUSE Labs Taipei
 
Challenges in GPU compilers
Challenges in GPU compilersChallenges in GPU compilers
Challenges in GPU compilersAnastasiaStulova
 
Basics_of_Kernel_Panic_Hang_and_ Kdump.pdf
Basics_of_Kernel_Panic_Hang_and_ Kdump.pdfBasics_of_Kernel_Panic_Hang_and_ Kdump.pdf
Basics_of_Kernel_Panic_Hang_and_ Kdump.pdfstroganovboris
 
NSC #2 - Challenge Solution
NSC #2 - Challenge SolutionNSC #2 - Challenge Solution
NSC #2 - Challenge SolutionNoSuchCon
 
Linux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudLinux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudAndrea Righi
 
Beneath the Linux Interrupt handling
Beneath the Linux Interrupt handlingBeneath the Linux Interrupt handling
Beneath the Linux Interrupt handlingBhoomil Chavda
 

Similar to printk() considered harmful (20)

HKG15-409: ARM Hibernation enablement on SoCs - a case study
HKG15-409: ARM Hibernation enablement on SoCs - a case studyHKG15-409: ARM Hibernation enablement on SoCs - a case study
HKG15-409: ARM Hibernation enablement on SoCs - a case study
 
netLec5.pdf
netLec5.pdfnetLec5.pdf
netLec5.pdf
 
Designing Tracing Tools
Designing Tracing ToolsDesigning Tracing Tools
Designing Tracing Tools
 
Linux Capabilities - eng - v2.1.5, compact
Linux Capabilities - eng - v2.1.5, compactLinux Capabilities - eng - v2.1.5, compact
Linux Capabilities - eng - v2.1.5, compact
 
Linux kernel debugging
Linux kernel debuggingLinux kernel debugging
Linux kernel debugging
 
BPF Tools 2017
BPF Tools 2017BPF Tools 2017
BPF Tools 2017
 
bcc/BPF tools - Strategy, current tools, future challenges
bcc/BPF tools - Strategy, current tools, future challengesbcc/BPF tools - Strategy, current tools, future challenges
bcc/BPF tools - Strategy, current tools, future challenges
 
Designing Tracing Tools
Designing Tracing ToolsDesigning Tracing Tools
Designing Tracing Tools
 
Let's trace Linux Lernel with KGDB @ COSCUP 2021
Let's trace Linux Lernel with KGDB @ COSCUP 2021Let's trace Linux Lernel with KGDB @ COSCUP 2021
Let's trace Linux Lernel with KGDB @ COSCUP 2021
 
Linux: the first second
Linux: the first secondLinux: the first second
Linux: the first second
 
Linux Kernel Platform Development: Challenges and Insights
 Linux Kernel Platform Development: Challenges and Insights Linux Kernel Platform Development: Challenges and Insights
Linux Kernel Platform Development: Challenges and Insights
 
When kdump is way too much
When kdump is way too muchWhen kdump is way too much
When kdump is way too much
 
Kernel debug log and console on openSUSE
Kernel debug log and console on openSUSEKernel debug log and console on openSUSE
Kernel debug log and console on openSUSE
 
Challenges in GPU compilers
Challenges in GPU compilersChallenges in GPU compilers
Challenges in GPU compilers
 
FIFOPt
FIFOPtFIFOPt
FIFOPt
 
Driver_linux
Driver_linuxDriver_linux
Driver_linux
 
Basics_of_Kernel_Panic_Hang_and_ Kdump.pdf
Basics_of_Kernel_Panic_Hang_and_ Kdump.pdfBasics_of_Kernel_Panic_Hang_and_ Kdump.pdf
Basics_of_Kernel_Panic_Hang_and_ Kdump.pdf
 
NSC #2 - Challenge Solution
NSC #2 - Challenge SolutionNSC #2 - Challenge Solution
NSC #2 - Challenge Solution
 
Linux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudLinux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloud
 
Beneath the Linux Interrupt handling
Beneath the Linux Interrupt handlingBeneath the Linux Interrupt handling
Beneath the Linux Interrupt handling
 

Recently uploaded

How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceanilsa9823
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 

Recently uploaded (20)

How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 

printk() considered harmful

  • 1. printk() considered harmful KS/LPC tech topic November 2016 Sergey Senozhatsky Samsung Electronics
  • 2. Table of contents 1. Deadlocks and recursion in printk() 2. Async printk() 3. pr_cont() * 4. Console semaphore * * bonus topics
  • 3. 1. Deadlocks in printk() ● There are several scenarios that can cause deadlocks in printk() ● For instance: ○ Re-entrant printk() [resolved] ■ Per-cpu buffers for safe printk() from NMI context ○ Recursions in printk() [unresolved] ■ A proposal [RFC]
  • 4. 1. Deadlocks in printk() ● An extension of NMI-printk() idea [1] ○ Use additional per-cpu buffers to handle printk() recursion ○ Mark unsafe regions with printk_safe_enter()/exit() calls, that set special per-cpu flags ○ printk()->vprintk_funct() then checks those per-cpu flags ○ And “redirects” unsafe printk() call to one of the safe callbacks: ■ vprintk_safe_nmi() ■ vprintk_safe()
  • 5. 1. Deadlocks in printk() ● This handles printk() recursion only: ○ printk() -> /* anything that calls printk() again */ -> printk() ● But what about printk() deadlocks...
  • 6. 1. Deadlocks in printk() SyS_ioctl tty_ioctl tty_mode_ioctl uart_set_termios uart_change_speed FOO_serial_set_termios spin_lock_irqsave(&port->lock) // lock the output port /* WARN_ON() / BUG_ON() / printk() */ vprintk_emit() console_unlock() call_console_drivers() FOO_write() spin_lock_irqsave(&port->lock) // deadlock
  • 7. 1. Deadlocks in printk() ● The fundamental issue is that printk() depends on two different lock groups - internal and external locks ● We can handle internal locks with printk_safe ● External locks are out of control: scheduler locks, clock locks, etc. ● printk_deferred(), basically, tells printk() to acquire ONLY internal locks (logbuf spin lock), avoiding any external locks
  • 8. 1. Deadlocks in printk() ● A proposal: ○ Make printk() behave like printk_deferred() ○ IOW, avoid any external locks in printk() if we are in atomic, queueing irq_work for printing ○ So we can eventually remove printk_deferred(), which is always soooo hard
  • 9. 1. Deadlocks in printk() ● Thoughts/questions?
  • 10. 2. Async printk() ● Printing CPU does an “endless” loop in console_unlock() ● console_unlock() is not always preemptible ● Things get worse once you have a slow serial console attached
  • 11. 2. Async printk() ● You never know how long will it take to printk() a message ● printk() can spin lockup, soft/hard lockup the system, etc. ● It’s not always safe to call printk() under spin_lock, RCU lock, etc. ○ It, thus, can provoke memory pressure, etc. ● It’s not always safe to call printk() from IRQ ● It’s not always safe to call printk_deferred()
  • 12. 2. Async printk() ● A proposal (started by Jan Kara) [2]: ○ Offload printing to a special printk kthread ○ printk() stores the message and queue irq_work /* or wake_up() the printk kthread if the proposal from #1 won’t fly */ ○ wake_up_klogd_work_func() wakes up the printk kthread instead of doing console_unlock() from IRQ
  • 13. 2. Async printk() ● A proposal (continue): ○ So there is no time-unbound console_unlock() possibly standing behind printk() and deferred_printk() ○ The printk kthread is always preemptible ○ We switch back to sync printk mode when in panic. ● printk() already depends on scheduler (wake_up() in semaphore) ● With printk_safe we also minimize the impact of wake_up() ○ Recursive printk() from wake_up()
  • 14. 2. Async printk() ● Thoughts/questions?
  • 15. 3. pr_cont() ● pr_cont() is NOT SMP safe ● “That said, we should strive to avoid the need for KERN_CONT. It does result in real problems for logging, and should generally not be seen as a good feature. If we someday can get rid of the feature entirely, because nobody does any fragmented printk calls, that would be lovely.” Linus Torvalds
  • 16. 3. pr_cont() ● pr_cont() is not going to die version git grep “pr_cont(“ git grep “KERN_CONT” tags/v3.8 761 547 tags/v4.5 1123 515 tags/v4.9-rc2 1194 501
  • 17. 3. pr_cont() ● Proposals: ○ Make pr_cont buffer per-thread [hardly possible] ○ Use fake pr_cont buffers [Petr Mladek posted a PoC patch set [3]] ○ Add a new cont API that will use a temp kmalloc()-d or per-CPU LOG_LINE_MAX buffer [an extended version of [4]] ■ kmalloc() might not always work. OOM pr_cont(), so per-CPU buffers look a bit better ■ Stop caring about pr_cont() in the meantime ■ Probably add pr_cont() usage WARN to checkpatch.pl
  • 19. 4. Console semaphore ● Console driver list is protected by a single console_sem semaphore ● Not every console_lock() caller: ○ Modifies the consoles list (read-only access) ○ Is a kernel thread (user-space can access that list in syscalls) ○ Want to print the messages from console_unlock() ○ Want to wait in TASK_UNINTERRUPTIBLE for current console_sem owner ■ Which possibly can be in console_unlock() printing loop ● Example: ○ cat /proc/consoles does console_lock()/console_unlock()
  • 20. 4. Console semaphore ● Do we want to replace console sem with a READ/WRITE lock? ○ So processes that don’t modify the list can acquire console_sem in read mode ● Direct console_unlock() callers will still do that “endless” printing loop ● [flashback] ○ In async printk() we wake_up() printk kthread from wake_up_klogd_work_func()
  • 21. 4. Console semaphore ● Do we want to split console_unlock() function into ○ async_flush_console_unlock() function ■ Basically, just unlock console_sem and wake_up() printk kthread ○ sync_flush_console_unlock() function ■ Flush the logbuf and then unlock console_sem ■ There are paths that probably want to flush logbuf from console_unlock()
  • 22. 4. Console semaphore ● Thoughts/questions?
  • 24. Links 1. https://marc.info/?l=linux-kernel&m=147758348104960 2. https://marc.info/?l=linux-kernel&m=146314209118602 3. https://marc.info/?l=linux-kernel&m=146860197621876 4. https://marc.info/?l=linux-kernel&m=147188048515796