Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752595AbbGUHGw (ORCPT ); Tue, 21 Jul 2015 03:06:52 -0400 Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:41047 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750771AbbGUHGu (ORCPT ); Tue, 21 Jul 2015 03:06:50 -0400 Date: Tue, 21 Jul 2015 00:06:20 -0700 From: Calvin Owens To: Christoph Hellwig CC: Nagalakshmi Nandigama , Praveen Krishnamoorthy , Sreekanth Reddy , Abhijit Mahajan , , , , , Bart Van Assche Subject: Re: [PATCH 1/2] mpt2sas: Refcount sas_device objects and fix unsafe list usage Message-ID: <20150721070620.GB1353000@mail.thefacebook.com> References: <1433821856-2815280-1-git-send-email-calvinowens@fb.com> <1436675096-324527-1-git-send-email-calvinowens@fb.com> <1436675096-324527-2-git-send-email-calvinowens@fb.com> <20150713065206.GC31842@infradead.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Disposition: inline In-Reply-To: <20150713065206.GC31842@infradead.org> User-Agent: Mutt/1.5.20 (2009-12-10) X-Originating-IP: [192.168.52.123] X-Proofpoint-Spam-Reason: safe X-FB-Internal: Safe X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:5.14.151,1.0.33,0.0.0000 definitions=2015-07-21_02:2015-07-20,2015-07-21,1970-01-01 signatures=0 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3799 Lines: 107 On Sunday 07/12 at 23:52 -0700, Christoph Hellwig wrote: > On Sat, Jul 11, 2015 at 09:24:55PM -0700, Calvin Owens wrote: > > These objects can be referenced concurrently throughout the driver, we > > need a way to make sure threads can't delete them out from under each > > other. This patch adds the refcount, and refactors the code to use it. > > > > Additionally, we cannot iterate over the sas_device_list without > > holding the lock, or we risk corrupting random memory if items are > > added or deleted as we iterate. This patch refactors _scsih_probe_sas() > > to use the sas_device_list in a safe way. > > > > Cc: Christoph Hellwig > > Cc: Bart Van Assche > > Signed-off-by: Calvin Owens > > --- > > drivers/scsi/mpt2sas/mpt2sas_base.h | 22 +- > > drivers/scsi/mpt2sas/mpt2sas_scsih.c | 434 ++++++++++++++++++++----------- > > drivers/scsi/mpt2sas/mpt2sas_transport.c | 12 +- > > 3 files changed, 315 insertions(+), 153 deletions(-) > > > > diff --git a/drivers/scsi/mpt2sas/mpt2sas_base.h b/drivers/scsi/mpt2sas/mpt2sas_base.h > > index caff8d1..78f41ac 100644 > > --- a/drivers/scsi/mpt2sas/mpt2sas_base.h > > +++ b/drivers/scsi/mpt2sas/mpt2sas_base.h > > @@ -238,6 +238,7 @@ > > * @flags: MPT_TARGET_FLAGS_XXX flags > > * @deleted: target flaged for deletion > > * @tm_busy: target is busy with TM request. > > + * @sdev: The sas_device associated with this target > > */ > > struct MPT2SAS_TARGET { > > struct scsi_target *starget; > > @@ -248,6 +249,7 @@ struct MPT2SAS_TARGET { > > u32 flags; > > u8 deleted; > > u8 tm_busy; > > + struct _sas_device *sdev; > > }; > > > > > > @@ -376,8 +378,24 @@ struct _sas_device { > > u8 phy; > > u8 responding; > > u8 pfa_led_on; > > + struct kref refcount; > > }; > > > > +static inline void sas_device_get(struct _sas_device *s) > > +{ > > + kref_get(&s->refcount); > > +} > > + > > +static inline void sas_device_free(struct kref *r) > > +{ > > + kfree(container_of(r, struct _sas_device, refcount)); > > +} > > + > > +static inline void sas_device_put(struct _sas_device *s) > > +{ > > + kref_put(&s->refcount, sas_device_free); > > +} > > + > > /** > > * struct _raid_device - raid volume link list > > * @list: sas device list > > @@ -1095,7 +1113,9 @@ struct _sas_node *mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER * > > u16 handle); > > struct _sas_node *mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER > > *ioc, u64 sas_address); > > -struct _sas_device *mpt2sas_scsih_sas_device_find_by_sas_address( > > +struct _sas_device *mpt2sas_get_sdev_by_addr( > > + struct MPT2SAS_ADAPTER *ioc, u64 sas_address); > > +struct _sas_device *__mpt2sas_get_sdev_by_addr( > > struct MPT2SAS_ADAPTER *ioc, u64 sas_address); > > > > void mpt2sas_port_enable_complete(struct MPT2SAS_ADAPTER *ioc); > > diff --git a/drivers/scsi/mpt2sas/mpt2sas_scsih.c b/drivers/scsi/mpt2sas/mpt2sas_scsih.c > > index 3f26147..fad80ce 100644 > > --- a/drivers/scsi/mpt2sas/mpt2sas_scsih.c > > +++ b/drivers/scsi/mpt2sas/mpt2sas_scsih.c > > @@ -526,8 +526,43 @@ _scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc, > > } > > } > > > > +struct _sas_device * > > +__mpt2sas_get_sdev_from_target(struct MPT2SAS_TARGET *tgt_priv) > > +{ > > + struct _sas_device *ret; > > + > > Does this need a: > > assert_spin_locked(&ioc->sas_device_lock); > > ? Yeah: I'll add that. Thanks very much, Calvin > Otherwise this looks sensible to me. -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/