From patchwork Mon Apr 25 08:41:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 12825404 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 71D3DC433EF for ; Mon, 25 Apr 2022 08:41:40 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.312558.529863 (Exim 4.92) (envelope-from ) id 1niuHg-0004Kn-Ig; Mon, 25 Apr 2022 08:41:28 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 312558.529863; Mon, 25 Apr 2022 08:41:28 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1niuHg-0004Kc-Fb; Mon, 25 Apr 2022 08:41:28 +0000 Received: by outflank-mailman (input) for mailman id 312558; Mon, 25 Apr 2022 08:41:27 +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 1niuHf-0003e7-01 for xen-devel@lists.xenproject.org; Mon, 25 Apr 2022 08:41:27 +0000 Received: from de-smtp-delivery-102.mimecast.com (de-smtp-delivery-102.mimecast.com [194.104.111.102]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 7e336170-c473-11ec-a405-831a346695d4; Mon, 25 Apr 2022 10:41:26 +0200 (CEST) Received: from EUR04-DB3-obe.outbound.protection.outlook.com (mail-db3eur04lp2053.outbound.protection.outlook.com [104.47.12.53]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id de-mta-10-ZktRNjkqPfWbQ1YRYp5RlQ-2; Mon, 25 Apr 2022 10:41:23 +0200 Received: from DU2PR04MB8616.eurprd04.prod.outlook.com (2603:10a6:10:2db::16) by DB8PR04MB6891.eurprd04.prod.outlook.com (2603:10a6:10:112::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5186.21; Mon, 25 Apr 2022 08:41:22 +0000 Received: from DU2PR04MB8616.eurprd04.prod.outlook.com ([fe80::5cb0:5195:4203:7c2f]) by DU2PR04MB8616.eurprd04.prod.outlook.com ([fe80::5cb0:5195:4203:7c2f%9]) with mapi id 15.20.5186.021; Mon, 25 Apr 2022 08:41:22 +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: 7e336170-c473-11ec-a405-831a346695d4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=mimecast20200619; t=1650876085; 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=R8DiCPGmZfdIitLCnmZ7S3mCQH5fBWGDgn8DzVJxrZM=; b=cG5HNqJ85x5KfaghM7F4h1epa3hfnbhsd4u+khx3ZB5JZ6+D84+6O0jx9bm54GQ/P/qxsf xmgl3PpI0YXapJV5ei/DTTg1r4zNWazfHFzbIBCHjMrLX0gUPKSHcFhGjDx445mYMNlA0f /UuAgjHrNja+egtScCbMNOHYobjaz7o= X-MC-Unique: ZktRNjkqPfWbQ1YRYp5RlQ-2 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=coDZ6RpsG602xyEM7tLBo7nhvZW3Ko6pEtf1UXXXmMOcujikRDFcu7Aoh0BjUypvHQi/nVS7pLvgb2b1Pm8OtxoEWItdP42EXLRrJIGnos2xgNrZqGLR3GnEtGlOTIwSIqPi3yH7JO6l9DErytmtoOfuzsgFuu+raBzNrawSWOtDrgyDggb1QgMAufpqM6jrWfkR9WbSFZnkpZEZldkwkDzIswXnNTQ212PkNuIdxS3pOTt/P3dSTdXZCxIp+dwwc8leld9s2v2YTIIETVllWga08w398fri79N9RKFLQTsVbIPqc+JkR4n1a8p4ShqJ2SKZp/Dp4IqhYTC3Kc9//w== 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=R8DiCPGmZfdIitLCnmZ7S3mCQH5fBWGDgn8DzVJxrZM=; b=M3o93GbzCeH6THWaLvpzP3/l2TovQH+tocAd70n3h5+tkyqjDbH2zQ2VytALlubRohBExvTfP2o0tyIOfFGkzTk5XCWBYOzHs6gspcDoMvgpEKz9qAVvEeoMZYq9eYePDgStbhGeCEB+aSsj7OOWo6r2eI4VwX+nvMg4w5feLEQdM8Y/Yh/nDvmCvBpqPG6/ibaUOlXuPd+yKNCRbIOuaPJIpP+gBhz/RbeMSZx/fS7dbQtqUd2Z7mgG0t8UNJ2nVGLSDY3f1ZwEd4yyddatpMNXbbwdawoU9ngHJWuhwKFeifRKKd7bs2x6xZeS7GilOm0p8TbSPXWZV4tSq2fdlQ== 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: Date: Mon, 25 Apr 2022 10:41:23 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.8.1 Subject: [PATCH v4 14/21] 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: In-Reply-To: X-ClientProxiedBy: AM6P194CA0043.EURP194.PROD.OUTLOOK.COM (2603:10a6:209:84::20) To DU2PR04MB8616.eurprd04.prod.outlook.com (2603:10a6:10:2db::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 16e2feb1-0837-4112-01c3-08da2697606e X-MS-TrafficTypeDiagnostic: DB8PR04MB6891: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: zXiGfoVkqc9pAj1Wobos8VX3wDvPd7bajFrbhXS3nJqZbhuq9LGg0a2vBL7nVq/vbjtA9B8Dl1Sp8tXqNSHNe0gFl7BOt3cbop5pV5b3WbYvg+q//Fb2sabk3ZMLTIMbAOQTuwNqxIjwxHUxuF6uzF/KbOdn9jLj3I2Nwy3xycN9dC3WjQWmI9bJ6S1QgVEXjEEzTqmmbHJzceWMy73CwuoNftvM303NAn1l7dGPaKIeLfXLMiG6qqhVv/4vlq6GhX7l2AHCdQFc9Jlk/MamVjoORqqYPWPFPCSwxX0jfKA74gYbmv0gb5uSldYWL2Tlw1AzZeK1RkqvwW+Fg+tYL+1ONzoHPIjKOd/jYKDLYjxXAmz3gdv0kvzKoJYG+Rl4STnUzrpaWEqHH9+OVz73OU+Za4NjuAyTkQ1R/tkIKF0BBnuCIhM8zkKecc4o6QFO0BL7Pn5FSabySHh+h9+MMaTz9JvAUbvffb+xNDIMtPGCzc/vbJd9N5cNXN+PTkh2gnb14v4FzpkkzYVDHphSMMbdHGETC3OIs/g366s6WZKBjhvKQ5QYsAL6ZxJzLPTkb8xOByeFtNORmF8IYLY2PnxY9qGxF2EJGvo/HQGmfyUZympB1UM4f24EeOS8LcOCnuMnvet88tSs9FP6NbJju4AiMywXJPR75LkHcJmojkdDmtZ0ma3NXBgAwcw86EHt/OsuhltnbDkUHNM1Zkuz6q3aodmk7tJdUMJHqiRNf2A= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DU2PR04MB8616.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(66556008)(26005)(66946007)(6512007)(38100700002)(508600001)(66476007)(316002)(6506007)(83380400001)(54906003)(6486002)(186003)(6916009)(86362001)(31696002)(2616005)(2906002)(5660300002)(31686004)(8936002)(36756003)(4326008)(8676002)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?CNIh2Ohvpk3rJSkKbkyQkcNbzBXq?= =?utf-8?q?5dKn5VbOVUKabEjUz3LTuhsy+BhrENIRBgGqWFz3k7f3ZX1WJdiMsIIgpSseVW9Eh?= =?utf-8?q?SE+Abt4nBBL4UmoReKvnWfGVtg+EMTW9+aVlV5UldxQEVur61ApOOQno8OjnuMSOA?= =?utf-8?q?YH310KpQ8uisTElPRFurGVE/sQjUlXS9RhP2n1U3omOGAaquKQB15K8ye2bnNxM5V?= =?utf-8?q?zYCoaS2Fum0dQmDnwr1bIILoI7bRch2X9+BmcB8clqp3DltsOaaU5kP8G4TVvuN9e?= =?utf-8?q?1xoSDplHa9JlNj7loNHK+u3Bf0T5strcVnW1Far2kPCkL9bQFbo30bn5oIufhY/BS?= =?utf-8?q?5no7lLBE1s3U0bvzJJtEP1LUID3Aw/3Qb2tvrckCA3LBDtRnIAOxbJzjn9C7VF+a5?= =?utf-8?q?9c5DBPt6WEnkMTCv4x71AFIBz1Si/r9f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zmxehqN6f3PrtDooJU+?= =?utf-8?q?OAHg6SVgIRTJPlQEJn6SPJKdS6a570cQ6ipPI2KMi4nK7wuCTWiibU2MQZR0RyzkF?= =?utf-8?q?OKSyTeBx6w0ZlFihLtukVxC9hc1rNmdCXGwr1UzUgzMfPhi6VkZnJXCnWtaQGKEZz?= =?utf-8?q?dbVQ4J/FGk0geQL3PaZf2HplFyQpyDtVi2GQ8gN0GmxjnOYJNV+LPyCNxFHlBoWXo?= =?utf-8?q?aNXP1DEhZCWXAMPo2ivzPf4KSvKX/acs5QWhSnlo6PCNh4P5r1h2PZL1XajdlK+/X?= =?utf-8?q?Cjt1/Ku4h+1P/Zyd/34+G65ofQyLE0VG4intUhdtp4MHsJZBhyB0diSWs00zWnxey?= =?utf-8?q?7D/Gyn4v7h9CiKebTCkHwoT97i5i6u5w=3D=3D?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: 16e2feb1-0837-4112-01c3-08da2697606e X-MS-Exchange-CrossTenant-AuthSource: DU2PR04MB8616.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Apr 2022 08:41:22.7491 (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: e2I5vDMK/fGIZa0bT3yfQpKU/+SM2HnQVxQsKx/3m4fpNuRjVuhNv3vxrem1NWc5BF9rceepUr7X0ihfTYDvpg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB8PR04MB6891 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é --- 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,105 @@ +#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, + * - page tables to be passed here need to be initialized with correct + * markers. + */ + +#include +#include +#include + +/* 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) + +#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 ) + 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_NR +#undef CONTIG_LEVEL_SHIFT +#undef CONTIG_MASK + +#endif /* __ASM_X86_PT_CONTIG_MARKERS_H */