NetBSD Problem Report #58679

From www@netbsd.org  Thu Sep 19 12:25:14 2024
Return-Path: <www@netbsd.org>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256
	 client-signature RSA-PSS (2048 bits) client-digest SHA256)
	(Client CN "mail.NetBSD.org", Issuer "mail.NetBSD.org CA" (not verified))
	by mollari.NetBSD.org (Postfix) with ESMTPS id 17F991A923D
	for <gnats-bugs@gnats.NetBSD.org>; Thu, 19 Sep 2024 12:25:14 +0000 (UTC)
Message-Id: <20240919122513.030161A923E@mollari.NetBSD.org>
Date: Thu, 19 Sep 2024 12:25:12 +0000 (UTC)
From: ondrej@sury.org
Reply-To: ondrej@sury.org
To: gnats-bugs@NetBSD.org
Subject: sched_getaffinity_np() always returns an empty set
X-Send-Pr-Version: www-1.0

>Number:         58679
>Category:       lib
>Synopsis:       sched_getaffinity_np() always returns an empty set
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    lib-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Sep 19 12:30:00 +0000 2024
>Last-Modified:  Thu Sep 19 16:15:01 +0000 2024
>Originator:     Ond&#345;ej Surư
>Release:        9.4 and 10.0
>Organization:
ISC
>Environment:
NetBSD netbsd-10.home.sury.org 10.0 NetBSD 10.0 (GENERIC) #0: Thu Mar 28 08:33:33 UTC 2024  mkrepro@mkrepro.NetBSD.org:/usr/src/sys/arch/amd64/compile/GENERIC amd64
>Description:
The sched_getaffinity_np() (and pthread_getaffinity_np()) always return an empty set.  It was suggested on IRC this is a bug and just not my misunderstanding how the API should work.
>How-To-Repeat:
Compile and run following code:

```
#include <sched.h>
#include <unistd.h>
#include <stdio.h>

static int cpu_count(cpuset_t* cpuset) {
  int rc;
  cpuid_t i;

  rc = 0;
  for (i = 0;; i++) {
    int r = cpuset_isset(i, cpuset);
    fprintf(stderr, "cpuset_isset(%d, %p) -> %d\n", (int)i, cpuset, r);
    if (r < 0)
      break;
    if (r)
      rc++;
  }

  return rc;
}


int main(void) {
  long rc = -1;
  cpuset_t* set = cpuset_create();
  if (set != NULL) {
    if (0 == sched_getaffinity_np(getpid(), cpuset_size(set), set))
      rc = cpu_count(set);
    cpuset_destroy(set);
  }

  printf("Number of processors reported by sched_getaffinity_np: %d\n", rc);

  if (rc < 1)
    rc = sysconf(_SC_NPROCESSORS_ONLN);

  printf("Number of processors reported by sysconf(_SC_NPROCESSORS_ONLN): %d\n", rc);

  return 0;
}
```

the output is:

```
cpuset_isset(0, 0x7c712ce02008) -> 0
cpuset_isset(1, 0x7c712ce02008) -> 0
cpuset_isset(2, 0x7c712ce02008) -> 0
cpuset_isset(3, 0x7c712ce02008) -> 0
cpuset_isset(4, 0x7c712ce02008) -> 0
cpuset_isset(5, 0x7c712ce02008) -> 0
cpuset_isset(6, 0x7c712ce02008) -> 0
cpuset_isset(7, 0x7c712ce02008) -> 0
cpuset_isset(8, 0x7c712ce02008) -> 0
cpuset_isset(9, 0x7c712ce02008) -> 0
cpuset_isset(10, 0x7c712ce02008) -> 0
cpuset_isset(11, 0x7c712ce02008) -> 0
cpuset_isset(12, 0x7c712ce02008) -> 0
cpuset_isset(13, 0x7c712ce02008) -> 0
cpuset_isset(14, 0x7c712ce02008) -> 0
cpuset_isset(15, 0x7c712ce02008) -> 0
cpuset_isset(16, 0x7c712ce02008) -> 0
cpuset_isset(17, 0x7c712ce02008) -> 0
cpuset_isset(18, 0x7c712ce02008) -> 0
cpuset_isset(19, 0x7c712ce02008) -> 0
cpuset_isset(20, 0x7c712ce02008) -> 0
cpuset_isset(21, 0x7c712ce02008) -> 0
cpuset_isset(22, 0x7c712ce02008) -> 0
cpuset_isset(23, 0x7c712ce02008) -> 0
cpuset_isset(24, 0x7c712ce02008) -> 0
cpuset_isset(25, 0x7c712ce02008) -> 0
cpuset_isset(26, 0x7c712ce02008) -> 0
cpuset_isset(27, 0x7c712ce02008) -> 0
cpuset_isset(28, 0x7c712ce02008) -> 0
cpuset_isset(29, 0x7c712ce02008) -> 0
cpuset_isset(30, 0x7c712ce02008) -> 0
cpuset_isset(31, 0x7c712ce02008) -> 0
cpuset_isset(32, 0x7c712ce02008) -> -1
Number of processors reported by sched_getaffinity_np: 0
Number of processors reported by sysconf(_SC_NPROCESSORS_ONLN): 6
```
>Fix:
:shrug:

>Audit-Trail:
From: Martin Husemann <martin@duskware.de>
To: gnats-bugs@netbsd.org
Cc: 
Subject: Re: lib/58679: sched_getaffinity_np() always returns an empty set
Date: Thu, 19 Sep 2024 14:54:24 +0200

 The default affinity of a process in NetBSD is the empty set (that is:
 no specific affinity is defined).

 I don't think this should be considered a bug.

 If you set some affinity, the sched_getaffinity* calls will return the mask
 you set.

 Martin

From: Christos Zoulas <christos@zoulas.com>
To: gnats-bugs@netbsd.org
Cc: lib-bug-people@netbsd.org, gnats-admin@netbsd.org,
 netbsd-bugs@netbsd.org, ondrej@sury.org
Subject: Re: lib/58679: sched_getaffinity_np() always returns an empty set
Date: Thu, 19 Sep 2024 10:14:26 -0400

 Not really a bug.

 christos

 [10:10am] 26>cat getaffinity.c
 #include <sched.h>
 #include <unistd.h>
 #include <err.h>
 #include <stdio.h>

 static int
 cpu_count(cpuset_t *cpuset)
 {
          int             rc;
          cpuid_t         i;

          rc = 0;
          for (i = 0;; i++) {
                  int             r = cpuset_isset(i, cpuset);
                  fprintf(stderr, "cpuset_isset(%d, %p) -> %d\n", (int)i, 
 cpuset, r);
                  if (r < 0)
                          break;
                  if (r)
                          rc++;
          }

          return rc;
 }


 int
 main(void)
 {
          int             rc = -1;
          cpuset_t       *set = cpuset_create();
          if (set != NULL) {
                  cpuset_set(0, set);
                  if (-1 == sched_setaffinity_np(getpid(), 
 cpuset_size(set), set))
                          err(1, "setaffinity");
                  if (0 == sched_getaffinity_np(getpid(), 
 cpuset_size(set), set))
                          rc = cpu_count(set);
                  cpuset_destroy(set);
          }

          printf("Number of processors reported by sched_getaffinity_np: 
 %d\n", rc);

          if (rc < 1)
                  rc = sysconf(_SC_NPROCESSORS_ONLN);

          printf("Number of processors reported by 
 sysconf(_SC_NPROCESSORS_ONLN): %d\n", rc);

          return 0;
 }

From: mlelstv@serpens.de (Michael van Elst)
To: gnats-bugs@netbsd.org
Cc: 
Subject: Re: lib/58679: sched_getaffinity_np() always returns an empty set
Date: Thu, 19 Sep 2024 14:17:51 -0000 (UTC)

 gnats-admin@NetBSD.org ("Martin Husemann via gnats") writes:

 >The following reply was made to PR lib/58679; it has been noted by GNATS.

 >From: Martin Husemann <martin@duskware.de>
 >To: gnats-bugs@netbsd.org
 >Cc: 
 >Subject: Re: lib/58679: sched_getaffinity_np() always returns an empty set
 >Date: Thu, 19 Sep 2024 14:54:24 +0200

 > The default affinity of a process in NetBSD is the empty set (that is:
 > no specific affinity is defined).
 > 
 > I don't think this should be considered a bug.
 > 
 > If you set some affinity, the sched_getaffinity* calls will return the mask
 > you set.


 While the behaviour of sched_getaffinity isn't well defined, that's
 not true for the pthread_getaffinity_np function.

 The default affinity mask there is defined as 'all ones' and an
 'all zeros' mask is invalid for setting affinity.

From: =?utf-8?Q?Ond=C5=99ej_Sur=C3=BD?= <ondrej@sury.org>
To: gnats-bugs@netbsd.org
Cc: lib-bug-people@netbsd.org, gnats-admin@netbsd.org,
 netbsd-bugs@netbsd.org
Subject: Re: lib/58679: sched_getaffinity_np() always returns an empty set
Date: Thu, 19 Sep 2024 16:25:07 +0200

 In that case, please consider this to be a documentation bug as the NetBSD i=
 mplementation differs from sched_getaffinity (Linux) and cpuset_getaffinity (=
 FreeBSD). And it is reasonable to expect the similarity between those.

 As a side note, it doesn=E2=80=99t make it any easier to implement the code t=
 hat pulls the number CPUs available as one must call both sched_getaffinity_=
 np() and if that fails fallback to sysconf(). At least I expect the process d=
 oes inherit the affinity from the parent, right?

 Thank you,
 Ondrej
 --
 Ond=C5=99ej Sur=C3=BD (He/Him)

 > On 19. 9. 2024, at 14:55, Martin Husemann via gnats <gnats-admin@netbsd.or=
 g> wrote:
 >=20
 > The default affinity of a process in NetBSD is the empty set (that is:
 > no specific affinity is defined).
 >=20
 > I don't think this should be considered a bug.
 >=20
 > If you set some affinity, the sched_getaffinity* calls will return the mas=
 k
 > you set.

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.47 2022/09/11 19:34:41 kim Exp $
$NetBSD: gnats_config.sh,v 1.9 2014/08/02 14:16:04 spz Exp $
Copyright © 1994-2024 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.