From mboxrd@z Thu Jan 1 00:00:00 1970 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=-9.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,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 B3C11ECE59F for ; Tue, 8 Oct 2019 07:50:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 880552053B for ; Tue, 8 Oct 2019 07:50:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730455AbfJHHuf (ORCPT ); Tue, 8 Oct 2019 03:50:35 -0400 Received: from smtp.infotech.no ([82.134.31.41]:34862 "EHLO smtp.infotech.no" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730430AbfJHHu3 (ORCPT ); Tue, 8 Oct 2019 03:50:29 -0400 Received: from localhost (localhost [127.0.0.1]) by smtp.infotech.no (Postfix) with ESMTP id 48C0B204269; Tue, 8 Oct 2019 09:50:24 +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 HasRdlmDCVsT; Tue, 8 Oct 2019 09:50:24 +0200 (CEST) Received: from xtwo70.bingwo.ca (unknown [82.134.31.172]) by smtp.infotech.no (Postfix) with ESMTPA id B3AF5204154; Tue, 8 Oct 2019 09:50:23 +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 v5 17/23] sg: rework sg_mmap Date: Tue, 8 Oct 2019 09:50:16 +0200 Message-Id: <20191008075022.30055-18-dgilbert@interlog.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191008075022.30055-1-dgilbert@interlog.com> References: <20191008075022.30055-1-dgilbert@interlog.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-scsi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org Simple rework of the sg_mmap() function. Signed-off-by: Douglas Gilbert --- drivers/scsi/sg.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/drivers/scsi/sg.c b/drivers/scsi/sg.c index befcbfbcece1..2ad86aaaf74d 100644 --- a/drivers/scsi/sg.c +++ b/drivers/scsi/sg.c @@ -1449,14 +1449,15 @@ static const struct vm_operations_struct sg_mmap_vm_ops = { .fault = sg_vma_fault, }; +/* Entry point for mmap(2) system call */ static int sg_mmap(struct file *filp, struct vm_area_struct *vma) { - struct sg_fd *sfp; - unsigned long req_sz, len, sa; - struct sg_scatter_hold *rsv_schp; int k, length; int ret = 0; + unsigned long req_sz, len, sa; + struct sg_scatter_hold *rsv_schp; + struct sg_fd *sfp; if (!filp || !vma) return -ENXIO; @@ -1469,19 +1470,23 @@ sg_mmap(struct file *filp, struct vm_area_struct *vma) SG_LOG(3, sfp, "%s: vm_start=%p, len=%d\n", __func__, (void *)vma->vm_start, (int)req_sz); if (vma->vm_pgoff) - return -EINVAL; /* want no offset */ - rsv_schp = &sfp->reserve; + return -EINVAL; /* only an offset of 0 accepted */ + /* Check reserve request is inactive and has large enough buffer */ mutex_lock(&sfp->f_mutex); - if (req_sz > rsv_schp->buflen) { - ret = -ENOMEM; /* cannot map more than reserved buffer */ + if (sfp->res_in_use) { + ret = -EBUSY; + goto out; + } + rsv_schp = &sfp->reserve; + if (req_sz > (unsigned long)rsv_schp->buflen) { + ret = -ENOMEM; goto out; } - sa = vma->vm_start; 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(unsigned long, len, (unsigned long)length); sa += len; } -- 2.23.0