Received: by 2002:a05:6a10:9e8c:0:0:0:0 with SMTP id y12csp372195pxx; Wed, 28 Oct 2020 06:57:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyUyye+4CKI2XBfWa4vH0GylYvIVjzeuZCenFQzMP2jJlQC40vDbAAVKZFW/FsLLY/ktv2G X-Received: by 2002:a05:6402:1bd9:: with SMTP id ch25mr7603660edb.365.1603893474834; Wed, 28 Oct 2020 06:57:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603893474; cv=none; d=google.com; s=arc-20160816; b=vmATJNSBbQMwU4kYZTw1+knRexuUMAGtasQ3wlfuTfUuxreU/83AZ0qoSUC5zN8Fgo BA2zIa7n+HloFURAQMfWD8ZgvXOp2J9h2mbia1REAClD/2W0K1keF5hg5bQdlS6SETxI Q/sCUv/lAzHAkYbfRY2yMG85hRNw1gyVwxLg3yjLta6Duvb83B8uo9ZEnhOI5LdMpRFk sOeStCjW/16XvepE5Igvg83QsJvmS/GazCMWNMTt1onTwscHHagcPJ+/tN8SyHus3Uto zKYADjJyeIhphjiYNtz5KRA2tQkuRCM81Djl/eYSKP1GzhvB+8CEvu8DRZsjkfYXsQ0/ K6gQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=RBXRQ+31LULnd9t4DOSZr6xIZbubrHxDpc/eX4bkijc=; b=WTRk70Hb4NmUfRmtZ/mh7GMpJS2Ug0ymJqtiXYODy3C3DRQ4nTV5a3/4bnRpwoc6Et KG9lBVtiGkoLh1FIixPlt3ljhPZps+6gIscijf12WDOGt03+4gs3j9rs+AhuedLae1dd paMVeg/6GkeAIcGUliUii7GHtn4+SL4uLPxUzbmIP4FRuXPWY9rtE9VIwkrWLnntr7EK cnIJu7MSKR/RGMDUbY/fFQLwuAIJrbyltu6e7hYjgkve77L3JEL7KQo9hlj/5fWSrT9t Gau3yoQHF0HuwI/aletOFkC9aQwIiUb4RTnbMaH0J8ht3u4d8PZJIU+fmc17rJfllVjs rIzA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=AaZ4OU4Q; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w11si2719069ejq.533.2020.10.28.06.57.32; Wed, 28 Oct 2020 06:57:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=AaZ4OU4Q; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1803571AbgJ0PxG (ORCPT + 99 others); Tue, 27 Oct 2020 11:53:06 -0400 Received: from mail.kernel.org ([198.145.29.99]:44914 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1798554AbgJ0P24 (ORCPT ); Tue, 27 Oct 2020 11:28:56 -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 6230E20728; Tue, 27 Oct 2020 15:28:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603812518; bh=qdq0E/jGIruANVQF+VFYNqx9xPDbKDwAH9Ni7hWWuyM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AaZ4OU4QWGrqQ0jBGetXqW1tCxClAkclDgudFgW3kxjkEfKRyb9wYahRoWVbu/YOt By6m9TF7v9KCVwcdod8V58T7SRPL9DetJ/GyuiJraetPX9pUpm6KSHBwNJN5Ehw4Lv 7sLe+u7cs7VorfjLEj6MgXF8wTmXczqejcl03MNA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Souptick Joarder , Dan Carpenter , John Hubbard , Sasha Levin Subject: [PATCH 5.9 236/757] drivers/virt/fsl_hypervisor: Fix error handling path Date: Tue, 27 Oct 2020 14:48:06 +0100 Message-Id: <20201027135501.658519219@linuxfoundation.org> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20201027135450.497324313@linuxfoundation.org> References: <20201027135450.497324313@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Souptick Joarder [ Upstream commit 7f360bec37857bfd5a48cef21d86f58a09a3df63 ] First, when memory allocation for sg_list_unaligned failed, there is a bug of calling put_pages() as we haven't pinned any pages. Second, if get_user_pages_fast() failed we should unpin num_pinned pages. This will address both. As part of these changes, minor update in documentation. Fixes: 6db7199407ca ("drivers/virt: introduce Freescale hypervisor management driver") Signed-off-by: Souptick Joarder Reviewed-by: Dan Carpenter Reviewed-by: John Hubbard Link: https://lore.kernel.org/r/1598995271-6755-1-git-send-email-jrdr.linux@gmail.com Signed-off-by: Greg Kroah-Hartman Signed-off-by: Sasha Levin --- drivers/virt/fsl_hypervisor.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/drivers/virt/fsl_hypervisor.c b/drivers/virt/fsl_hypervisor.c index 1b0b11b55d2a0..46ee0a0998b6f 100644 --- a/drivers/virt/fsl_hypervisor.c +++ b/drivers/virt/fsl_hypervisor.c @@ -157,7 +157,7 @@ static long ioctl_memcpy(struct fsl_hv_ioctl_memcpy __user *p) unsigned int i; long ret = 0; - int num_pinned; /* return value from get_user_pages() */ + int num_pinned = 0; /* return value from get_user_pages_fast() */ phys_addr_t remote_paddr; /* The next address in the remote buffer */ uint32_t count; /* The number of bytes left to copy */ @@ -174,7 +174,7 @@ static long ioctl_memcpy(struct fsl_hv_ioctl_memcpy __user *p) return -EINVAL; /* - * The array of pages returned by get_user_pages() covers only + * The array of pages returned by get_user_pages_fast() covers only * page-aligned memory. Since the user buffer is probably not * page-aligned, we need to handle the discrepancy. * @@ -224,7 +224,7 @@ static long ioctl_memcpy(struct fsl_hv_ioctl_memcpy __user *p) /* * 'pages' is an array of struct page pointers that's initialized by - * get_user_pages(). + * get_user_pages_fast(). */ pages = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL); if (!pages) { @@ -241,7 +241,7 @@ static long ioctl_memcpy(struct fsl_hv_ioctl_memcpy __user *p) if (!sg_list_unaligned) { pr_debug("fsl-hv: could not allocate S/G list\n"); ret = -ENOMEM; - goto exit; + goto free_pages; } sg_list = PTR_ALIGN(sg_list_unaligned, sizeof(struct fh_sg_list)); @@ -250,7 +250,6 @@ static long ioctl_memcpy(struct fsl_hv_ioctl_memcpy __user *p) num_pages, param.source != -1 ? FOLL_WRITE : 0, pages); if (num_pinned != num_pages) { - /* get_user_pages() failed */ pr_debug("fsl-hv: could not lock source buffer\n"); ret = (num_pinned < 0) ? num_pinned : -EFAULT; goto exit; @@ -292,13 +291,13 @@ static long ioctl_memcpy(struct fsl_hv_ioctl_memcpy __user *p) virt_to_phys(sg_list), num_pages); exit: - if (pages) { - for (i = 0; i < num_pages; i++) - if (pages[i]) - put_page(pages[i]); + if (pages && (num_pinned > 0)) { + for (i = 0; i < num_pinned; i++) + put_page(pages[i]); } kfree(sg_list_unaligned); +free_pages: kfree(pages); if (!ret) -- 2.25.1