On Mon, Mar 28, 2022 at 02:30:05PM +1300, Kai Huang <kai.huang@xxxxxxxxx> wrote: > > > > + > > > +static int sanitize_cmrs(struct cmr_info *cmr_array, int cmr_num) > > > +{ > > > + int i, j; > > > + > > > + /* > > > + * Intel TDX module spec, 20.7.3 CMR_INFO: > > > + * > > > + * TDH.SYS.INFO leaf function returns a MAX_CMRS (32) entry > > > + * array of CMR_INFO entries. The CMRs are sorted from the > > > + * lowest base address to the highest base address, and they > > > + * are non-overlapping. > > > + * > > > + * This implies that BIOS may generate invalid empty entries > > > + * if total CMRs are less than 32. Skip them manually. > > > + */ > > > + for (i = 0; i < cmr_num; i++) { > > > + struct cmr_info *cmr = &cmr_array[i]; > > > + struct cmr_info *prev_cmr = NULL; > > > + > > > + /* Skip further invalid CMRs */ > > > + if (!cmr_valid(cmr)) > > > + break; > > > + > > > + if (i > 0) > > > + prev_cmr = &cmr_array[i - 1]; > > > + > > > + /* > > > + * It is a TDX firmware bug if CMRs are not > > > + * in address ascending order. > > > + */ > > > + if (prev_cmr && ((prev_cmr->base + prev_cmr->size) > > > > + cmr->base)) { > > > + pr_err("Firmware bug: CMRs not in address ascending order.\n"); > > > + return -EFAULT; > > > + } > > > + } > > > + > > > + /* > > > + * Also a sane BIOS should never generate invalid CMR(s) between > > > + * two valid CMRs. Sanity check this and simply return error in > > > + * this case. > > > + */ > > > + for (j = i; j < cmr_num; j++) > > > + if (cmr_valid(&cmr_array[j])) { > > > + pr_err("Firmware bug: invalid CMR(s) among valid CMRs.\n"); > > > + return -EFAULT; > > > + } > > > > This check doesn't make sense because above i-for loop has break. > > The break in above i-for loop will hit at the first invalid CMR entry. Yes "j = > i" will make double check on this invalid CMR entry, but it should have no > problem. Or we can change to "j = i + 1" to skip the first invalid CMR entry. > > Does this make sense? It makes sense. Somehow I missed j = i. I scratch my review. -- Isaku Yamahata <isaku.yamahata@xxxxxxxxx>