Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

[SCSI] scsi_host regression: fix scsi host leak

commit 9c7701088a61cc0cf8a6e1c68d1e74e3cc2ee0b7
Author: Dave Young <hidave.darkstar@gmail.com>
Date: Tue Jan 22 14:01:34 2008 +0800

scsi: use class iteration api

Isn't a correct replacement for the original hand rolled host
lookup. The problem is that class_find_child would get a reference to
the host's class device which is never released. Since the host class
device holds a reference to the host gendev, the host can never be
freed.

In 2.6.26 we started using class_find_device, and this function also
gets a reference to the device, so we end up with an extra ref
and the host will not get released.

This patch adds a put_device to balance the class_find_device() get. I
kept the scsi_host_get in scsi_host_lookup, because the target layer
is using scsi_host_lookup and it looks like it needs the SHOST_DEL
check.

Signed-off-by: Mike Christie <michaelc@cs.wisc.edu>
Signed-off-by: James Bottomley <James.Bottomley@HansenPartnership.com>

authored by

Mike Christie and committed by
James Bottomley
3ed78972 d1daeabf

+7 -2
+7 -2
drivers/scsi/hosts.c
··· 456 456 * 457 457 * Return value: 458 458 * A pointer to located Scsi_Host or NULL. 459 + * 460 + * The caller must do a scsi_host_put() to drop the reference 461 + * that scsi_host_get() took. The put_device() below dropped 462 + * the reference from class_find_device(). 459 463 **/ 460 464 struct Scsi_Host *scsi_host_lookup(unsigned short hostnum) 461 465 { ··· 467 463 struct Scsi_Host *shost = ERR_PTR(-ENXIO); 468 464 469 465 cdev = class_find_device(&shost_class, &hostnum, __scsi_host_match); 470 - if (cdev) 466 + if (cdev) { 471 467 shost = scsi_host_get(class_to_shost(cdev)); 472 - 468 + put_device(cdev); 469 + } 473 470 return shost; 474 471 } 475 472 EXPORT_SYMBOL(scsi_host_lookup);