search
HomeSystem TutorialLINUXThe processing mechanism for blocking IO processes in Linux drivers

The processing mechanism for blocking IO processes in Linux drivers

Feb 11, 2024 pm 04:45 PM
linuxlinux tutoriallinux systemlinux commandshell scriptoverflowGetting started with linuxlinux learning

We need to consider how to respond when the driver cannot satisfy the request immediately. Normally, the calling process does not care about the status of the driver, so we need to handle it accordingly in the driver. A common approach is to block requests to the process.

Blocking I/O means that if the required resources cannot be obtained when performing device operations, the current process will be suspended until operable conditions are met before the operation is performed. The suspended process goes to sleep and is removed from the scheduler's run queue until the waiting conditions are met. On the contrary, non-blocking I/O does not suspend the process when the device operation cannot be performed, but chooses to give up or continue polling until the operation can be performed.

Waiting queue is a classic mechanism for handling blocking I/O.

1. Blocking the I/O processing flow in the driver

In summary, the blocking I/O processing flow includes 4 parts. First, initialize the waiting queue list, which stores the processes that need to be blocked. Then initialize a waiting queue and add the process that currently needs to be blocked to the waiting queue linked list. Then set the process to be interruptible and block the process from sleeping. Finally, when certain conditions are met, that is, resources are available, the processes in the waiting queue are awakened.
The following figure describes the blocking I/O processing flow:

The processing mechanism for blocking IO processes in Linux drivers

2. Initialize the waiting queue list

Before initializing the waiting queue linked list, we first need to define a wait_queue_head_t variable. For example, in the ISP driver of RK3399, the data structure struct rkisp1_stream contains wait_queue_head_t done;. Perform initialization operations by calling init_waitqueue_head(&stream->done);.

void rkisp1_stream_init(struct rkisp1_device *dev, u32 id)
{
 struct rkisp1_stream *stream = &dev->stream[id];

 memset(stream, 0, sizeof(*stream));
 stream->id = id;
 stream->ispdev = dev;

 INIT_LIST_HEAD(&stream->buf_queue);
 init_waitqueue_head(&stream->done);
 spin_lock_init(&stream->vbq_lock);
 ...
}

wait_queue_head_tThe prototype of the variable is __wait_queue_head, as shown below:

struct __wait_queue_head {
 spinlock_t  lock;
 struct list_head task_list;
};

init_waitqueue_head()The function that is actually executed is __init_waitqueue_head(), and its function definition is as follows:

void __init_waitqueue_head(wait_queue_head_t *q, const char *name, struct lock_class_key *key)
{
 spin_lock_init(&q->lock);
 lockdep_set_class_and_name(&q->lock, key, name);
 INIT_LIST_HEAD(&q->task_list);
}

3. 等待队列处理

调用DECLARE_WAITQUEUE(wait, current)将当前进程初始化为等待队列。注意,这里的等待队列和等待队列链表头可不是一个东东。

#define DECLARE_WAITQUEUE(name, tsk)     \
 wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk)

等待队列的定义如下:

struct __wait_queue {
 unsigned int  flags;
 void   *private;
 wait_queue_func_t func;
 struct list_head task_list;
};

等待队列和等待队列链表头是通过add_wait_queue()结合到一起的。

init_waitqueue_head(&delay_wait);
add_wait_queue(&delay_wait, &wait);

以上代码是将等待队列进程加入到等待队列链表中:

static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
{
 list_add(&new->task_list, &head->task_list);
}

4. 阻塞进程处理

阻塞进程处理包括两部分内容,首先设置进程的睡眠状态,包括TASK_INTERRUPTIBLETASK_UNINTERRUPTIBLE两种。前者用于可中断睡眠,后者用于不可中断睡眠。然后,将当前进程退出调度器让出CPU的使用权。

set_current_state(TASK_INTERRUPTIBLE);
schedule();

5. 唤醒处理

唤醒处理通常位于中断处理函数或某些动作成功执行之后,特定条件满足时,唤醒通过阻塞队列睡眠的进程。例如:

void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
       int success, int behind)
{
 if (!bitmap)
  return;
 if (behind) {
  if (atomic_dec_and_test(&bitmap->behind_writes))
   wake_up(&bitmap->behind_wait);
  pr_debug("dec write-behind count %d/%lu\n",
    atomic_read(&bitmap->behind_writes),
    bitmap->mddev->bitmap_info.max_write_behind);
 }
...
}

The above is the detailed content of The processing mechanism for blocking IO processes in Linux drivers. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:良许Linux教程网. If there is any infringement, please contact admin@php.cn delete
How does memory management differ between Linux and Windows?How does memory management differ between Linux and Windows?May 13, 2025 am 12:04 AM

LinuxandWindowsmanagememorydifferentlyduetotheirdesignphilosophies.Linuxusesovercommittingforbetterperformancebutrisksout-of-memoryerrors,whileWindowsemploysdemand-pagingandmemorycompressionforstabilityandefficiency.Thesedifferencesimpactdevelopmenta

How to Manage Firewalld and UFW for Linux SecurityHow to Manage Firewalld and UFW for Linux SecurityMay 12, 2025 am 10:56 AM

Linux systems rely on firewalls to safeguard against unauthorized network access. These software barriers control network traffic, permitting or blocking data packets based on predefined rules. Operating primarily at the network layer, they manage

How to Check If Your Linux System is a Desktop or LaptopHow to Check If Your Linux System is a Desktop or LaptopMay 12, 2025 am 10:48 AM

Determining if your Linux system is a desktop or laptop is crucial for system optimization. This guide outlines simple commands to identify your system type. The hostnamectl Command: This command provides a concise way to check your system's chassis

How to Increase TCP/IP Connections in LinuxHow to Increase TCP/IP Connections in LinuxMay 12, 2025 am 10:23 AM

Guide to adjust the number of TCP/IP connections for Linux servers Linux systems are often used in servers and network applications. Administrators often encounter the problem that the number of TCP/IP connections reaches the upper limit, resulting in user connection errors. This article will guide you how to improve the maximum number of TCP/IP connections in Linux systems. Understanding TCP/IP connection number TCP/IP (Transmission Control Protocol/Internet Protocol) is the basic communication protocol of the Internet. Each TCP connection requires system resources. When there are too many active connections, the system may reject new connections or slow down. By increasing the maximum number of connections allowed, server performance can be improved and more concurrent users can be handled. Check the current number of Linux connections limits Change settings

How to Convert SVG to PNG in Linux TerminalHow to Convert SVG to PNG in Linux TerminalMay 12, 2025 am 10:21 AM

SVG (Scalable Vector Graphics) files are ideal for logos and illustrations due to their resizability without quality loss. However, PNG (Portable Network Graphics) format often offers better compatibility with websites and applications. This guide d

How to Create Your Own Android and iOS Apps with LiveCodeHow to Create Your Own Android and iOS Apps with LiveCodeMay 12, 2025 am 10:10 AM

LiveCode: A Cross-Platform Development Revolution LiveCode, a programming language debuting in 1993, simplifies app development for everyone. Its high-level, English-like syntax and dynamic typing enable the creation of robust applications with ease

How to Reset a USB Device from the Linux TerminalHow to Reset a USB Device from the Linux TerminalMay 12, 2025 am 10:07 AM

This guide provides a step-by-step process for resetting a malfunctioning USB device via the Linux command line. Troubleshooting unresponsive or disconnected USB drives is simplified using these commands. Step 1: Identifying Your USB Device First, i

How to Set a Temporary Static IP Address on LinuxHow to Set a Temporary Static IP Address on LinuxMay 12, 2025 am 10:06 AM

Temporarily setting a static IP address on Linux is invaluable for network troubleshooting or specific session configurations. This guide details how to achieve this using command-line tools, noting that the changes are not persistent across reboots

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor