NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

kern/61128: futex: a shared futex key on MAP_SHARED anonymous memory makes the next write copy the page, losing other processes' stores



>Number:         61128
>Category:       kern
>Synopsis:       futex: a shared futex key on MAP_SHARED anonymous memory makes the next write copy the page, losing other processes' stores
>Confidential:   no
>Severity:       serious
>Priority:       high
>Responsible:    kern-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sat Dec 05 01:30:00 +0000 2026
>Originator:     Elias Benali
>Release:        current/trunk as of 2026-12-03
>Organization:
pgfault.com
>Environment:
NetBSD netbsd 11.99.11 NetBSD 11.99.11 (GENERIC) #0: Thu Dec  3 23:44:39 PST 2026  ines@spire3:/tmp/up-obj/GENERIC amd64
>Description:
A shared futex operation (without FUTEX_PRIVATE_FLAG) on mmapped (SHARED | ANON) keys the futex with uvm_voaddr_acquire()

For memory in an amap that function names the page by its vm_anon and holds a reference on it while they key is in use:

  anon->an_ref++;
  // ...
  UVM_VOADDR_SET_ANON(voaddr, anon);

The fault code reads an anon an_ref>1 as shared with another amap  and thereforce as COW, so the copy lands in the shared amap and writes get lost.

uvm_fault_upper_direct() maps it read-only and uvm_fault_upper() promotes it on a write fault;

So while some process holds a key on the page (a waiter is sleeping on it or a FUTEX_WAKE is in progress), a write fault in any process that maps it copies the page into a new anon and install that anon in the shared amap with amap_add(..., replace);

The other processes mappings of the old page are removed ONLY AFTER THE COPY.

Stores they make to the old page between the copy and the removal are lost, including atomic ops.
>How-To-Repeat:
- Spawn 8 processes, sharing one MAP_SHARED | MAP_ANON page.
- each increments its own counter and a shared atomic counter and calls FUTEX_WAKE(shared) on a word in the page, 100'000 times.

struct page {
  volatile unsigned long count[8][8];
  volatile unsigned long total;
  volatile int word;
} *pg = mmap(NULL, sizeof(*pg), PROT_READ|PROT_WRITE,
             MAP_SHARED|MAP_ANON, -1, 0);

/* in child i: */
for (j = 0; j < 100000; j++) {
  pg->count[i][0]++;
  atomic_inc_ulong(&pg->total);
  syscall(SYS___futex, &pg->word, FUTEX_WAKE, 1,
          NULL, NULL, 0, 0);
}

After all the children exit, total should be 800'000;

On the stock GENERIC it was 799999, 799993 and 799989 in three runs and some per-child counts fell short too.

Without the futex calls every run comes up exact.
>Fix:
- identify amap memory by (amap, slot), rather than the anon.
- give a COW entry its own amap first (amap_copy) before taking they key.
  otherwise a key taken would go stale as soon as the copy happens.
- use the private key, (vmspace,va) for a shared futex in private memory.
  except for minhert(MAP_INHERIT_SHARE) which ends up being handed to a child shared on a fork()




Home | Main Index | Thread Index | Old Index