Re: [PATCH 4/9] x86/mm/pat: don't BUG() on inconsistent page count
From: Mike Rapoport
Date: Wed Nov 25 2026 - 04:11:40 EST
On Tue, Nov 24, 2026 at 10:57:39PM +0000, Edgecombe, Rick P wrote:
> On Thu, 2026-11-12 at 18:31 +0200, Mike Rapoport (Microsoft) wrote:
> > If a step of __change_page_attr_set_clr() covers more pages than remain
> > in the range, or no pages at all, that's a bug in CPA, but it does not
> > justify crashing the kernel.
>
> Agreed, but does it also justify special handling to try to return an error to
> the caller about the bug in CPA? Why not just change the BUG_ON() to WARN?
If we only replace BUG_ON() with WARN_ON(), either rempages wraps around
and we keep changing pages past the end, or we loop forever.
In any case we must break the loop and returning -EINVAL seems the most
appropriate action there.
> > Warn and fail the request instead.
> >
> > Signed-off-by: Mike Rapoport (Microsoft) <rppt@xxxxxxxxxx>
> > ---
> > arch/x86/mm/pat/set_memory.c | 5 ++++-
> > 1 file changed, 4 insertions(+), 1 deletion(-)
> >
> > diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c
> > index aa7052d247e60..a1bea16a3c3bb 100644
> > --- a/arch/x86/mm/pat/set_memory.c
> > +++ b/arch/x86/mm/pat/set_memory.c
> > @@ -2101,7 +2101,10 @@ static int __change_page_attr_set_clr(struct cpa_data *cpa, int primary)
> > * CPA operation. Either a large page has been
> > * preserved or a single page update happened.
> > */
> > - BUG_ON(cpa->numpages > rempages || !cpa->numpages);
> > + if (WARN_ON_ONCE(cpa->numpages > rempages || !cpa->numpages)) {
> > + ret = -EINVAL;
> > + goto out;
> > + }
> > rempages -= cpa->numpages;
> > cpa->curpage += cpa->numpages;
> > }
> >
> > --
>
--
Sincerely yours,
Mike.