Re: [PATCH] mm: shmem: do not call PageHWPoison on a ERR-page

From: Yang Shi
Date: Sat Nov 13 2021 - 17:30:55 EST


On Sat, Nov 13, 2021 at 12:23 PM Linus Torvalds
<torvalds@xxxxxxxxxxxxxxxxxxxx> wrote:
>
> On Sat, Nov 13, 2021 at 12:16 PM Linus Torvalds
> <torvalds@xxxxxxxxxxxxxxxxxxxx> wrote:
> >
> > Neither of the fixes were sent to me, and honestly, I think the real
> > issue is that the original commit is just too broken for words.
>
> Side note: the one you pointed to (by Ajay), had the comment that it
> could be done differently as an optimization.
>
> And I very much agree with that, although I think it would be a lot
> more than an optimization: just doing
>
> if (error)
> return ERR_PTR(error);
>
> earlier in the function would have avoided the issue entirely, and
> would have made the code a lot easier to read too.
>
> But what made me decide to just revert it entirely was that the
> original commit that caused this all also had stuff like this:
>
> - return shmem_getpage(inode, index, pagep, SGP_WRITE);
> + ret = shmem_getpage(inode, index, pagep, SGP_WRITE);
> +
> + if (*pagep && PageHWPoison(*pagep)) {
> + unlock_page(*pagep);
> + put_page(*pagep);
> + ret = -EIO;
> + }
> +
> + return ret;
>
> which is another example of exactly the same issue: ignoring errors,
> and then acting on other information and creating new errors.
>
> Again, that code should have checked and handled errors first, and
> then - if there wasn't an error - added that new HWpoison handling.
>
> So that just made me go "this is not worth fixing up, this just needs
> re-doing", and thus I just went for the revert instead.

I'm so sorry for the inconvenience.

The above snippet is actually ok since if *pagep returned via
shmem_getpage()'s parameter is not NULL, then ret is 0. When
shmem_getpage() returns error code, *pagep is NULL IIUC. So it
actually doesn't ignore errors then create and return new error.

But I do agree it seems tricky for someone who is not familiar with
shmem code. And if shmem code is changed in the future it may be error
prone. I could rewrite it to:

if (ret < 0)
goto out;

if (*pagep && PageHWPoison(*pagep)) {
unlock_page(*pagep);
put_page(*pagep);
ret = -EIO;
}

out:
return ret;

And fold in Ajay's fix (will take Muchun's version which returns error
earlier). Hopefully it still can make -rc1. Of course rc2 is fine
either.

>
> Linus