Re: [PATCH] sysinfo: include availram field in sysinfo struct

3 messages, 3 authors, 2022-01-07 · open the first message on its own page

Re: [PATCH] sysinfo: include availram field in sysinfo struct

From: Vlastimil Babka <hidden>
Date: 2022-01-07 16:58:29

CC linux-api

On 1/7/22 14:44, Pintu Agarwal wrote:
quoted hunk
On Fri, 7 Jan 2022 at 17:35, Christian Brauner
[off-list ref] wrote:
quoted
On Thu, Jan 06, 2022 at 08:27:47PM +0300, Cyrill Gorcunov wrote:
quoted
On Thu, Jan 06, 2022 at 10:19:55PM +0530, Pintu Agarwal wrote:
quoted
quoted
quoted
diff --git a/include/uapi/linux/sysinfo.h b/include/uapi/linux/sysinfo.h
index 435d5c2..6e77e90 100644
--- a/include/uapi/linux/sysinfo.h
+++ b/include/uapi/linux/sysinfo.h
@@ -12,6 +12,7 @@ struct sysinfo {
      __kernel_ulong_t freeram;       /* Available memory size */
      __kernel_ulong_t sharedram;     /* Amount of shared memory */
      __kernel_ulong_t bufferram;     /* Memory used by buffers */
+     __kernel_ulong_t availram;      /* Memory available for allocation */
      __kernel_ulong_t totalswap;     /* Total swap space size */
      __kernel_ulong_t freeswap;      /* swap space still available */
      __u16 procs;                    /* Number of current processes */
Hi! Sorry, but I don't understand -- the sysinfo structure seems to
be part of user API, no? Don't we break it up here?
Yes, the corresponding user space header /usr/include/linux/sysinfo.h
also needs to be updated.
When we generate the kernel header it will be updated automatically.
Wait. The userspace may pass old structure here, and in result we
return incorrect layout which won't match old one, no? Old binary
code has no clue about this header update.
Yes, that won't work as done.

If we want to do this it needs to be done at the end of the struct right
before the padding field and the newly added field substracted from the
padding. (Not the preferred way to do it these days for new structs.)

A new kernel can then pass in the struct with the newly added field and
an old kernel can just fill the struct in as usual. New kernel will
update the field with the correct value.

But there's a catch depending on the type of value.
The problem with these types of extensions is that you'll often need
indicators to and from the kernel whether the extension is supported.

Consider an extension where 0 is a valid value meaning "this resource is
completely used". Since the kernel and userspace always agree on the
size of the struct the kernel will zero the whole struct. So if in your
newly added field 0 is a valid value you can't differentiate between 0
as a valid value indicating that your resource isn't available and 0 as
the kernel not supporting your extension.

Other APIs solve this and similar problems by having a request mask and
a return mask.  Userspace fills in what values it wants reported in the
request mask and kernel sets the supported flags in the return mask.
This way you can differentiate between the two (see statx).

If the 0 example is not a concern or acceptable for userspace it's
probably fine. But you need to document that having 0 returned can mean
both things.

Or, you select a value different from 0 (-1?) that you can use to
indicate to userspace that the resource is used up so 0 can just mean
"kernel doesn't support this extension".
Thanks all for your inputs.
As Eric suggested in other thread (pasting here for reference):
{
quoted
Before the padding and you should reduce the size of the padding by the
size of your new field.
quoted
quoted
Also, I could not understand what this is for ?
Do we need to update this since sture is changed ?
quoted
In general padding like that is so new fields can be added.  The
comment about libc5 makes me a wonder a bit, but I expect libc5 just
added the padding in it's copy of the structure that it exported to
userspace many many years ago so that new fields could be added.
quoted
Eric
}

I made the changes like below and this seems to work even with older user space.
I mean earlier, when I ran "free" command it was giving "stack
smashing..." error,
but now the "free" command (which comes as part of busybox) works fine
even without recompiling with the updated header.

These are the header changes for quick look:
{{{
diff --git a/include/uapi/linux/sysinfo.h b/include/uapi/linux/sysinfo.h
index 6e77e90..fe84c6a 100644
--- a/include/uapi/linux/sysinfo.h
+++ b/include/uapi/linux/sysinfo.h
@@ -12,7 +12,6 @@ struct sysinfo {
        __kernel_ulong_t freeram;       /* Available memory size */
        __kernel_ulong_t sharedram;     /* Amount of shared memory */
        __kernel_ulong_t bufferram;     /* Memory used by buffers */
-       __kernel_ulong_t availram;      /* Memory available for allocation */
        __kernel_ulong_t totalswap;     /* Total swap space size */
        __kernel_ulong_t freeswap;      /* swap space still available */
        __u16 procs;                    /* Number of current processes */
@@ -20,7 +19,8 @@ struct sysinfo {
        __kernel_ulong_t totalhigh;     /* Total high memory size */
        __kernel_ulong_t freehigh;      /* Available high memory size */
        __u32 mem_unit;                 /* Memory unit size in bytes */
-       char _f[20-2*sizeof(__kernel_ulong_t)-sizeof(__u32)];   /*
Padding: libc5 uses this.. */
+       __kernel_ulong_t availram;      /* Memory available for allocation */
+       char _f[20-3*sizeof(__kernel_ulong_t)-sizeof(__u32)];   /*
Padding: libc5 uses this.. */
 };
}}}

If this is fine, I will push the new patch set.
Please CC linux-api@vger.kernel.org on the new posting.
Thanks,
Pintu

Re: [PATCH] sysinfo: include availram field in sysinfo struct

From: Pintu Agarwal <hidden>
Date: 2022-01-07 17:47:44

On Fri, 7 Jan 2022 at 22:28, Vlastimil Babka [off-list ref] wrote:
CC linux-api

On 1/7/22 14:44, Pintu Agarwal wrote:
quoted
On Fri, 7 Jan 2022 at 17:35, Christian Brauner
[off-list ref] wrote:
quoted
On Thu, Jan 06, 2022 at 08:27:47PM +0300, Cyrill Gorcunov wrote:
quoted
On Thu, Jan 06, 2022 at 10:19:55PM +0530, Pintu Agarwal wrote:
quoted
quoted
quoted
diff --git a/include/uapi/linux/sysinfo.h b/include/uapi/linux/sysinfo.h
index 435d5c2..6e77e90 100644
--- a/include/uapi/linux/sysinfo.h
+++ b/include/uapi/linux/sysinfo.h
@@ -12,6 +12,7 @@ struct sysinfo {
      __kernel_ulong_t freeram;       /* Available memory size */
      __kernel_ulong_t sharedram;     /* Amount of shared memory */
      __kernel_ulong_t bufferram;     /* Memory used by buffers */
+     __kernel_ulong_t availram;      /* Memory available for allocation */
      __kernel_ulong_t totalswap;     /* Total swap space size */
      __kernel_ulong_t freeswap;      /* swap space still available */
      __u16 procs;                    /* Number of current processes */
Hi! Sorry, but I don't understand -- the sysinfo structure seems to
be part of user API, no? Don't we break it up here?
Yes, the corresponding user space header /usr/include/linux/sysinfo.h
also needs to be updated.
When we generate the kernel header it will be updated automatically.
Wait. The userspace may pass old structure here, and in result we
return incorrect layout which won't match old one, no? Old binary
code has no clue about this header update.
Yes, that won't work as done.

If we want to do this it needs to be done at the end of the struct right
before the padding field and the newly added field substracted from the
padding. (Not the preferred way to do it these days for new structs.)

A new kernel can then pass in the struct with the newly added field and
an old kernel can just fill the struct in as usual. New kernel will
update the field with the correct value.

But there's a catch depending on the type of value.
The problem with these types of extensions is that you'll often need
indicators to and from the kernel whether the extension is supported.

Consider an extension where 0 is a valid value meaning "this resource is
completely used". Since the kernel and userspace always agree on the
size of the struct the kernel will zero the whole struct. So if in your
newly added field 0 is a valid value you can't differentiate between 0
as a valid value indicating that your resource isn't available and 0 as
the kernel not supporting your extension.

Other APIs solve this and similar problems by having a request mask and
a return mask.  Userspace fills in what values it wants reported in the
request mask and kernel sets the supported flags in the return mask.
This way you can differentiate between the two (see statx).

If the 0 example is not a concern or acceptable for userspace it's
probably fine. But you need to document that having 0 returned can mean
both things.

Or, you select a value different from 0 (-1?) that you can use to
indicate to userspace that the resource is used up so 0 can just mean
"kernel doesn't support this extension".
Thanks all for your inputs.
As Eric suggested in other thread (pasting here for reference):
{
quoted
Before the padding and you should reduce the size of the padding by the
size of your new field.
quoted
quoted
Also, I could not understand what this is for ?
Do we need to update this since sture is changed ?
quoted
In general padding like that is so new fields can be added.  The
comment about libc5 makes me a wonder a bit, but I expect libc5 just
added the padding in it's copy of the structure that it exported to
userspace many many years ago so that new fields could be added.
quoted
Eric
}

I made the changes like below and this seems to work even with older user space.
I mean earlier, when I ran "free" command it was giving "stack
smashing..." error,
but now the "free" command (which comes as part of busybox) works fine
even without recompiling with the updated header.

These are the header changes for quick look:
{{{
diff --git a/include/uapi/linux/sysinfo.h b/include/uapi/linux/sysinfo.h
index 6e77e90..fe84c6a 100644
--- a/include/uapi/linux/sysinfo.h
+++ b/include/uapi/linux/sysinfo.h
@@ -12,7 +12,6 @@ struct sysinfo {
        __kernel_ulong_t freeram;       /* Available memory size */
        __kernel_ulong_t sharedram;     /* Amount of shared memory */
        __kernel_ulong_t bufferram;     /* Memory used by buffers */
-       __kernel_ulong_t availram;      /* Memory available for allocation */
        __kernel_ulong_t totalswap;     /* Total swap space size */
        __kernel_ulong_t freeswap;      /* swap space still available */
        __u16 procs;                    /* Number of current processes */
@@ -20,7 +19,8 @@ struct sysinfo {
        __kernel_ulong_t totalhigh;     /* Total high memory size */
        __kernel_ulong_t freehigh;      /* Available high memory size */
        __u32 mem_unit;                 /* Memory unit size in bytes */
-       char _f[20-2*sizeof(__kernel_ulong_t)-sizeof(__u32)];   /*
Padding: libc5 uses this.. */
+       __kernel_ulong_t availram;      /* Memory available for allocation */
+       char _f[20-3*sizeof(__kernel_ulong_t)-sizeof(__u32)];   /*
Padding: libc5 uses this.. */
 };
}}}

If this is fine, I will push the new patch set.
Please CC linux-api@vger.kernel.org on the new posting.
@Christian Brauner,
Regarding 0 case I guess it is fine.
Just to cross check, I used my test program to run with some other
kernel (where there are no changes to sysinfo).
I see that the field returns 0.
# ./test-sysinfo.out
Total RAM: 249320 kB
Free RAM: 233416 kB
Avail RAM: 0 kB

And this is fine and this is also good.
This also indicates 2 things:
a) Either "availram" field is not available in this kernel version
(less than 5.1x)
==> Thus it should fall back to parsing MemAvailable from /proc/meminfo
b) Or, MemAvailable field itself is not available (less than 3.1x)

I will push the new patch set now..

Thanks all!
Pintu

RE: [PATCH] sysinfo: include availram field in sysinfo struct

From: David Laight <hidden>
Date: 2022-01-07 22:18:38

quoted
These are the header changes for quick look:
{{{
diff --git a/include/uapi/linux/sysinfo.h b/include/uapi/linux/sysinfo.h
index 6e77e90..fe84c6a 100644
--- a/include/uapi/linux/sysinfo.h
+++ b/include/uapi/linux/sysinfo.h
@@ -12,7 +12,6 @@ struct sysinfo {
        __kernel_ulong_t freeram;       /* Available memory size */
        __kernel_ulong_t sharedram;     /* Amount of shared memory */
        __kernel_ulong_t bufferram;     /* Memory used by buffers */
-       __kernel_ulong_t availram;      /* Memory available for allocation */
        __kernel_ulong_t totalswap;     /* Total swap space size */
        __kernel_ulong_t freeswap;      /* swap space still available */
        __u16 procs;                    /* Number of current processes */
@@ -20,7 +19,8 @@ struct sysinfo {
        __kernel_ulong_t totalhigh;     /* Total high memory size */
        __kernel_ulong_t freehigh;      /* Available high memory size */
        __u32 mem_unit;                 /* Memory unit size in bytes */
-       char _f[20-2*sizeof(__kernel_ulong_t)-sizeof(__u32)];   /*
Padding: libc5 uses this.. */
+       __kernel_ulong_t availram;      /* Memory available for allocation */
+       char _f[20-3*sizeof(__kernel_ulong_t)-sizeof(__u32)];   /*
Padding: libc5 uses this.. */
 };
}}}

If this is fine, I will push the new patch set.
Please CC linux-api@vger.kernel.org on the new posting.
That is probably still broken.
If __kernel_ulong_t is 64bit there is architecture
dependant padding after mem_unit.

In particular a 32bit x86 app running on a 64bit kernel
will probably see the wrong layout.

You definitely need a compile-time assert on the total structure size.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help