From patchwork Sun Aug 23 22:12:23 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Douglas Gilbert X-Patchwork-Id: 257882 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 55420C433E3 for ; Sun, 23 Aug 2020 22:13:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3D65F2067C for ; Sun, 23 Aug 2020 22:13:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727048AbgHWWNe (ORCPT ); Sun, 23 Aug 2020 18:13:34 -0400 Received: from smtp.infotech.no ([82.134.31.41]:54050 "EHLO smtp.infotech.no" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726971AbgHWWNU (ORCPT ); Sun, 23 Aug 2020 18:13:20 -0400 Received: from localhost (localhost [127.0.0.1]) by smtp.infotech.no (Postfix) with ESMTP id CBAD720424C; Mon, 24 Aug 2020 00:13:18 +0200 (CEST) X-Virus-Scanned: by amavisd-new-2.6.6 (20110518) (Debian) at infotech.no Received: from smtp.infotech.no ([127.0.0.1]) by localhost (smtp.infotech.no [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id LTbOA8Wj-b75; Mon, 24 Aug 2020 00:13:17 +0200 (CEST) Received: from xtwo70.bingwo.ca (host-45-78-251-166.dyn.295.ca [45.78.251.166]) by smtp.infotech.no (Postfix) with ESMTPA id 12175204258; Mon, 24 Aug 2020 00:13:15 +0200 (CEST) From: Douglas Gilbert To: linux-scsi@vger.kernel.org Cc: martin.petersen@oracle.com, jejb@linux.vnet.ibm.com, hare@suse.de Subject: [PATCH v10 15/40] sg: rework sg_vma_fault Date: Sun, 23 Aug 2020 18:12:23 -0400 Message-Id: <20200823221248.15678-16-dgilbert@interlog.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200823221248.15678-1-dgilbert@interlog.com> References: <20200823221248.15678-1-dgilbert@interlog.com> MIME-Version: 1.0 Sender: linux-scsi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org Simple refactoring of the sg_vma_fault() function. Reviewed-by: Hannes Reinecke Signed-off-by: Douglas Gilbert --- drivers/scsi/sg.c | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/drivers/scsi/sg.c b/drivers/scsi/sg.c index 7f40e32359f4..0e6fcc41f280 100644 --- a/drivers/scsi/sg.c +++ b/drivers/scsi/sg.c @@ -1429,14 +1429,16 @@ sg_fasync(int fd, struct file *filp, int mode) return fasync_helper(fd, filp, mode, &sfp->async_qp); } +/* Note: the error return: VM_FAULT_SIGBUS causes a "bus error" */ static vm_fault_t sg_vma_fault(struct vm_fault *vmf) { - struct vm_area_struct *vma = vmf->vma; - struct sg_fd *sfp; + int k, length; unsigned long offset, len, sa; + struct vm_area_struct *vma = vmf->vma; struct sg_scatter_hold *rsv_schp; - int k, length; + struct sg_device *sdp; + struct sg_fd *sfp; const char *nbp = "==NULL, bad"; if (!vma) { @@ -1448,20 +1450,31 @@ sg_vma_fault(struct vm_fault *vmf) pr_warn("%s: sfp%s\n", __func__, nbp); goto out_err; } + sdp = sfp->parentdp; + if (sdp && unlikely(SG_IS_DETACHING(sdp))) { + SG_LOG(1, sfp, "%s: device detaching\n", __func__); + goto out_err; + } rsv_schp = &sfp->reserve; offset = vmf->pgoff << PAGE_SHIFT; - if (offset >= rsv_schp->buflen) - return VM_FAULT_SIGBUS; + if (offset >= (unsigned int)rsv_schp->buflen) { + SG_LOG(1, sfp, "%s: offset[%lu] >= rsv.buflen\n", __func__, + offset); + goto out_err; + } sa = vma->vm_start; SG_LOG(3, sfp, "%s: vm_start=0x%lx, off=%lu\n", __func__, sa, offset); length = 1 << (PAGE_SHIFT + rsv_schp->page_order); - for (k = 0; k < rsv_schp->num_sgat && sa < vma->vm_end; k++) { + for (k = 0; k < rsv_schp->num_sgat && sa < vma->vm_end; ++k) { len = vma->vm_end - sa; - len = (len < length) ? len : length; + len = min_t(int, len, (int)length); if (offset < len) { - struct page *page = nth_page(rsv_schp->pages[k], - offset >> PAGE_SHIFT); - get_page(page); /* increment page count */ + struct page *page; + struct page *pp; + + pp = rsv_schp->pages[k]; + page = nth_page(pp, offset >> PAGE_SHIFT); + get_page(page); /* increment page count */ vmf->page = page; return 0; /* success */ }