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

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
Cyber Security Alliance
 

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
 
Our Favourite Teacher
Our Favourite TeacherOur Favourite Teacher
Our Favourite Teacher
prosvsports
 
Tqm training
Tqm trainingTqm training
Tqm training
srvichare
 

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

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

%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 

Recently uploaded (20)

%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 

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