From patchwork Fri May 27 11:17:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 12863298 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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 71C9CC433EF for ; Fri, 27 May 2022 11:17:27 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.337978.562705 (Exim 4.92) (envelope-from ) id 1nuXxz-0002A7-DV; Fri, 27 May 2022 11:17:15 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 337978.562705; Fri, 27 May 2022 11:17:15 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nuXxz-00029y-Al; Fri, 27 May 2022 11:17:15 +0000 Received: by outflank-mailman (input) for mailman id 337978; Fri, 27 May 2022 11:17:14 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nuXxy-0001xA-Ha for xen-devel@lists.xenproject.org; Fri, 27 May 2022 11:17:14 +0000 Received: from de-smtp-delivery-102.mimecast.com (de-smtp-delivery-102.mimecast.com [194.104.109.102]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 8ef28913-ddae-11ec-bd2c-47488cf2e6aa; Fri, 27 May 2022 13:17:13 +0200 (CEST) Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05lp2111.outbound.protection.outlook.com [104.47.17.111]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id de-mta-39-dBmytWmVNeSxHDLZLFECnA-1; Fri, 27 May 2022 13:17:11 +0200 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) by AM6PR04MB5831.eurprd04.prod.outlook.com (2603:10a6:20b:a8::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.13; Fri, 27 May 2022 11:17:09 +0000 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::dfa:a64a:432f:e26b]) by VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::dfa:a64a:432f:e26b%7]) with mapi id 15.20.5293.013; Fri, 27 May 2022 11:17:09 +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: 8ef28913-ddae-11ec-bd2c-47488cf2e6aa DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=mimecast20200619; t=1653650233; 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=fkjYnsuJuIkyi038qFhXwji2/Rxpp4JG0amuTKIuhJU=; b=FYDjvmFsXTb2LDfTo+Wfp+gP6bbl9LChp6S3wnzMHnqiCBWTcTDhIgzzmeembQrnQaapWk 3A1XGTujwxTBoEWmQN+YXIjRjqKkC7LRVBi3flmWGfrV4NtGBZo1jIXxtAQrU0kCL0NhiF HKuptOrvVBklbrXHTpcryCZJGK0+yow= X-MC-Unique: dBmytWmVNeSxHDLZLFECnA-1 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=GaiVRM7IsofcFjdsYnr/lvCCJ2LKSMJcB1TSnm7OWgBWE9dgispmLhM2/DJMTRB8TKmLJ98e8V/xn3YXuzAwkis8LEciMRRxhI1I2cfwK4KutMZuXdclPMOtsNIuJyO4O9C11JAR9l1ZeVvyGN9GxVVJA7AENiNkB1iX/fuKi2PieCijDrnGw0FGBfglfbFqjrRvcB23yTjOozGiHEUIrIs0H9HFzJ0TqXfYmatPoWym3snI6swZFsAfdMUTvQnfyVL3sIz8mM2sI4tBqReXEjpJXj8+xL6U78j/0wn9Hh2w7QASxwgGDzliGnUH8K4InQ2VcIAYTw/TrThIlHanJg== 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=RkfpNnHTVuEAPWGDCPWVLqkwh9PtWcL+B3weEOaUpQY=; b=lFAHPueMCaUYBOdm9GM43qgC7pk9uslA+VmHteOjHVpgfm4AMLpummoEBFHtw+TTh77NYgQ+8FKZaDuCHx6h06gJOJwc9ZlUd7uT7/E7HCuVLD1gXEAuCW/+TP5g+k95QX7MG5osptpUv7RCONr9aa/oJ2b0gkDkeqbSd5AUr85nCBhcp++FGZySFEV5FcTjEOcI6R+/lQR2K6SDrvDqQ2pzgCj1JoFFyGP+XxTl4KVaRwRPU5hcOUkytBIvkbx6cBIycln3BjW9jj5cTxxeRK4YSUUJut++fHQ1vTkIf/OOfTa5BHpoFTate0Jsjih1ht655C8xaW0ILR8shbL1nQ== 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: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Message-ID: <1fec512a-8c7b-69b5-40bf-88b42e9ecb7d@suse.com> Date: Fri, 27 May 2022 13:17:08 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: [PATCH v5 07/15] x86: introduce helper for recording degree of contiguity in page tables Content-Language: en-US From: Jan Beulich To: "xen-devel@lists.xenproject.org" CC: Andrew Cooper , Paul Durrant , =?utf-8?q?Roger_Pau_Monn=C3=A9?= , Wei Liu References: <80448822-bc1c-9f7d-ade5-fdf7c46421fe@suse.com> In-Reply-To: <80448822-bc1c-9f7d-ade5-fdf7c46421fe@suse.com> X-ClientProxiedBy: AS8PR05CA0020.eurprd05.prod.outlook.com (2603:10a6:20b:311::25) To VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: d999bae7-e061-4c62-c6b6-08da3fd270e7 X-MS-TrafficTypeDiagnostic: AM6PR04MB5831: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: x2aP4wBf3Asdk8nuF2D3A7UefiwMF/84sheBRy3YoZpDJRgKCyZGrvNxmC9+W2inPTpip0sd8rijs/OmRWiyvTe6StmhQ83k8tJJssSQzWN1HD9RhoVvaww4A/r/qtaLfSzyTXBXge7FXEo+0ngg+c5h5Y6FTR2/yWBEPSrwN0NA6UeuEewNv4THS0VDSpX9FWMvUt7hfr3wZe+H7D4/slFvyFoIpSGG3DzsB+AEqTI9fafSGlYxKmCIODJIdliQ9YI82q+gKjesx85aZ2iHTYCkqAPd35eLodPMku4XAZWriKG9RkbqB3v/VN+PzlVTS+rgyr/2T4sM/8e9hCDV02Slhk+AcYZ/4PTckkkP+4yZZeg3xGbharO35w/Wz2wvU+XIec5PbgGyy3/UUrZpdZxOlHnw6h7MnJf7hCZGcIprJACUgXCNFmn6+Q/oXijH4eMemYVPwZn8HjZlgh1492mRkS3+qCmfRf2ZQFwGoWJJLr2vgNBA1iNIUyIBkVm1TrWyahY64id6lou468cHyeZ8k6RtHk2EHuUQaH25UHsAk0C7rpwDhsFfhOUjbYPJXss/KxcvCzLUju2UvvEsNh+MwZBo34BvbyuWhzsZ2peS1ZpdQaA9jwBOdvtmQyNUKA4n6A7la8Y62+bcICTd0IZeaWnWp2j8KodgmU+WS4WjQVIPSbdk8qnQP0U0eUTbkRsHpiDWhHO1s1UcuhhdJsCODgUPNYXNRUhcKNVjjrUV/oog9OIFCge2q32HMuEC X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:VE1PR04MB6560.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(6512007)(2616005)(26005)(6506007)(316002)(66556008)(66946007)(54906003)(508600001)(8676002)(4326008)(66476007)(6486002)(86362001)(38100700002)(31696002)(186003)(83380400001)(5660300002)(31686004)(2906002)(8936002)(36756003)(6916009)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: lpnYAR/1UiEYox+o/8FG7fJd210Tsl/sW23QRdqypYJ9a3cQUYuOCXabIVCBUs7Re7KxfLAGNk8ZaWQ0SvDqmfLy6vDZDuVKwJ14Eb64yOYj14G5utPDguKBat08KH6UqMTse/zo1sRmZ2ER2Lb76jKs3/Bi9c3GorEkWmLzvUr0a2YQdp2yK1f9JuqtAFKjnBlzAeeDUUgmEOsc3ZLD40fpIYlLCdt5ndKM0mPVo1d7wGae+CP6XWA/qKwk/euYQhCATBSiOi6OQOADC2OV5/gzu+G8JlMICuum5dX4L/ExI5m/4eCGVK97Au5GwOpIiqwa/RHkP6894not3ezpYtZsiSGcN/zBf9Ew55a/zoHwnKmUSOFRC5zIWqb7JcpXU7NZolAU21txuEuHD2iOxtQim+FP9CAbzm6S3NZJOGLadX4C2egbVZIDRNjauFsRtZgC+6JWcAIPTb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ts3EC6uhuYog7oVBiqlDx3i2FmjXZmvjKdzQIKZGNI/H3RfwDxZ+Eqff1Th/hAOOyPJnxgjhA6XFTQYYidU/ZjuUrbWN+rqaXkvphkSNc+4HpGTWIlBMb4+zt/oHZ0N6K7AHco9mqKBoct+BGfQ+PE8d0OTKKjcaURlb2ZOtJkLh21fX0dA/5WBiCYeOFD8PGMiaNwQLY0Q== X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: d999bae7-e061-4c62-c6b6-08da3fd270e7 X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6560.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 May 2022 11:17:09.7672 (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: znrazilBfjJYgAwkY3rO3/hQTUHWvi2dn6/2sLX9NHq0giw6Ga3hK+mTl0zJLAc2VhsJ86b5AQNuw0FpLVnX6Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6PR04MB5831 This is a re-usable helper (kind of a template) which gets introduced without users so that the individual subsequent patches introducing such users can get committed independently of one another. See the comment at the top of the new file. To demonstrate the effect, if a page table had just 16 entries, this would be the set of markers for a page table with fully contiguous mappings: index 0 1 2 3 4 5 6 7 8 9 A B C D E F marker 4 0 1 0 2 0 1 0 3 0 1 0 2 0 1 0 "Contiguous" here means not only present entries with successively increasing MFNs, each one suitably aligned for its slot, but also a respective number of all non-present entries. Signed-off-by: Jan Beulich Reviewed-by: Roger Pau Monné --- @Roger: I've retained your R-b, but I was on the edge of dropping it. --- v5: Bail early from step 1 if possible. Arrange for consumers who are just after CONTIG_{LEVEL_SHIFT,NR}. Extend comment. v3: Rename function and header. Introduce IS_CONTIG(). v2: New. --- /dev/null +++ b/xen/arch/x86/include/asm/pt-contig-markers.h @@ -0,0 +1,110 @@ +#ifndef __ASM_X86_PT_CONTIG_MARKERS_H +#define __ASM_X86_PT_CONTIG_MARKERS_H + +/* + * Short of having function templates in C, the function defined below is + * intended to be used by multiple parties interested in recording the + * degree of contiguity in mappings by a single page table. + * + * Scheme: Every entry records the order of contiguous successive entries, + * up to the maximum order covered by that entry (which is the number of + * clear low bits in its index, with entry 0 being the exception using + * the base-2 logarithm of the number of entries in a single page table). + * While a few entries need touching upon update, knowing whether the + * table is fully contiguous (and can hence be replaced by a higher level + * leaf entry) is then possible by simply looking at entry 0's marker. + * + * Prereqs: + * - CONTIG_MASK needs to be #define-d, to a value having at least 4 + * contiguous bits (ignored by hardware), before including this file (or + * else only CONTIG_LEVEL_SHIFT and CONTIG_NR will become available), + * - page tables to be passed to the helper need to be initialized with + * correct markers, + * - not-present entries need to be entirely clear except for the marker. + */ + +/* This is the same for all anticipated users, so doesn't need passing in. */ +#define CONTIG_LEVEL_SHIFT 9 +#define CONTIG_NR (1 << CONTIG_LEVEL_SHIFT) + +#ifdef CONTIG_MASK + +#include +#include +#include + +#define GET_MARKER(e) MASK_EXTR(e, CONTIG_MASK) +#define SET_MARKER(e, m) \ + ((void)((e) = ((e) & ~CONTIG_MASK) | MASK_INSR(m, CONTIG_MASK))) + +#define IS_CONTIG(kind, pt, i, idx, shift, b) \ + ((kind) == PTE_kind_leaf \ + ? (((pt)[i] ^ (pt)[idx]) & ~CONTIG_MASK) == (1ULL << ((b) + (shift))) \ + : !((pt)[i] & ~CONTIG_MASK)) + +enum PTE_kind { + PTE_kind_null, + PTE_kind_leaf, + PTE_kind_table, +}; + +static bool pt_update_contig_markers(uint64_t *pt, unsigned int idx, + unsigned int level, enum PTE_kind kind) +{ + unsigned int b, i = idx; + unsigned int shift = (level - 1) * CONTIG_LEVEL_SHIFT + PAGE_SHIFT; + + ASSERT(idx < CONTIG_NR); + ASSERT(!(pt[idx] & CONTIG_MASK)); + + /* Step 1: Reduce markers in lower numbered entries. */ + while ( i ) + { + b = find_first_set_bit(i); + i &= ~(1U << b); + if ( GET_MARKER(pt[i]) <= b ) + break; + SET_MARKER(pt[i], b); + } + + /* An intermediate table is never contiguous with anything. */ + if ( kind == PTE_kind_table ) + return false; + + /* + * Present entries need in-sync index and address to be a candidate + * for being contiguous: What we're after is whether ultimately the + * intermediate table can be replaced by a superpage. + */ + if ( kind != PTE_kind_null && + idx != ((pt[idx] >> shift) & (CONTIG_NR - 1)) ) + return false; + + /* Step 2: Check higher numbered entries for contiguity. */ + for ( b = 0; b < CONTIG_LEVEL_SHIFT && !(idx & (1U << b)); ++b ) + { + i = idx | (1U << b); + if ( !IS_CONTIG(kind, pt, i, idx, shift, b) || GET_MARKER(pt[i]) != b ) + break; + } + + /* Step 3: Update markers in this and lower numbered entries. */ + for ( ; SET_MARKER(pt[idx], b), b < CONTIG_LEVEL_SHIFT; ++b ) + { + i = idx ^ (1U << b); + if ( !IS_CONTIG(kind, pt, i, idx, shift, b) || GET_MARKER(pt[i]) != b ) + break; + idx &= ~(1U << b); + } + + return b == CONTIG_LEVEL_SHIFT; +} + +#undef IS_CONTIG +#undef SET_MARKER +#undef GET_MARKER +#undef CONTIG_MASK + +#endif /* CONTIG_MASK */ + +#endif /* __ASM_X86_PT_CONTIG_MARKERS_H */