Received: by 10.213.65.68 with SMTP id h4csp891823imn; Tue, 20 Mar 2018 19:09:51 -0700 (PDT) X-Google-Smtp-Source: AG47ELvnYNScOuFIyseF3TXP1ea7tB7UUkXHf4BUTEEJQyx7XhR2W+lyKFAiTJbhYCoRXelXE98y X-Received: by 10.99.55.93 with SMTP id g29mr9601162pgn.161.1521598191372; Tue, 20 Mar 2018 19:09:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521598191; cv=none; d=google.com; s=arc-20160816; b=j/cTFjqjjfZHAaltb7fZsrSsbDxvH4rh6PE4ZX81JLG08q4W7PWl4qS7Zez7jeuJvE IS6F3IZlXKFXIh+rBc+MaXboFf6bCFV7ARalABwchQkjcry/p5ISaK4cvzdArwCq3TCu XUw/pjf1tMUBWNwHoe5X5feTeIsy8JwCas/bcOyUzzu7DQb5fbyza7T4p7WJ7Z6vS6E1 PVsMIB3uPXdu95RgrJ0FgJt+qFmmw749GKRRe2B6WQFF7oWuiHTP9qMAdycBEen+Uq4E Ly9R9aPCXUez0FgU5z+fjufcpF7Oi8aQ7yBZlTkPNgEH5SFUDKM5zOdz1zTvK/znc/gj NpvQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:references:in-reply-to:date :subject:cc:to:from:arc-authentication-results; bh=S+dVkUuASsjBjsyJ9gIY1qqWBzbgGBWV+ZtdfkVRGCw=; b=bd3Mr8uUaK2jr0Xij0XoSVJHvyJwzz31ZE/L+kqSl+VW6xBwmQx30J54bXj/H18fp4 8z4Iw3ajvGBXV42i7MIQIElA8tQjDsJnNH4GES+tFYGj7fKh2MMnz9q6qjKjKFNTmdpV rnKBxyGFWD4kohBwwHlCdfJHU5ePz7zngjsD/y6cMBNdQF5vIOmTjMJjsnuhkPVQomCF upWpdbg3T8NxA4ZsIGcjEm01lgDfAdE3WfVUGx8ynJOn5ruX1kifoEjBhNmylzhxCXHR 384LWXOXS5io3KtyjSpAO//y2Z9wTntzoc4b4hD3ssbzvCQqx5o7fgtBRISGZTnJYU+v FTnA== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p6si2073528pgc.456.2018.03.20.19.09.37; Tue, 20 Mar 2018 19:09:51 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751888AbeCUCIl (ORCPT + 99 others); Tue, 20 Mar 2018 22:08:41 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:36908 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751774AbeCUCIg (ORCPT ); Tue, 20 Mar 2018 22:08:36 -0400 Received: from pps.filterd (m0098416.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w2L24EtI002827 for ; Tue, 20 Mar 2018 22:08:35 -0400 Received: from e33.co.us.ibm.com (e33.co.us.ibm.com [32.97.110.151]) by mx0b-001b2d01.pphosted.com with ESMTP id 2gucjr314b-1 (version=TLSv1.2 cipher=AES256-SHA256 bits=256 verify=NOT) for ; Tue, 20 Mar 2018 22:08:35 -0400 Received: from localhost by e33.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 20 Mar 2018 20:08:34 -0600 Received: from b03cxnp08026.gho.boulder.ibm.com (9.17.130.18) by e33.co.us.ibm.com (192.168.1.133) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Tue, 20 Mar 2018 20:08:31 -0600 Received: from b03ledav003.gho.boulder.ibm.com (b03ledav003.gho.boulder.ibm.com [9.17.130.234]) by b03cxnp08026.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id w2L28Vus10944898; Tue, 20 Mar 2018 19:08:31 -0700 Received: from b03ledav003.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 4F65E6A041; Tue, 20 Mar 2018 20:08:31 -0600 (MDT) Received: from tuxmaker.boeblingen.de.ibm.com (unknown [9.152.85.9]) by b03ledav003.gho.boulder.ibm.com (Postfix) with ESMTP id DD9DB6A03F; Tue, 20 Mar 2018 20:08:29 -0600 (MDT) From: Dong Jia Shi To: linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org, kvm@vger.kernel.org Cc: cohuck@redhat.com, borntraeger@de.ibm.com, bjsdjshi@linux.vnet.ibm.com, pasic@linux.vnet.ibm.com, pmorel@linux.vnet.ibm.com Subject: [PATCH 2/4] vfio: ccw: refactor and improve pfn_array_alloc_pin() Date: Wed, 21 Mar 2018 03:08:20 +0100 X-Mailer: git-send-email 2.13.5 In-Reply-To: <20180321020822.86255-1-bjsdjshi@linux.vnet.ibm.com> References: <20180321020822.86255-1-bjsdjshi@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 18032102-0008-0000-0000-0000097C4BF9 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00008713; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000254; SDB=6.01006058; UDB=6.00512229; IPR=6.00785341; MB=3.00020153; MTD=3.00000008; XFM=3.00000015; UTC=2018-03-21 02:08:33 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18032102-0009-0000-0000-0000467954F7 Message-Id: <20180321020822.86255-3-bjsdjshi@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2018-03-21_01:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=2 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1709140000 definitions=main-1803210024 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This refactors pfn_array_alloc_pin() and also improves it by adding defensive code in error handling so that calling pfn_array_unpin_free() after error return won't lead to problem. This mains does: 1. Merge pfn_array_pin() into pfn_array_alloc_pin(), since there is no other user of pfn_array_pin(). As a result, also remove kernel-doc for pfn_array_pin() and add kernel-doc for pfn_array_alloc_pin(). 2. For a vfio_pin_pages() failure, set pa->pa_nr to zero to indicate zero pages were pinned. 3. Set pa->pa_iova_pfn to NULL right after it was freed. Signed-off-by: Dong Jia Shi --- drivers/s390/cio/vfio_ccw_cp.c | 84 ++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 48 deletions(-) diff --git a/drivers/s390/cio/vfio_ccw_cp.c b/drivers/s390/cio/vfio_ccw_cp.c index 2be114db02f9..3abc9770910a 100644 --- a/drivers/s390/cio/vfio_ccw_cp.c +++ b/drivers/s390/cio/vfio_ccw_cp.c @@ -46,65 +46,32 @@ struct ccwchain { }; /* - * pfn_array_pin() - pin user pages in memory + * pfn_array_alloc_pin() - alloc memory for PFNs, then pin user pages in memory * @pa: pfn_array on which to perform the operation * @mdev: the mediated device to perform pin/unpin operations + * @iova: target guest physical address + * @len: number of bytes that should be pinned from @iova * - * Attempt to pin user pages in memory. + * Attempt to allocate memory for PFNs, and pin user pages in memory. * * Usage of pfn_array: - * @pa->pa_iova starting guest physical I/O address. Assigned by caller. + * @pa->pa_iova starting guest physical I/O address. Assigned by callee. * @pa->pa_iova_pfn array that stores PFNs of the pages need to pin. Allocated - * by caller. + * by callee. * @pa->pa_pfn array that receives PFNs of the pages pinned. Allocated by - * caller. - * @pa->pa_nr number of pages from @pa->pa_iova to pin. Assigned by - * caller. - * number of pages pinned. Assigned by callee. + * callee. + * @pa->pa_nr initiated as 0 by caller. + * number of pages pinned from @pa->pa_iova. Assigned by callee. * * Returns: * Number of pages pinned on success. - * If @pa->pa_nr is 0 or negative, returns 0. + * If @pa->pa_nr is not 0 initially, returns -EINVAL. * If no pages were pinned, returns -errno. */ -static int pfn_array_pin(struct pfn_array *pa, struct device *mdev) -{ - int i, ret; - - if (pa->pa_nr <= 0) { - pa->pa_nr = 0; - return 0; - } - - pa->pa_iova_pfn[0] = pa->pa_iova >> PAGE_SHIFT; - for (i = 1; i < pa->pa_nr; i++) - pa->pa_iova_pfn[i] = pa->pa_iova_pfn[i - 1] + 1; - - ret = vfio_pin_pages(mdev, pa->pa_iova_pfn, pa->pa_nr, - IOMMU_READ | IOMMU_WRITE, pa->pa_pfn); - - if (ret > 0 && ret != pa->pa_nr) { - vfio_unpin_pages(mdev, pa->pa_iova_pfn, ret); - pa->pa_nr = 0; - return 0; - } - - return ret; -} - -/* Unpin the pages before releasing the memory. */ -static void pfn_array_unpin_free(struct pfn_array *pa, struct device *mdev) -{ - vfio_unpin_pages(mdev, pa->pa_iova_pfn, pa->pa_nr); - pa->pa_nr = 0; - kfree(pa->pa_iova_pfn); -} - -/* Alloc memory for PFNs, then pin pages with them. */ static int pfn_array_alloc_pin(struct pfn_array *pa, struct device *mdev, u64 iova, unsigned int len) { - int ret = 0; + int i, ret = 0; if (!len) return 0; @@ -126,18 +93,39 @@ static int pfn_array_alloc_pin(struct pfn_array *pa, struct device *mdev, return -ENOMEM; pa->pa_pfn = pa->pa_iova_pfn + pa->pa_nr; - ret = pfn_array_pin(pa, mdev); + pa->pa_iova_pfn[0] = pa->pa_iova >> PAGE_SHIFT; + for (i = 1; i < pa->pa_nr; i++) + pa->pa_iova_pfn[i] = pa->pa_iova_pfn[i - 1] + 1; + + ret = vfio_pin_pages(mdev, pa->pa_iova_pfn, pa->pa_nr, + IOMMU_READ | IOMMU_WRITE, pa->pa_pfn); - if (ret > 0) - return ret; - else if (!ret) + if (ret < 0) { + goto err_out; + } else if (ret > 0 && ret != pa->pa_nr) { + vfio_unpin_pages(mdev, pa->pa_iova_pfn, ret); ret = -EINVAL; + goto err_out; + } + + return ret; +err_out: + pa->pa_nr = 0; kfree(pa->pa_iova_pfn); + pa->pa_iova_pfn = NULL; return ret; } +/* Unpin the pages before releasing the memory. */ +static void pfn_array_unpin_free(struct pfn_array *pa, struct device *mdev) +{ + vfio_unpin_pages(mdev, pa->pa_iova_pfn, pa->pa_nr); + pa->pa_nr = 0; + kfree(pa->pa_iova_pfn); +} + static int pfn_array_table_init(struct pfn_array_table *pat, int nr) { pat->pat_pa = kcalloc(nr, sizeof(*pat->pat_pa), GFP_KERNEL); -- 2.13.5