Re: request resources at init time vs open time

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, Apr 30, 2006 at 16:26:04 +0100, aoliveira@xxxxxxxxxxxxxx wrote:
> Hi!
> I'm writing some network device drivers (some ISA, some PCI) and would
> like to understand when to request_mem_region(), ioremap(),
> pci_enable_device(), etc... on module init, or on open?
> 
> LDD3 says one should not grab resources on init, but on open instead,
> which makes perfect sense to me.  However, (almost) all network device
> drivers in the kernel grab their resources (except irq) on init.
> Why is that?

> I guess that, for ISA devices, one has to request resources on init
> to probe for the device anyway, so they might as well stay allocated,
> instead of releasing and then requesting again on open.  Would that be
> acceptable?
> 
> For PCI devices, probing is simpler, or even unnecessary, so I guess
> it would be easy (desireable?) to just call pci_enable_device(),
> request_region(), ioremap(), etc... on open.  But I noticed that's
> not the usual practice for network device drivers in the kernel,
> which do it all on init...  I'm confused.

Network devices are not associated with either block or character device
special files and therefore don't have any open. They could allocate the
resources when the device is brought up (and I suppose that's just what
they do with irq), but since the device can be configured even when
down, I think it is a lot simpler to just allocate at init time in this
case.

> I also wonder if the __init and __exit prefixes have a say about this...
> I suppose doing all requests on __init functions and all releases
> on __exit functions might save some memory?!?

__init functions are only used during init and their code is REMOVED
from memory once init is complete.

__exit functions are only used during exit and since that only ever
happens for modules, they are NOT COMPILED at all for kernel proper.

(Guess this should be a FAQ (on www.kernelnewbies.org/FAQ) -- anyone
wants to add it?)

> I would like to understand the advantages and disadvantages of grabbing
> resources on init, versus on open, so I can do it "the right way"! ;-)
> Or is this whole issue not that important, and both methods are fine?

Basically, grab resources as late as possible, but not on demand (ie.
don't use constructs like 'if(have_resource_X) allocate_resource_X'). So
if there is an open and the resources are only needed in context of the
open file descriptor, do it in open (and free them in release). If the
resources are used throughtout the life of the device and shared among
open files, or if stateless access is done (as is the case of network
devices), then allocating at init is ok.

-- 
						 Jan 'Bulb' Hudec <bulb@xxxxxx>

Attachment: signature.asc
Description: Digital signature


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux