Received: by 10.213.65.68 with SMTP id h4csp37994imn; Mon, 19 Mar 2018 18:47:04 -0700 (PDT) X-Google-Smtp-Source: AG47ELsY9ilMJwN8ZEADLBNP4YhFHFkjVd5btG1xD6N+s6I/bQ7wWtT+fMZE6JS/dv76rR5j9e/9 X-Received: by 10.99.121.77 with SMTP id u74mr5935098pgc.89.1521510424704; Mon, 19 Mar 2018 18:47:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521510424; cv=none; d=google.com; s=arc-20160816; b=Be3AUoWZQf3MtWmjMwDU/I4O4D5+OQXF7yQBtWrozJ223ouR0iP6/o8B9GRsxDD28+ gXamsgwBoDLPgckCyJzC6oCwTGhSv2AszGymkfIb8BlSeKSnyQrXzZJfi2q8tmcyzY68 dudYdp/mem345N1iM6pxqhVqfBflz+EWbALcoX0Fo3YREmaJPtf/uvdehvwATMY4fFX0 QTrSjrInc04gYtkBGNV7ywVPLqNYABJdtB04T9OAkatsB71gxXdo3nJfqMMtjt7O4fj6 oVPp201JEuSLXAdY3YKDAJQ0BvcIX/tSZe8xv76mwww7EizspwUktc0izSIOGwMw7DWM kKJg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=92UALv014qQnNjdqmR4he8Ddh2p3B+azxMiDH3KvDik=; b=QdZQnPR5FfXAegieAw2cq8mRE7DlwsKPRIGO0rVS3ddlQOsgVUeEvq24rYBqVDuveJ q5tqII4rlr83Zmumw+3qF0MTl/160E+ZxB1g89dbTKH2/+At+z02b0U6wQk02hBEpI4m ddjgw/E8kz2HRipPxRswqcpC5ZxtbD/pI2sHsZ6FM4xX+vhUZVGrF58qccsZ/Uqk4oAd U9t1i5KWrC2OdTEzjVnGZ8y+KZvBr5Ppj6opgrUMSQx3WZwP2WmI2A7ulzl8SE7II0it pNoz4XhykfCD9qnufFpCsnv35FQ2fsemvvlzIBUFrrtwC4l9IgAEoiwE3fxNAoP7PTLN UL4g== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d67si448106pfb.232.2018.03.19.18.46.51; Mon, 19 Mar 2018 18:47:04 -0700 (PDT) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S935893AbeCSTYt (ORCPT + 99 others); Mon, 19 Mar 2018 15:24:49 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:49998 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1031004AbeCSSYg (ORCPT ); Mon, 19 Mar 2018 14:24:36 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id CD16AD09; Mon, 19 Mar 2018 18:24:35 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Christoph Hellwig , Hannes Reinecke , Johannes Thumshirn , "Martin K. Petersen" , Sasha Levin Subject: [PATCH 4.9 150/241] scsi: sg: close race condition in sg_remove_sfp_usercontext() Date: Mon, 19 Mar 2018 19:06:55 +0100 Message-Id: <20180319180757.394220388@linuxfoundation.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180319180751.172155436@linuxfoundation.org> References: <20180319180751.172155436@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.9-stable review patch. If anyone has any objections, please let me know. ------------------ 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 Signed-off-by: Greg Kroah-Hartman --- drivers/scsi/sg.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) --- a/drivers/scsi/sg.c +++ b/drivers/scsi/sg.c @@ -524,6 +524,7 @@ sg_read(struct file *filp, char __user * } else count = (old_hdr->result == 0) ? 0 : -EIO; sg_finish_rem_req(srp); + sg_remove_request(sfp, srp); retval = count; free_old_hdr: kfree(old_hdr); @@ -564,6 +565,7 @@ sg_new_read(Sg_fd * sfp, char __user *bu } err_out: err2 = sg_finish_rem_req(srp); + sg_remove_request(sfp, srp); return err ? : err2 ? : count; } @@ -800,6 +802,7 @@ sg_common_write(Sg_fd * sfp, Sg_request SCSI_LOG_TIMEOUT(1, sg_printk(KERN_INFO, sfp->parentdp, "sg_common_write: start_req err=%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 } sg_finish_rem_req(srp); + sg_remove_request(sfp, srp); return -ENODEV; } @@ -1302,6 +1306,7 @@ sg_rq_end_io_usercontext(struct work_str struct sg_fd *sfp = srp->parentfp; sg_finish_rem_req(srp); + sg_remove_request(sfp, srp); kref_put(&sfp->f_ref, sg_remove_sfp); } @@ -1846,8 +1851,6 @@ sg_finish_rem_req(Sg_request *srp) else sg_remove_scat(sfp, req_schp); - sg_remove_request(sfp, srp); - return ret; } @@ -2194,12 +2197,17 @@ sg_remove_sfp_usercontext(struct work_st struct sg_fd *sfp = container_of(work, struct sg_fd, ew.work); struct sg_device *sdp = sfp->parentdp; Sg_request *srp; + unsigned long iflags; /* Cleanup any responses which were never read(). */ + write_lock_irqsave(&sfp->rq_list_lock, iflags); while (!list_empty(&sfp->rq_list)) { srp = list_first_entry(&sfp->rq_list, Sg_request, entry); sg_finish_rem_req(srp); + list_del(&srp->entry); + srp->parentfp = NULL; } + write_unlock_irqrestore(&sfp->rq_list_lock, iflags); if (sfp->reserve.bufflen > 0) { SCSI_LOG_TIMEOUT(6, sg_printk(KERN_INFO, sdp,