Received: by 10.223.185.116 with SMTP id b49csp1084611wrg; Sat, 3 Mar 2018 15:07:18 -0800 (PST) X-Google-Smtp-Source: AG47ELu+xONBNNNSdMkdw332KnvgMPYd3MJ0pHyo61NoFXrezlaoS+k5fxCd+uazGAU0Ldb2Iy+d X-Received: by 10.99.147.91 with SMTP id w27mr8472321pgm.208.1520118437957; Sat, 03 Mar 2018 15:07:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520118437; cv=none; d=google.com; s=arc-20160816; b=kRSIZ1rIZdD6ajm1qWhI6uDfX7E/wED4aX1gPdwq3GcFTvFKIcvQZjfJBYXOtqcUPV UENNL6ZUbI2R9qa+8x1gdVqc6X1CFmQ1O0qPBXXKzlocPORZQS89mDGgxv4bHNel3E3d 2y64LT+4K3TgtWkGJQqiArUZpi8kXNZVrEbXf4FnQlzSffBxp3PTgiHR+Z5LG6kfFFAe JvGgFDpe/vLKsLpjyDyqf7dwWICRMu4DeI05DIuWh+5QciRZgntR2AXN94ppJxX5hSrr 1zHBGWeyR96ZWO1fPFPEnctn35C5Lfr5+5pJH4SZ5yp2yQR+9hUP4r8QjgyOPwaxlK/D 6Bag== 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=An1r8BTK0ckusMnCGRiBRst39xfW7jSS/ooViELnpkE=; b=0BRrT0zcS+hx2qQXMqA06ZWU5uKuNcSPUBzWqqFdiK8XbKDCS9HnWWKKkUqckvNP6t C3WiIO3gBMytvyIc+kAqch1mdwPqCYIn3bC3d+uqbNK6WJuIfzhHabJrYYc8h+5RfjQJ W97XeCkGd1UAm7q/rQ6fUUsMe7J9QnexoYHEOk4kiXqaybIYOlNofcBHGOlsddze7WAI m9AqOuJLQYP0xDSksA8XCH1hK5+6xoBOENyldzOCYN1WuSNZsfUm480EO/UY8B+RBuOH tnzHM9Lc2Pq7LiCThMxLKZzwaDmUPfy9+FVEYYsyIEru4t/ugl3/CtV1b09zjMN1PezZ vb1w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=OzkH/paL; 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 i70si6050518pgc.148.2018.03.03.15.07.03; Sat, 03 Mar 2018 15:07:17 -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=OzkH/paL; 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 S935239AbeCCXGW (ORCPT + 99 others); Sat, 3 Mar 2018 18:06:22 -0500 Received: from mail-bl2nam02on0129.outbound.protection.outlook.com ([104.47.38.129]:62880 "EHLO NAM02-BL2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S935185AbeCCWkL (ORCPT ); Sat, 3 Mar 2018 17:40:11 -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=An1r8BTK0ckusMnCGRiBRst39xfW7jSS/ooViELnpkE=; b=OzkH/paLfidkT+fh2YHjM1UwlTovIyAv1R5WgJlkvnYOd4ruO/vNKMuWBgOS9SlPC+pJBMWVi4ig+/yPtscSEkh/EsoZpx/MZ/c0T80+zTCKf6TWeliCexkHwojYncoUCOBctBEeSGFk7phixMAMcnztMVMx75HwIS6h5liztz4= Received: from MW2PR2101MB1034.namprd21.prod.outlook.com (52.132.149.10) by MW2PR2101MB1033.namprd21.prod.outlook.com (52.132.146.32) 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:40:06 +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:40:06 +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.4 079/115] scsi: sg: close race condition in sg_remove_sfp_usercontext() Thread-Topic: [PATCH AUTOSEL for 4.4 079/115] scsi: sg: close race condition in sg_remove_sfp_usercontext() Thread-Index: AQHTsz9jHoKusSIcEEuhvZfBeIKuhQ== Date: Sat, 3 Mar 2018 22:31:36 +0000 Message-ID: <20180303223010.27106-79-alexander.levin@microsoft.com> References: <20180303223010.27106-1-alexander.levin@microsoft.com> In-Reply-To: <20180303223010.27106-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;MW2PR2101MB1033;7:PdjQJuRc25cJOaJLI73DRoUFPD76w4JbUAxPLjVQe7TQuRo5yLpIEn9BSusoU/DcyXc4DwgN+gTlU55YcfWbyn7EwgqvaEuoBLe5GkdazouVebh3sRIKqZtR8Mvd+KgR4j7ZmkZEyb63+slaQd0qts7PPIr2vTghRwfGvWGQgql0iDN9wzNAncJxXToy08IuoXnrn/QWz5WyNsxNDfNh4g4PT/JiM4X2tIMtQiApZq+7JsOx3uvQ3PyeFNIb/lJg x-ms-office365-filtering-ht: Tenant x-ms-office365-filtering-correlation-id: 2242a27a-afcf-4c56-e6b0-08d58157b66c x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:(7020095)(4652020)(4534165)(4627221)(201703031133081)(201702281549075)(48565401081)(5600026)(4604075)(3008032)(2017052603307)(7193020);SRVR:MW2PR2101MB1033; x-ms-traffictypediagnostic: MW2PR2101MB1033: 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)(3002001)(10201501046)(93006095)(93001095)(3231220)(944501244)(52105095)(6055026)(61426038)(61427038)(6041288)(20161123558120)(20161123564045)(20161123562045)(20161123560045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(6072148)(201708071742011);SRVR:MW2PR2101MB1033;BCL:0;PCL:0;RULEID:;SRVR:MW2PR2101MB1033; x-forefront-prvs: 0600F93FE1 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(39860400002)(39380400002)(376002)(366004)(346002)(396003)(189003)(199004)(3280700002)(3846002)(316002)(6666003)(99286004)(110136005)(2900100001)(54906003)(2501003)(10090500001)(53936002)(5250100002)(106356001)(76176011)(36756003)(107886003)(2950100002)(6486002)(22452003)(6512007)(6436002)(575784001)(86362001)(6116002)(1076002)(6506007)(2906002)(68736007)(102836004)(4326008)(59450400001)(97736004)(186003)(478600001)(72206003)(26005)(3660700001)(8676002)(25786009)(66066001)(14454004)(5660300001)(305945005)(86612001)(81166006)(8936002)(10290500003)(105586002)(7736002)(81156014)(22906009)(217873001);DIR:OUT;SFP:1102;SCL:1;SRVR:MW2PR2101MB1033;H:MW2PR2101MB1034.namprd21.prod.outlook.com;FPR:;SPF:None;PTR:InfoNoRecords;MX:1;A:1;LANG:en; received-spf: None (protection.outlook.com: microsoft.com does not designate permitted sender hosts) x-microsoft-antispam-message-info: lgINJ9TF7B8WcaJvIdHs6HUMo/x7hs3mcHu+EjkqClDvwxmVnMKjIexzBzdgP7tFJ/Bkwnk0fi90siGuKzNqFPX1QWbbspsWTAHuUUKOISAnBggS9q+iMhg8y8WbnjSML9LqoYJVA2nPJH8Z7Sh6ggMg5na5VFBFqTJ+joGC11o= 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: 2242a27a-afcf-4c56-e6b0-08d58157b66c X-MS-Exchange-CrossTenant-originalarrivaltime: 03 Mar 2018 22:31:36.1520 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW2PR2101MB1033 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 27f92f4ea6bc..dedaf3e89bb5 100644 --- a/drivers/scsi/sg.c +++ b/drivers/scsi/sg.c @@ -535,6 +535,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); @@ -575,6 +576,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 @@ -811,6 +813,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)) { @@ -823,6 +826,7 @@ sg_common_write(Sg_fd * sfp, Sg_request * srp, } =20 sg_finish_rem_req(srp); + sg_remove_request(sfp, srp); return -ENODEV; } =20 @@ -1312,6 +1316,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 @@ -1856,8 +1861,6 @@ sg_finish_rem_req(Sg_request *srp) else sg_remove_scat(sfp, req_schp); =20 - sg_remove_request(sfp, srp); - return ret; } =20 @@ -2204,12 +2207,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