Re: svn commit: r214611 - head/sys/kern

From: Kostik Belousov <kostikbel_at_gmail.com>
Date: Wed, 15 Jun 2011 10:39:34 +0300
On Wed, Jun 15, 2011 at 09:41:28AM +0400, Sergey Kandaurov wrote:
> On 15 June 2011 06:20, David Xu <davidxu_at_freebsd.org> wrote:
> > On 2011/06/14 20:02, Sergey Kandaurov wrote:
> >> On 1 November 2010 03:42, David Xu <davidxu_at_freebsd.org> wrote:
> >>> Author: davidxu
> >>> Date: Mon Nov š1 00:42:25 2010
> >>> New Revision: 214611
> >>> URL: http://svn.freebsd.org/changeset/base/214611
> >>>
> >>> Log:
> >>> šUse integer for size of cpuset, as it won't be bigger than INT_MAX,
> >>> šThis is requested by bge.
> >>> šAlso move the sysctl into file kern_cpuset.c, because it should
> >>> šalways be there, it is independent of thread scheduler.
> >>
> >> Hi.
> >> This breaks for me fetching a cpusetsize value with sysconf(3) interface,
> >> as after this change sysconf(3) consumers expect a long return type, while
> >> sysctl kern.sched.cpusetsize has switched from long to int type in kernel.
> >> That makes for me sizeof(cpusize_t) from 8 to incorrect 34359738376.
> >>
> >> In particular, kvm_getpcpu(3) uses sysconf(3) to fetch cpusetsize on
> >> live kernel. That gives me a broken result:
> >> kvm_open: kcpusetsize: 8
> >> pcpu[0] = 0x801072300
> >> kvm_open: kcpusetsize: 34359738376
> >> pcpu[1] = 0xffffffffffffffff
> >> kvm_open: kcpusetsize: 8
> >> pcpu[2] = 0x801072600
> >> kvm_open: kcpusetsize: 34359738376
> >> pcpu[3] = 0xffffffffffffffff
> >>
> >> This small test indicates that that's due to int->long type conversion:
> >> š š š š long lvalue;
> >> š š š š size_t len;
> >>
> >> š š š š len = sizeof(lvalue);
> >> š š š š if (sysctlbyname("kern.sched.cpusetsize", &lvalue, &len, NULL, 0) < 0)
> >> š š š š š š š š err(1, "sysctlbyname");
> >> š š š š printf("sysctl: %ld\n", lvalue);
> >> š š š š printf("sysctl: %d -- explicitly casted to (int)\n", (int)lvalue);
> >> š š š š printf("sysconf: %ld\n", sysconf(_SC_CPUSET_SIZE));
> >> š š š š printf("sysconf: %d -- explicitly casted to (int)\n",
> >> (int)sysconf(_SC_CPUSET_SIZE));
> >>
> >> That prints:
> >> sysctl: 34359738376
> >> sysctl: 8 -- explicitly casted to (int)
> >> sysconf: 34359738376
> >> sysconf: 8 -- explicitly casted to (int)
> >>
> >> The other way to solve this other than reverting is to "fix" all cpusetsize
> >> consumers in userland. Now sysconf() saves long returned value to int:
> >>
> >> Index: lib/libkvm/kvm_pcpu.c
> >> ===================================================================
> >> --- lib/libkvm/kvm_pcpu.c š š š (revision 223073)
> >> +++ lib/libkvm/kvm_pcpu.c š š š (working copy)
> >> _at__at_ -120,7 +120,7 _at__at_
> >> švoid *
> >> škvm_getpcpu(kvm_t *kd, int cpu)
> >> š{
> >> - š š š long kcpusetsize;
> >> + š š š int kcpusetsize;
> >> š š š š ssize_t nbytes;
> >> š š š š uintptr_t readptr;
> >> š š š š char *buf;
> >>
> >> So, after applying the above change all is ok:
> >> kvm_open: kcpusetsize: 8
> >> pcpu[0] = 0x801072300
> >> kvm_open: kcpusetsize: 8
> >> pcpu[1] = 0x801072600
> >> kvm_open: kcpusetsize: 8
> >> pcpu[2] = 0x801072900
> >> kvm_open: kcpusetsize: 8
> >> pcpu[3] = 0x801072c00
> >>
> >>
> > Try this patch, I think it should fix it.
> >
> > Index: lib/libc/gen/sysconf.c
> > ===================================================================
> > --- lib/libc/gen/sysconf.c š š š(revision 221356)
> > +++ lib/libc/gen/sysconf.c š š š(working copy)
> > _at__at_ -599,11 +599,11 _at__at_
> >
> > š#ifdef _SC_CPUSET_SIZE
> > š š š šcase _SC_CPUSET_SIZE:
> > - š š š š š š š len = sizeof(lvalue);
> > - š š š š š š š if (sysctlbyname("kern.sched.cpusetsize", &lvalue, &len, NULL,
> > + š š š š š š š len = sizeof(value);
> > + š š š š š š š if (sysctlbyname("kern.sched.cpusetsize", &value, &len, NULL,
> > š š š š š š š š š š0) == -1)
> > š š š š š š š š š š š šreturn (-1);
> > - š š š š š š š return (lvalue);
> > + š š š š š š š return ((long)(value));
> > š#endif
> >
> > š š š šdefault:
> >
> 
> Great, thanks! Look good for me.
> Nitpicking:
>  return ((long)value); should be enough (extra parenthesis).
This patch accomodates the userland to the changed ABI. Why it was
changed at all ? I would argue that keeping the stable ABI there is
more important then using a 'clean' type.

At least, the stable branches usermode is broken on the current kernel.

Received on Wed Jun 15 2011 - 05:39:40 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:40:14 UTC