<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Mar 7, 2016 at 3:53 PM, Greg KH <span dir="ltr"><<a href="mailto:greg@kroah.com" target="_blank">greg@kroah.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class=""><div class="h5">On Mon, Mar 07, 2016 at 03:37:24PM -0500, Kenneth Adam Miller wrote:<br>
><br>
><br>
> On Mon, Mar 7, 2016 at 3:29 PM, Greg KH <<a href="mailto:greg@kroah.com">greg@kroah.com</a>> wrote:<br>
><br>
> On Mon, Mar 07, 2016 at 03:21:44PM -0500, Kenneth Adam Miller wrote:<br>
> ><br>
> ><br>
> > On Mon, Mar 7, 2016 at 3:17 PM, Greg KH <<a href="mailto:greg@kroah.com">greg@kroah.com</a>> wrote:<br>
> ><br>
> > On Mon, Mar 07, 2016 at 03:00:50PM -0500, Kenneth Adam Miller wrote:<br>
> > > I have a driver that manages three sets of identical data<br>
> structures that<br>
> > > differ only in address values. Currently, I pray that the device<br>
> file to<br>
> > which<br>
> > > I have callbacks mapped for the driver gets called sequentially,<br>
> because<br>
> > there<br>
> > > are pairs of mmap's that need to be made. I know that this isn't<br>
> the most<br>
> > ideal<br>
> > > way to do it, so I'm searching for a better way rather than to swap<br>
> out<br>
> > the<br>
> > > values on each method call. <br>
> > ><br>
> > > There are several things I am aware of but for each one I have<br>
> questions:<br>
> > > 1) there are kernel module parameters<br>
> > > If I use kernel module parameters, I need to be able to insert the<br>
> kernel<br>
> > > module three times in order to have each one have a distinct set of<br>
> > global<br>
> > > memory and mapped callbacks to distinct files. Can that be done?<br>
> Second,<br>
> > I will<br>
> > > need to compile the driver statically later. How can I pass those<br>
> > parameters<br>
> > > that would otherwise be on the command line in statically?<br>
> ><br>
> > Never use kernel module parameters for a driver, nor for any other<br>
> > kernel module you create. They are global and don't work for<br>
> > device-specific issues.<br>
> ><br>
> > > 2) I can compile the driver in three times with a compile time<br>
> flag. This<br>
> > is<br>
> > > the simplest and easiest, but it requires some buildroot and<br>
> makefile foo<br>
> > that<br>
> > > I think is a dirty hack.<br>
> ><br>
> > It's also never accepted, don't do that.<br>
> ><br>
> > > 3) I could have the init function create three separate files,<br>
> since it<br>
> > is on<br>
> > > init that I discover what my values are. But then I have to also<br>
> > associate<br>
> > > identical functions that reference global variables in the kernel<br>
> object.<br>
> > > Duplicating the code would be worse that compiling the same code<br>
> three<br>
> > times<br>
> > > with a kernel parameter, even though that would help me solve my<br>
> distinct<br>
> > > globals problem. So how could I parameterisze a char device with<br>
> data<br>
> > specific<br>
> > > to the instance?<br>
> ><br>
> > open() gives you the hook to do so, please just do that. There's a<br>
> > whole kernel tree full of examples of how to do this, take a look at<br>
> > existing code please.<br>
> ><br>
> ><br>
> > After I had the idea in the second email, I think that using the kernel<br>
> api to<br>
> > distinguish which char device a callback maps to in order to utilize the<br>
> > corresponding data is the best way.<br>
> ><br>
> > If I could do something along the lines of retrieving the file name, as<br>
> in a<br>
> > char *,<br>
><br>
> There is no such "thing" in the kernel (think of symlinks, or different<br>
> names for the same major:minor pair).<br>
><br>
> > from the file * that is passed in with the callback, or distinguish any<br>
> > one of these:<br>
> ><br>
> > static dev_t LSKSMM_dev;<br>
> > static struct cdev LSKSMM_cdev;<br>
> > static struct class *LSKSMM_class;<br>
> > static struct device *LSKSMM_device;<br>
><br>
> Those are all different things, none of them get passed into open().<br>
><br>
> I don't think you have thought this through very far, where is your<br>
> source code to take a look at it?<br>
><br>
> > which are also created on module init, it would really make things<br>
> convenient<br>
> > and easy. I'm currently digging around in the kernel headers, but I think<br>
> > probably somebody somewhere knows what I'm looking for. Some unique field<br>
> that<br>
> > I can retain on init that I can get back in my mmap/ioctl call to<br>
> recognize<br>
> > what data to use.<br>
><br>
> Again, it's all provided directly to you in your open() call, what's<br>
> wrong with that?<br>
><br>
><br>
> Currently, my kernel driver is opened twice and mmap'd twice by each process.<br>
<br>
</div></div>Again, any pointers to your source code?<br></blockquote><div><br></div><div>Can't release it. It looks a lot like this though:<br><br><a href="https://github.com/claudioscordino/mmap_alloc/blob/master/mmap_alloc.c">https://github.com/claudioscordino/mmap_alloc/blob/master/mmap_alloc.c</a><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<span class=""><br>
> I have three processes, but I have to initialize them on startup with<br>
> a startup script.<br>
<br>
</span>What does that have to do with the kernel code?<br></blockquote><div><br></div><div>It forces the design of my kernel driver to have to take into consideration how to map associated private data with a particular call, so that a process's pair of mmap calls don't get what some other process should have gotten. 3 processes, 2 items per process that have to match. As in, A1 & A2 to process 1, B1 & B2 to process 2, ect. But since they're racy, A1 could end up going to the first caller; I can't just rotate through a buffer containing the private data items is what I'm saying.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<span class=""><br>
> So they come up as daemons, racing, which is a problem. I know that on<br>
> init I can create three entries in /dev/, distinguished by a number or<br>
> something that makes the device unique.<br>
<br>
</span>The kernel creates the /dev/ entries, don't do it from userspace, if you<br>
expect things to work properly.<br></blockquote><div><br></div><div>I already have my driver creating the /dev entries correctly. I had mentioned the data types required in the process of doing that:</div><div><div>static dev_t LSKSMM_dev;</div><div>static struct cdev LSKSMM_cdev;</div><div>static struct class *LSKSMM_class;</div><div>static struct device *LSKSMM_device;</div></div><div><br></div><div>If any one of those has a field in it in common with what a file struct has, but that is distinct across files, then that would be perfect.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<span class=""><br>
> When a mmap call hits is when I need to know what specific file that<br>
> the mmap corresponds to.<br>
<br>
</span>Again, open() is giving you this, why do you keep saying it isn't?<br>
<br>
And again, there are at least 3 different ways of determining this at<br>
open() time (5 total I think, last time I looked), each way depends on<br>
your driver structure and how your code needs to work, so I can't just<br>
say "do it this way", sorry.<br></blockquote><div><br></div><div>I know it by file pointer, but not by what I have in dev_t, cdev, class *, or device *. Or possibly it's in one of those but I just don't know where.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<span class=""><br>
> I have to identify it associatively by name or by the identifiers that<br>
> the kernel consumes for it's internal class and/or device entry.<br>
<br>
</span>Why?<br></blockquote><div><br></div><div>Cannot mmap concurrently, will produce incorrect results.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<span class=""><br>
> I don't know that I could do that with what I'm given in open, because<br>
> while I'm sure that provides some information, it doesn't provide the<br>
> the information when I need it.<br>
<br>
</span>All of the char drivers in the kernel kind of refute that excuse :)<br></blockquote><div><br></div><div>I don't know that any char device has potentially 6 different data items it has to manage, split between what should be discrete processes. Again, suppose I recorded what I got in open, from inode (and not file pointer, since it for some reason must do it in open). Then how do I associate two otherwise identical calls in mmap without using file pointer to a particular private resource? I create the dev_t, cdev, class and device in my init. I need to associate them with a file pointer, I can't use an inode, unless I have to use an inode in the process of retaining some information in the file structure by pointer. Which will then later be used in mmap.</div><div><br></div><div>In any case, inode is contained in file pointer, so if I have everything I need there, then the same associating code would work in either open or mmap.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<span class=""><br>
> I don't have anything in my open callback except a printk.<br>
<br>
</span>Then fix that!</blockquote><div><br></div><div>It would be inappropriate to move all the code in my mmap callback that does most of the work into the open callback.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<span class=""><br>
> My mmap does all the work, and has to distinguish the right private<br>
> item to use with what device file.<br>
<br>
</span>Again, set that in your open() callback, that's what it is there for!<br>
<br>
there is a whole kernel tree full of examples of how to do this, please<br>
use them for insight.<br></blockquote><div><br></div><div>Ok, so after digging some this is what I have come up with. Just to checkk - I should look up the inode by the file pointer, then the dev_t, against which I can associate with the dev_t that I create in the init. If those are equal, then I have a way to know what file is being used in the file system since I can retain the dev_t items after init. I can use that knowledge to look up my private data items. Clear?</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<br>
thanks,<br>
<br>
greg k-h<br>
</blockquote></div><br></div></div>