From patchwork Tue Oct 27 13:48:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 306923 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,DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, 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 625FAC388F9 for ; Tue, 27 Oct 2020 18:15:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 151B3207E8 for ; Tue, 27 Oct 2020 18:15:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603822516; bh=2J6mySmFY3XBdGmC07zXWF/0AmGr+PLeMDMLR5N7YJ8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=Y6IZWhnJBQNTnf/B3/UjkJ//0UX8Ze5mNjQZhZKnJEVwA3x15FhEeZqZe9MZzzUF/ zue1jCcYf1unSNs5Bag5iBag3tJX8VGMgtH6DLSyKknTM1FZ87vm8jdhmLLErhWvYX vN68aM0Pg3NPt71a6L62Znvc55vTlvSHNyeyammY= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1826017AbgJ0SPP (ORCPT ); Tue, 27 Oct 2020 14:15:15 -0400 Received: from mail.kernel.org ([198.145.29.99]:34128 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756361AbgJ0OMu (ORCPT ); Tue, 27 Oct 2020 10:12:50 -0400 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id B644A2076A; Tue, 27 Oct 2020 14:12:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603807970; bh=2J6mySmFY3XBdGmC07zXWF/0AmGr+PLeMDMLR5N7YJ8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Iaq7u8Ni5bMbqsW/6B1wV6sGjLpS8PhYG43q6aY2xOvK8+jV3c0ZygYGKTzT/qNuE +XwlkrQMyrUH6FfM0LAt9W9Yd9qPjMCx16uolTffLgnFmJyUw04pKw7Snuu2GxJ/62 tuO+9i6NXzgE441n6MC75g204+FwNxLAgOWNMgs4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, John Hubbard , Ira Weiny , Dan Carpenter , Souptick Joarder , Sasha Levin Subject: [PATCH 4.14 067/191] misc: mic: scif: Fix error handling path Date: Tue, 27 Oct 2020 14:48:42 +0100 Message-Id: <20201027134912.964916740@linuxfoundation.org> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20201027134909.701581493@linuxfoundation.org> References: <20201027134909.701581493@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Souptick Joarder [ Upstream commit a81072a9c0ae734b7889929b0bc070fe3f353f0e ] Inside __scif_pin_pages(), when map_flags != SCIF_MAP_KERNEL it will call pin_user_pages_fast() to map nr_pages. However, pin_user_pages_fast() might fail with a return value -ERRNO. The return value is stored in pinned_pages->nr_pages. which in turn is passed to unpin_user_pages(), which expects pinned_pages->nr_pages >=0, else disaster. Fix this by assigning pinned_pages->nr_pages to 0 if pin_user_pages_fast() returns -ERRNO. Fixes: ba612aa8b487 ("misc: mic: SCIF memory registration and unregistration") Cc: John Hubbard Cc: Ira Weiny Cc: Dan Carpenter Reviewed-by: John Hubbard Signed-off-by: Souptick Joarder Link: https://lore.kernel.org/r/1600570295-29546-1-git-send-email-jrdr.linux@gmail.com Signed-off-by: Greg Kroah-Hartman Signed-off-by: Sasha Levin --- drivers/misc/mic/scif/scif_rma.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/misc/mic/scif/scif_rma.c b/drivers/misc/mic/scif/scif_rma.c index 95745dc4e0ecf..f282c2eaab29b 100644 --- a/drivers/misc/mic/scif/scif_rma.c +++ b/drivers/misc/mic/scif/scif_rma.c @@ -1404,6 +1404,8 @@ int __scif_pin_pages(void *addr, size_t len, int *out_prot, NULL); up_write(&mm->mmap_sem); if (nr_pages != pinned_pages->nr_pages) { + if (pinned_pages->nr_pages < 0) + pinned_pages->nr_pages = 0; if (try_upgrade) { if (ulimit) __scif_dec_pinned_vm_lock(mm, @@ -1424,7 +1426,6 @@ int __scif_pin_pages(void *addr, size_t len, int *out_prot, if (pinned_pages->nr_pages < nr_pages) { err = -EFAULT; - pinned_pages->nr_pages = nr_pages; goto dec_pinned; } @@ -1437,7 +1438,6 @@ int __scif_pin_pages(void *addr, size_t len, int *out_prot, __scif_dec_pinned_vm_lock(mm, nr_pages, 0); /* Something went wrong! Rollback */ error_unmap: - pinned_pages->nr_pages = nr_pages; scif_destroy_pinned_pages(pinned_pages); *pages = NULL; dev_dbg(scif_info.mdev.this_device,