public inbox for stable@vger.kernel.org
 help / color / mirror / Atom feed
From: Phil Auld <pauld@redhat.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: linux-kernel@vger.kernel.org,
	"Rafael J . Wysocki" <rafael@kernel.org>,
	Barry Song <21cnbao@gmail.com>, Tian Tao <tiantao6@hisilicon.com>,
	Yury Norov <yury.norov@gmail.com>,
	feng xiangjun <fengxj325@gmail.com>,
	stable@vger.kernel.org
Subject: Re: [PATCH] drivers/base: Fix unsigned comparison to -1 in CPUMAP_FILE_MAX_BYTES
Date: Tue, 6 Sep 2022 13:24:44 -0400	[thread overview]
Message-ID: <YxeCXALgsIGxiSlG@lorien.usersys.redhat.com> (raw)
In-Reply-To: <Yxd9LRH+3wkM0fot@kroah.com>

On Tue, Sep 06, 2022 at 07:02:37PM +0200 Greg Kroah-Hartman wrote:
> On Tue, Sep 06, 2022 at 12:04:30PM -0400, Phil Auld wrote:
> > As PAGE_SIZE is unsigned long, -1 > PAGE_SIZE when NR_CPUS <= 3.
> > This leads to very large file sizes:
> > 
> > topology$ ls -l
> > total 0
> > -r--r--r-- 1 root root 18446744073709551615 Sep  5 11:59 core_cpus
> 
> Yeah, lots of CPUs!  :)
>

Yep, apparently things like lscpu fail with this size. 


> > -r--r--r-- 1 root root                 4096 Sep  5 11:59 core_cpus_list
> > -r--r--r-- 1 root root                 4096 Sep  5 10:58 core_id
> > -r--r--r-- 1 root root 18446744073709551615 Sep  5 10:10 core_siblings
> > -r--r--r-- 1 root root                 4096 Sep  5 11:59 core_siblings_list
> > -r--r--r-- 1 root root 18446744073709551615 Sep  5 11:59 die_cpus
> > -r--r--r-- 1 root root                 4096 Sep  5 11:59 die_cpus_list
> > -r--r--r-- 1 root root                 4096 Sep  5 11:59 die_id
> > -r--r--r-- 1 root root 18446744073709551615 Sep  5 11:59 package_cpus
> > -r--r--r-- 1 root root                 4096 Sep  5 11:59 package_cpus_list
> > -r--r--r-- 1 root root                 4096 Sep  5 10:58 physical_package_id
> > -r--r--r-- 1 root root 18446744073709551615 Sep  5 10:10 thread_siblings
> > -r--r--r-- 1 root root                 4096 Sep  5 11:59 thread_siblings_list
> > 
> > Adjust the inequality to catch the case when NR_CPUS is configured
> > to a small value.
> > 
> > Fixes: 7ee951acd31a ("drivers/base: fix userspace break from using bin_attributes for cpumap and cpulist")
> > Reported-by: feng xiangjun <fengxj325@gmail.com>
> > Signed-off-by: Phil Auld <pauld@redhat.com>
> > Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> > Cc: "Rafael J. Wysocki" <rafael@kernel.org>
> > Cc: Yury Norov <yury.norov@gmail.com>
> > Cc: stable@vger.kernel.org
> > Cc: feng xiangjun <fengxj325@gmail.com>
> > ---
> >  include/linux/cpumask.h | 5 +++--
> >  1 file changed, 3 insertions(+), 2 deletions(-)
> > 
> > diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h
> > index bd047864c7ac..7b1349612d6d 100644
> > --- a/include/linux/cpumask.h
> > +++ b/include/linux/cpumask.h
> > @@ -1127,9 +1127,10 @@ cpumap_print_list_to_buf(char *buf, const struct cpumask *mask,
> >   * cover a worst-case of every other cpu being on one of two nodes for a
> >   * very large NR_CPUS.
> >   *
> > - *  Use PAGE_SIZE as a minimum for smaller configurations.
> > + *  Use PAGE_SIZE as a minimum for smaller configurations while avoiding
> > + *  unsigned comparison to -1.
> >   */
> > -#define CPUMAP_FILE_MAX_BYTES  ((((NR_CPUS * 9)/32 - 1) > PAGE_SIZE) \
> > +#define CPUMAP_FILE_MAX_BYTES  ((((NR_CPUS * 9)/32) > PAGE_SIZE + 1) \
> >  					? (NR_CPUS * 9)/32 - 1 : PAGE_SIZE)
> >  #define CPULIST_FILE_MAX_BYTES  (((NR_CPUS * 7)/2 > PAGE_SIZE) ? (NR_CPUS * 7)/2 : PAGE_SIZE)
> >  
> > -- 
> > 2.31.1
> > 
> 
> Nice catch.  What type of systems did you run this on to verify it will
> work?
>

Feng ran it on his 2 cpu laptop.  I ran it on my usual test bed (80 cpus) but
configured both with NR_CPUS at 8192 and at 2.

It's not really dependent on the actual system. It's a compile time config
option.  I failed to set it small when testing the first one.

The fix here is just maths :) 

With NR_CPUS == 2:

# ls -l /sys/devices/system/cpu/cpu0/topology/
total 0
-r--r--r--. 1 root root 4096 Sep  6 11:40 cluster_cpus
-r--r--r--. 1 root root 4096 Sep  6 11:40 cluster_cpus_list
-r--r--r--. 1 root root 4096 Sep  6 11:40 cluster_id
-r--r--r--. 1 root root 4096 Sep  6 11:40 core_cpus
-r--r--r--. 1 root root 4096 Sep  6 11:40 core_cpus_list
-r--r--r--. 1 root root 4096 Sep  6 11:40 core_id
-r--r--r--. 1 root root 4096 Sep  6 11:29 core_siblings
-r--r--r--. 1 root root 4096 Sep  6 11:40 core_siblings_list
...



Cheers,
Phil

> thanks,
> 
> greg k-h
> 

-- 


  reply	other threads:[~2022-09-06 17:31 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-06 16:04 [PATCH] drivers/base: Fix unsigned comparison to -1 in CPUMAP_FILE_MAX_BYTES Phil Auld
2022-09-06 17:02 ` Greg Kroah-Hartman
2022-09-06 17:24   ` Phil Auld [this message]
2022-09-06 18:09 ` Yury Norov
2022-09-06 18:15   ` Yury Norov
2022-09-06 18:57   ` Phil Auld
2022-09-06 19:11     ` Yury Norov

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=YxeCXALgsIGxiSlG@lorien.usersys.redhat.com \
    --to=pauld@redhat.com \
    --cc=21cnbao@gmail.com \
    --cc=fengxj325@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rafael@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=tiantao6@hisilicon.com \
    --cc=yury.norov@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