LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-20-2016, 03:36 PM   #1
Dr.Lightning
LQ Newbie
 
Registered: Mar 2016
Posts: 24

Rep: Reputation: Disabled
Can driver remember calling application's buffer address?


I'm writing a driver for Fedora. It's mostly working but I need to add a feature. My question relates to private or shared memory. I realize a driver may have a type of global [kernel] memory, which will be common to all the code in the driver regardless of which user application called it. I'm wondering if a driver may also have some [kernel] memory that's unique to the user application calling it. And then, or course, I believe I already know the driver can carefully access the user application's own memory.

Specifically, I want the user application to pass a buffer address to the driver, just as it would during a read() call. But for this special call, that will be implemented using a cover function over ioctl, I want the driver to SAVE the address of that buffer. This is going to be an auxiliary status buffer.

Then, later, when the user application calls read() with a read buffer address, I want to not only copy_to_user the read data into the read buffer, I *also* want to copy_to_user some auxiliary information into that auxiliary buffer. In order to do this, of course, the driver needs to remember that buffer address. Also, I believe this code may get executed for multiple calling user applications, so the memory where the driver saves the buffer address needs to be unique per the caller. I believe it may somehow need to tie into the file handle or something. I don't know.

I haven't researched too deeply into this yet. I haven't come up with good search keywords to find the info efficiently. So I'm asking here to get a head start...

Thanks.
 
Old 03-21-2016, 08:10 AM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Accessing Specific Memory Locations in C

How can you access a memory location using C? their are many more links within that page that may lead you to a better understanding of what it is you need to know as well.

keywords used to find the links and others I did not look at where "memory accessing addresses"

memory: because that is what you are working with.
accessing addresses: because that is what you need to do with the memory.
you could also add what programming language you are using to narrow it down more.

I hope that helps

Last edited by BW-userx; 03-21-2016 at 08:20 AM.
 
Old 03-21-2016, 09:18 AM   #3
Dr.Lightning
LQ Newbie
 
Registered: Mar 2016
Posts: 24

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
Accessing Specific Memory Locations in C

How can you access a memory location using C? their are many more links within that page that may lead you to a better understanding of what it is you need to know as well.

keywords used to find the links and others I did not look at where "memory accessing addresses"

memory: because that is what you are working with.
accessing addresses: because that is what you need to do with the memory.
you could also add what programming language you are using to narrow it down more.

I hope that helps
Thanks, but sorry, you missed my point. I guess I didn't express myself well enough. I have come up with an alternative, so nevermind, I guess.
 
Old 03-21-2016, 11:32 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,786

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
I think that should be tied to the process id, but probably misunderstood
 
Old 03-21-2016, 12:20 PM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by Dr.Lightning View Post
Thanks, but sorry, you missed my point. I guess I didn't express myself well enough. I have come up with an alternative, so nevermind, I guess.
shifted into never mind mode.. whatever it is you're trying to finagle I hope it works out for you.
 
Old 03-21-2016, 03:10 PM   #6
Dr.Lightning
LQ Newbie
 
Registered: Mar 2016
Posts: 24

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
shifted into never mind mode.. whatever it is you're trying to finagle I hope it works out for you.
Thanks to all. I got it working without the need for the driver to remember anything per caller.
 
Old 03-22-2016, 09:02 AM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,633
Blog Entries: 4

Rep: Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931
That's definitely an architecture to be preferred. When an application program makes a system-call that refers to a specific (of course ...) virtual address, there's a really good chance that the real-memory pages in question are already available. In any case, the driver will have to force those pages to be resident and locked for the duration, and will have to wait for this if need be. In this scenario, actual waiting is unlikely ... and, because the process/thread is stopped, interference with the application program is much less likely. The application programmer is much less likely to encounter a race-condition, so debugging is much easier.

The nicest way to do it (IMHO ...) is to create a virtual device structure, which the user-land process of course must "open," which gives you a convenient per-process kernel memory area in which to store things. Let the user-land process then "explicitly rendezvous with" the kernel in order to request, and to receive at that moment, anything that it needs to receive.

Last edited by sundialsvcs; 03-22-2016 at 09:03 AM.
 
Old 03-22-2016, 10:21 AM   #8
Dr.Lightning
LQ Newbie
 
Registered: Mar 2016
Posts: 24

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by sundialsvcs View Post
That's definitely an architecture to be preferred. When an application program makes a system-call that refers to a specific (of course ...) virtual address, there's a really good chance that the real-memory pages in question are already available. In any case, the driver will have to force those pages to be resident and locked for the duration, and will have to wait for this if need be. In this scenario, actual waiting is unlikely ... and, because the process/thread is stopped, interference with the application program is much less likely. The application programmer is much less likely to encounter a race-condition, so debugging is much easier.

The nicest way to do it (IMHO ...) is to create a virtual device structure, which the user-land process of course must "open," which gives you a convenient per-process kernel memory area in which to store things. Let the user-land process then "explicitly rendezvous with" the kernel in order to request, and to receive at that moment, anything that it needs to receive.
Thanks for your thoughts. I believe I'm doing what you say. Specifically, there is a virtual device to open. The user application has a status structure that it passes immediately after a traditional open() call, using a device-specific cover function that inside calls ioctl to send the structure to the driver. The driver uses some structure details for configuration. Later, the user application tries to read the device. In the past, I used a traditional read(). Now instead I'm using another device-specific cover function that inside calls ioctl. The cover function also receives a structure pointer (same one as the config, in fact, but different members of interest). So now the user application is calling cover(fp,&struct) rather than read(fp,buffer,len). Inside the driver, copy_from_user() and copy_to_user() are used to get some initial metadata, do the read, and pass metadata back. This structure, in user memory, now contains that metadata that I originally wanted to be memorized by the driver per caller, the original subject of this post.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Calling startx from inside the application nitin_kumgoyal Slackware 2 12-07-2013 03:42 PM
[SOLVED] I'm trying to remember the ip command to assign an interface to an IP address... BMan8577 Linux - Newbie 3 12-18-2011 09:45 PM
Copy ALSA buffer to the buffer allocated in my driver. yethish Programming 1 08-04-2011 11:27 AM
motif application crashes while calling XtDestroyWidget sanushchacko Linux - Software 0 08-30-2010 09:56 AM
how to translate virtual address to physical address in linux application saurin Programming 1 11-18-2009 09:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:04 PM.

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