linux-ide.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC,PATCHSET] Managed device resources
@ 2006-12-26 15:18 Tejun Heo
  2006-12-26 15:18 ` [PATCH 1/12] devres: device resource management core Tejun Heo
                   ` (12 more replies)
  0 siblings, 13 replies; 17+ messages in thread
From: Tejun Heo @ 2006-12-26 15:18 UTC (permalink / raw)
  To: gregkh, jeff, linux-kernel, linux-ide, htejun

Hello, all.

This patchset implements managed device resources, in short, devres.

## Intro

devres came up while trying to convert libata to use iomap.  Each
iomapped address should be kept and unmapped on driver detach.  For
example, a plain SFF ATA controller (that is, good old PCI IDE) in
native mode makes use of 5 PCI BARs and all of them should be
maintained.

As with many other device drivers, libata low level drivers have
sufficient bugs in ->remove and ->probe failure path.  Well, yes,
that's probably because libata low level driver developers are lazy
bunch, but aren't all low level driver developers?  After spending a
day fiddling with braindamaged hardware with no document or
braindamaged document, if it's finally working, well, it's working.

For one reason or another, low level drivers don't receive as much
attention or testing as core code, and bugs on driver detach or
initilaization failure doesn't happen often enough to be noticeable.
Init failure path is worse because it's much less travelled while
needs to handle multiple entry points.

So, many low level drivers end up leaking resources on driver detach
and having half broken failure path implementation in ->probe() which
would leak resources or even cause oops when failure occurs.  iomap
adds more to this mix.  So do msi and msix.

## Devres

devres is basically linked list of arbitrarily sized memory areas
associated with a struct device.  Each devres entry is associated with
a release function.  A devres can be released in several ways.  No
matter what, all devres entries are released on driver detach.  On
release, the associated release function is invoked and then the
devres entry is freed.

Managed interface is created for resources commonly used by device
drivers using devres.  For example, coherent DMA memory is acquired
using dma_alloc_coherent().  The managed version is called
dmam_alloc_coherent().  It is identical to dma_alloc_coherent() except
for the DMA memory allocated using it is managed and will be
automatically released on driver detach.  Implementation looks like
the following.

  struct dma_devres {
	size_t		size;
	void		*vaddr;
	dma_addr_t	dma_handle;
  };

  static void dmam_coherent_release(struct device *dev, void *res)
  {
	struct dma_devres *this = res;

	dma_free_coherent(dev, this->size, this->vaddr, this->dma_handle);
  }

  dmam_alloc_coherent(dev, size, dma_handle, gfp)
  {
	struct dma_devres *dr;
	void *vaddr;

	dr = devres_alloc(dmam_coherent_release, sizeof(*dr), gfp);
	...

	/* alloc DMA memory as usual */
	vaddr = dma_alloc_coherent(...);
	...

	/* record size, vaddr, dma_handle in dr */
	dr->vaddr = vaddr;
	...

	devres_add(dev, dr);

	return vaddr;
  }

If a driver uses dmam_alloc_coherent(), the area is guaranteed to be
freed whether initialization fails half-way or the device gets
detached.  If most resources are acquired using managed interface, a
driver can have much simpler init and exit code.  Init path basically
looks like the following.

  my_init_one()
  {
	struct mydev *d;

	d = devm_kzalloc(dev, sizeof(*d), GFP_KERNEL);
	if (!d)
		return -ENOMEM;

	d->ring = dmam_alloc_coherent(...);
	if (!d->ring)
		return -ENOMEM;

	if (check something)
		return -EINVAL;
	...

	return register_to_upper_layer(d);
  }

And exit path,

  my_remove_one()
  {
	unregister_from_upper_layer(d);
	shutdown_my_hardware();
  }

As shown above, low level drivers can be simplified a lot by using
devres.  Complexity is shifted from less maintained low level drivers
to better maintained higher layer.  Also, as init failure path is
shared with exit path, both can get more testing.

## Devres group

Devres entries can be grouped using devres group.  When a group is
released, all contained normal devres entries and properly nested
groups are released.  One usage is to rollback series of acquired
resources on failure.  For example,

  if (!devres_open_group(dev, NULL, GFP_KERNEL))
	return -ENOMEM;

  acquire A;
  if (failed)
	goto err;

  acquire B;
  if (failed)
	goto err;
  ...

  devres_remove_group(dev, NULL);

  return 0;

 err:
  devres_release_group(dev, NULL);
  return err_code;

As resource acquision failure usually means probe failure, constructs
like above are usually useful in midlayer driver (e.g. libata core
layer) where interface function shouldn't have side effect on failure.
For LLDs, just returning error code suffices in most cases.

## Details

Lifetime of a devres entry begins on devres allocation and finishes
when it is released or destroyed (removed and freed) - no reference
counting.

devres core guarantees atomicity to all basic devres operations and
has support for single-instance devres types (atomic
lookup-and-add-if-not-found).  Other than that, synchronizing
concurrent accesses to allocated devres data is caller's
responsibility.  This is usually non-issue because bus ops and
resource allocations already do the job.

All devres interface functions can be called without context if the
right gfp mask is given.

## Overhead

Each devres bookkeeping info is allocated together with requested data
area.  With debug option turned off, bookkeeping info occupies 16
bytes on 32bit machines and 24 bytes on 64bit (three pointers rounded
up to ull alignment).  If singly linked list is used, it can be
reduced to two pointers (8 bytes on 32bit, 16 bytes on 64bit).

Each devres group occupies 8 pointers.  It can be reduced to 6 if
singly linked list is used.

Memory space overhead on ahci controller with two ports is between 300
and 400 bytes on 32bit machine after naive conversion (we can
certainly invest a bit more effort into libata core layer).

## Patchset

This patchset contains the following 12 patches and is against the
current 2.6.20-rc2 (3bf8ba38f38d3647368e4edcf7d019f9f8d9184a).

01	: implement devres core
02-06	: implement managed resource interface for IO region, IRQ, DMA,
	  PCI and iomap
07	: libata fix (just included for following patches, please
	  ignore for review)
08	: update libata core layer to use devres
09	: update SATA LLDs to use devres
10	: remove unused parts
11	: implement pcim_iomap_regions()
12	: convert libata to iomap

Patches #01-06 implements devres and #08-12 apply it to libata.  The
libata part is not complete as none of PATA drivers is converted yet,
so, patch #10 will break libata PATA drivers.  Other than that
everything should work.

New config option CONFIG_DEBUG_DEVRES is added.  If set, kernel
parameter devres.log is available.  If set, it will log devres
activities.  This can be turned on/off using sysfs node.

Thanks.  Happy new year.

--
tejun

^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2007-01-08  4:07 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-12-26 15:18 [RFC,PATCHSET] Managed device resources Tejun Heo
2006-12-26 15:18 ` [PATCH 1/12] devres: device resource management core Tejun Heo
2006-12-26 15:18 ` [PATCH 6/12] devres: implement managed iomap interface Tejun Heo
2006-12-26 15:18 ` [PATCH 4/12] devres: implement managed DMA interface Tejun Heo
2006-12-26 15:18 ` [PATCH 7/12] libata: handle pci_enable_device() failure while resuming Tejun Heo
2006-12-26 15:18 ` [PATCH 3/12] devres: implement managed IRQ interface Tejun Heo
2006-12-26 15:18 ` [PATCH 8/12] libata: update libata core layer to use devres Tejun Heo
2006-12-26 15:18 ` [PATCH 2/12] devres: implement managed IO region interface Tejun Heo
2006-12-26 15:18 ` [PATCH 5/12] devres: implement managed PCI interface Tejun Heo
2006-12-26 15:18 ` [PATCH 9/12] libata: update SATA LLDs to use devres Tejun Heo
2006-12-26 15:18 ` [PATCH 11/12] devres: implement pcim_iomap_regions() Tejun Heo
2006-12-26 15:18 ` [PATCH 10/12] libata: remove unused functions Tejun Heo
2006-12-30 21:31 ` [RFC,PATCHSET] Managed device resources Denis Vlasenko
2007-01-04 22:19 ` Greg KH
2007-01-04 22:26   ` Jeff Garzik
2007-01-04 22:32     ` Greg KH
2007-01-08  4:07       ` Tejun Heo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).