Skip to content

Commit 7880a2d

Browse files
DjDeveloperrchriskearney
authored andcommitted
mm: reuse backing folios for small-granule anonymous COW
Closing a subpage pool after each COW fault leaves every remaining slot unavailable for later faults. On a 16K kernel with a 4K process ABI, a 64MiB densely modified fork child therefore uses 256MiB of backing. Retain bounded, address-group pools for eligible faults in each mm. Keep charge domains and memory policy explicit, preserve independent fork ownership, and close pools after exit_mmap while referenced owners remain alive. Copy into a reserved slot under the folio lock, decide first rmap/LRU publication after relocking, and fall back to private backing if nonlinear rmap metadata cannot be allocated. Distribute concurrent faults across 16 address-group pools, skip busy backing on the normal path, and use exact per-offset free counts to avoid repeatedly scanning incompatible partial owners. Speculative allocation and charge do not invoke OOM; under pressure, retry reusable backing before ordinary allocation. Existing policy-sensitive, locked, droppable and multi-node faults keep private backing. Add a user4k selftest covering concurrent, sparse, phased and random COW, mlock and lazy-free transitions, and a descendant that retains data after the pool-owning mm exits. Each case checks untouched bytes and parent isolation. Physical density and reclaim were tested separately on Neo hardware; the selftest does not infer them from logical RSS. Assisted-by: LLM Signed-off-by: Diljit Singh <[email protected]> (cherry picked from commit 5469651)
1 parent c836492 commit 7880a2d

7 files changed

Lines changed: 690 additions & 39 deletions

File tree

‎include/linux/mm_subpage.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ struct mm_subpage_pool *mm_subpage_pool_create_granule(gfp_t gfp, unsigned int s
4545
void mm_subpage_pool_get(struct mm_subpage_pool *pool);
4646
void mm_subpage_pool_put(struct mm_subpage_pool *pool);
4747
void mm_subpage_pool_close(struct mm_subpage_pool *pool);
48+
#ifdef CONFIG_MM_SUBPAGE
49+
/* Optional per-mm pool: default policy, one memory node, matching memcg. */
50+
struct mm_subpage_pool *mm_subpage_cow_pool_get(struct mm_struct *mm,
51+
struct vm_area_struct *vma, unsigned long address);
52+
void mm_subpage_cow_pool_exit(struct mm_struct *mm);
53+
bool mm_subpage_cow_folio_matches(struct mm_subpage_pool *pool, struct folio *folio);
54+
bool mm_subpage_cow_wait_busy_at(struct mm_subpage_pool *pool, unsigned int offset);
55+
/* Return an initialized slot with its folio locked; may wait without a pool lock. */
56+
struct mm_subpage *mm_subpage_alloc_at_locked(struct mm_subpage_pool *pool,
57+
unsigned int offset);
58+
struct mm_subpage *mm_subpage_copy_at_locked(struct mm_subpage_pool *pool,
59+
const struct mm_subpage *source, unsigned int offset);
60+
#else
61+
static inline void mm_subpage_cow_pool_exit(struct mm_struct *mm) { }
62+
#endif
4863

4964
/* Success consumes the folio reference. On error the caller retains it. */
5065
int mm_subpage_pool_add_folio(struct mm_subpage_pool *pool, struct folio *folio,

‎include/linux/mm_types.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,8 @@ struct mm_struct {
13031303
#ifdef CONFIG_MM_SUBPAGE
13041304
/* VM_ACCOUNT granules; global commitment rounds this sum once. */
13051305
unsigned long committed_user_pages;
1306+
/* Shared COW slots; never inherited by a forked mm. */
1307+
struct mm_subpage_cow_pools *cow_subpage_pool;
13061308
#endif
13071309
unsigned long locked_vm; /* Locked bytes >> MM_ACCOUNT_SHIFT */
13081310
atomic64_t pinned_vm; /* Pinned native physical pages */

‎kernel/fork.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <linux/completion.h>
3333
#include <linux/personality.h>
3434
#include <linux/mempolicy.h>
35+
#include <linux/mm_subpage.h>
3536
#include <linux/sem.h>
3637
#include <linux/file.h>
3738
#include <linux/fdtable.h>
@@ -1097,6 +1098,7 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p,
10971098
mm->map_count = 0;
10981099
#ifdef CONFIG_MM_SUBPAGE
10991100
mm->committed_user_pages = 0;
1101+
mm->cow_subpage_pool = NULL;
11001102
#endif
11011103
mm->locked_vm = 0;
11021104
atomic64_set(&mm->pinned_vm, 0);
@@ -1212,6 +1214,7 @@ static inline void __mmput(struct mm_struct *mm)
12121214
ksm_exit(mm);
12131215
khugepaged_exit(mm); /* must run before exit_mmap */
12141216
exit_mmap(mm);
1217+
mm_subpage_cow_pool_exit(mm);
12151218
mm_put_huge_zero_folio(mm);
12161219
set_mm_exe_file(mm, NULL);
12171220
if (!list_empty(&mm->mmlist)) {

‎mm/memory.c‎

Lines changed: 129 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888

8989
#include "pgalloc-track.h"
9090
#include <linux/mm_subpage.h>
91+
#include "user-subpage-internal.h"
9192

9293
#include "internal.h"
9394
#include "swap.h"
@@ -1325,6 +1326,98 @@ static inline struct folio *folio_prealloc(struct mm_struct *src_mm,
13251326
}
13261327

13271328
#ifdef CONFIG_MM_SUBPAGE
1329+
/* Skipping a busy owner must not trigger an avoidable global or memcg OOM. */
1330+
static struct folio *cow_subpage_speculative_folio(struct mm_struct *mm,
1331+
struct vm_area_struct *vma, unsigned long addr)
1332+
{
1333+
gfp_t alloc_gfp = GFP_HIGHUSER_MOVABLE | __GFP_NORETRY | __GFP_NOWARN;
1334+
gfp_t charge_gfp = GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN;
1335+
unsigned int order = max_t(unsigned int, mm_page_shift(mm), PAGE_SHIFT) - PAGE_SHIFT;
1336+
struct folio *folio = vma_alloc_folio(alloc_gfp, order, vma, addr);
1337+
1338+
if (!folio)
1339+
return NULL;
1340+
if (mem_cgroup_charge(folio, mm, charge_gfp)) {
1341+
folio_put(folio);
1342+
return NULL;
1343+
}
1344+
folio_throttle_swaprate(folio, charge_gfp);
1345+
return folio;
1346+
}
1347+
1348+
/* The returned initialized slot owns a reference and has its folio locked. */
1349+
static struct mm_subpage *cow_subpage_prealloc(struct mm_struct *mm,
1350+
struct vm_area_struct *vma, unsigned long addr,
1351+
struct mm_subpage *source, bool force_private, bool *pooled)
1352+
{
1353+
struct mm_subpage_pool *pool;
1354+
struct mm_subpage *slot;
1355+
struct folio *folio;
1356+
unsigned int offset = vma_page_offset_at(vma, addr).offset;
1357+
int err;
1358+
1359+
retry_private:
1360+
pool = force_private ? NULL : mm_subpage_cow_pool_get(mm, vma, addr);
1361+
*pooled = pool != NULL;
1362+
if (!pool)
1363+
pool = mm_subpage_pool_create_granule(GFP_KERNEL, mm_page_shift(mm));
1364+
if (!pool)
1365+
return ERR_PTR(-ENOMEM);
1366+
for (;;) {
1367+
slot = source ? mm_subpage_copy_at_locked(pool, source, offset) :
1368+
mm_subpage_alloc_at_locked(pool, offset);
1369+
if (!IS_ERR(slot) || PTR_ERR(slot) != -EAGAIN)
1370+
break;
1371+
folio = NULL;
1372+
if (*pooled) {
1373+
folio = cow_subpage_speculative_folio(mm, vma, addr);
1374+
if (!folio && mm_subpage_cow_wait_busy_at(pool, offset)) {
1375+
cond_resched();
1376+
continue;
1377+
}
1378+
}
1379+
/* No reusable owner exists after speculative allocation failed.
1380+
* Ordinary allocation/charge semantics are now appropriate.
1381+
*/
1382+
if (!folio)
1383+
folio = folio_prealloc(mm, vma, addr, false);
1384+
if (!folio) {
1385+
slot = ERR_PTR(-ENOMEM);
1386+
break;
1387+
}
1388+
/* A concurrent cgroup move may change the fresh folio's charge. */
1389+
if (*pooled && !mm_subpage_cow_folio_matches(pool, folio)) {
1390+
folio_put(folio);
1391+
mm_subpage_pool_put(pool);
1392+
force_private = true;
1393+
goto retry_private;
1394+
}
1395+
err = mm_subpage_pool_add_folio_at(pool, folio, offset,
1396+
*pooled ? GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN : GFP_KERNEL);
1397+
if (*pooled && err == -ENOMEM) {
1398+
if (mm_subpage_cow_wait_busy_at(pool, offset)) {
1399+
folio_put(folio);
1400+
cond_resched();
1401+
continue;
1402+
}
1403+
err = mm_subpage_pool_add_folio_at(pool, folio, offset, GFP_KERNEL);
1404+
}
1405+
if (err) {
1406+
folio_put(folio);
1407+
if (err == -EEXIST) {
1408+
cond_resched();
1409+
continue;
1410+
}
1411+
slot = ERR_PTR(err);
1412+
break;
1413+
}
1414+
}
1415+
if (!*pooled)
1416+
mm_subpage_pool_close(pool);
1417+
mm_subpage_pool_put(pool);
1418+
return slot;
1419+
}
1420+
13281421
static struct mm_subpage *anon_subpage_prealloc(struct mm_struct *mm,
13291422
struct vm_area_struct *vma, unsigned long addr)
13301423
{
@@ -4541,11 +4634,11 @@ static vm_fault_t wp_subpage_copy(struct vm_fault *vmf)
45414634
struct vm_area_struct *vma = vmf->vma;
45424635
struct mm_struct *mm = vma->vm_mm;
45434636
struct mm_subpage *source = NULL, *dest = NULL;
4544-
struct mm_subpage_pool *pool = NULL;
45454637
struct folio *old = vmf->page ? page_folio(vmf->page) : NULL;
45464638
struct folio *new = NULL;
45474639
struct mmu_notifier_range range;
45484640
bool copied = false, unshare = vmf->flags & FAULT_FLAG_UNSHARE;
4641+
bool force_private = false, pooled, fresh, retry_private = false;
45494642
unsigned int offset;
45504643
vm_fault_t ret = VM_FAULT_SIGBUS;
45514644
pte_t entry;
@@ -4598,42 +4691,49 @@ static vm_fault_t wp_subpage_copy(struct vm_fault *vmf)
45984691
goto out;
45994692
offset = vma_page_offset_at(vma, vmf->address).offset;
46004693
ret = VM_FAULT_OOM;
4601-
pool = mm_subpage_pool_create_granule(GFP_KERNEL, mm_page_shift(mm));
4602-
if (!pool)
4603-
goto out;
4604-
new = folio_prealloc(mm, vma, vmf->address, false);
4605-
if (!new)
4606-
goto out;
4607-
err = mm_subpage_pool_add_folio(pool, new, GFP_KERNEL);
4608-
if (err) {
4609-
folio_put(new);
4610-
goto out;
4611-
}
4612-
dest = source ? mm_subpage_copy_at(pool, source, offset) :
4613-
mm_subpage_alloc_at(pool, offset);
4694+
retry_copy:
4695+
dest = cow_subpage_prealloc(mm, vma, vmf->address, source, force_private, &pooled);
46144696
if (IS_ERR(dest)) {
46154697
if (PTR_ERR(dest) == -EHWPOISON)
46164698
ret = VM_FAULT_HWPOISON;
46174699
dest = NULL;
46184700
goto out;
46194701
}
4702+
new = mm_subpage_folio(dest);
46204703
if (old && !source) {
46214704
err = copy_user_subpage_range(&new->page, offset, vmf->page,
46224705
pte_phys_mm(mm, vmf->orig_pte) & ~PAGE_MASK,
46234706
mm_page_size(mm));
46244707
if (err) {
4708+
folio_unlock(new);
46254709
ret = VM_FAULT_HWPOISON;
46264710
goto out;
46274711
}
46284712
}
4629-
mm_subpage_pool_close(pool);
4713+
/* Initialisation is complete; the slot reference prevents migration/free.
4714+
* Do not carry a folio lock through MMU notifier callbacks.
4715+
*/
4716+
folio_unlock(new);
46304717
mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm,
46314718
vmf->address, vmf->address + mm_page_size(mm));
46324719
mmu_notifier_invalidate_range_start(&range);
46334720
/* Keep the folio alive after a concurrent zap can consume its PTE ref. */
46344721
folio_get(new);
46354722
folio_lock(new);
46364723
ret = 0;
4724+
/* A different fault may publish this owner's first PTE while we wait.
4725+
* Only the first publisher establishes folio rmap/LRU. Preflight nonlinear
4726+
* slot identity before taking PTL, with a fresh-folio fallback on ENOMEM.
4727+
*/
4728+
fresh = !mm_subpage_anon_root(dest);
4729+
if (!fresh) {
4730+
err = mm_subpage_bind_rmap(dest, vma, vmf->address);
4731+
if (err) {
4732+
ret = err == -ENOMEM ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
4733+
retry_private = pooled && err == -ENOMEM;
4734+
goto unlock_folio;
4735+
}
4736+
}
46374737
vmf->pte = pte_offset_map_lock(mm, vmf->pmd, vmf->address, &vmf->ptl);
46384738
if (!vmf->pte)
46394739
goto unlock_folio;
@@ -4642,7 +4742,8 @@ static vm_fault_t wp_subpage_copy(struct vm_fault *vmf)
46424742
goto unlock_pte;
46434743
}
46444744
mm_subpage_set_exclusive(dest);
4645-
err = mm_subpage_add_new_anon_rmap(dest, vma, vmf->address);
4745+
err = fresh ? mm_subpage_add_new_anon_rmap(dest, vma, vmf->address) :
4746+
mm_subpage_add_anon_rmap(dest, vma, vmf->address);
46464747
if (WARN_ON_ONCE(err)) {
46474748
ret = VM_FAULT_SIGBUS;
46484749
goto unlock_pte;
@@ -4660,7 +4761,8 @@ static vm_fault_t wp_subpage_copy(struct vm_fault *vmf)
46604761
flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte));
46614762
/* Break-before-make also orders removal of the old slot's rmap. */
46624763
ptep_clear_flush(vma, vmf->address, vmf->pte);
4663-
subpage_add_new_lru(new, vma, 1);
4764+
if (fresh)
4765+
subpage_add_new_lru(new, vma, 1);
46644766
set_pte_at(mm, vmf->address, vmf->pte, entry);
46654767
update_mmu_cache_range(vmf, vma, vmf->address, vmf->pte, 1);
46664768
if (source) {
@@ -4683,13 +4785,17 @@ static vm_fault_t wp_subpage_copy(struct vm_fault *vmf)
46834785
folio_unlock(new);
46844786
folio_put(new);
46854787
mmu_notifier_invalidate_range_end(&range);
4686-
out:
4687-
if (pool) {
4688-
mm_subpage_pool_close(pool);
4689-
if (dest)
4690-
mm_subpage_put(dest);
4691-
mm_subpage_pool_put(pool);
4788+
if (retry_private) {
4789+
mm_subpage_put(dest);
4790+
dest = NULL;
4791+
force_private = true;
4792+
retry_private = false;
4793+
ret = VM_FAULT_OOM;
4794+
goto retry_copy;
46924795
}
4796+
out:
4797+
if (dest)
4798+
mm_subpage_put(dest);
46934799
if (source) {
46944800
if (copied)
46954801
mm_subpage_put(source); /* Retired PTE reference, after TLBI. */

0 commit comments

Comments
 (0)