SlideShare a Scribd company logo
1 of 25
© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Kernel Programming
2© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
What to Expect?
How to do programming in “Kernel C” for
Achieving Concurrency
Keeping Time
Providing Delays
Timer Control
3© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Concurrency
4© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Concurrency with Locking
Mutexes
Header: <linux/mutex.h>
Type: struct mutex
APIs
DEFINE_MUTEX
mutex_is_locked
mutex_lock, mutex_trylock, mutex_unlock
Semaphores
Header: <linux/semaphore.h>
Type: struct semaphore
APIs
sema_init
down, down_trylock, down_interruptible, up
5© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Concurrency w/ Locking (cont.)
Spin Locks
Header <linux/spinlock.h>
Type: spinlock_t
APIs
spin_lock_init
spin_[try]lock, spin_unlock
Reader-Writer Locks
Header: <linux/spinlock.h>
Type: rwlock_t
APIs
read_lock, read_unlock
write_lock, write_unlock
6© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Concurrency without Locking
Atomic Variables
Header: <asm-generic/atomic.h>
Type: atomic_t
Macros
ATOMIC_INIT
atomic_read, atomic_set
atomic_add, atomic_sub, atomic_inc, atomic_dec
atomic_xchg
7© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Concurrency w/o Locking (cont.)
Atomic Bit Operations
Header: <linux/bitops.h>
APIs
rol8, rol16, rol32, ror8, ror16, ror32
find_first_bit, find_first_zero_bit
find_last_bit
find_next_bit, find_next_zero_bit
Header: <asm-generic/bitops.h>
APIs
set_bit, clear_bit, change_bit
test_and_set_bit, test_and_clear_bit, test_and_change_bit
8© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Wait Queues
Wait Queues
Header: <linux/wait.h>
Wait Queue Head APIs
DECLARE_WAIT_QUEUE_HEAD(wq);
wait_event_interruptible(wq, cond);
wait_event_interruptible_timeout(wq, cond, timeout);
wake_up_interruptible(&wq);
... (non-interruptible set)
Wait Queue APIs
DECLARE_WAITQUEUE(w, current);
add_wait_queue(&wq, &w);
remove_wait_queue(&wq, &w);
9© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Time Keeping
10© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Time since Bootup
tick – Kernel's unit of time. Also called jiffy
HZ – ticks per second
Defined in Header: <linux/param.h>
Typically, 1000 for desktops, 100 for embedded systems
1 tick = 1ms (desktop), 10ms (embedded systems)
Variables: jiffies & jiffies_64
Header: <linux/jiffies.h>
APIs
time_after, time_before, time_in_range, ...
get_jiffies_64, ...
msec_to_jiffies, timespec_to_jiffies, timeval_to_jiffies, …
jiffies_to_msec, jiffies_to_timespec, jiffies_to_timeval, ...
11© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Time since Bootup (cont.)
Platform specific “Time Stamp Counter”
On x86
Header: <asm/msr.h>
API: rdtsc(ul low_tsc_ticks, ul high_tsc_ticks);
Getting it generically
Header: <linux/timex.h>
API: read_current_timer(unsigned long *timer_val);
12© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Absolute Time
Header: <linux/time.h>
APIs
mktime(y, m, d, h, m, s) – Seconds since Epoch
void do_gettimeofday(struct timeval *tv);
struct timespec current_kernel_time(void);
13© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Delays
14© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Long Delays
Busy wait: cpu_relax
while (time_before(jiffies, j1))
cpu_relax();
Yielding: schedule/schedule_timeout
while (time_before(jiffies, j1))
schedule();
15© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Short Delays but Busy Waiting
Header: <linux/delay.h>
Arch. specific Header: <asm/delay.h>
APIs
void ndelay(unsigned long ndelays);
void udelay(unsigned long udelays);
void mdelay(unsigned long mdelays);
16© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Long Delays: Back to Yielding
Header: <linux/delay.h>
APIs
void msleep(unsigned int millisecs);
unsigned long msleep_interruptible(unsigned int millisecs);
void ssleep(unsigned int secs);
17© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Timers
18© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Kernel Timers
Back end of the various delays
Header: <linux/timer.h>
Type: struct timer_list
APIs
void init_timer(struct timer_list *); /* Nullifies */
struct timer_list TIMER_INITIALIZER(f, t, p);
void add_timer(struct timer_list *);
void del_timer(struct timer_list *);
int mod_timer(struct timer_list *, unsigned long);
int del_timer_sync(struct timer_list *);
19© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Tasklets
Timers without specific Timing
Header: <linux/interrupt.h>
Type: struct tasklet_struct
APIs
void tasklet_init(struct tasklet_struct *t, void (*func)
(unsigned long), unsigned long data);
void tasklet_kill(struct tasklet_struct *t);
DECLARE_TASKLET(name, func, data);
tasklet_enable(t), tasklet_disable(t)
tasklet_[hi_]schedule(t);
20© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Work Queues
In context of “Special Kernel Thread”
Header: <linux/workqueue.h>
Types: struct workqueue_struct, struct work_struct
Work Queue APIs
q = create_workqueue(name);
q = create_singlethread_workqueue(name);
flush_workqueue(q);
destroy_workqueue(q);
Work APIs
DECLARE_WORK(w, void (*function)(void *), void *data);
INIT_WORK(w, void (*function)(void *), void *data);
Combined APIs
int queue_work(q, &w);
int queue_delayed_work(q, &w, d);
int cancel_delayed_work(&w);
Global Shared Work Queue API
schedule_work(&w);
21© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Helper Interfaces
22© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Other Helper Interfaces in Latest
Kernels
User Mode Helper
Linked Lists
Hash Lists
Notifier Chains
Completion Interface
Kthread Helpers
23© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
24© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
What to Expect?
How to do programming in “Kernel C” for
Achieving Concurrency
With & without Locking
Wait Queues
Keeping Time
Relative & Absolute
Providing Delays
Long and Short
Busy Wait and Yielding
Timer Control
Kernel Timers
Tasklets
Work Queues
25© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Any Queries?

More Related Content

What's hot (20)

Bootloaders
BootloadersBootloaders
Bootloaders
 
SPI Drivers
SPI DriversSPI Drivers
SPI Drivers
 
BeagleBone Black Bootloaders
BeagleBone Black BootloadersBeagleBone Black Bootloaders
BeagleBone Black Bootloaders
 
Processes
ProcessesProcesses
Processes
 
Linux DMA Engine
Linux DMA EngineLinux DMA Engine
Linux DMA Engine
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
Kernel Timing Management
Kernel Timing ManagementKernel Timing Management
Kernel Timing Management
 
Embedded Software Design
Embedded Software DesignEmbedded Software Design
Embedded Software Design
 
Character Drivers
Character DriversCharacter Drivers
Character Drivers
 
Linux Kernel Overview
Linux Kernel OverviewLinux Kernel Overview
Linux Kernel Overview
 
Video Drivers
Video DriversVideo Drivers
Video Drivers
 
Synchronization
SynchronizationSynchronization
Synchronization
 
Linux User Space Debugging & Profiling
Linux User Space Debugging & ProfilingLinux User Space Debugging & Profiling
Linux User Space Debugging & Profiling
 
System Calls
System CallsSystem Calls
System Calls
 
Signals
SignalsSignals
Signals
 
Embedded Storage Management
Embedded Storage ManagementEmbedded Storage Management
Embedded Storage Management
 
BeagleBoard-xM Booting Process
BeagleBoard-xM Booting ProcessBeagleBoard-xM Booting Process
BeagleBoard-xM Booting Process
 
Mobile Hacking using Linux Drivers
Mobile Hacking using Linux DriversMobile Hacking using Linux Drivers
Mobile Hacking using Linux Drivers
 
Processes
ProcessesProcesses
Processes
 
Low-level Accesses
Low-level AccessesLow-level Accesses
Low-level Accesses
 

Viewers also liked (9)

Audio Drivers
Audio DriversAudio Drivers
Audio Drivers
 
USB Drivers
USB DriversUSB Drivers
USB Drivers
 
SPI Drivers
SPI DriversSPI Drivers
SPI Drivers
 
PCI Drivers
PCI DriversPCI Drivers
PCI Drivers
 
Interrupts
InterruptsInterrupts
Interrupts
 
Serial Drivers
Serial DriversSerial Drivers
Serial Drivers
 
Platform Drivers
Platform DriversPlatform Drivers
Platform Drivers
 
Network Drivers
Network DriversNetwork Drivers
Network Drivers
 
I2C Drivers
I2C DriversI2C Drivers
I2C Drivers
 

Similar to Kernel Programming

리눅스 드라이버 #2
리눅스 드라이버 #2리눅스 드라이버 #2
리눅스 드라이버 #2Sangho Park
 
Dan Norris: Exadata security
Dan Norris: Exadata securityDan Norris: Exadata security
Dan Norris: Exadata securityKyle Hailey
 
Node.js primer for ITE students
Node.js primer for ITE studentsNode.js primer for ITE students
Node.js primer for ITE studentsQuhan Arunasalam
 
OUGLS 2016: How profiling works in MySQL
OUGLS 2016: How profiling works in MySQLOUGLS 2016: How profiling works in MySQL
OUGLS 2016: How profiling works in MySQLGeorgi Kodinov
 
Quick and Easy Device Drivers for Embedded Linux Using UIO
Quick and Easy Device Drivers for Embedded Linux Using UIOQuick and Easy Device Drivers for Embedded Linux Using UIO
Quick and Easy Device Drivers for Embedded Linux Using UIOChris Simmonds
 
Kernel Timing Management
Kernel Timing ManagementKernel Timing Management
Kernel Timing Managementpradeep_tewani
 
Mysql performance tuning
Mysql performance tuningMysql performance tuning
Mysql performance tuningPhilip Zhong
 
Oracle Solaris 11.1 New Features
Oracle Solaris 11.1 New FeaturesOracle Solaris 11.1 New Features
Oracle Solaris 11.1 New FeaturesOrgad Kimchi
 
101 1.3 runlevels , shutdown, and reboot
101 1.3 runlevels , shutdown, and reboot101 1.3 runlevels , shutdown, and reboot
101 1.3 runlevels , shutdown, and rebootAcácio Oliveira
 
101 1.3 runlevels, shutdown, and reboot v2
101 1.3 runlevels, shutdown, and reboot v2101 1.3 runlevels, shutdown, and reboot v2
101 1.3 runlevels, shutdown, and reboot v2Acácio Oliveira
 
SHARE.ORG Orlando 2015
SHARE.ORG Orlando 2015SHARE.ORG Orlando 2015
SHARE.ORG Orlando 2015Filipe Miranda
 
Introduction to Embedded Systems
Introduction to Embedded SystemsIntroduction to Embedded Systems
Introduction to Embedded SystemsAnil Kumar Pugalia
 
Hangover - Que pasó ayer? Troubleshooting con vistas ASH & S-ASH
Hangover - Que pasó ayer?   Troubleshooting con vistas ASH & S-ASHHangover - Que pasó ayer?   Troubleshooting con vistas ASH & S-ASH
Hangover - Que pasó ayer? Troubleshooting con vistas ASH & S-ASHRoy Salazar
 

Similar to Kernel Programming (20)

리눅스 드라이버 #2
리눅스 드라이버 #2리눅스 드라이버 #2
리눅스 드라이버 #2
 
Embedded Applications
Embedded ApplicationsEmbedded Applications
Embedded Applications
 
Introduction to Linux
Introduction to LinuxIntroduction to Linux
Introduction to Linux
 
Dan Norris: Exadata security
Dan Norris: Exadata securityDan Norris: Exadata security
Dan Norris: Exadata security
 
Node.js primer for ITE students
Node.js primer for ITE studentsNode.js primer for ITE students
Node.js primer for ITE students
 
OUGLS 2016: How profiling works in MySQL
OUGLS 2016: How profiling works in MySQLOUGLS 2016: How profiling works in MySQL
OUGLS 2016: How profiling works in MySQL
 
Quick and Easy Device Drivers for Embedded Linux Using UIO
Quick and Easy Device Drivers for Embedded Linux Using UIOQuick and Easy Device Drivers for Embedded Linux Using UIO
Quick and Easy Device Drivers for Embedded Linux Using UIO
 
Kernel Timing Management
Kernel Timing ManagementKernel Timing Management
Kernel Timing Management
 
Linux scheduler
Linux schedulerLinux scheduler
Linux scheduler
 
Mysql performance tuning
Mysql performance tuningMysql performance tuning
Mysql performance tuning
 
Oracle Solaris 11.1 New Features
Oracle Solaris 11.1 New FeaturesOracle Solaris 11.1 New Features
Oracle Solaris 11.1 New Features
 
101 1.3 runlevels , shutdown, and reboot
101 1.3 runlevels , shutdown, and reboot101 1.3 runlevels , shutdown, and reboot
101 1.3 runlevels , shutdown, and reboot
 
101 1.3 runlevels, shutdown, and reboot v2
101 1.3 runlevels, shutdown, and reboot v2101 1.3 runlevels, shutdown, and reboot v2
101 1.3 runlevels, shutdown, and reboot v2
 
SHARE.ORG Orlando 2015
SHARE.ORG Orlando 2015SHARE.ORG Orlando 2015
SHARE.ORG Orlando 2015
 
Introduction to Embedded Systems
Introduction to Embedded SystemsIntroduction to Embedded Systems
Introduction to Embedded Systems
 
Hangover - Que pasó ayer? Troubleshooting con vistas ASH & S-ASH
Hangover - Que pasó ayer?   Troubleshooting con vistas ASH & S-ASHHangover - Que pasó ayer?   Troubleshooting con vistas ASH & S-ASH
Hangover - Que pasó ayer? Troubleshooting con vistas ASH & S-ASH
 
Vx works RTOS
Vx works RTOSVx works RTOS
Vx works RTOS
 
Sjug aug 2010_cloud
Sjug aug 2010_cloudSjug aug 2010_cloud
Sjug aug 2010_cloud
 
Linux Porting
Linux PortingLinux Porting
Linux Porting
 
Inter Process Communication
Inter Process CommunicationInter Process Communication
Inter Process Communication
 

More from Anil Kumar Pugalia (15)

File System Modules
File System ModulesFile System Modules
File System Modules
 
Embedded C
Embedded CEmbedded C
Embedded C
 
Playing with R L C Circuits
Playing with R L C CircuitsPlaying with R L C Circuits
Playing with R L C Circuits
 
References
ReferencesReferences
References
 
Functional Programming with LISP
Functional Programming with LISPFunctional Programming with LISP
Functional Programming with LISP
 
Power of vi
Power of viPower of vi
Power of vi
 
gcc and friends
gcc and friendsgcc and friends
gcc and friends
 
"make" system
"make" system"make" system
"make" system
 
Hardware Design for Software Hackers
Hardware Design for Software HackersHardware Design for Software Hackers
Hardware Design for Software Hackers
 
RPM Building
RPM BuildingRPM Building
RPM Building
 
Linux Network Management
Linux Network ManagementLinux Network Management
Linux Network Management
 
Timers
TimersTimers
Timers
 
Threads
ThreadsThreads
Threads
 
Linux Memory Management
Linux Memory ManagementLinux Memory Management
Linux Memory Management
 
Linux File System
Linux File SystemLinux File System
Linux File System
 

Recently uploaded

"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 

Recently uploaded (20)

"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 

Kernel Programming

  • 1. © 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Kernel Programming
  • 2. 2© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. What to Expect? How to do programming in “Kernel C” for Achieving Concurrency Keeping Time Providing Delays Timer Control
  • 3. 3© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Concurrency
  • 4. 4© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Concurrency with Locking Mutexes Header: <linux/mutex.h> Type: struct mutex APIs DEFINE_MUTEX mutex_is_locked mutex_lock, mutex_trylock, mutex_unlock Semaphores Header: <linux/semaphore.h> Type: struct semaphore APIs sema_init down, down_trylock, down_interruptible, up
  • 5. 5© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Concurrency w/ Locking (cont.) Spin Locks Header <linux/spinlock.h> Type: spinlock_t APIs spin_lock_init spin_[try]lock, spin_unlock Reader-Writer Locks Header: <linux/spinlock.h> Type: rwlock_t APIs read_lock, read_unlock write_lock, write_unlock
  • 6. 6© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Concurrency without Locking Atomic Variables Header: <asm-generic/atomic.h> Type: atomic_t Macros ATOMIC_INIT atomic_read, atomic_set atomic_add, atomic_sub, atomic_inc, atomic_dec atomic_xchg
  • 7. 7© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Concurrency w/o Locking (cont.) Atomic Bit Operations Header: <linux/bitops.h> APIs rol8, rol16, rol32, ror8, ror16, ror32 find_first_bit, find_first_zero_bit find_last_bit find_next_bit, find_next_zero_bit Header: <asm-generic/bitops.h> APIs set_bit, clear_bit, change_bit test_and_set_bit, test_and_clear_bit, test_and_change_bit
  • 8. 8© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Wait Queues Wait Queues Header: <linux/wait.h> Wait Queue Head APIs DECLARE_WAIT_QUEUE_HEAD(wq); wait_event_interruptible(wq, cond); wait_event_interruptible_timeout(wq, cond, timeout); wake_up_interruptible(&wq); ... (non-interruptible set) Wait Queue APIs DECLARE_WAITQUEUE(w, current); add_wait_queue(&wq, &w); remove_wait_queue(&wq, &w);
  • 9. 9© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Time Keeping
  • 10. 10© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Time since Bootup tick – Kernel's unit of time. Also called jiffy HZ – ticks per second Defined in Header: <linux/param.h> Typically, 1000 for desktops, 100 for embedded systems 1 tick = 1ms (desktop), 10ms (embedded systems) Variables: jiffies & jiffies_64 Header: <linux/jiffies.h> APIs time_after, time_before, time_in_range, ... get_jiffies_64, ... msec_to_jiffies, timespec_to_jiffies, timeval_to_jiffies, … jiffies_to_msec, jiffies_to_timespec, jiffies_to_timeval, ...
  • 11. 11© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Time since Bootup (cont.) Platform specific “Time Stamp Counter” On x86 Header: <asm/msr.h> API: rdtsc(ul low_tsc_ticks, ul high_tsc_ticks); Getting it generically Header: <linux/timex.h> API: read_current_timer(unsigned long *timer_val);
  • 12. 12© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Absolute Time Header: <linux/time.h> APIs mktime(y, m, d, h, m, s) – Seconds since Epoch void do_gettimeofday(struct timeval *tv); struct timespec current_kernel_time(void);
  • 13. 13© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Delays
  • 14. 14© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Long Delays Busy wait: cpu_relax while (time_before(jiffies, j1)) cpu_relax(); Yielding: schedule/schedule_timeout while (time_before(jiffies, j1)) schedule();
  • 15. 15© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Short Delays but Busy Waiting Header: <linux/delay.h> Arch. specific Header: <asm/delay.h> APIs void ndelay(unsigned long ndelays); void udelay(unsigned long udelays); void mdelay(unsigned long mdelays);
  • 16. 16© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Long Delays: Back to Yielding Header: <linux/delay.h> APIs void msleep(unsigned int millisecs); unsigned long msleep_interruptible(unsigned int millisecs); void ssleep(unsigned int secs);
  • 17. 17© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Timers
  • 18. 18© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Kernel Timers Back end of the various delays Header: <linux/timer.h> Type: struct timer_list APIs void init_timer(struct timer_list *); /* Nullifies */ struct timer_list TIMER_INITIALIZER(f, t, p); void add_timer(struct timer_list *); void del_timer(struct timer_list *); int mod_timer(struct timer_list *, unsigned long); int del_timer_sync(struct timer_list *);
  • 19. 19© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Tasklets Timers without specific Timing Header: <linux/interrupt.h> Type: struct tasklet_struct APIs void tasklet_init(struct tasklet_struct *t, void (*func) (unsigned long), unsigned long data); void tasklet_kill(struct tasklet_struct *t); DECLARE_TASKLET(name, func, data); tasklet_enable(t), tasklet_disable(t) tasklet_[hi_]schedule(t);
  • 20. 20© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Work Queues In context of “Special Kernel Thread” Header: <linux/workqueue.h> Types: struct workqueue_struct, struct work_struct Work Queue APIs q = create_workqueue(name); q = create_singlethread_workqueue(name); flush_workqueue(q); destroy_workqueue(q); Work APIs DECLARE_WORK(w, void (*function)(void *), void *data); INIT_WORK(w, void (*function)(void *), void *data); Combined APIs int queue_work(q, &w); int queue_delayed_work(q, &w, d); int cancel_delayed_work(&w); Global Shared Work Queue API schedule_work(&w);
  • 21. 21© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Helper Interfaces
  • 22. 22© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Other Helper Interfaces in Latest Kernels User Mode Helper Linked Lists Hash Lists Notifier Chains Completion Interface Kthread Helpers
  • 23. 23© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved.
  • 24. 24© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. What to Expect? How to do programming in “Kernel C” for Achieving Concurrency With & without Locking Wait Queues Keeping Time Relative & Absolute Providing Delays Long and Short Busy Wait and Yielding Timer Control Kernel Timers Tasklets Work Queues
  • 25. 25© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Any Queries?