Re: [PATCH v5 15/22] x86/virt/tdx: Allocate and set up PAMTs for TDMRs

From: Huang, Kai
Date: Wed Aug 17 2022 - 19:44:00 EST


On Wed, 2022-08-17 at 15:46 -0700, Sagi Shahar wrote:
> > +/*
> > + * Calculate PAMT size given a TDMR and a page size.  The returned
> > + * PAMT size is always aligned up to 4K page boundary.
> > + */
> > +static unsigned long tdmr_get_pamt_sz(struct tdmr_info *tdmr,
> > +                                     enum tdx_page_sz pgsz)
> > +{
> > +       unsigned long pamt_sz;
> > +       int pamt_entry_nr;
>             ^
> This should be an 'unsigned long'. Otherwise you get an integer
> overflow for large memory machines.

Agreed. Thanks.

>
> > +
> > +       switch (pgsz) {
> > +       case TDX_PG_4K:
> > +               pamt_entry_nr = tdmr->size >> PAGE_SHIFT;
> > +               break;
> > +       case TDX_PG_2M:
> > +               pamt_entry_nr = tdmr->size >> PMD_SHIFT;
> > +               break;
> > +       case TDX_PG_1G:
> > +               pamt_entry_nr = tdmr->size >> PUD_SHIFT;
> > +               break;
> > +       default:
> > +               WARN_ON_ONCE(1);
> > +               return 0;
> > +       }
> > +
> > +       pamt_sz = pamt_entry_nr * tdx_sysinfo.pamt_entry_size;
> > +       /* TDX requires PAMT size must be 4K aligned */
> > +       pamt_sz = ALIGN(pamt_sz, PAGE_SIZE);
> > +
> > +       return pamt_sz;
> > +}
> > +