Re: [RFC PATCH v2 16/26] KVM: arm64: Prepare Hyp memory protection
From: Will Deacon
Date: Fri Feb 05 2021 - 13:00:21 EST
On Thu, Feb 04, 2021 at 10:47:08AM +0000, Quentin Perret wrote:
> On Wednesday 03 Feb 2021 at 14:37:10 (+0000), Will Deacon wrote:
> > On Fri, Jan 08, 2021 at 12:15:14PM +0000, Quentin Perret wrote:
> > > +static inline unsigned long __hyp_pgtable_max_pages(unsigned long nr_pages)
> > > +{
> > > + unsigned long total = 0, i;
> > > +
> > > + /* Provision the worst case scenario with 4 levels of page-table */
> > > + for (i = 0; i < 4; i++) {
> >
> > Looks like you want KVM_PGTABLE_MAX_LEVELS, so maybe move that into a
> > header?
>
> Will do.
>
> >
> > > + nr_pages = DIV_ROUND_UP(nr_pages, PTRS_PER_PTE);
> > > + total += nr_pages;
> > > + }
> >
> > ... that said, I'm not sure this needs to iterate at all. What exactly are
> > you trying to compute?
>
> I'm trying to figure out how many pages I will need to construct a
> page-table covering nr_pages contiguous pages. The first iteration tells
> me how many level 0 pages I need to cover nr_pages, the second iteration
> how many level 1 pages I need to cover the level 0 pages, and so on...
Ah, you iterate from leaves back to the root. Got it, thanks.
> I might be doing this naively though. Got a better idea?
I thought I did, but I ended up with something based on a geometric series
and it looks terrible to code-up in C without, err, iterating like you do.
So yeah, ignore me :)
> > > +
> > > + return total;
> > > +}
> > > +
> > > +static inline unsigned long hyp_s1_pgtable_size(void)
> > > +{
> > > + struct hyp_memblock_region *reg;
> > > + unsigned long nr_pages, res = 0;
> > > + int i;
> > > +
> > > + if (kvm_nvhe_sym(hyp_memblock_nr) <= 0)
> > > + return 0;
> >
> > It's a bit grotty having this be signed. Why do we need to encode the error
> > case differently from the 0 case?
>
> Here specifically we don't, but it is needed in early_init_dt_add_memory_hyp()
> to distinguish the overflow case from the first memblock being added.
Fair enough, but if you figure out a way for hyp_memblock_nr to be unsigned,
I think that would be preferable.
Will