From: Martynas <m@lambda.lt>
To: Y Song <ys114321@gmail.com>
Cc: netdev <netdev@vger.kernel.org>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>
Subject: Re: [PATCH] bpf: selftests: handle sparse CPU allocations
Date: Thu, 31 Jan 2019 10:57:10 +0200 [thread overview]
Message-ID: <1548925030.2449932.1647631648.68AFD94B@webmail.messagingengine.com> (raw)
In-Reply-To: <CAH3MdRUKvPpxmOL_BGjsKeiWTEWCgKNiDKGsXsVzDLbY5F+Dzw@mail.gmail.com>
Oops, thanks for noticing the issue with strlen in the for loop. Changing to "int len = strlen(buff); for (i = 0; i < len; i++) <..>" should fix the issue. I'm going to re-submit the patch.
Also, I put some testing results: https://gist.github.com/brb/5369b5cfd08babb80cf2c4081dc19762
On Thu, Jan 31, 2019, at 7:25 AM, Y Song wrote:
> [ My reply somehow rejected by netdev, this is to send it again. ]
>
> On Wed, Jan 30, 2019 at 1:19 AM Martynas Pumputis <m@lambda.lt> wrote:
> >
> > Previously, bpf_num_possible_cpus() had a bug when calculating a
> > number of possible CPUs in the case of sparse CPU allocations, as
> > it was considering only the first range or element of
> > /sys/devices/system/cpu/possible.
> >
> > E.g. in the case of "0,2-3" (CPU 1 is not available), the function
> > returned 1 instead of 3.
> >
> > This patch fixes the function by making it parse all CPU ranges and
> > elements.
> >
> > Signed-off-by: Martynas Pumputis <m@lambda.lt>
> > ---
> > tools/testing/selftests/bpf/bpf_util.h | 29 +++++++++++++++++---------
> > 1 file changed, 19 insertions(+), 10 deletions(-)
> >
> > diff --git a/tools/testing/selftests/bpf/bpf_util.h b/tools/testing/selftests/bpf/bpf_util.h
> > index 315a44fa32af..8cab50408204 100644
> > --- a/tools/testing/selftests/bpf/bpf_util.h
> > +++ b/tools/testing/selftests/bpf/bpf_util.h
> > @@ -13,7 +13,7 @@ static inline unsigned int bpf_num_possible_cpus(void)
> > unsigned int start, end, possible_cpus = 0;
> > char buff[128];
> > FILE *fp;
> > - int n;
> > + int n, i, j = 0;
> >
> > fp = fopen(fcpu, "r");
> > if (!fp) {
> > @@ -21,17 +21,26 @@ static inline unsigned int bpf_num_possible_cpus(void)
> > exit(1);
> > }
> >
> > - while (fgets(buff, sizeof(buff), fp)) {
> > - n = sscanf(buff, "%u-%u", &start, &end);
> > - if (n == 0) {
> > - printf("Failed to retrieve # possible CPUs!\n");
> > - exit(1);
> > - } else if (n == 1) {
> > - end = start;
> > + if (!fgets(buff, sizeof(buff), fp)) {
> > + printf("Failed to read %s!\n", fcpu);
> > + exit(1);
> > + }
> > +
> > + for (i = 0; i <= strlen(buff); i++) {
> > + if (buff[i] == ',' || buff[i] == '\0') {
> > + buff[i] = '\0';
>
> This does not sound right. For example, the cpu list "0,2-3",
> you will change "," to '\0" so buffer becomes "0\02-3".
> The next iteration you will get strlen(buff) = 1.
> The "2-3" will be skipped.
>
> > + n = sscanf(&buff[j], "%u-%u", &start, &end);
> > + if (n <= 0) {
> > + printf("Failed to retrieve # possible CPUs!\n");
> > + exit(1);
> > + } else if (n == 1) {
> > + end = start;
> > + }
> > + possible_cpus += end - start + 1;
> > + j = i + 1;
> > }
> > - possible_cpus = start == 0 ? end + 1 : 0;
> > - break;
> > }
> > +
> > fclose(fp);
> >
> > return possible_cpus;
> > --
> > 2.20.1
> >
prev parent reply other threads:[~2019-01-31 8:57 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-01-30 9:19 [PATCH] bpf: selftests: handle sparse CPU allocations Martynas Pumputis
2019-01-31 5:25 ` Y Song
2019-01-31 8:57 ` Martynas [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=1548925030.2449932.1647631648.68AFD94B@webmail.messagingengine.com \
--to=m@lambda.lt \
--cc=ast@kernel.org \
--cc=daniel@iogearbox.net \
--cc=netdev@vger.kernel.org \
--cc=ys114321@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).