From patchwork Fri May 15 03:42:02 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Calvin Owens X-Patchwork-Id: 6411041 Return-Path: X-Original-To: patchwork-linux-scsi@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id ED8CFC0432 for ; Fri, 15 May 2015 03:44:26 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 1E6A120306 for ; Fri, 15 May 2015 03:44:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 35E1C20303 for ; Fri, 15 May 2015 03:44:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1945922AbbEODoK (ORCPT ); Thu, 14 May 2015 23:44:10 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:30945 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933937AbbEODmq (ORCPT ); Thu, 14 May 2015 23:42:46 -0400 Received: from pps.filterd (m0004077 [127.0.0.1]) by mx0b-00082601.pphosted.com (8.14.5/8.14.5) with SMTP id t4F3bSLq011715; Thu, 14 May 2015 20:42:44 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-type; s=facebook; bh=l+SpTdnj60BowBMRjKKYNnpsjhVQrdIJvubkUnu2u2M=; b=Q97LpZKRLL1L0B6tDJaK2/9MjSaaNSOkEJbKOlez6Y+Hx8uFxnA0q+9GOaUfaCAOPAAo uGyxSJiqtWq6LTWuPi95dgxKsAlZxODwmR1lkR8BcreQL2sg2ZjzGWfo4bpG6Fo+5tQd ZstolBwcAnwu5O8o5UBftnTnsxVJe1P+3Tc= Received: from mail.thefacebook.com ([199.201.64.23]) by mx0b-00082601.pphosted.com with ESMTP id 1ud79tr7t7-6 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT); Thu, 14 May 2015 20:42:44 -0700 Received: from devbig030.prn2.facebook.com (192.168.52.13) by PRN-CHUB04.TheFacebook.com (192.168.16.14) with Microsoft SMTP Server id 14.3.195.1; Thu, 14 May 2015 20:42:37 -0700 From: Calvin Owens To: Nagalakshmi Nandigama , Praveen Krishnamoorthy , Sreekanth Reddy , Abhijit Mahajan CC: , , , , Subject: [PATCH 6/6] Fix unsafe fw_event_list usage Date: Thu, 14 May 2015 20:42:02 -0700 Message-ID: <1431661322-3097935-7-git-send-email-calvinowens@fb.com> X-Mailer: git-send-email 1.8.1 In-Reply-To: <1431661322-3097935-1-git-send-email-calvinowens@fb.com> References: <20150506184852.GA1007288@mail.thefacebook.com> <1431661322-3097935-1-git-send-email-calvinowens@fb.com> MIME-Version: 1.0 X-Originating-IP: [192.168.52.13] 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-05-15_01:2015-05-14, 2015-05-15, 1970-01-01 signatures=0 Sender: linux-scsi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID,T_RP_MATCHES_RCVD,UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Since the fw_event deletes itself from the list, cleanup_queue() can walk onto garbage pointers or walk off into freed memory. This refactors the code in _scsih_fw_event_cleanup_queue() to not iterate over the fw_event_list without a lock. Signed-off-by: Calvin Owens --- drivers/scsi/mpt2sas/mpt2sas_scsih.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/drivers/scsi/mpt2sas/mpt2sas_scsih.c b/drivers/scsi/mpt2sas/mpt2sas_scsih.c index 8d8c814..f504e28 100644 --- a/drivers/scsi/mpt2sas/mpt2sas_scsih.c +++ b/drivers/scsi/mpt2sas/mpt2sas_scsih.c @@ -2939,6 +2939,23 @@ mpt2sas_port_enable_complete(struct MPT2SAS_ADAPTER *ioc) fw_event_work_put(fw_event); } +static struct fw_event_work *dequeue_next_fw_event(struct MPT2SAS_ADAPTER *ioc) +{ + unsigned long flags; + struct fw_event_work *fw_event = NULL; + + spin_lock_irqsave(&ioc->fw_event_lock, flags); + if (!list_empty(&ioc->fw_event_list)) { + fw_event = list_first_entry(&ioc->fw_event_list, + struct fw_event_work, list); + list_del_init(&fw_event->list); + fw_event_work_get(fw_event); + } + spin_unlock_irqrestore(&ioc->fw_event_lock, flags); + + return fw_event; +} + /** * _scsih_fw_event_cleanup_queue - cleanup event queue * @ioc: per adapter object @@ -2951,17 +2968,18 @@ mpt2sas_port_enable_complete(struct MPT2SAS_ADAPTER *ioc) static void _scsih_fw_event_cleanup_queue(struct MPT2SAS_ADAPTER *ioc) { - struct fw_event_work *fw_event, *next; + struct fw_event_work *fw_event; if (list_empty(&ioc->fw_event_list) || !ioc->firmware_event_thread || in_interrupt()) return; - list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) { + while ((fw_event = dequeue_next_fw_event(ioc))) { if (cancel_delayed_work_sync(&fw_event->delayed_work)) { _scsih_fw_event_free(ioc, fw_event); continue; } + fw_event_work_put(fw_event); } }