From patchwork Fri Sep 24 09:45:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 12514783 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=-17.3 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 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 1AC1CC433EF for ; Fri, 24 Sep 2021 09:46:13 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id C0F0960238 for ; Fri, 24 Sep 2021 09:46:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org C0F0960238 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=suse.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.xenproject.org Received: from list by lists.xenproject.org with outflank-mailman.195097.347609 (Exim 4.92) (envelope-from ) id 1mThmO-0006WY-BT; Fri, 24 Sep 2021 09:46:04 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 195097.347609; Fri, 24 Sep 2021 09:46:04 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mThmO-0006WR-8Z; Fri, 24 Sep 2021 09:46:04 +0000 Received: by outflank-mailman (input) for mailman id 195097; Fri, 24 Sep 2021 09:46:02 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mThmM-0006WJ-Oi for xen-devel@lists.xenproject.org; Fri, 24 Sep 2021 09:46:02 +0000 Received: from de-smtp-delivery-102.mimecast.com (unknown [194.104.111.102]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 39f2bbb5-1d1c-11ec-bab9-12813bfff9fa; Fri, 24 Sep 2021 09:46:01 +0000 (UTC) Received: from EUR04-HE1-obe.outbound.protection.outlook.com (mail-he1eur04lp2058.outbound.protection.outlook.com [104.47.13.58]) (Using TLS) by relay.mimecast.com with ESMTP id de-mta-4-59ajN07nOGOgxzxYJAVcBg-1; Fri, 24 Sep 2021 11:45:59 +0200 Received: from VI1PR04MB5600.eurprd04.prod.outlook.com (2603:10a6:803:e7::16) by VI1PR04MB4383.eurprd04.prod.outlook.com (2603:10a6:803:6b::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4544.13; Fri, 24 Sep 2021 09:45:58 +0000 Received: from VI1PR04MB5600.eurprd04.prod.outlook.com ([fe80::4d37:ec64:4e90:b16b]) by VI1PR04MB5600.eurprd04.prod.outlook.com ([fe80::4d37:ec64:4e90:b16b%7]) with mapi id 15.20.4544.018; Fri, 24 Sep 2021 09:45:58 +0000 Received: from [10.156.60.236] (37.24.206.209) by PR2P264CA0028.FRAP264.PROD.OUTLOOK.COM (2603:10a6:101:1::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4544.13 via Frontend Transport; Fri, 24 Sep 2021 09:45:57 +0000 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 39f2bbb5-1d1c-11ec-bab9-12813bfff9fa DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=mimecast20200619; t=1632476760; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=s/xqY7vvGGkXpN8hylBtpj3jGQh5qHksSw0RnRhvYDs=; b=nOn+jCd+yqX5EKGTQj2C9wEqaD3ySdJh52qjDu9JBUl2nNdrsf3YT3FhQiwll5JQbj9ddm 8vmL1CSuniV9WDVJuUbnWyIW6jjQlywJa2CiKLe2Kd23TqpBUjB0BtxRLBW6HB0rXeBVoZ n5R2RCnT5zQJWplfFs757nI4KKzQWhg= X-MC-Unique: 59ajN07nOGOgxzxYJAVcBg-1 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=dNc2RIpnVnaqZHNOG2WnMYVTe89i6ol1WPN0xGE3jKUjfrIXQak94pNocn/1SosWtrJ8dcVuczMHDS0CJk2YhI95gs+Gh+UIo4i4477B7qtb6iT7s3pxQdquoOTDhsJtsp5Tvup0NQM4BjVlevU49r1DRDxySKiJtAsRpHwVsZ3Pi8Y0/2ppjIewf//W94z7jhFlZzqmGMPB/DsPGzErA5B1M3FBJBf8wbxMj9hEZT/l0GRlxiJxWzaGI63UhD5LoITGGWc/y0efi65e32Tsy9+bxlOeb+gx9zTj9cLcnAH1iutCQk60FwDObHSsYlmPICrY30PiDHocFDJjP/n7IA== 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; bh=s/xqY7vvGGkXpN8hylBtpj3jGQh5qHksSw0RnRhvYDs=; b=QzCTRbv6cMqoEZYHQEZ9SWxXbyhf+A1bRCL9ABdK2dF8LWUU3OPGfl1FNo53XKJUdiPT9fz8t7y4cZd7ElmJKD+qDMnAQGwWd0WtkJ3YV8i0gm9I0Yovd5yFNXNNq7nBOoENXq8dAsOLiqD+WgcfrKICT+m1eYQheK68QaSJe/Bdccl2jaw2vd/THNyG+p7W2coASuVLNeYuvJ8eaudJ4ZZDvM83PRBFzEeT6Gz2XrJ/ndQMCMGSqPbsbvSg/NfxyPrkxcc4aM2MSEqaVZXmcQiQY1s5wnfHg8HtKfVCSjMtbRW+5vWqm5HqKeD1EFMdfyU2ZnTWSN768dDcdUalsw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none Authentication-Results: xen.org; dkim=none (message not signed) header.d=none;xen.org; dmarc=none action=none header.from=suse.com; Subject: [PATCH v2 05/18] IOMMU: have iommu_{,un}map() split requests into largest possible chunks From: Jan Beulich To: "xen-devel@lists.xenproject.org" Cc: Andrew Cooper , Paul Durrant References: <957f067b-9fe1-2350-4266-51982f09d3a9@suse.com> Message-ID: <134785fb-8ac1-50f0-de75-e0d6fe22f711@suse.com> Date: Fri, 24 Sep 2021 11:45:57 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 In-Reply-To: <957f067b-9fe1-2350-4266-51982f09d3a9@suse.com> Content-Language: en-US X-ClientProxiedBy: PR2P264CA0028.FRAP264.PROD.OUTLOOK.COM (2603:10a6:101:1::16) To VI1PR04MB5600.eurprd04.prod.outlook.com (2603:10a6:803:e7::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 6bee5668-25ff-40b2-1e4e-08d97f401c46 X-MS-TrafficTypeDiagnostic: VI1PR04MB4383: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:1468; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: yo0pDFLvICmiv31hJub9ecyxA+bFBlDU40VRVGYb7N41qolLU6/m2UjCnfjIkZKQ4vzwcbn6TsGabGGEb0gr8IQuAiHmw8kq+yBb/Bup4VhT5M0SJGaucSCUXghLuz9g0mRiMYJU63py4hglNop8bVMo+ovVb0A+XycLkbvcONKh9Wvy5LgzVSqmRBYauNSZs0jkYBGb/Z2sfhXHWnmLEzCJql9KXfCgZQopUHSlxnVZPzAGp7z6sqKBqiXk/kvMBmtrB+UYJ/8lkSURjb4qHxAqCAC4wRe92v5/f9QTh5mnq7LjB+0uoXnfwGlBs7cTjp8w42lW0egEb8kdDGwKH/dqaNicRhi8SVTHjy5FvvhugJw7p3VmgEQ0GCz3aQYav77fV1Fa+F2xkiT2FeEJhcpNvbGno7by0gQWF/R4gMWJL32K+N/PPKzlvejVd0ZMvfPhADhwcEmNMWImNYbkRYOJ2iP5a30pxtMzg8TUY4noJktx+Rx2laai85oNHzm/dxZLPOQmdwMDYwPoSSZyXh3anwv5ND+1wZ86RWdP+JjmFjlZmisKmQ9KyhvU4Hrp5tVoSVcsiMT3ddtw/XF0rAleNSh/0g/nltkuAx9dcz9/jruImqaovNzGnh8/BTSM5QMMWz+TDvJKHPP1S3vr3zdbvknSP7B3ieUZQAEiTtcjk7rx7aqW/+UgYFtUIjXkDU2pOMlcMqx8oyu8gUkXkkzgXIsuWqn3NYytaR0fjpQ= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:VI1PR04MB5600.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(366004)(5660300002)(508600001)(31686004)(8676002)(16576012)(38100700002)(83380400001)(54906003)(26005)(36756003)(66946007)(31696002)(2906002)(186003)(66476007)(2616005)(66556008)(86362001)(956004)(6916009)(8936002)(316002)(4326008)(6486002)(43740500002)(45980500001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-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-8?q?0BeaQPtzxOfvmJEpUt2CpZ12MRdUXC1Y6NjbWZAhQO/rjCCJbk2C9SgqE2pta5wbo?= =?utf-8?q?DyHN+EY4Ae6VSIJLvcKtRhT5TdhEzpyTatfyZYk4c/iniI/jDkX6smxagk68xqehM?= =?utf-8?q?3rViVTFHFMCuk8zyaP/zw2SjsFvRUL/mJbrLjxyRySdZYL0t8xdskac4ZGuuUHNu4?= =?utf-8?q?R2ZaBaanLxLumVk4gJZLZ+6wImcZ24nixjMIEiyjDQPG6gmA3bljHgOOuYFoPNyDD?= =?utf-8?q?EzZ/6zUvo4IAnMO58I+uFgQQlwkz/bW56x+v8xefbuJcw2BlZM9LXdAJC6ylQIAYz?= =?utf-8?q?I8N14+ntVY0+9OR/Yfrp51XBruTv66UV0KrU0TY9rKYR0gyEhg+VhdJcXHYLvaKQY?= =?utf-8?q?Wsv6KHIPiXHWyA0u6BuxtdDKO8v0gk34ZnrftwU9R3EdniEpeja+Y27b6FQee1ujl?= =?utf-8?q?tuFhnZY82FEWJ0jY?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6bee5668-25ff-40b2-1e4e-08d97f401c46 X-MS-Exchange-CrossTenant-AuthSource: VI1PR04MB5600.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Sep 2021 09:45:57.9876 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f7a17af6-1c5c-4a36-aa8b-f5be247aa4ba X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: aF/11JB0CpfQDFHdLGJYfxGBwxlEPSpV3e77QoVf3Tkwyzf6tqZTggYotoKUkTsSMv3xKYaeqnP2RKR4Zs+VaA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR04MB4383 Introduce a helper function to determine the largest possible mapping that allows covering a request (or the next part of it that is left to be processed). In order to not add yet more recurring dfn_add() / mfn_add() to the two callers of the new helper, also introduce local variables holding the values presently operated on. Signed-off-by: Jan Beulich --- a/xen/drivers/passthrough/iommu.c +++ b/xen/drivers/passthrough/iommu.c @@ -260,12 +260,38 @@ void iommu_domain_destroy(struct domain arch_iommu_domain_destroy(d); } -int iommu_map(struct domain *d, dfn_t dfn, mfn_t mfn, +static unsigned int mapping_order(const struct domain_iommu *hd, + dfn_t dfn, mfn_t mfn, unsigned long nr) +{ + unsigned long res = dfn_x(dfn) | mfn_x(mfn); + unsigned long sizes = hd->platform_ops->page_sizes; + unsigned int bit = find_first_set_bit(sizes), order = 0; + + ASSERT(bit == PAGE_SHIFT); + + while ( (sizes = (sizes >> bit) & ~1) ) + { + unsigned long mask; + + bit = find_first_set_bit(sizes); + mask = (1UL << bit) - 1; + if ( nr <= mask || (res & mask) ) + break; + order += bit; + nr >>= bit; + res >>= bit; + } + + return order; +} + +int iommu_map(struct domain *d, dfn_t dfn0, mfn_t mfn0, unsigned long page_count, unsigned int flags, unsigned int *flush_flags) { const struct domain_iommu *hd = dom_iommu(d); unsigned long i; + unsigned int order; int rc = 0; if ( !is_iommu_enabled(d) ) @@ -273,10 +299,16 @@ int iommu_map(struct domain *d, dfn_t df ASSERT(!IOMMUF_order(flags)); - for ( i = 0; i < page_count; i++ ) + for ( i = 0; i < page_count; i += 1UL << order ) { - rc = iommu_call(hd->platform_ops, map_page, d, dfn_add(dfn, i), - mfn_add(mfn, i), flags, flush_flags); + dfn_t dfn = dfn_add(dfn0, i); + mfn_t mfn = mfn_add(mfn0, i); + unsigned long j; + + order = mapping_order(hd, dfn, mfn, page_count - i); + + rc = iommu_call(hd->platform_ops, map_page, d, dfn, mfn, + flags | IOMMUF_order(order), flush_flags); if ( likely(!rc) ) continue; @@ -284,14 +316,18 @@ int iommu_map(struct domain *d, dfn_t df if ( !d->is_shutting_down && printk_ratelimit() ) printk(XENLOG_ERR "d%d: IOMMU mapping dfn %"PRI_dfn" to mfn %"PRI_mfn" failed: %d\n", - d->domain_id, dfn_x(dfn_add(dfn, i)), - mfn_x(mfn_add(mfn, i)), rc); + d->domain_id, dfn_x(dfn), mfn_x(mfn), rc); + + for ( j = 0; j < i; j += 1UL << order ) + { + dfn = dfn_add(dfn0, j); + order = mapping_order(hd, dfn, _mfn(0), i - j); - while ( i-- ) /* if statement to satisfy __must_check */ - if ( iommu_call(hd->platform_ops, unmap_page, d, dfn_add(dfn, i), - 0, flush_flags) ) + if ( iommu_call(hd->platform_ops, unmap_page, d, dfn, order, + flush_flags) ) continue; + } if ( !is_hardware_domain(d) ) domain_crash(d); @@ -322,20 +358,25 @@ int iommu_legacy_map(struct domain *d, d return rc; } -int iommu_unmap(struct domain *d, dfn_t dfn, unsigned long page_count, +int iommu_unmap(struct domain *d, dfn_t dfn0, unsigned long page_count, unsigned int *flush_flags) { const struct domain_iommu *hd = dom_iommu(d); unsigned long i; + unsigned int order; int rc = 0; if ( !is_iommu_enabled(d) ) return 0; - for ( i = 0; i < page_count; i++ ) + for ( i = 0; i < page_count; i += 1UL << order ) { - int err = iommu_call(hd->platform_ops, unmap_page, d, dfn_add(dfn, i), - 0, flush_flags); + dfn_t dfn = dfn_add(dfn0, i); + int err; + + order = mapping_order(hd, dfn, _mfn(0), page_count - i); + err = iommu_call(hd->platform_ops, unmap_page, d, dfn, + order, flush_flags); if ( likely(!err) ) continue; @@ -343,7 +384,7 @@ int iommu_unmap(struct domain *d, dfn_t if ( !d->is_shutting_down && printk_ratelimit() ) printk(XENLOG_ERR "d%d: IOMMU unmapping dfn %"PRI_dfn" failed: %d\n", - d->domain_id, dfn_x(dfn_add(dfn, i)), err); + d->domain_id, dfn_x(dfn), err); if ( !rc ) rc = err;