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

The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Intelisync
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
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
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
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.
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 

Recently uploaded (20)

The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
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
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
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 ...
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 

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