Received: by 10.223.185.116 with SMTP id b49csp1100766wrg; Sat, 3 Mar 2018 15:34:20 -0800 (PST) X-Google-Smtp-Source: AG47ELs4Z7v9NMj9wjtM4xedAVsNtC/rm4bxsK2LQMJUT1R3/4NsGiBfhvBprl848SRlJ4+lvayj X-Received: by 10.167.128.80 with SMTP id y16mr10308246pfm.91.1520120060138; Sat, 03 Mar 2018 15:34:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520120060; cv=none; d=google.com; s=arc-20160816; b=KZNsdrPqpIu5AWvviWwXrv9hX3JOwHRPX2YjUo2BjS8vvZXlceRsA3ojc3//Kel/2U E/zMqviPP+GOTOXRThzJ2XwR4uer9yAwFwCUFY5dFgoL4am1d9h/h5X8VF7rXy3zb1CK MJlLW83fC646IdpkOQOoTn6RWXOl9oo/wlpljBAyCUhUZe9nf25w7/u/OvWCE6RC0C13 mxu68669kMroIxIa09NQp9qg9wDB1WYPgO2uKhMKfVC30761n2YUctsjJuuhAjT5LVVO taUQNEWFxGO+3a8ynVkAeEzjz0QiZ6LhwOfYxJKvSzk+pg9JYjCcaPWw3XQQCkjV/JI8 UpYg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :spamdiagnosticmetadata:spamdiagnosticoutput:content-language :accept-language:in-reply-to:references:message-id:date:thread-index :thread-topic:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=7mk7dNjFm/D+1YKZYgoaT56ARq3RQ8SGQoUlRzpy2jc=; b=PUoBXkax5YdGO/rZ7EACN9392AYpufggNtT+dCB+UUvpbltWTw47x4dXSlJ374ZaQj ypzP6HwIWtvrvsKNj8r/Yq2/O9BQeiJO0NiRqbu3m3KXQYEFO/AUlZ6osyq0TgrMdeWC k4QH8dgn+3eU9xI5VUco2r5XwgIdc9FIClsEjXu4jXrVZj5e4F/5SX3I/lqkKeoKx6Qa 3MAawIeqdOak//Pk/UnGtQD/FMRfrLH4UCYoC14uzi4iuFxyMszQIhVqIUVVDrt0gDu2 sAkZJC+VAVI5FdC+sfny2GAmlIyR88LSPjtZddTZd97sM/plI0MXji9XIqAqXecsV8jL onUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=XCF9mEnd; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t13si7376286pfa.294.2018.03.03.15.34.06; Sat, 03 Mar 2018 15:34:20 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=XCF9mEnd; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934496AbeCCWgC (ORCPT + 99 others); Sat, 3 Mar 2018 17:36:02 -0500 Received: from mail-bn3nam01on0090.outbound.protection.outlook.com ([104.47.33.90]:43384 "EHLO NAM01-BN3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S933950AbeCCWfx (ORCPT ); Sat, 3 Mar 2018 17:35:53 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=7mk7dNjFm/D+1YKZYgoaT56ARq3RQ8SGQoUlRzpy2jc=; b=XCF9mEndb9BEzuS6A7tFN7+PEimbwIVJTUYRBz+gj4Z95A16LpwAgSXkZ9o9zqsiUcRsGcLRC3Jf5xU6yDYoKuL0t79/JbDKZnSdSc8UxKaf4WT6PM0hOG/IRMnV/vWc1hp7XJfD4xQY8rUIwabRRY5Xf80sFY1azWcVGI5YURg= Received: from MW2PR2101MB1034.namprd21.prod.outlook.com (52.132.149.10) by MW2PR2101MB1034.namprd21.prod.outlook.com (52.132.149.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.567.3; Sat, 3 Mar 2018 22:35:43 +0000 Received: from MW2PR2101MB1034.namprd21.prod.outlook.com ([fe80::1d56:338f:e2b:cec0]) by MW2PR2101MB1034.namprd21.prod.outlook.com ([fe80::1d56:338f:e2b:cec0%3]) with mapi id 15.20.0567.006; Sat, 3 Mar 2018 22:35:43 +0000 From: Sasha Levin To: "linux-kernel@vger.kernel.org" , "stable@vger.kernel.org" CC: Hannes Reinecke , Hannes Reinecke , "Martin K . Petersen" , Sasha Levin Subject: [PATCH AUTOSEL for 4.9 157/219] scsi: sg: close race condition in sg_remove_sfp_usercontext() Thread-Topic: [PATCH AUTOSEL for 4.9 157/219] scsi: sg: close race condition in sg_remove_sfp_usercontext() Thread-Index: AQHTsz8XzS/2fURCLUeKAmp1feheug== Date: Sat, 3 Mar 2018 22:29:28 +0000 Message-ID: <20180303222716.26640-157-alexander.levin@microsoft.com> References: <20180303222716.26640-1-alexander.levin@microsoft.com> In-Reply-To: <20180303222716.26640-1-alexander.levin@microsoft.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [52.168.54.252] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1;MW2PR2101MB1034;7:lbpMI+ed9NWDJCx8D+zyMPhjUkPdLcnbdsirv5grHeBAYQUgvBsGOEAcBy7XKk3F/LN7OK+X/K/xtONJh+tYrkIRgfwU8dxUIkUXhWDknqCYrCC/YQ+O/pyyd6iMcHAYhQ0DBJLrLFgYiJ+BVZGrJHmCCBopRHN86l27FOvVEe+hYpq9//lyEk9XTqaZPI7Y7JSP+lDf3fo0ALhbS7Jw/bN1OF0RCLX4EVn3DoQafxVkqo6zqd/yKTcM8C4jeNkN x-ms-office365-filtering-ht: Tenant x-ms-office365-filtering-correlation-id: 8eddea73-c341-44f8-c458-08d58157197e x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:(7020095)(4652020)(4534165)(4627221)(201703031133081)(201702281549075)(48565401081)(5600026)(4604075)(3008032)(2017052603307)(7193020);SRVR:MW2PR2101MB1034; x-ms-traffictypediagnostic: MW2PR2101MB1034: authentication-results: spf=none (sender IP is ) smtp.mailfrom=Alexander.Levin@microsoft.com; x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(28532068793085)(89211679590171)(146099531331640); x-exchange-antispam-report-cfa-test: BCL:0;PCL:0;RULEID:(8211001083)(61425038)(6040501)(2401047)(5005006)(8121501046)(3231220)(944501244)(52105095)(93006095)(93001095)(10201501046)(3002001)(6055026)(61426038)(61427038)(6041288)(20161123562045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123564045)(20161123558120)(20161123560045)(6072148)(201708071742011);SRVR:MW2PR2101MB1034;BCL:0;PCL:0;RULEID:;SRVR:MW2PR2101MB1034; x-forefront-prvs: 0600F93FE1 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(376002)(346002)(39860400002)(39380400002)(366004)(396003)(199004)(189003)(99286004)(22452003)(72206003)(316002)(3846002)(6116002)(186003)(26005)(478600001)(76176011)(54906003)(110136005)(102836004)(97736004)(1076002)(2501003)(3660700001)(66066001)(5250100002)(5660300001)(86362001)(10090500001)(10290500003)(86612001)(6506007)(68736007)(575784001)(106356001)(25786009)(8676002)(4326008)(59450400001)(6512007)(305945005)(2906002)(14454004)(107886003)(8936002)(7736002)(81166006)(3280700002)(53936002)(105586002)(81156014)(6666003)(6486002)(36756003)(2900100001)(6436002)(2950100002)(22906009)(217873001);DIR:OUT;SFP:1102;SCL:1;SRVR:MW2PR2101MB1034;H:MW2PR2101MB1034.namprd21.prod.outlook.com;FPR:;SPF:None;PTR:InfoNoRecords;A:1;MX:1;LANG:en; received-spf: None (protection.outlook.com: microsoft.com does not designate permitted sender hosts) x-microsoft-antispam-message-info: CFHv1XFZkZIvJXWTkVemqielqrbWCEDkxIZMzRBjifccRMx6Y9gEAuSgS8QB6uXOZgYUxUsCQjvRwnxXbjm5T5yZbmOsPhLgDbX3CUZBH0+aU4PH5xM+EopewMwZ8/9XMb46q847vFQy+vzGuefLFyhJTWxJmImEq+tOT5IIrxA= spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: microsoft.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8eddea73-c341-44f8-c458-08d58157197e X-MS-Exchange-CrossTenant-originalarrivaltime: 03 Mar 2018 22:29:28.1038 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW2PR2101MB1034 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Hannes Reinecke [ Upstream commit 97d27b0dd015e980ade63fda111fd1353276e28b ] sg_remove_sfp_usercontext() is clearing any sg requests, but needs to take 'rq_list_lock' when modifying the list. Reported-by: Christoph Hellwig Signed-off-by: Hannes Reinecke Reviewed-by: Johannes Thumshirn Tested-by: Johannes Thumshirn Reviewed-by: Christoph Hellwig Signed-off-by: Martin K. Petersen Signed-off-by: Sasha Levin --- drivers/scsi/sg.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/drivers/scsi/sg.c b/drivers/scsi/sg.c index 6b6064dc3467..e6333cae8550 100644 --- a/drivers/scsi/sg.c +++ b/drivers/scsi/sg.c @@ -524,6 +524,7 @@ sg_read(struct file *filp, char __user *buf, size_t cou= nt, loff_t * ppos) } else count =3D (old_hdr->result =3D=3D 0) ? 0 : -EIO; sg_finish_rem_req(srp); + sg_remove_request(sfp, srp); retval =3D count; free_old_hdr: kfree(old_hdr); @@ -564,6 +565,7 @@ sg_new_read(Sg_fd * sfp, char __user *buf, size_t count= , Sg_request * srp) } err_out: err2 =3D sg_finish_rem_req(srp); + sg_remove_request(sfp, srp); return err ? : err2 ? : count; } =20 @@ -800,6 +802,7 @@ sg_common_write(Sg_fd * sfp, Sg_request * srp, SCSI_LOG_TIMEOUT(1, sg_printk(KERN_INFO, sfp->parentdp, "sg_common_write: start_req err=3D%d\n", k)); sg_finish_rem_req(srp); + sg_remove_request(sfp, srp); return k; /* probably out of space --> ENOMEM */ } if (atomic_read(&sdp->detaching)) { @@ -812,6 +815,7 @@ sg_common_write(Sg_fd * sfp, Sg_request * srp, } =20 sg_finish_rem_req(srp); + sg_remove_request(sfp, srp); return -ENODEV; } =20 @@ -1302,6 +1306,7 @@ sg_rq_end_io_usercontext(struct work_struct *work) struct sg_fd *sfp =3D srp->parentfp; =20 sg_finish_rem_req(srp); + sg_remove_request(sfp, srp); kref_put(&sfp->f_ref, sg_remove_sfp); } =20 @@ -1846,8 +1851,6 @@ sg_finish_rem_req(Sg_request *srp) else sg_remove_scat(sfp, req_schp); =20 - sg_remove_request(sfp, srp); - return ret; } =20 @@ -2194,12 +2197,17 @@ sg_remove_sfp_usercontext(struct work_struct *work) struct sg_fd *sfp =3D container_of(work, struct sg_fd, ew.work); struct sg_device *sdp =3D sfp->parentdp; Sg_request *srp; + unsigned long iflags; =20 /* Cleanup any responses which were never read(). */ + write_lock_irqsave(&sfp->rq_list_lock, iflags); while (!list_empty(&sfp->rq_list)) { srp =3D list_first_entry(&sfp->rq_list, Sg_request, entry); sg_finish_rem_req(srp); + list_del(&srp->entry); + srp->parentfp =3D NULL; } + write_unlock_irqrestore(&sfp->rq_list_lock, iflags); =20 if (sfp->reserve.bufflen > 0) { SCSI_LOG_TIMEOUT(6, sg_printk(KERN_INFO, sdp, --=20 2.14.1