[PATCH v7] Mark pages with MADV_DONTNEED to shrink and grow rather than mremap()

Wilco Dijkstra Wilco.Dijkstra@arm.com
Tue Sep 30 14:48:38 GMT 2025


Hi William,

That looks good, just a few minor issues with check_may_overcommit():

>  static inline bool
> +check_may_overcommit (void)
>  {
> +  static int may_overcommit = -1;
>  
> +  if (__glibc_likely (atomic_load_relaxed (&may_overcommit) >= 0))
> +    return may_overcommit;

OK

> +  int fd = __open_nocancel ("/proc/sys/vm/overcommit_memory",
> +                            O_RDONLY | O_CLOEXEC);
> +  if (fd >= 0)
>      {
> +      char val;
> +      ssize_t n = __read_nocancel (fd, &val, 1);
> +      int expected = -1;
> +      atomic_compare_exchange_weak (&may_overcommit, &expected,
> +                                    !(n > 0 && val == '2'));

This is an odd use of compare&exchange and overkill for the initialize-once
idiom. We just need to compute the value in a local, and then ...

> +      __close_nocancel_nostatus (fd);
>      }

... write it here unconditionally using a basic atomic write.

> +  return may_overcommit;
>  }

We can just return the locally computed value at this point.

This may return without updating the value if the file doesn't exist and then we
repeatedly retry (which is expensive and not what the code did originally).
Also by reading it without atomics you might get a partially updated value if
other threads write to it at the same time.

Cheers,
Wilco


More information about the Libc-alpha mailing list