2005-04-04 18:27:59

by Matthew Dharm

[permalink] [raw]
Subject: mmap() and ioctl()

This probably is a silly question, but....

Is is possible to open a file, mmap() it into memory, then pass the address
of that map via an ioctl() call to the kernel, which will copy_from_user()
that data?

Yeah, that's an odd concept, I know... I could always malloc() some
memory, read the file in, and then ioctl() it. But, if I could get away
with a direct mmap(), that would be much better for me.

Matt

--
Matthew Dharm Home: [email protected]
Maintainer, Linux USB Mass Storage Driver

I say, what are all those naked people doing?
-- Big client to Stef
User Friendly, 12/14/1997


Attachments:
(No filename) (637.00 B)
(No filename) (189.00 B)
Download all attachments

2005-04-04 19:03:13

by linux-os (Dick Johnson)

[permalink] [raw]
Subject: Re: mmap() and ioctl()

On Mon, 4 Apr 2005, Matthew Dharm wrote:

> This probably is a silly question, but....
>
> Is is possible to open a file, mmap() it into memory, then pass the address
> of that map via an ioctl() call to the kernel, which will copy_from_user()
> that data?
>

Yes. A user-mode pointer, passed via ioctl() is valid in the kernel
in the context of the user, i.e., during read() write() ioctl().

However, it is not valid if it is accessed by some other process or
an interrupt. In other words, you can't store it somewhere and
access it later in some other context.

> Yeah, that's an odd concept, I know... I could always malloc() some
> memory, read the file in, and then ioctl() it. But, if I could get away
> with a direct mmap(), that would be much better for me.
>
> Matt
>

Since you need to copy anyway, you could mmap() your kernel
data (impliment mmap in your driver). Then you mmap both
"files" the same way and copy to/from in user-mode.

Cheers,
Dick Johnson
Penguin : Linux version 2.6.11 on an i686 machine (5537.79 BogoMips).
Notice : All mail here is now cached for review by Dictator Bush.
98.36% of all statistics are fiction.

2005-04-04 19:21:27

by Matthew Dharm

[permalink] [raw]
Subject: Re: mmap() and ioctl()

On Mon, Apr 04, 2005 at 03:02:26PM -0400, Richard B. Johnson wrote:
> On Mon, 4 Apr 2005, Matthew Dharm wrote:
>
> >This probably is a silly question, but....
> >
> >Is is possible to open a file, mmap() it into memory, then pass the address
> >of that map via an ioctl() call to the kernel, which will copy_from_user()
> >that data?
> >
>
> Yes. A user-mode pointer, passed via ioctl() is valid in the kernel
> in the context of the user, i.e., during read() write() ioctl().
>
> However, it is not valid if it is accessed by some other process or
> an interrupt. In other words, you can't store it somewhere and
> access it later in some other context.

Right, I've got that part. The plan has been to mmap(), ioctl(), and
inside the ioctl do a copy_from_user() into a kernel-context buffer.

> >Yeah, that's an odd concept, I know... I could always malloc() some
> >memory, read the file in, and then ioctl() it. But, if I could get away
> >with a direct mmap(), that would be much better for me.
>
> Since you need to copy anyway, you could mmap() your kernel
> data (impliment mmap in your driver). Then you mmap both
> "files" the same way and copy to/from in user-mode.

That's an interesting concept, and one I'm not familiar with. Any useful
pointers (beyond UTSL)? I'll admit to being much more familiar with SCSI
and USB internals than I am with something like device-layer interfacing.

It sounds like you're saying that my driver can implement an mmap() method
(similar to the ioctl method), and then I can just mmap the source file and
the driver /dev node and do a memcpy() between them.

That's an interesting idea, but potentially not what I need. The data
needs to go with some command information and a buffer to stuff the results
in. This is basically a co-processor device I'm talking to. The basic
data path here is from a file, through the driver, to a custom piece of
hardware (and back again).

Tho, anything that allows me to move the data from the disk up to a place
where I can pci_map_single() it faster is a Good Thing(tm).

Matt

--
Matthew Dharm Home: [email protected]
Maintainer, Linux USB Mass Storage Driver

Sir, for the hundreth time, we do NOT carry 600-round boxes of belt-fed
suction darts!
-- Salesperson to Greg
User Friendly, 12/30/1997


Attachments:
(No filename) (2.29 kB)
(No filename) (189.00 B)
Download all attachments

2005-04-05 15:31:16

by Aleksey Gorelov

[permalink] [raw]
Subject: RE: mmap() and ioctl()

>-----Original Message-----
>From: [email protected]
>[mailto:[email protected]] On Behalf Of Matthew Dharm
>Sent: Monday, April 04, 2005 12:20 PM
>To: Richard B. Johnson
>Cc: Kernel Developer List
>Subject: Re: mmap() and ioctl()
>

[snip]

>That's an interesting concept, and one I'm not familiar with.
>Any useful
>pointers (beyond UTSL)? I'll admit to being much more
>familiar with SCSI
>and USB internals than I am with something like device-layer
>interfacing.
Try this:

http://lwn.net/Kernel/LDD3/

Aleks.