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 0FA44C87FD1 for ; Tue, 5 Aug 2025 16:05:14 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.1070714.1434332 (Exim 4.92) (envelope-from ) id 1ujK9w-00060B-N4; Tue, 05 Aug 2025 16:05:04 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 1070714.1434332; Tue, 05 Aug 2025 16:05: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 1ujK9w-000604-JR; Tue, 05 Aug 2025 16:05:04 +0000 Received: by outflank-mailman (input) for mailman id 1070714; Tue, 05 Aug 2025 16:05:02 +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 1ujK9u-0005KP-PA for xen-devel@lists.xenproject.org; Tue, 05 Aug 2025 16:05:02 +0000 Received: from mail-ed1-x535.google.com (mail-ed1-x535.google.com [2a00:1450:4864:20::535]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id f119556d-7215-11f0-a321-13f23c93f187; Tue, 05 Aug 2025 18:05:01 +0200 (CEST) Received: by mail-ed1-x535.google.com with SMTP id 4fb4d7f45d1cf-615398dc162so10087224a12.3 for ; Tue, 05 Aug 2025 09:05:01 -0700 (PDT) Received: from [10.156.60.236] (ip-037-024-206-209.um08.pools.vodafone-ip.de. [37.24.206.209]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-615a8fe7a20sm8663522a12.34.2025.08.05.09.04.59 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 05 Aug 2025 09:05:00 -0700 (PDT) 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: f119556d-7215-11f0-a321-13f23c93f187 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1754409901; x=1755014701; darn=lists.xenproject.org; h=content-transfer-encoding:in-reply-to:autocrypt:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:from:to:cc:subject:date:message-id:reply-to; bh=IkJAYVyzbq4UhxLCXnefKHK4q1RB9cUHgpCWqkKsAuI=; b=augDAyeZ65FFA4VdFyYEjiDJVy2yfPLJkgCYEmDglG6dhX8ouhLsaaK8JcIzoXKOwU ghlis2DImkv1HEj79k8fvmlejo6LhQOKEW98v/UF9l5HklxnjBDvzqhxxmT4InSAL9cC nVfttTcjpz6VIuw+xvCl4Fsu1XU27Dkf3AQNzS3ZPtDG2TFHiBWwq2mqUo6d466QBtgi f72bVvqWmX1poxUJNvlJ0Q9zW+W54PL29AeYiv09i1wMceMI8PH/RX/SaQL9AuiLPQ2/ quuvyDIH//L7QTPIoifkJ8e8G+nKCpzr0FdYwyjk0ZV06nlbxxv47vQyYe1/AmpRy72B GKSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754409901; x=1755014701; h=content-transfer-encoding:in-reply-to:autocrypt:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=IkJAYVyzbq4UhxLCXnefKHK4q1RB9cUHgpCWqkKsAuI=; b=dvric2fWzErT5l3kluLuQQdLporODAyLtUC/qPQxSUvqIBcHNBNucev0SHH8eNIHiI s8bB8AqI0ksnC9SyJCZaTJ9PhjNhdr3zVpGqiIockW20ru4hEUjyg2AxT2aKalRjkFuB BVUfArKD6NXwc5VLO63tVBR9ttEywtFCg62gszx4P2KzEZHx/2/1omicrE0bxOTfdJdW 34wRgpBedP0uRFq+EEVuVvUJeFZtfdCkNjxFq2xwbxF72yPeuHGFTDjeFRnQjnPqX37f cuJ3eLcy0COhl9LVPjfHaOFApKVC6rlaBnO5/A091miC/AWwaxrzNxssHVfcB3FFItym Vhyg== X-Forwarded-Encrypted: i=1; AJvYcCVpGb26M8lJf333iQrlzUhXhZEKDxEAa1+lKhX9A2cZNUZuiks0G1zalAzXGGqyj2MroTiXX868ET4=@lists.xenproject.org X-Gm-Message-State: AOJu0YzeG23z4L6wmj0kJ8Pckf/urKegybAAz7TtsCk/39v6l0vtIduY CCpUP2gTVNV1DjvBH/6tk4BAc10Fbc8cgF2KBkFGn92YYt2MX3VdMBKTpxk1tj/8IQ== X-Gm-Gg: ASbGncv/E7mfiqXmdk9YIOAd172toMAFASs2LduEyQ/SotJk4/7dhUdz/e1RExCazYY WBPrlsi6BbsaHIEO+o4oCbb5tUz3NJ0quqvJBwGdGUr3RSJghHdzE16TaUzWti3bpxp3QIGwYhK YBq+pGdWwUn6jjs3+x5UHonMVw0qZo8hj5ZQW9KKnpPST9k2Je/0HT/Sr6Ik5GBgY88uc44XPfp mqBvfSkk9uvwLjD3n+Af4sZCYAOYCS4lCf8qkzFKaaYsArgEK5c8NDxa/cqg4TaY2FpK4FQZ6Kg 8Re691DivVTj9+y3+Z/KzvKZaGd/jRF182DqisBM8x/maL8Lc1SDEfAFjoe35fo4sdHYdM5IbU/ MD1Ha7s6VI7cyQAr2LQ75gS9AyGNDnVONFG67wzj0yuweM/1B/Gl0l8hj63wrueLGCmkuEfzg8M A8sWr+v/c= X-Google-Smtp-Source: AGHT+IHWeUVtspS8GrGYYkrKVwLlqrvJJoextQ0jmJXjbHXoJOO9MQxLu/txbENk7Wbaj4he4DvsTA== X-Received: by 2002:a05:6402:3590:b0:615:8bee:56b6 with SMTP id 4fb4d7f45d1cf-615e717303fmr11309634a12.34.1754409900714; Tue, 05 Aug 2025 09:05:00 -0700 (PDT) Message-ID: Date: Tue, 5 Aug 2025 18:04:59 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v3 12/20] xen/riscv: implement p2m_set_range() To: Oleksii Kurochko Cc: Alistair Francis , Bob Eshleman , Connor Davis , Andrew Cooper , Anthony PERARD , Michal Orzel , Julien Grall , =?UTF-8?Q?Roger_Pau_Monn=C3=A9?= , Stefano Stabellini , xen-devel@lists.xenproject.org References: Content-Language: en-US From: Jan Beulich Autocrypt: addr=jbeulich@suse.com; keydata= xsDiBFk3nEQRBADAEaSw6zC/EJkiwGPXbWtPxl2xCdSoeepS07jW8UgcHNurfHvUzogEq5xk hu507c3BarVjyWCJOylMNR98Yd8VqD9UfmX0Hb8/BrA+Hl6/DB/eqGptrf4BSRwcZQM32aZK 7Pj2XbGWIUrZrd70x1eAP9QE3P79Y2oLrsCgbZJfEwCgvz9JjGmQqQkRiTVzlZVCJYcyGGsD /0tbFCzD2h20ahe8rC1gbb3K3qk+LpBtvjBu1RY9drYk0NymiGbJWZgab6t1jM7sk2vuf0Py O9Hf9XBmK0uE9IgMaiCpc32XV9oASz6UJebwkX+zF2jG5I1BfnO9g7KlotcA/v5ClMjgo6Gl MDY4HxoSRu3i1cqqSDtVlt+AOVBJBACrZcnHAUSuCXBPy0jOlBhxPqRWv6ND4c9PH1xjQ3NP nxJuMBS8rnNg22uyfAgmBKNLpLgAGVRMZGaGoJObGf72s6TeIqKJo/LtggAS9qAUiuKVnygo 3wjfkS9A3DRO+SpU7JqWdsveeIQyeyEJ/8PTowmSQLakF+3fote9ybzd880fSmFuIEJldWxp Y2ggPGpiZXVsaWNoQHN1c2UuY29tPsJgBBMRAgAgBQJZN5xEAhsDBgsJCAcDAgQVAggDBBYC AwECHgECF4AACgkQoDSui/t3IH4J+wCfQ5jHdEjCRHj23O/5ttg9r9OIruwAn3103WUITZee e7Sbg12UgcQ5lv7SzsFNBFk3nEQQCACCuTjCjFOUdi5Nm244F+78kLghRcin/awv+IrTcIWF hUpSs1Y91iQQ7KItirz5uwCPlwejSJDQJLIS+QtJHaXDXeV6NI0Uef1hP20+y8qydDiVkv6l IreXjTb7DvksRgJNvCkWtYnlS3mYvQ9NzS9PhyALWbXnH6sIJd2O9lKS1Mrfq+y0IXCP10eS FFGg+Av3IQeFatkJAyju0PPthyTqxSI4lZYuJVPknzgaeuJv/2NccrPvmeDg6Coe7ZIeQ8Yj t0ARxu2xytAkkLCel1Lz1WLmwLstV30g80nkgZf/wr+/BXJW/oIvRlonUkxv+IbBM3dX2OV8 AmRv1ySWPTP7AAMFB/9PQK/VtlNUJvg8GXj9ootzrteGfVZVVT4XBJkfwBcpC/XcPzldjv+3 HYudvpdNK3lLujXeA5fLOH+Z/G9WBc5pFVSMocI71I8bT8lIAzreg0WvkWg5V2WZsUMlnDL9 mpwIGFhlbM3gfDMs7MPMu8YQRFVdUvtSpaAs8OFfGQ0ia3LGZcjA6Ik2+xcqscEJzNH+qh8V m5jjp28yZgaqTaRbg3M/+MTbMpicpZuqF4rnB0AQD12/3BNWDR6bmh+EkYSMcEIpQmBM51qM EKYTQGybRCjpnKHGOxG0rfFY1085mBDZCH5Kx0cl0HVJuQKC+dV2ZY5AqjcKwAxpE75MLFkr wkkEGBECAAkFAlk3nEQCGwwACgkQoDSui/t3IH7nnwCfcJWUDUFKdCsBH/E5d+0ZnMQi+G0A nAuWpQkjM1ASeQwSHEeAWPgskBQL In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 31.07.2025 17:58, Oleksii Kurochko wrote: > This patch introduces p2m_set_range() and its core helper p2m_set_entry() for Nit: This patch doesn't introduce p2m_set_range(); it merely fleshes it out. > --- a/xen/arch/riscv/include/asm/p2m.h > +++ b/xen/arch/riscv/include/asm/p2m.h > @@ -7,11 +7,13 @@ > #include > #include > > +#include > #include > > extern unsigned int p2m_root_order; > #define P2M_ROOT_ORDER p2m_root_order > #define P2M_ROOT_PAGES BIT(P2M_ROOT_ORDER, U) > +#define P2M_ROOT_LEVEL HYP_PT_ROOT_LEVEL I think I commented on this before, and I would have hoped for at least a remark in the description to appear (perhaps even a comment here): It's okay(ish) to tie these together for now, but in the longer run I don't expect this is going to be wanted. If e.g. we ran Xen in Sv57 mode, there would be no reason at all to force all P2Ms to use 5 levels of page tables. > @@ -175,13 +179,257 @@ int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preempted) > return 0; > } > > +/* > + * Find and map the root page table. The caller is responsible for > + * unmapping the table. > + * > + * The function will return NULL if the offset into the root table is > + * invalid. > + */ > +static pte_t *p2m_get_root_pointer(struct p2m_domain *p2m, gfn_t gfn) > +{ > + unsigned long root_table_indx; > + > + root_table_indx = gfn_x(gfn) >> XEN_PT_LEVEL_ORDER(P2M_ROOT_LEVEL); Right now page table layouts / arrangements are indeed similar enough to share accessor constructs. Nevertheless I find it problematic (doc-wise at the very least) that a Xen page table construct is used to access a P2M page table. If and when these needed to be decoupled, it would likely help of the distinction was already made, by - for now - simply introducing aliases (here e.g. P2M_LEVEL_ORDER(), expanding to XEN_PT_LEVEL_ORDER() for the time being). > + if ( root_table_indx >= P2M_ROOT_PAGES ) > + return NULL; > + > + return __map_domain_page(p2m->root + root_table_indx); > +} > + > +static inline void p2m_write_pte(pte_t *p, pte_t pte, bool clean_pte) > +{ > + write_pte(p, pte); > + if ( clean_pte ) > + clean_dcache_va_range(p, sizeof(*p)); Not necessarily for right away, but if multiple adjacent PTEs are written without releasing the lock, this then redundant cache flushing can be a performance issue. > +} > + > +static inline void p2m_clean_pte(pte_t *p, bool clean_pte) > +{ > + pte_t pte; > + > + memset(&pte, 0, sizeof(pte)); Why memset()? Why not simply give the variable an appropriate initializer? Or use ... > + p2m_write_pte(p, pte, clean_pte); ... a compound literal here, like you do ... > +} > + > +static pte_t p2m_pte_from_mfn(mfn_t mfn, p2m_type_t t) > +{ > + panic("%s: hasn't been implemented yet\n", __func__); > + > + return (pte_t) { .pte = 0 }; ... here? (Just {} would also do, if I'm not mistaken.) > +} > + > +#define P2M_TABLE_MAP_NONE 0 > +#define P2M_TABLE_MAP_NOMEM 1 > +#define P2M_TABLE_SUPER_PAGE 2 > +#define P2M_TABLE_NORMAL 3 > + > +/* > + * Take the currently mapped table, find the corresponding the entry > + * corresponding to the GFN, and map the next table, if available. Nit: Double "corresponding". > + * The previous table will be unmapped if the next level was mapped > + * (e.g P2M_TABLE_NORMAL returned). > + * > + * `alloc_tbl` parameter indicates whether intermediate tables should > + * be allocated when not present. > + * > + * Return values: > + * P2M_TABLE_MAP_NONE: a table allocation isn't permitted. > + * P2M_TABLE_MAP_NOMEM: allocating a new page failed. > + * P2M_TABLE_SUPER_PAGE: next level or leaf mapped normally. > + * P2M_TABLE_NORMAL: The next entry points to a superpage. > + */ > +static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl, > + unsigned int level, pte_t **table, > + unsigned int offset) > +{ > + panic("%s: hasn't been implemented yet\n", __func__); > + > + return P2M_TABLE_MAP_NONE; > +} > + > +/* Free pte sub-tree behind an entry */ > +static void p2m_free_subtree(struct p2m_domain *p2m, > + pte_t entry, unsigned int level) > +{ > + panic("%s: hasn't been implemented yet\n", __func__); > +} > + > +/* > + * Insert an entry in the p2m. This should be called with a mapping > + * equal to a page/superpage. > + */ > +static int p2m_set_entry(struct p2m_domain *p2m, > + gfn_t gfn, > + unsigned long page_order, > + mfn_t mfn, > + p2m_type_t t) > +{ > + unsigned int level; > + unsigned int target = page_order / PAGETABLE_ORDER; > + pte_t *entry, *table, orig_pte; > + int rc; > + /* A mapping is removed if the MFN is invalid. */ > + bool removing_mapping = mfn_eq(mfn, INVALID_MFN); Comment and code don't fit together. Many MFNs are invalid (any for which mfn_valid() returns false), yet you only check for INVALID_MFN here. > + DECLARE_OFFSETS(offsets, gfn_to_gaddr(gfn)); > + > + ASSERT(p2m_is_write_locked(p2m)); > + > + /* > + * Check if the level target is valid: we only support > + * 4K - 2M - 1G mapping. > + */ > + ASSERT((target <= 2) && !(page_order % PAGETABLE_ORDER)); If you think you need to check this, don't you also want to check that GFN and MFN (the latter if it isn't INVALID_MFN) fit the requested order? > + table = p2m_get_root_pointer(p2m, gfn); > + if ( !table ) > + return -EINVAL; > + > + for ( level = P2M_ROOT_LEVEL; level > target; level-- ) > + { > + /* > + * Don't try to allocate intermediate page table if the mapping > + * is about to be removed. > + */ > + rc = p2m_next_level(p2m, !removing_mapping, > + level, &table, offsets[level]); > + if ( (rc == P2M_TABLE_MAP_NONE) || (rc == P2M_TABLE_MAP_NOMEM) ) > + { > + rc = (rc == P2M_TABLE_MAP_NONE) ? -ENOENT : -ENOMEM; > + /* > + * We are here because p2m_next_level has failed to map > + * the intermediate page table (e.g the table does not exist > + * and they p2m tree is read-only). It is a valid case > + * when removing a mapping as it may not exist in the > + * page table. In this case, just ignore it. > + */ > + rc = removing_mapping ? 0 : rc; Nit: Stray blank. > + goto out; > + } > + > + if ( rc != P2M_TABLE_NORMAL ) > + break; > + } > + > + entry = table + offsets[level]; > + > + /* > + * If we are here with level > target, we must be at a leaf node, > + * and we need to break up the superpage. > + */ > + if ( level > target ) > + { > + panic("Shattering isn't implemented\n"); > + } > + > + /* > + * We should always be there with the correct level because all the > + * intermediate tables have been installed if necessary. > + */ > + ASSERT(level == target); > + > + orig_pte = *entry; > + > + if ( removing_mapping ) > + p2m_clean_pte(entry, p2m->clean_pte); > + else > + { > + pte_t pte = p2m_pte_from_mfn(mfn, t); > + > + p2m_write_pte(entry, pte, p2m->clean_pte); > + > + p2m->max_mapped_gfn = gfn_max(p2m->max_mapped_gfn, > + gfn_add(gfn, BIT(page_order, UL) - 1)); > + p2m->lowest_mapped_gfn = gfn_min(p2m->lowest_mapped_gfn, gfn); > + } > + > + p2m->need_flush = true; > + > + /* > + * Currently, the infrastructure required to enable CONFIG_HAS_PASSTHROUGH > + * is not ready for RISC-V support. > + * > + * When CONFIG_HAS_PASSTHROUGH=y, iommu_iotlb_flush() should be done > + * here. > + */ > +#ifdef CONFIG_HAS_PASSTHROUGH > +# error "add code to flush IOMMU TLB" > +#endif > + > + rc = 0; > + > + /* > + * Free the entry only if the original pte was valid and the base > + * is different (to avoid freeing when permission is changed). > + */ > + if ( pte_is_valid(orig_pte) && > + !mfn_eq(pte_get_mfn(*entry), pte_get_mfn(orig_pte)) ) I'm puzzled by this 2nd check: A permission change would - I expect - only occur to a leaf entry. If the new entry is a super-page one and the old wasn't, don't you still need to free the sub-tree, no matter whether the MFNs are the same? Plus consider the special case of MFN 0: If you clear an entry using MFN 0, you will find old and new PTEs' both having the same MFN. > static int p2m_set_range(struct p2m_domain *p2m, > gfn_t sgfn, > unsigned long nr, > mfn_t smfn, > p2m_type_t t) > { > - return -EOPNOTSUPP; > + int rc = 0; > + unsigned long left = nr; > + > + /* > + * Any reference taken by the P2M mappings (e.g. foreign mapping) will > + * be dropped in relinquish_p2m_mapping(). As the P2M will still > + * be accessible after, we need to prevent mapping to be added when the > + * domain is dying. > + */ > + if ( unlikely(p2m->domain->is_dying) ) > + return -EACCES; > + > + while ( left ) > + { > + unsigned long order = p2m_mapping_order(sgfn, smfn, left); > + > + rc = p2m_set_entry(p2m, sgfn, order, smfn, t); > + if ( rc ) > + break; > + > + sgfn = gfn_add(sgfn, BIT(order, UL)); > + if ( !mfn_eq(smfn, INVALID_MFN) ) > + smfn = mfn_add(smfn, BIT(order, UL)); > + > + left -= BIT(order, UL); > + } > + > + return !left ? 0 : left == nr ? rc : (nr - left); The function returning "int", you may be truncating the return value here. In the worst case indicating success (0) or an error (negative) when some of the upper bits were set. Also looks like you could get away with a single conditional operator here: return !left || left == nr ? rc : (nr - left); Jan