LDD3 | Notes of Linux Device Driver , 3rd edition
kandi X-RAY | LDD3 Summary
kandi X-RAY | LDD3 Summary
Notes of Linux Device Driver, 3rd edition.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of LDD3
LDD3 Key Features
LDD3 Examples and Code Snippets
Community Discussions
Trending Discussions on LDD3
QUESTION
According to LDD3, both kobject_init
and kobject_add
increase the reference count. So kobject_create_and_add
should create an object with 2 references. Right?
But in linux/sample/kobject/kobject-example.c
, the module kobject_create_and_add
an object, but only put it once, when exiting. It implies there's only one reference count. What's wrong?
ANSWER
Answered 2021-Feb-11 at 07:02So
kobject_create_and_add
should create an object with 2 references. Right?
Not really. It creates an object (which should set its reference counter to 1
) and then adds it to the parent object (which should increment the parent object's reference counter). Two different objects are involved, and two different reference counters are handled.
Calling kobject_put()
decrements the reference counter of the object and, when the new reference counter value tests accordingly, calls kobject_release()
on the object internally. The latter invokes kobject_cleanup()
, which, in turn, invokes kobject_put()
on the parent object thus taking care of its reference counter, too.
QUESTION
I am learning the "Linux device drivers". I created a character device named char_device
. When I read the data from the device it keeps printing the message to terminal infinitely crashing the machine.
Source code of the read operation in the driver:
...ANSWER
Answered 2020-Nov-30 at 14:43Programs such as cat
will read the current input file until it encounters an end-of-file condition or a read error. By convention, a read()
return value of 0 indicates an end-of-file condition when a non-zero amount of data is requested. A return value of -1
indicates a read error with the error number in the errno
variable.
At the kernel level, the read
file operation handler should return 0 to indicate an end-of-file condition (but may also do so when the requested amount in 0), and should return a negated errno
value to indicate a read error.
OP's current read
file operation handler, my_read
, copies the contents of a string literal, "Hello from the kernel world!\n"
, to the user memory buffer, limited to the requested read amount or the length of the string, whichever is smallest. It never returns an end-of-file condition. The only time it returns 0 is when the requested amount is 0, but that is not a valid end-of-file condition.
One thing that my_read
could do is to use the passed in file position information to determine where to start reading, and to limit the amount to be copied. It should update the position accordingly. Then it can return 0 when to indicate end-of-file when there is no more data to be copied. Here is a possible implementation:
QUESTION
I never found the definition for scull_follow in the book, so I'm trying to understand it based off a github repo(https://github.com/martinezjavier/ldd3). Here is the code I am trying to understand:
...ANSWER
Answered 2020-Jul-11 at 07:38This:
QUESTION
From http://www.makelinux.net/ldd3/chp-7-sect-1.shtml
Needless to say, both
jiffies
andjiffies_64
must be considered read-only
I wrote a program to verify and it successfully updates the jiffies
value.
ANSWER
Answered 2020-Feb-04 at 13:53What you are reading is merely a warning. It is an unwritten contract between you (kernel module developer) and the kernel. You shouldn't modify the value of jiffies
since it is not up to you to do so, and is updated by the kernel according to a set of complicated rules that you should not worry about. The jiffies
value is used internally by the scheduler, so bad things can happen modifying it. Chances are that the variable you see in your module is only a thread-local copy of the real one, so modifying could have no effect. In any case, you shouldn't do it. It is only provided to you as additional information that your module might need to know to implement some logic.
Of course, since you are working in C, there is no concept of "permissions" for variables. Anything that is mapped in a readable and writable region of memory can be modified, you could even modify data in read-only memory by changing the permissions first. You can do all sorts of bad stuff if you want. There are a lot of things you're not supposed to alter, even if you have the ability to do so.
QUESTION
When write accessing a shared resource protected by a seqlock, a writer must obtain an exclusive lock before entering the critical section. So, as with spinlocks, it makes sense for write accessing with seqlock to have common variants like *_irqsave and *_bh. But LDD3 (on page 128) says:
If your seqlock might be accessed from an interrupt handler, you should use the IRQ-safe versions instead:
...
ANSWER
Answered 2019-Dec-04 at 17:22Since 3.9 kernel there are no such functions.
In general you're right: seqlock readers should be able to work with inconsistent data.
So if we read the counter, then some interrupt arrives, then we make sure that data is in inconsistent state - just do re-reading.
P.S. LDD3 - this tutorial is quite good, but not so relevant.
In such cases you can do some git investigation. Here it that commit.
QUESTION
I'm trying to compile and run a "Hello World" module from the book "Linux Device Drivers" the program ~/ldd3/hello.c I'm trying to compile is:
...ANSWER
Answered 2018-Dec-10 at 10:59In order to see kernel messages, you can use dmesg
.
Alternatively, you can see the syslog tail var/log/syslog
.
QUESTION
I am currently reading a book entitled "Linux device drivers" from O'Reilly.
Thing is that this book imo isn't really a guide on how to write drivers but it instead explains all the apis and their prinicples. So I tried writing a small driver -which doesn't do anything interesting -with what I read so far. Thing is:
I don't know which file I can execute
cat
on orecho
to in order to invoke my callback functionsit looks nothing like all the other code snippets I found online
The different pieces of code:
my code (https://paste.ubuntu.com/p/8tVyTJTPBQ/) creates:
$ls /sys/module/main/
oresize holders initsize initstate notes refcnt sections srcversion taint uevent
no new entry in /dev
code snippet using device_create: https://paste.ubuntu.com/p/cJxjdyXjhX/ source creates:
$ ls /sys/module/main/
coresize holders initsize initstate notes refcnt sections srcversion taint uevent
$ ls -l /dev/ebbchar
crw------- 1 root root 238, 0 Mai 28 07:52 /dev/ebbchar
code using kobjects: https://paste.ubuntu.com/p/nt3XvZs7vF/ source creates:
$ls -l /sys/kernel/
drwxr-xr-x 2 root root 0 Dec 17 16:29 etx_sysfs
I can see that my code successfully created a bunch of files under /sys/kernel. Now what is the difference in endgoal between my code and the two other snippets? Should I use device_create/kobjects or maybe none of those? The book I am reading doesn't mention anywhere the functions used by the 2 other pieces of code. So not sure which way I am supposed to follow...
Thanks_xe
...ANSWER
Answered 2018-May-29 at 02:07device_create()
creates a device and registers it with sysfs, and create necessary kobjects.
To create necessary kobjects, kobject-related functions(kobject_init()
, kobject_add()
, ...) are called in device_create()
.
If you need to create a device, you should call one of device creation functions like device_create()
.
QUESTION
I am currently reading LDD3, and have some question about how Linux actually proesses input from a device in /dev/input/mouse*?
When I cat /dev/input/mouse2
(for my logitech mouse) I see input when I move.
How does the kernel take the bytes from this char device and make it display the mouse on my screen? I have also read about the Linux input subsystem kernel API, which confuses me because why would I need to write to an inode I create at /dev/input/mouse* when I can just send information directly through this API?
...ANSWER
Answered 2018-Apr-23 at 17:11The Linux kernel doesn't process /dev/input/mouse*
The kernel is what provides /dev/input/mouse*
Userspace programs like X.org can then read this device and show a cursor moving accordingly.
QUESTION
I'm currently trying to implement a (not that ?) simple kernel block device driver.
I inspired mainly from the book Linux Device Drivers, 3rd Edition which is not totally up-to-date anymore as it was published in 2005.
Anyway the logic is still there and I learnt a lot from it. However examples are not really effective as many things have changed since 2005.
I found a github repository where examples should be updated to work on recent kernels but I think there is still some things to update as I can't adapt examples to make it work on kernel 4.9.0
Here is how my module is made:
At initialization :
- Register the module as block device with
register_blkdev
- Allocate the device data buffer
- Initialize spinlock
- Initialize request queue
- Configure request queue
- Allocate
gendisk
structure - Fill
gendisk
structure - Create the disk with
add_disk
Then I implemented a function to handle request events from request queue and treat read and write events on the block device.
Here is the function : (It's highly inspired from LLD-3rd with some modifications to match current kernel functions)
...ANSWER
Answered 2018-Apr-12 at 16:08The message from the commit containing the culprit change, authored by Ming Lei:
block: remove struct request buffer member
This was used in the olden days, back when onions were proper yellow. Basically it mapped to the current buffer to be transferred. With highmem being added more than a decade ago, most drivers map pages out of a bio, and
rq->buffer
isn't pointing at anything valid.Convert old style drivers to just use
bio_data()
.
There are some good resources for reading about BIO, such as this post on lwn. A relevant snippet:
char *bio_data(struct bio *bio)
Returns the kernel virtual address for the data buffer.
So it looks like you will have more success using bio_data(rq->bio)
in place of rq->buffer
.
Edit by author:
Also found this link which is in two parts and presents in first part the bio layer and in second part the request layer.
QUESTION
In the device tree I am using, in one of its node, the filed interrupts
is:
ANSWER
Answered 2018-Feb-07 at 14:50What are exactly the <0x0 0x1d 0x4> numbers? I know that, in according to elinux.org, (interrupts = <0x0 0x1d 0x4>;)
Firstly you need to look at the interrupt-parent of the device node, this parent will #interrupt-cells property which specifies number of bits needed to encode a interrupt source, so from your entry interrupts = <0x0 0x1d 0x4>; means the following:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install LDD3
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page