linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Stefan Roesch <shr@devkernel.io>
To: David Hildenbrand <david@redhat.com>
Cc: willy@infradead.org, zzqq0103.hey@gmail.com,
	akpm@linux-foundation.org, linux-mm@kvack.org,
	linux-fsdevel@vger.kernel.org
Subject: Re: [PATCH v2] mm: fix div by zero in bdi_ratio_from_pages
Date: Wed, 08 Jan 2025 22:28:06 -0800	[thread overview]
Message-ID: <87seps7b5f.fsf@devkernel.io> (raw)
In-Reply-To: <f85f46ac-9893-4b6b-89a4-f5b0d435886e@redhat.com>


David Hildenbrand <david@redhat.com> writes:

...

>
> I would have done it slightly differently, something like:
>
>
> diff --git a/mm/page-writeback.c b/mm/page-writeback.c
> index d213ead956750..4b02f18f7d01f 100644
> --- a/mm/page-writeback.c
> +++ b/mm/page-writeback.c
> @@ -685,13 +685,15 @@ static int bdi_check_pages_limit(unsigned long pages)
>         return 0;
>  }
>  -static unsigned long bdi_ratio_from_pages(unsigned long pages)
> +static long bdi_ratio_from_pages(unsigned long pages)
>  {
>         unsigned long background_thresh;
>         unsigned long dirty_thresh;
>         unsigned long ratio;
>           global_dirty_limits(&background_thresh, &dirty_thresh);
> +       if (!dirty_thresh)
> +               return -EINVAL;
>         ratio = div64_u64(pages * 100ULL * BDI_RATIO_SCALE, dirty_thresh);
>           return ratio;
> @@ -790,13 +792,15 @@ int bdi_set_min_bytes(struct backing_dev_info *bdi, u64 min_bytes)
>  {
>         int ret;
>         unsigned long pages = min_bytes >> PAGE_SHIFT;
> -       unsigned long min_ratio;
> +       long min_ratio;
>           ret = bdi_check_pages_limit(pages);
>         if (ret)
>                 return ret;
>           min_ratio = bdi_ratio_from_pages(pages);
> +       if (min_ratio < 0)
> +               return min_ratio;
>         return __bdi_set_min_ratio(bdi, min_ratio);
>  }
>  @@ -809,13 +813,15 @@ int bdi_set_max_bytes(struct backing_dev_info *bdi, u64
> max_bytes)
>  {
>         int ret;
>         unsigned long pages = max_bytes >> PAGE_SHIFT;
> -       unsigned long max_ratio;
> +       long max_ratio;
>           ret = bdi_check_pages_limit(pages);
>         if (ret)
>                 return ret;
>           max_ratio = bdi_ratio_from_pages(pages);
> +       if (min_ratio < 0)

I assume you meant max_ratio.


> +               return min_ratio;
>         return __bdi_set_max_ratio(bdi, max_ratio);
>  }
>  -- Cheers,
>

I'll use your recommendation for the next version of the patch.


      reply	other threads:[~2025-01-09  6:29 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-08  1:47 [PATCH v2] mm: fix div by zero in bdi_ratio_from_pages Stefan Roesch
2025-01-08 11:18 ` David Hildenbrand
2025-01-09  6:28   ` Stefan Roesch [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87seps7b5f.fsf@devkernel.io \
    --to=shr@devkernel.io \
    --cc=akpm@linux-foundation.org \
    --cc=david@redhat.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=willy@infradead.org \
    --cc=zzqq0103.hey@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).