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 <jbeulich@suse.com>
---
v2: New.
--- /dev/null
+++ b/xen/include/asm-x86/contig-marker.h
@@ -0,0 +1,105 @@
+#ifndef __ASM_X86_CONTIG_MARKER_H
+#define __ASM_X86_CONTIG_MARKER_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 <xen/bitops.h>
+#include <xen/lib.h>
+#include <xen/page-size.h>
+
+/* 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)))
+
+enum PTE_kind {
+ PTE_kind_null,
+ PTE_kind_leaf,
+ PTE_kind_table,
+};
+
+static bool 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 ( (kind == PTE_kind_leaf
+ ? ((pt[i] ^ pt[idx]) & ~CONTIG_MASK) != (1ULL << (b + shift))
+ : pt[i] & ~CONTIG_MASK) ||
+ 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 ( (kind == PTE_kind_leaf
+ ? ((pt[i] ^ pt[idx]) & ~CONTIG_MASK) != (1ULL << (b + shift))
+ : pt[i] & ~CONTIG_MASK) ||
+ GET_MARKER(pt[i]) != b )
+ break;
+ idx &= ~(1U << b);
+ }
+
+ return b == CONTIG_LEVEL_SHIFT;
+}
+
+#undef SET_MARKER
+#undef GET_MARKER
+#undef CONTIG_NR
+#undef CONTIG_LEVEL_SHIFT
+#undef CONTIG_MASK
+
+#endif /* __ASM_X86_CONTIG_MARKER_H */
On Fri, Sep 24, 2021 at 11:55:30AM +0200, Jan Beulich wrote: > 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 <jbeulich@suse.com> > --- > v2: New. > > --- /dev/null > +++ b/xen/include/asm-x86/contig-marker.h > @@ -0,0 +1,105 @@ > +#ifndef __ASM_X86_CONTIG_MARKER_H > +#define __ASM_X86_CONTIG_MARKER_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. Given this requirement I think it would make sense to place the page table marker initialization currently placed in iommu_alloc_pgtable as a helper here also? > + */ > + > +#include <xen/bitops.h> > +#include <xen/lib.h> > +#include <xen/page-size.h> > + > +/* 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))) > + > +enum PTE_kind { > + PTE_kind_null, > + PTE_kind_leaf, > + PTE_kind_table, > +}; > + > +static bool update_contig_markers(uint64_t *pt, unsigned int idx, Maybe pt_update_contig_markers, so it's not such a generic name. > + 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)) ) Don't you just need to check that the address is aligned to at least idx, not that it's exactly aligned? AFAICT this will return early if the address has an alignment that exceeds the requirements imposed by idx. > + 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 ( (kind == PTE_kind_leaf > + ? ((pt[i] ^ pt[idx]) & ~CONTIG_MASK) != (1ULL << (b + shift)) Maybe this could be a macro, CHECK_CONTIG or some such? It's also used below. I would also think this would be clearer as: (pt[idx] & ~CONTIG_MASK) + (1ULL << (shift + b)) == (pt[i] & ~CONTIG_MASK) > + : pt[i] & ~CONTIG_MASK) || Isn't PTE_kind_null always supposed to be empty? (ie: wouldn't this check always succeed?) > + 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 ( (kind == PTE_kind_leaf > + ? ((pt[i] ^ pt[idx]) & ~CONTIG_MASK) != (1ULL << (b + shift)) > + : pt[i] & ~CONTIG_MASK) || > + GET_MARKER(pt[i]) != b ) > + break; > + idx &= ~(1U << b); There's an iteration where idx will be 0, and then there's no further point in doing the & anymore? Thanks, Roger.
On 15.12.2021 14:57, Roger Pau Monné wrote: > On Fri, Sep 24, 2021 at 11:55:30AM +0200, Jan Beulich wrote: >> --- /dev/null >> +++ b/xen/include/asm-x86/contig-marker.h >> @@ -0,0 +1,105 @@ >> +#ifndef __ASM_X86_CONTIG_MARKER_H >> +#define __ASM_X86_CONTIG_MARKER_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. > > Given this requirement I think it would make sense to place the page > table marker initialization currently placed in iommu_alloc_pgtable as > a helper here also? I would be nice, yes, but it would also cause problems. I specifically do not want to make the function here "inline". Hence a source file including it would need to be given a way to suppress its visibility to the compiler. Which would mean #ifdef-ary I'd prefer to avoid. Yet by saying "prefer" I mean to leave open that I could be talked into doing what you suggest. >> + */ >> + >> +#include <xen/bitops.h> >> +#include <xen/lib.h> >> +#include <xen/page-size.h> >> + >> +/* 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))) >> + >> +enum PTE_kind { >> + PTE_kind_null, >> + PTE_kind_leaf, >> + PTE_kind_table, >> +}; >> + >> +static bool update_contig_markers(uint64_t *pt, unsigned int idx, > > Maybe pt_update_contig_markers, so it's not such a generic name. I can do that. The header may then want to be named pt-contig-marker.h or pt-contig-markers.h. Thoughts? >> + 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)) ) > > Don't you just need to check that the address is aligned to at least > idx, not that it's exactly aligned? No, that wouldn't be sufficient. We're not after a general "is contiguous" here, but strictly after "is this slot meeting the requirements for the whole table eventually getting replaced by a superpage". >> + 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 ( (kind == PTE_kind_leaf >> + ? ((pt[i] ^ pt[idx]) & ~CONTIG_MASK) != (1ULL << (b + shift)) > > Maybe this could be a macro, CHECK_CONTIG or some such? It's also used > below. Hmm, yes, this might indeed help readability. There's going to be a lot of parameters though; not sure whether omitting all(?) parameters for such a locally used macro would be considered acceptable. > I would also think this would be clearer as: > > (pt[idx] & ~CONTIG_MASK) + (1ULL << (shift + b)) == (pt[i] & ~CONTIG_MASK) By using + we'd consider entries contiguous which for our purposes shouldn't be considered so. Yes, the earlier check should already have caught that case, but I'd like the checks to be as tight as possible. >> + : pt[i] & ~CONTIG_MASK) || > > Isn't PTE_kind_null always supposed to be empty? Yes (albeit this could be relaxed, but then the logic here would need to know where the "present" bit(s) is/are). > (ie: wouldn't this check always succeed?) No - "kind" describes pt[idx], not pt[i]. >> + 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 ( (kind == PTE_kind_leaf >> + ? ((pt[i] ^ pt[idx]) & ~CONTIG_MASK) != (1ULL << (b + shift)) >> + : pt[i] & ~CONTIG_MASK) || >> + GET_MARKER(pt[i]) != b ) >> + break; >> + idx &= ~(1U << b); > > There's an iteration where idx will be 0, and then there's no further > point in doing the & anymore? Yes, but doing the & anyway is cheaper than adding a conditional. Jan
© 2016 - 2024 Red Hat, Inc.