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 Received: from smtp4.osuosl.org (smtp4.osuosl.org [140.211.166.137]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E061FC433F5 for ; Thu, 24 Mar 2022 12:46:44 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp4.osuosl.org (Postfix) with ESMTP id 7963841C6F; Thu, 24 Mar 2022 12:46:44 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp4.osuosl.org ([127.0.0.1]) by localhost (smtp4.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id q9vlTEGKFZ1L; Thu, 24 Mar 2022 12:46:43 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp4.osuosl.org (Postfix) with ESMTPS id 3405541C69; Thu, 24 Mar 2022 12:46:43 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 136ADC0031; Thu, 24 Mar 2022 12:46:43 +0000 (UTC) Received: from smtp3.osuosl.org (smtp3.osuosl.org [IPv6:2605:bc80:3010::136]) by lists.linuxfoundation.org (Postfix) with ESMTP id B2165C001A for ; Thu, 24 Mar 2022 12:46:41 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp3.osuosl.org (Postfix) with ESMTP id 9F92F60E08 for ; Thu, 24 Mar 2022 12:46:41 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Authentication-Results: smtp3.osuosl.org (amavisd-new); dkim=pass (2048-bit key) header.d=nvidia.com Received: from smtp3.osuosl.org ([127.0.0.1]) by localhost (smtp3.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id R07jcpFVKKdx for ; Thu, 24 Mar 2022 12:46:40 +0000 (UTC) X-Greylist: whitelisted by SQLgrey-1.8.0 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on20614.outbound.protection.outlook.com [IPv6:2a01:111:f400:7eae::614]) by smtp3.osuosl.org (Postfix) with ESMTPS id BC30660D65 for ; Thu, 24 Mar 2022 12:46:40 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=aEn9cvuD65yhPdNf0HGvVZ6g+Qo5eNIfgk43QLhqs968qA/Jywbz3QVgeacEzIjAT4EV0QcTmY4lN25TLLL9ZQo97TCqiW/xOAwbvKYmrBMbo4Q1ZGx9eKR1U7cWNKkVfBmIBEbZxvUnRQFg/FOtfer44MGTFYiKCopCTM2pDNiF5FXolgKzQN6rOkNxLR//6Z+doPiew02fgvDKhOd+QSru33tE7933BmBZE4JLJgiSjhEtIuXMw60gblzk+SX4C7UWkcr15NRF7gOdjOPvaf60pYGWdoVvlG7XtauULqr1OKCLZTzTg3sL1UV7xT/tREf0YmxjsrGd9YlU0Jw6Uw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=g0vyq7u2dmz6kZektDZbXnT3mjHsc6nuDl/K8lRWWoI=; b=IaL56o6w9UxQRezPiZbhenLzUpcwMkFk+33ZX7d15pqL0XXXXR+Q+Ziw7tGIKWZJtPVWGC1E/Cna7ADbjBYx1kJ6NZfX+INgnTmze2tOEFtZNLvTvxq3Ll6Vg7yjPlJyqji5xPVU5Fx/1CnzEGa+GbwJr68mQJqoYN3ywLOm6uZUa9HqhiLIf8TcbWFS1e84X2BQDLevDLeM9ZV9+MSnrU9dpzn7MAxrqZsl+7E4Z8Or3pZBcyPKztBcqSH+vYsNlA0OoeG4tKESyEvibgXXF8YGhp23Q6UoLLObH3ZHArAyVtX/N+VdF/VIMv9p7KOdumiMyIx+hjpq4YFI0RdEJw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=g0vyq7u2dmz6kZektDZbXnT3mjHsc6nuDl/K8lRWWoI=; b=VvHdOU7/HsNj1PZg/KuhsJpSfEUiHTknINjB1KZ1gKvztNs2s8V7TwRYrepAJ7KVsp/hJVE4lFT1JXb5PHY1gwgdXbOlSiv2PW7D/QKneswl5bde5gjzTsY2ri/uaQA4tO/7zZAcpvVqEurBBeE2escbZBo5kZeuTX3pFqCSXThb/3GxdTDf5ZYp2onhYcSiYQDBlviq996IGcxr/TT3GJcS6zKOFw3AfI/IZnGz1iZ8Cw95QWIvCVZA+UEUu4KJXZcTIts26EO/9FMm1K1kb3KAl+5RDjsHPbygwMnwNe/WnLcsdkEibKX8hJHhb2jPGjdJR5LNl/d2jFOrHobqAw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from MN2PR12MB4192.namprd12.prod.outlook.com (2603:10b6:208:1d5::15) by BYAPR12MB5704.namprd12.prod.outlook.com (2603:10b6:a03:9d::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5102.16; Thu, 24 Mar 2022 12:46:36 +0000 Received: from MN2PR12MB4192.namprd12.prod.outlook.com ([fe80::11a0:970a:4c24:c70c]) by MN2PR12MB4192.namprd12.prod.outlook.com ([fe80::11a0:970a:4c24:c70c%6]) with mapi id 15.20.5102.019; Thu, 24 Mar 2022 12:46:36 +0000 Date: Thu, 24 Mar 2022 09:46:35 -0300 To: "Tian, Kevin" Subject: Re: [PATCH RFC 07/12] iommufd: Data structure to provide IOVA to PFN mapping Message-ID: <20220324124635.GA1287230@nvidia.com> References: <0-v1-e79cd8d168e8+6-iommufd_jgg@nvidia.com> <7-v1-e79cd8d168e8+6-iommufd_jgg@nvidia.com> <20220322161544.54fd459d.alex.williamson@redhat.com> <20220323181532.GA1184709@nvidia.com> Content-Disposition: inline In-Reply-To: X-ClientProxiedBy: MN2PR20CA0042.namprd20.prod.outlook.com (2603:10b6:208:235::11) To MN2PR12MB4192.namprd12.prod.outlook.com (2603:10b6:208:1d5::15) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 6ebcd9d0-b9cc-433c-a3ad-08da0d94553d X-MS-TrafficTypeDiagnostic: BYAPR12MB5704:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: a9q7JdX9uEmSM6F3V+N4WLOna8Q46xpxHxXbOxGdECweONusT0SJkHGHH/qww4A/fCKDUSdUGDuz3I9yKIhE0jctJoyJjNRXth+AXHWHMfCK6gw9LaGkiJIu6BeOQUpM+FJiwSwA69U/hKo6HYZ7xoOXX09nPcWvrwKI5v37hlCodzuvJE09e68SQ004/x0p45lmX+/FxwiRQhklNpa2dzjyj9VuQSz/G6WRIbyTWCZiB7fCIQH+nE9wWnIx1Xnu36pSBQbYg6lO6O6cvfSkcHwqzsBh43NUgGYkjwYd8BveLdaHr7CoDygwFM4T+fBZH5WYmD4ldYxOvYMmAH/NKMGwIFQw/Nl24yx79iSXE+zDxILtStwAaXqfR5wavzYXA1BI39mMSwLfRjWuLDETuw3shjxnqaQwJzAzAzYoiuXOZn5pgtmeDemSCrbpqxlWAEh2Ac+PQyflWPikHRoyCF6k/ELBgMbVWkA4tJ2qoXTJmP+DkQ0OXa4I/Kie/IvJDv1+1FgXbgRwffFLNho5ur3nzFJdYy3yxIgyufFQgsWg29u23uaxlbLyCqR7enwFYHYbmUPoDvsWVsZwMNRaXZrEU1ZfMow2VBj/EvdsKGx5rEWu44SjesBPGU99nQm+CUYQy2z45A1TGYW2/X2Phg== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:MN2PR12MB4192.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230001)(4636009)(366004)(186003)(8676002)(6486002)(26005)(66556008)(4326008)(36756003)(66476007)(83380400001)(66946007)(8936002)(38100700002)(7416002)(2616005)(5660300002)(316002)(6512007)(1076003)(6916009)(54906003)(33656002)(6506007)(2906002)(508600001)(86362001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?VW2nBxhMxWp4MHdsJTN2y/4iGNeFGP+Cofb/i14nfBIsugd6cqJ+f16gr4+6?= =?us-ascii?Q?CnIw0JR9zHTKzsFaTZSEAfC6IKpuBBCBvbePAERtiUukl/8PZx78b1EgGAue?= =?us-ascii?Q?kk3Krq+FjJqY4e7wHJobc4m4IXFHCdaqSJkyMP+Yns4Hn8QPy5Ue1bnL5dgN?= =?us-ascii?Q?Sl+0sX62FNREolF7CBXIBlebymgKeOrT2c/Em4xbz4zA5bIe3Ygp2x8tDGKc?= =?us-ascii?Q?zLDU5NQeKX1ovazeBJUzBmWD+mXMeHhiPmwtoKDOSZ2RplN4i7XEGLxmUS6Q?= =?us-ascii?Q?/NDS5LL5GuGVwxGH8l1YRin+EpjDyGYD5OlgfcOZ/I7elAuaRgiOTx5H8cNe?= =?us-ascii?Q?w4Z0GErT4wKFuRN+WMFGlLlTWcgobN0eMuKmbN9Qgrz7/GMV3fKCaxT/lXSU?= =?us-ascii?Q?OyqEMGGwLL2I5IcvastdXHRKRe+RIjcSF8cOW+j07MbC8z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viOZqZLB4flwdylu660sxTbVzsA2xj7g0zg6hlTYpZPR?= =?us-ascii?Q?0B8132Rgr+PQ63mYusqs80klYHOrurc3WRYpQRpkPcDoS8hWWF8uV+PcSRB9?= =?us-ascii?Q?J4eQQ78qh1SIa8VvLWHRlmyJh0kZgUGQGS28CK98PZ4eKYEAR27oM5aRw9HU?= =?us-ascii?Q?jEo0tdTVvtWj4WCpM1Ary7WMckIPkut565XrbyAIx7pNH3qe40NT79ijwGuW?= =?us-ascii?Q?jpV6fpkGuOi7cCTdwFQy4C/pGL+EmUUOYBhsJr6Qv26ldVeyWhXnLWEgCaZI?= =?us-ascii?Q?GIliMOD+/+R+RoYfo2o9Zyk4y3nlwIVUXP78CYidEP5ALGnSYhgQkdzSxDcD?= =?us-ascii?Q?Un1AMVoJvCD2SzxKfgjkbsJ/Gtmv+PK+a+GnAlWLuke1Vrc7gQ3xDlw3cA/r?= =?us-ascii?Q?Ccz9YIqmyQ=3D=3D?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6ebcd9d0-b9cc-433c-a3ad-08da0d94553d X-MS-Exchange-CrossTenant-AuthSource: MN2PR12MB4192.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2022 12:46:36.5126 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: /02/Utx2kCvNSACBYs+JFh3qkxZw5zz1bhcqz0SpAML0Z71DH9wUs7hW/OU3gyOH X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR12MB5704 Cc: Jean-Philippe Brucker , Chaitanya Kulkarni , "kvm@vger.kernel.org" , "Michael S. Tsirkin" , Jason Wang , Cornelia Huck , Niklas Schnelle , "iommu@lists.linux-foundation.org" , Daniel Jordan , Alex Williamson , "Martins, Joao" , David Gibson X-BeenThere: iommu@lists.linux-foundation.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: Development issues for Linux IOMMU support List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Jason Gunthorpe via iommu Reply-To: Jason Gunthorpe Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: iommu-bounces@lists.linux-foundation.org Sender: "iommu" On Thu, Mar 24, 2022 at 03:09:46AM +0000, Tian, Kevin wrote: > + /* > + * Can't cross areas that are not aligned to the system page > + * size with this API. > + */ > + if (cur_iova % PAGE_SIZE) { > + rc = -EINVAL; > + goto out_remove; > + } > > Currently it's done after iopt_pages_add_user() but before cur_iova > is adjusted, which implies the last add_user() will not be reverted in > case of failed check here. Oh, yes that's right too.. The above is wrong even, it didn't get fixed when page_offset was done. So more like this: diff --git a/drivers/iommu/iommufd/io_pagetable.c b/drivers/iommu/iommufd/io_pagetable.c index 1c08ae9b848fcf..9505f119df982e 100644 --- a/drivers/iommu/iommufd/io_pagetable.c +++ b/drivers/iommu/iommufd/io_pagetable.c @@ -23,7 +23,7 @@ static unsigned long iopt_area_iova_to_index(struct iopt_area *area, if (IS_ENABLED(CONFIG_IOMMUFD_TEST)) WARN_ON(iova < iopt_area_iova(area) || iova > iopt_area_last_iova(area)); - return (iova - (iopt_area_iova(area) & PAGE_MASK)) / PAGE_SIZE; + return (iova - (iopt_area_iova(area) - area->page_offset)) / PAGE_SIZE; } static struct iopt_area *iopt_find_exact_area(struct io_pagetable *iopt, @@ -436,31 +436,45 @@ int iopt_access_pages(struct io_pagetable *iopt, unsigned long iova, unsigned long index; /* Need contiguous areas in the access */ - if (iopt_area_iova(area) < cur_iova || !area->pages) { + if (iopt_area_iova(area) > cur_iova || !area->pages) { rc = -EINVAL; goto out_remove; } index = iopt_area_iova_to_index(area, cur_iova); last_index = iopt_area_iova_to_index(area, last); + + /* + * The API can only return aligned pages, so the starting point + * must be at a page boundary. + */ + if ((cur_iova - (iopt_area_iova(area) - area->page_offset)) % + PAGE_SIZE) { + rc = -EINVAL; + goto out_remove; + } + + /* + * and an interior ending point must be at a page boundary + */ + if (last != last_iova && + (iopt_area_last_iova(area) - cur_iova + 1) % PAGE_SIZE) { + rc = -EINVAL; + goto out_remove; + } + rc = iopt_pages_add_user(area->pages, index, last_index, out_pages, write); if (rc) goto out_remove; if (last == last_iova) break; - /* - * Can't cross areas that are not aligned to the system page - * size with this API. - */ - if (cur_iova % PAGE_SIZE) { - rc = -EINVAL; - goto out_remove; - } cur_iova = last + 1; out_pages += last_index - index; atomic_inc(&area->num_users); } + if (cur_iova != last_iova) + goto out_remove; up_read(&iopt->iova_rwsem); return 0; diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index 5c47d706ed9449..a46e0f0ae82553 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -1221,5 +1221,6 @@ TEST_F(vfio_compat_mock_domain, get_info) /* FIXME test VFIO_IOMMU_MAP_DMA */ /* FIXME test VFIO_IOMMU_UNMAP_DMA */ /* FIXME test 2k iova alignment */ +/* FIXME cover boundary cases for iopt_access_pages() */ TEST_HARNESS_MAIN _______________________________________________ iommu mailing list iommu@lists.linux-foundation.org https://lists.linuxfoundation.org/mailman/listinfo/iommu