LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-08-2019, 11:34 AM   #1
beandigital
LQ Newbie
 
Registered: Jan 2013
Posts: 21

Rep: Reputation: Disabled
Block driver aligning BIO buffer address


I am developing a block driver for an embedded device. The device has a DMA controller, but it expects the address to be aligned 8 byte boundary. I have using an example ram disk driver to develop my driver. But when I look at the address that is returned from the BIO, there is no alignment. Is there a way to align it? I have included the code I am using below.

Code:
#define BV_PAGE(bv) ((bv).bv_page)
#define BV_OFFSET(bv) ((bv).bv_offset)
#define BV_LEN(bv) ((bv).bv_len)

rq_for_each_segment(bv, req, iter)
{
  buffer = page_address(BV_PAGE(bv)) + BV_OFFSET(bv);
 
Old 11-09-2019, 07:59 AM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,142

Rep: Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264
There are two reasons that the kernel copies write data from user to kernel space before issuing the I/O request. One is to avoid problems if the user space buffer gets freed or changed during the I/O, and the other is to align the data. The kernel block cache is all aligned on page boundaries.

A ram disk probably doesn't care about boundaries so doesn't need to do this. It also doesn't need to do the extra copy since it can just copy directly to the target ram area.

If you look at the async I/O driver, it requires the user to provide aligned buffers and does DMA directly from/to user space. You can either do that or copy to aligned buffers allocated in the kernel.
 
Old 11-09-2019, 09:33 AM   #3
beandigital
LQ Newbie
 
Registered: Jan 2013
Posts: 21

Original Poster
Rep: Reputation: Disabled
Thanks for your reply. I dont really understand what you said as I am relatively new to Linux programming. I am actually a hardware engineer, so just trying to work this out myself. Is there an example that you could point me to? Thanks
 
Old 11-09-2019, 04:12 PM   #4
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,142

Rep: Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264
The free book Linux Device Drivers contains working examples for all types of drivers, including block devices. There is a whole chapter on Memory management and DMA.

https://lwn.net/Kernel/LDD3/
 
1 members found this post helpful.
  


Reply

Tags
block device, driver



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Does sd card driver support the processing of single request (bio) or the queue alex_ren Linux - Kernel 1 10-27-2011 11:24 PM
Block Bio Issue.... pvsachindranath Linux - Kernel 2 10-19-2011 12:13 AM
Copy ALSA buffer to the buffer allocated in my driver. yethish Programming 1 08-04-2011 11:27 AM
What is the difference between the free buffer and buffer in the buffer hash queue? Swagata Linux - Enterprise 0 05-25-2006 11:57 PM
aligning torsmo kurrupt Linux - Software 1 06-28-2005 02:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration