diff mbox series

[-next,2/2] PM: hibernate: add check of preallocate mem for image size pages

Message ID 20221101022840.1351163-3-tgsp002@gmail.com (mailing list archive)
State Not Applicable, archived
Headers show
Series Fixes to the hibernate_preallocate_memory() | expand

Commit Message

金塔尖 Nov. 1, 2022, 2:28 a.m. UTC
From: xiongxin <xiongxin@kylinos.cn>

Added a check on the return value of preallocate_image_highmem(). If
memory preallocate is insufficient, S4 cannot be done;

I am playing 4K video on a machine with AMD or other graphics card and
only 8GiB memory, and the kernel is not configured with CONFIG_HIGHMEM.
When doing the S4 test, the analysis found that when the pages get from
minimum_image_size() is large enough, The preallocate_image_memory() and
preallocate_image_highmem() calls failed to obtain enough memory. Add
the judgment that memory preallocate is insufficient;

"pages -= free_unnecessary_pages()" below will let pages to drop a lot,
so I wonder if it makes sense to add a judgment here.

Cc: stable@vger.kernel.org
Signed-off-by: xiongxin <xiongxin@kylinos.cn>
Signed-off-by: huanglei <huanglei@kylinos.cn>
---
 kernel/power/snapshot.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

Comments

Rafael J. Wysocki Nov. 3, 2022, 4:47 p.m. UTC | #1
On Tue, Nov 1, 2022 at 3:28 AM TGSP <tgsp002@gmail.com> wrote:
>
> From: xiongxin <xiongxin@kylinos.cn>
>
> Added a check on the return value of preallocate_image_highmem(). If
> memory preallocate is insufficient, S4 cannot be done;
>
> I am playing 4K video on a machine with AMD or other graphics card and
> only 8GiB memory, and the kernel is not configured with CONFIG_HIGHMEM.
> When doing the S4 test, the analysis found that when the pages get from
> minimum_image_size() is large enough, The preallocate_image_memory() and
> preallocate_image_highmem() calls failed to obtain enough memory. Add
> the judgment that memory preallocate is insufficient;
>
> "pages -= free_unnecessary_pages()" below will let pages to drop a lot,
> so I wonder if it makes sense to add a judgment here.
>
> Cc: stable@vger.kernel.org
> Signed-off-by: xiongxin <xiongxin@kylinos.cn>
> Signed-off-by: huanglei <huanglei@kylinos.cn>
> ---
>  kernel/power/snapshot.c | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/kernel/power/snapshot.c b/kernel/power/snapshot.c
> index c20ca5fb9adc..670abf89cf31 100644
> --- a/kernel/power/snapshot.c
> +++ b/kernel/power/snapshot.c
> @@ -1738,6 +1738,7 @@ int hibernate_preallocate_memory(void)
>         struct zone *zone;
>         unsigned long saveable, size, max_size, count, highmem, pages = 0;
>         unsigned long alloc, save_highmem, pages_highmem, avail_normal;
> +       unsigned long size_highmem;

Please define this in the block where it will be used.

>         ktime_t start, stop;
>         int error;
>
> @@ -1863,7 +1864,13 @@ int hibernate_preallocate_memory(void)
>                 pages_highmem += size;

The line above can be dropped.

>                 alloc -= size;
>                 size = preallocate_image_memory(alloc, avail_normal);
> -               pages_highmem += preallocate_image_highmem(alloc - size);
> +               size_highmem += preallocate_image_highmem(alloc - size);

Did you mean "="?

Assuming you did, this could be

        size_highmem = size + preallocate_image_highmem(alloc - size);
        if (size_highmem < alloc) {

> +               if (size_highmem < (alloc - size)) {

The inner parens were not necessary.

> +                       pr_err("Image allocation is %lu pages short, exit\n",
> +                               alloc - size - pages_highmem);
> +                       goto err_out;
> +               }
> +               pages_highmem += size_highmem;
>                 pages += pages_highmem + size;
>         }
>
> --

But overall it would be better to avoid bailing out.
Rafael J. Wysocki Nov. 3, 2022, 4:57 p.m. UTC | #2
On Thu, Nov 3, 2022 at 5:47 PM Rafael J. Wysocki <rafael@kernel.org> wrote:
>
> On Tue, Nov 1, 2022 at 3:28 AM TGSP <tgsp002@gmail.com> wrote:
> >
> > From: xiongxin <xiongxin@kylinos.cn>
> >
> > Added a check on the return value of preallocate_image_highmem(). If
> > memory preallocate is insufficient, S4 cannot be done;
> >
> > I am playing 4K video on a machine with AMD or other graphics card and
> > only 8GiB memory, and the kernel is not configured with CONFIG_HIGHMEM.
> > When doing the S4 test, the analysis found that when the pages get from
> > minimum_image_size() is large enough, The preallocate_image_memory() and
> > preallocate_image_highmem() calls failed to obtain enough memory. Add
> > the judgment that memory preallocate is insufficient;
> >
> > "pages -= free_unnecessary_pages()" below will let pages to drop a lot,
> > so I wonder if it makes sense to add a judgment here.
> >
> > Cc: stable@vger.kernel.org
> > Signed-off-by: xiongxin <xiongxin@kylinos.cn>
> > Signed-off-by: huanglei <huanglei@kylinos.cn>
> > ---
> >  kernel/power/snapshot.c | 9 ++++++++-
> >  1 file changed, 8 insertions(+), 1 deletion(-)
> >
> > diff --git a/kernel/power/snapshot.c b/kernel/power/snapshot.c
> > index c20ca5fb9adc..670abf89cf31 100644
> > --- a/kernel/power/snapshot.c
> > +++ b/kernel/power/snapshot.c
> > @@ -1738,6 +1738,7 @@ int hibernate_preallocate_memory(void)
> >         struct zone *zone;
> >         unsigned long saveable, size, max_size, count, highmem, pages = 0;
> >         unsigned long alloc, save_highmem, pages_highmem, avail_normal;
> > +       unsigned long size_highmem;
>
> Please define this in the block where it will be used.
>
> >         ktime_t start, stop;
> >         int error;
> >
> > @@ -1863,7 +1864,13 @@ int hibernate_preallocate_memory(void)
> >                 pages_highmem += size;
>
> The line above can be dropped.

Not really, it needs to be replaced with

        size_highmem = size ;

> >                 alloc -= size;
> >                 size = preallocate_image_memory(alloc, avail_normal);
> > -               pages_highmem += preallocate_image_highmem(alloc - size);
> > +               size_highmem += preallocate_image_highmem(alloc - size);
>
> Did you mean "="?
>
> Assuming you did, this could be
>
>         size_highmem = size + preallocate_image_highmem(alloc - size);

And here

                  size_highmem += preallocate_image_highmem(alloc - size);

which is what you had in the original patch.

>         if (size_highmem < alloc) {
>
> > +               if (size_highmem < (alloc - size)) {
>
> The inner parens were not necessary.
>
> > +                       pr_err("Image allocation is %lu pages short, exit\n",
> > +                               alloc - size - pages_highmem);
> > +                       goto err_out;
> > +               }
> > +               pages_highmem += size_highmem;
> >                 pages += pages_highmem + size;
> >         }
> >
> > --
>
> But overall it would be better to avoid bailing out.
Rafael J. Wysocki Nov. 3, 2022, 5:11 p.m. UTC | #3
On Tue, Nov 1, 2022 at 3:28 AM TGSP <tgsp002@gmail.com> wrote:
>
> From: xiongxin <xiongxin@kylinos.cn>
>
> Added a check on the return value of preallocate_image_highmem(). If
> memory preallocate is insufficient, S4 cannot be done;
>
> I am playing 4K video on a machine with AMD or other graphics card and
> only 8GiB memory, and the kernel is not configured with CONFIG_HIGHMEM.
> When doing the S4 test, the analysis found that when the pages get from
> minimum_image_size() is large enough, The preallocate_image_memory() and
> preallocate_image_highmem() calls failed to obtain enough memory. Add
> the judgment that memory preallocate is insufficient;

So I'm not sure what the problem is.  Can you please explain it in more detail?

The if (pages < alloc) appears to be false in your case, so there
should be enough free pages to create an image.

Maybe reserved_size is too low?
diff mbox series

Patch

diff --git a/kernel/power/snapshot.c b/kernel/power/snapshot.c
index c20ca5fb9adc..670abf89cf31 100644
--- a/kernel/power/snapshot.c
+++ b/kernel/power/snapshot.c
@@ -1738,6 +1738,7 @@  int hibernate_preallocate_memory(void)
 	struct zone *zone;
 	unsigned long saveable, size, max_size, count, highmem, pages = 0;
 	unsigned long alloc, save_highmem, pages_highmem, avail_normal;
+	unsigned long size_highmem;
 	ktime_t start, stop;
 	int error;
 
@@ -1863,7 +1864,13 @@  int hibernate_preallocate_memory(void)
 		pages_highmem += size;
 		alloc -= size;
 		size = preallocate_image_memory(alloc, avail_normal);
-		pages_highmem += preallocate_image_highmem(alloc - size);
+		size_highmem += preallocate_image_highmem(alloc - size);
+		if (size_highmem < (alloc - size)) {
+			pr_err("Image allocation is %lu pages short, exit\n",
+				alloc - size - pages_highmem);
+			goto err_out;
+		}
+		pages_highmem += size_highmem;
 		pages += pages_highmem + size;
 	}