mempolicy: cleanups leading to NUMA mpol without vma
Subsystems (11)
Hugh Dickins
maintainercover mempolicy: cleanups leading to NUMA mpol without vma
Here is a series of patches based on v6.6-rc3: mostly cleanups in mm/mempolicy.c, but finally removing the pseudo-vma from shmem folio allocation, and removing the mmap_lock around folio migration for mbind and migrate_pages syscalls.
Applies cleanly to any v6.6-rc so far, but not quite to mm-unstable or
linux-next: a couple of rejects in mm/mempolicy.c (clashing with the vma_start_write() in do_mbind() and the foliation in mpol_misplaced()) and in mm/swap_state.c (clashing with zswap mods). Adjustments will also be needed if hugetlb page->index changes to PAGE_SIZE: interleave has to co-operate with that. After some review, a v2 will probably be issued, based then on mm-unstable.
01/12 hugetlbfs: drop shared NUMA mempolicy pretence 02/12 kernfs: drop shared NUMA mempolicy hooks
03/12 mempolicy: fix migrate_pages(2) syscall return 04/12 mempolicy trivia: delete those ancient pr_debug()s 05/12 mempolicy trivia: slightly more consistent naming 06/12 mempolicy trivia: use pgoff_t in shared mempolicy tree 07/12 mempolicy: mpol_shared_policy_init() without pseudo-vma 08/12 mempolicy: remove confusing MPOL_MF_LAZY dead code 09/12 mm: add page_rmappable_folio() wrapper
10/12 mempolicy: alloc_pages_mpol() for NUMA policy without vma 11/12 mempolicy: mmap_lock is not needed while migrating folios 12/12 mempolicy: migration attempt to match interleave nodes
fs/hugetlbfs/inode.c | 41 +- fs/kernfs/file.c | 49 -- fs/proc/task_mmu.c | 5 +- include/linux/gfp.h | 10 +- include/linux/huge_mm.h | 13 + include/linux/hugetlb.h | 11 - include/linux/mempolicy.h | 36 +- include/linux/mm.h | 2 +- include/uapi/linux/mempolicy.h | 2 +- ipc/shm.c | 21 +- mm/hugetlb.c | 38 +- mm/mempolicy.c | 952 ++++++++++++++++------------------- mm/page_alloc.c | 8 +- mm/shmem.c | 102 ++-- mm/swap.h | 9 +- mm/swap_state.c | 86 ++-- 16 files changed, 617 insertions(+), 768 deletions(-)
Hugh
Hugh Dickins
maintainerPATCH 1/12 hugetlbfs: drop shared NUMA mempolicy pretence
hugetlbfs_fallocate() goes through the motions of pasting a shared NUMA mempolicy onto its pseudo-vma, but how could there ever be a shared NUMA mempolicy for this file? hugetlb_vm_ops has never offered a set_policy method, and hugetlbfs_parse_param() has never supported any mpol options for a mount-wide default policy.
It's just an illusion: clean it away so as not to confuse others, giving us more freedom to adjust shmem's set_policy/get_policy implementation. But hugetlbfs_inode_info is still required, just to accommodate seals.
Yes, shared NUMA mempolicy support could be added to hugetlbfs, with a set_policy method and/or mpol mount option (Andi's first posting did include an admitted-unsatisfactory hugetlb_set_policy()); but it seems that nobody has bothered to add that in the nineteen years since v2.6.7 made it possible, and there is at least one company that has invested enough into hugetlbfs, that I guess they have learnt well enough how to manage its NUMA, without needing shared mempolicy.
--- fs/hugetlbfs/inode.c | 41 +---------------------------------------- include/linux/hugetlb.h | 2 -- 2 files changed, 1 insertion(+), 42 deletions(-)
fs/hugetlbfs/inode.c +1 -40
@@ -83,29 +83,6 @@ static const struct fs_parameter_spec hugetlb_fs_parameters[] = { {} }; -#ifdef CONFIG_NUMA -static inline void hugetlb_set_vma_policy (struct vm_area_struct *vma, -struct inode *inode,pgoff_t index) -{ - vma->vm_policy =mpol_shared_policy_lookup (&HUGETLBFS_I (inode)->policy, - index); -} - -static inline void hugetlb_drop_vma_policy (struct vm_area_struct *vma) -{ -mpol_cond_put (vma->vm_policy); -} -#else -static inline void hugetlb_set_vma_policy (struct vm_area_struct *vma, -struct inode *inode,pgoff_t index) -{ -} - -static inline void hugetlb_drop_vma_policy (struct vm_area_struct *vma) -{ -} -#endif -/* * Mask used when checking the page offset value passed in via system * calls. This value will be converted to a loff_t which is signed.
@@ -852,8 +829,7 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,/* * Initialize a pseudo vma as this is required by the huge page -* allocation routines. If NUMA is configured, use page index -* as input to create an allocation policy. +* allocation routines. */ vma_init (&pseudo_vma, mm);vm_flags_init (&pseudo_vma,VM_HUGETLB |VM_MAYSHARE |VM_SHARED );
@@ -901,9 +877,7 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,* folios in these areas, we need to consume the reserves * to keep reservation accounting consistent. */ -hugetlb_set_vma_policy (&pseudo_vma, inode, index); folio =alloc_hugetlb_folio (&pseudo_vma, addr,0 ); -hugetlb_drop_vma_policy (&pseudo_vma);if (IS_ERR (folio)) {mutex_unlock (&hugetlb_fault_mutex_table[hash]); error =PTR_ERR (folio);
@@ -1282,18 +1256,6 @@ static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)hugetlbfs_inc_free_inodes (sbinfo);return NULL ; } - -/* -* Any time after allocation, hugetlbfs_destroy_inode can be called -* for the inode. mpol_free_shared_policy is unconditionally called -* as part of hugetlbfs_destroy_inode. So, initialize policy here -* in case of a quick call to destroy. -* -* Note that the policy is initialized even if we are creating a -* private inode. This simplifies hugetlbfs_destroy_inode. -*/ -mpol_shared_policy_init (&p->policy,NULL ); -return &p->vfs_inode; }
@@ -1305,7 +1267,6 @@ static void hugetlbfs_free_inode(struct inode *inode)static void hugetlbfs_destroy_inode (struct inode *inode) {hugetlbfs_inc_free_inodes (HUGETLBFS_SB (inode->i_sb)); -mpol_free_shared_policy (&HUGETLBFS_I (inode)->policy); }static const struct address_space_operations hugetlbfs_aops = {
include/linux/hugetlb.h +0 -2
@@ -30,7 +30,6 @@ void free_huge_folio(struct folio *folio);#ifdef CONFIG_HUGETLB_PAGE -#include <linux/mempolicy.h> #include <linux/shm.h> #include <asm/tlbflush.h>
@@ -512,7 +511,6 @@ static inline struct hugetlbfs_sb_info *HUGETLBFS_SB(struct super_block *sb) }struct hugetlbfs_inode_info { -struct shared_policy policy;struct inode vfs_inode;unsigned int seals; };
Signature · 1 line
2.35.3
Re: [PATCH 01/12] hugetlbfs: drop shared NUMA mempolicy pretence
Quoted 5 lines · Hugh Dickins
hugetlbfs_fallocate()goes through the motions of pasting a shared NUMA mempolicy onto its pseudo-vma, but how could there ever be a shared NUMA mempolicy for this file?hugetlb_vm_opshas never offered aset_policymethod, andhugetlbfs_parse_param()has never supported any mpol options for a mount-wide default policy.
Hah. I was wondering, but never cared enough to investigate. Thanks for doing this.
Re: [PATCH 01/12] hugetlbfs: drop shared NUMA mempolicy pretence
In reply to Hugh DickinsQuoted 10 lines · Hugh Dickins
hugetlbfs_fallocate()goes through the motions of pasting a shared NUMA mempolicy onto its pseudo-vma, but how could there ever be a shared NUMA mempolicy for this file?hugetlb_vm_opshas never offered aset_policymethod, andhugetlbfs_parse_param()has never supported any mpol options for a mount-wide default policy.It's just an illusion: clean it away so as not to confuse others, giving us more freedom to adjust shmem's set_policy/get_policy implementation. But
hugetlbfs_inode_infois still required, just to accommodate seals.
Quoted 7 lines
Yes, shared NUMA mempolicy support could be added to hugetlbfs, with a
set_policymethod and/or mpol mount option (Andi's first posting did include an admitted-unsatisfactoryhugetlb_set_policy()); but it seems that nobody has bothered to add that in the nineteen years since v2.6.7 made it possible, and there is at least one company that has invested enough into hugetlbfs, that I guess they have learnt well enough how to manage its NUMA, without needing shared mempolicy.
TBH i'm not sure people in general rely on shared mempolicy. The original use case for it was to modify the numa policy of non anonymous shared memory files without modifying the program (e.g. Oracle database's shared memory segments)
But I don't think that particular usage model ever got any real
traction: at leas I haven't seen any real usage of it outside my tests.
I suspect people either are fine with just process policy or modify the program, in which case it's not a big burden to modify every user, so process policy or vma based mbind policy works fine.
Maybe it would be an interesting experiment to disable it everywhere with some flag and see if anybody complains.
On the other hand it might be Hyrum'ed by now.
-Andi
Hugh Dickins
maintainerRe: [PATCH 01/12] hugetlbfs: drop shared NUMA mempolicy pretence
Quoted 19 lines · Andi Kleen
Quoted 10 lines · Hugh Dickins
hugetlbfs_fallocate()goes through the motions of pasting a shared NUMA mempolicy onto its pseudo-vma, but how could there ever be a shared NUMA mempolicy for this file?hugetlb_vm_opshas never offered aset_policymethod, andhugetlbfs_parse_param()has never supported any mpol options for a mount-wide default policy.It's just an illusion: clean it away so as not to confuse others, giving us more freedom to adjust shmem's set_policy/get_policy implementation. But
hugetlbfs_inode_infois still required, just to accommodate seals.Quoted 7 lines
Yes, shared NUMA mempolicy support could be added to hugetlbfs, with a
set_policymethod and/or mpol mount option (Andi's first posting did include an admitted-unsatisfactoryhugetlb_set_policy()); but it seems that nobody has bothered to add that in the nineteen years since v2.6.7 made it possible, and there is at least one company that has invested enough into hugetlbfs, that I guess they have learnt well enough how to manage its NUMA, without needing shared mempolicy.
Quoted 4 lines
TBH i'm not sure people in general rely on shared mempolicy. The original use case for it was to modify the numa policy of non anonymous shared memory files without modifying the program (e.g. Oracle database's shared memory segments)
Ah, "without modifying the program": that makes a lot of sense, but I had never thought of it that way - I just saw it as the right way to manage the shared object (though an outlier, since we have so many other msyscall()s which do not manage the underlying shared object in this way).
Quoted 3 lines
But I don't think that particular usage model ever got any real
traction: at leas I haven't seen any real usage of it outside my tests.
If the hugetlbfs support had actually gone in, I imagine Oracle would have managed it that way; but they seem to have survived well without.
Quoted 9 lines
I suspect people either are fine with just process policy or modify the program, in which case it's not a big burden to modify every user, so process policy or vma based mbind policy works fine.
Maybe it would be an interesting experiment to disable it everywhere with some flag and see if anybody complains.
On the other hand it might be Hyrum'ed by now.
This is interesting info, Andi, thank you for providing.
I'm torn. shmem and mempolicy (and struct vm_operations_struct) would certainly be simpler without shared mempolicy: but I frankly don't have the time and courage to experiment with deprecating it now; and it is fundamentally right that such policy should be kept with the object, not with its mappings. I've assumed for years that it has to stay.
Hugh
Hugh Dickins
maintainerPATCH 2/12 kernfs: drop shared NUMA mempolicy hooks
In reply to Hugh DickinsIt seems strange that kernfs should be an outlier with a set_policy and get_policy in its kernfs_vm_ops. Ah, it dates back to v2.6.30's commit 095160aee954 ("sysfs: fix some bin_vm_ops errors"), when I had crashed on powerpc's pci_mmap_legacy_page_range() fallback to shmem_zero_setup().
Well, that was commendably thorough, to give sysfs-bin a set_policy and get_policy, just to avoid the way it was coded resulting in EINVAL from mmap when CONFIG_NUMA; but somehow feels a bit over-the-top to me now.
It's easier to say that nobody should expect to manage a shmem object's shared NUMA mempolicy via some kernfs backdoor to that object: delete that code (and there's no longer an EINVAL from mmap in the NUMA case).
This then leaves set_policy/get_policy as implemented only by shmem - though importantly also by SysV SHM, which has to interface with shmem which implements them, and with SHM_HUGETLB which does not.
--- fs/kernfs/file.c | 49 ------------------------------------------------ 1 file changed, 49 deletions(-)
fs/kernfs/file.c +0 -49
@@ -429,60 +429,11 @@ static int kernfs_vma_access(struct vm_area_struct *vma, unsigned long addr,return ret; } -#ifdef CONFIG_NUMA -static int kernfs_vma_set_policy (struct vm_area_struct *vma, -struct mempolicy *new) -{ -struct file *file = vma->vm_file; -struct kernfs_open_file *of =kernfs_of (file); -int ret; - -if (!of->vm_ops) -return 0 ; - -if (!kernfs_get_active (of->kn)) -return -EINVAL ; - - ret =0 ; -if (of->vm_ops->set_policy) - ret = of->vm_ops->set_policy (vma, new); - -kernfs_put_active (of->kn); -return ret; -} - -static struct mempolicy *kernfs_vma_get_policy (struct vm_area_struct *vma, -unsigned long addr) -{ -struct file *file = vma->vm_file; -struct kernfs_open_file *of =kernfs_of (file); -struct mempolicy *pol; - -if (!of->vm_ops) -return vma->vm_policy; - -if (!kernfs_get_active (of->kn)) -return vma->vm_policy; - - pol = vma->vm_policy; -if (of->vm_ops->get_policy) - pol = of->vm_ops->get_policy (vma, addr); - -kernfs_put_active (of->kn); -return pol; -} - -#endif -static const struct vm_operations_struct kernfs_vm_ops = { .open = kernfs_vma_open, .fault = kernfs_vma_fault, .page_mkwrite = kernfs_vma_page_mkwrite, .access = kernfs_vma_access, -#ifdef CONFIG_NUMA - .set_policy = kernfs_vma_set_policy, - .get_policy = kernfs_vma_get_policy, -#endif };static int kernfs_fop_mmap (struct file *file,struct vm_area_struct *vma)
Signature · 1 line
2.35.3
Re: [PATCH 02/12] kernfs: drop shared NUMA mempolicy hooks
Quoted 13 lines · Hugh Dickins
It seems strange that kernfs should be an outlier with a
set_policyandget_policyin itskernfs_vm_ops. Ah, it dates back to v2.6.30's commit095160aee954("sysfs: fix somebin_vm_opserrors"), when I had crashed on powerpc'spci_mmap_legacy_page_range()fallback toshmem_zero_setup().Well, that was commendably thorough, to give sysfs-bin a
set_policyandget_policy, just to avoid the way it was coded resulting in EINVAL from mmap whenCONFIG_NUMA; but somehow feels a bit over-the-top to me now.It's easier to say that nobody should expect to manage a shmem object's shared NUMA mempolicy via some kernfs backdoor to that object: delete that code (and there's no longer an EINVAL from mmap in the NUMA case).
Quoted 5 lines
This then leaves set_policy/get_policy as implemented only by shmem - though importantly also by SysV SHM, which has to interface with shmem which implements them, and with
SHM_HUGETLBwhich does not.Signed-off-by Hugh Dickins <[email protected]>
Hugh Dickins
maintainerPATCH 3/12 mempolicy: fix migrate_pages(2) syscall return nr_failed
In reply to Hugh Dickins"man 2 migrate_pages" says "On success migrate_pages() returns the number of pages that could not be moved". Although 5.3 and 5.4 commits fixed mbind(MPOL_MF_STRICT|MPOL_MF_MOVE*) to fail with EIO when not all pages could be moved (because some could not be isolated for migration), migrate_pages(2) was left still reporting only those pages failing at the migration stage, forgetting those failing at the earlier isolation stage.
Fix that by accumulating a long nr_failed count in struct queue_pages, returned by queue_pages_range() when it's not returning an error, for adding on to the nr_failed count from migrate_pages() in mm/migrate.c. A count of pages? It's more a count of folios, but changing it to pages would entail more work (also in mm/migrate.c): does not seem justified.
queue_pages_range() itself should only return -EIO in the "strictly unmovable" case (STRICT without any MOVEs): in that case it's best to break out as soon as nr_failed gets set; but otherwise it should continue to isolate pages for MOVing even when nr_failed - as the mbind(2) manpage promises. This fixes mbind(MPOL_MF_STRICT|MPOL_MF_MOVE*) behavior left over from 5.3 and 5.4, and the recent syzbot need for vma_start_write() before mbind_range(): but both of those may be fixed by smaller patches.
There's a case when nr_failed should be incremented when it was missed:queue_folios_pte_range() and queue_folios_hugetlb() count the transient migration entries, like queue_folios_pmd() already did. And there's a case when nr_failed should not be incremented when it would have been:
in meeting later PTEs of the same large folio, which can only be isolated
once: fixed by recording the current large folio in struct queue_pages.
Clean up the affected functions, fixing or updating many comments. Bool migrate_folio_add(), without -EIO: true if adding, or if skipping shared (but its arguable folio_estimated_sharers() heuristic left unchanged). Use MPOL_MF_WRLOCK flag to queue_pages_range(), instead of bool lock_vma. Use explicit STRICT|MOVE* flags where queue_pages_test_walk() checks for skipping, instead of hiding them behind MPOL_MF_VALID.
--- mm/mempolicy.c | 322 +++++++++++++++++++++++-------------------------- 1 file changed, 149 insertions(+), 173 deletions(-)
mm/mempolicy.c +149 -173
@@ -111,7 +111,8 @@/* Internal flags */ #define MPOL_MF_DISCONTIG_OK (MPOL_MF_INTERNAL << 0)/* Skip checks for continuous vmas */ -#define MPOL_MF_INVERT (MPOL_MF_INTERNAL << 1)/* Invert check for nodemask */ +#define MPOL_MF_INVERT (MPOL_MF_INTERNAL << 1)/* Invert check for nodemask */ +#define MPOL_MF_WRLOCK (MPOL_MF_INTERNAL << 2)/* Write-lock walked vmas */ static struct kmem_cache *policy_cache;static struct kmem_cache *sn_cache;
@@ -416,9 +417,19 @@ static const struct mempolicy_operations mpol_ops[MPOL_MAX] = { }, }; -static int migrate_folio_add (struct folio *folio,struct list_head *foliolist, +static bool migrate_folio_add (struct folio *folio,struct list_head *foliolist,unsigned long flags); +static bool strictly_unmovable (unsigned long flags) +{ +/* +* STRICT without MOVE flags lets do_mbind() fail immediately with -EIO +* if any misplaced page is found. +*/ +return (flags & (MPOL_MF_STRICT |MPOL_MF_MOVE |MPOL_MF_MOVE_ALL )) == +MPOL_MF_STRICT ; +} +struct queue_pages {struct list_head *pagelist;unsigned long flags;
@@ -426,6 +437,8 @@ struct queue_pages {unsigned long start;unsigned long end;struct vm_area_struct *first; +struct folio *large;/* note last large folio on pagelist */ +long nr_failed;/* could not be isolated at this time */ };/*
@@ -443,50 +456,27 @@ static inline bool queue_folio_required(struct folio *folio,return node_isset (nid, *qp->nmask) == !(flags &MPOL_MF_INVERT ); } -/* -* queue_folios_pmd() has three possible return values: -* 0 - folios are placed on the right node or queued successfully, or -* special page is met, i.e. huge zero page. -* 1 - there is unmovable folio, and MPOL_MF_MOVE* & MPOL_MF_STRICT were -* specified. -* -EIO - is migration entry or only MPOL_MF_STRICT was specified and an -* existing folio was already on a node that does not follow the -* policy. -*/ -static int queue_folios_pmd (pmd_t *pmd,spinlock_t *ptl,unsigned long addr, +static void queue_folios_pmd (pmd_t *pmd,spinlock_t *ptl,unsigned long addr,unsigned long end,struct mm_walk *walk) -__releases (ptl) { -int ret =0 ;struct folio *folio;struct queue_pages *qp = walk->private; -unsigned long flags;if (unlikely (is_pmd_migration_entry (*pmd))) { - ret = -EIO ; - goto unlock; + qp->nr_failed++; +return ; } folio =pfn_folio (pmd_pfn (*pmd));if (is_huge_zero_page (&folio->page)) { walk->action =ACTION_CONTINUE ; - goto unlock; +return ; }if (!queue_folio_required (folio, qp)) - goto unlock; - - flags = qp->flags; -/* go to folio migration */ -if (flags & (MPOL_MF_MOVE |MPOL_MF_MOVE_ALL )) { -if (!vma_migratable (walk->vma) || -migrate_folio_add (folio, qp->pagelist, flags)) { - ret =1 ; - goto unlock; - } - }else - ret = -EIO ; -unlock: -spin_unlock (ptl); -return ret; +return ; +if (!(qp->flags & (MPOL_MF_MOVE |MPOL_MF_MOVE_ALL )) || + !vma_migratable (walk->vma) || + !migrate_folio_add (folio, qp->pagelist, qp->flags)) + qp->nr_failed++; }/*
@@ -496,8 +486,6 @@ static int queue_folios_pmd(pmd_t *pmd, spinlock_t *ptl, unsigned long addr,* queue_folios_pte_range() has three possible return values: * 0 - folios are placed on the right node or queued successfully, or * special page is met, i.e. zero page. -* 1 - there is unmovable folio, and MPOL_MF_MOVE* & MPOL_MF_STRICT were -* specified. * -EIO - only MPOL_MF_STRICT was specified and an existing folio was already * on a node that does not follow the policy. */
@@ -508,14 +496,16 @@ static int queue_folios_pte_range(pmd_t *pmd, unsigned long addr,struct folio *folio;struct queue_pages *qp = walk->private;unsigned long flags = qp->flags; -bool has_unmovable = false;pte_t *pte, *mapped_pte;pte_t ptent;spinlock_t *ptl; ptl =pmd_trans_huge_lock (pmd, vma); -if (ptl) -return queue_folios_pmd (pmd, ptl, addr, end, walk); +if (ptl) { +queue_folios_pmd (pmd, ptl, addr, end, walk); +spin_unlock (ptl); + goto out; + } mapped_pte = pte =pte_offset_map_lock (walk->mm, pmd, addr, &ptl);if (!pte) {
@@ -524,8 +514,13 @@ static int queue_folios_pte_range(pmd_t *pmd, unsigned long addr, }for (; addr != end; pte++, addr +=PAGE_SIZE ) { ptent =ptep_get (pte); -if (!pte_present (ptent)) +if (pte_none (ptent))continue ; +if (!pte_present (ptent)) { +if (is_migration_entry (pte_to_swp_entry (ptent))) + qp->nr_failed++; +continue ; + } folio =vm_normal_folio (vma, addr, ptent);if (!folio ||folio_is_zone_device (folio))continue ;
@@ -537,97 +532,82 @@ static int queue_folios_pte_range(pmd_t *pmd, unsigned long addr,continue ;if (!queue_folio_required (folio, qp))continue ; -if (flags & (MPOL_MF_MOVE |MPOL_MF_MOVE_ALL )) { -/* MPOL_MF_STRICT must be specified if we get here */ -if (!vma_migratable (vma)) { - has_unmovable = true; +if (!(flags & (MPOL_MF_MOVE |MPOL_MF_MOVE_ALL )) || + !vma_migratable (vma)) { + qp->nr_failed++; +if (strictly_unmovable (flags))break ; - } - + } +if (migrate_folio_add (folio, qp->pagelist, flags)) {/* -* Do not abort immediately since there may be -* temporary off LRU pages in the range. Still -* need migrate other LRU pages. +* A large folio can only be isolated from LRU once, +* but may be mapped by many PTEs (and Copy-On-Write may +* intersperse PTEs of other folios). This is a common +* case, so don't mistake it for failure (but of course +* there can be other cases of multi-mapped pages which +* this quick check does not help to filter out - and a +* search of the pagelist might grow to be prohibitive). */ -if (migrate_folio_add (folio, qp->pagelist, flags)) - has_unmovable = true; - }else -break ; +if (folio_test_large (folio)) + qp->large = folio; + }else if (folio != qp->large) { + qp->nr_failed++; +if (strictly_unmovable (flags)) +break ; + } }pte_unmap_unlock (mapped_pte, ptl);cond_resched (); - -if (has_unmovable) -return 1 ; - -return addr != end ? -EIO :0 ; +out: +if (qp->nr_failed &&strictly_unmovable (flags)) +return -EIO ; +return 0 ; }static int queue_folios_hugetlb (pte_t *pte,unsigned long hmask,unsigned long addr,unsigned long end,struct mm_walk *walk) { -int ret =0 ;#ifdef CONFIG_HUGETLB_PAGE struct queue_pages *qp = walk->private; -unsigned long flags = (qp->flags &MPOL_MF_VALID ); +unsigned long flags = qp->flags;struct folio *folio;spinlock_t *ptl;pte_t entry; ptl =huge_pte_lock (hstate_vma (walk->vma), walk->mm, pte); entry =huge_ptep_get (pte); -if (!pte_present (entry)) +if (!pte_present (entry)) { +if (unlikely (is_hugetlb_entry_migration (entry))) + qp->nr_failed++; goto unlock; + } folio =pfn_folio (pte_pfn (entry));if (!queue_folio_required (folio, qp)) goto unlock; - -if (flags ==MPOL_MF_STRICT ) { -/* -* STRICT alone means only detecting misplaced folio and no -* need to further check other vma. -*/ - ret = -EIO ; +if (!(flags & (MPOL_MF_MOVE |MPOL_MF_MOVE_ALL )) || + !vma_migratable (walk->vma)) { + qp->nr_failed++; goto unlock; } - -if (!vma_migratable (walk->vma)) { -/* -* Must be STRICT with MOVE*, otherwise .test_walk() have -* stopped walking current vma. -* Detecting misplaced folio but allow migrating folios which -* have been queued. -*/ - ret =1 ; - goto unlock; - } -/* -* With MPOL_MF_MOVE, we try to migrate only unshared folios. If it -* is shared it is likely not worth migrating. +* Unless MPOL_MF_MOVE_ALL, we try to avoid migrating a shared folio. +* Choosing not to migrate a shared folio is not counted as a failure. * * To check if the folio is shared, ideally we want to make sure * every page is mapped to the same process. Doing that is very -* expensive, so check the estimated mapcount of the folio instead. +* expensive, so check the estimated sharers of the folio instead. */ -if (flags & (MPOL_MF_MOVE_ALL ) || - (flags &MPOL_MF_MOVE &&folio_estimated_sharers (folio) ==1 && - !hugetlb_pmd_shared (pte))) { -if (!isolate_hugetlb (folio, qp->pagelist) && - (flags &MPOL_MF_STRICT )) -/* -* Failed to isolate folio but allow migrating pages -* which have been queued. -*/ - ret =1 ; - } +if ((flags &MPOL_MF_MOVE_ALL ) || + (folio_estimated_sharers (folio) ==1 && !hugetlb_pmd_shared (pte))) +if (!isolate_hugetlb (folio, qp->pagelist)) + qp->nr_failed++; unlock:spin_unlock (ptl); -#else -BUG (); +if (qp->nr_failed &&strictly_unmovable (flags)) +return -EIO ;#endif -return ret; +return 0 ; }#ifdef CONFIG_NUMA_BALANCING
@@ -708,8 +688,11 @@ static int queue_pages_test_walk(unsigned long start, unsigned long end,return 1 ; } -/* queue pages from current vma */ -if (flags &MPOL_MF_VALID ) +/* +* Check page nodes, and queue pages to move, in the current vma. +* But if no moving, and no strict checking, the scan can be skipped. +*/ +if (flags & (MPOL_MF_STRICT |MPOL_MF_MOVE |MPOL_MF_MOVE_ALL ))return 0 ;return 1 ; }
@@ -731,22 +714,22 @@ static const struct mm_walk_ops queue_pages_lock_vma_walk_ops = {/* * Walk through page tables and collect pages to be migrated. * -* If pages found in a given range are on a set of nodes (determined by -* @nodes and @flags,) it's isolated and queued to the pagelist which is -* passed via @private. +* If pages found in a given range are not on the required set of @nodes, +* and migration is allowed, they are isolated and queued to the pagelist +* which is passed via @private. * -* queue_pages_range() has three possible return values: -* 1 - there is unmovable page, but MPOL_MF_MOVE* & MPOL_MF_STRICT were -* specified. -* 0 - queue pages successfully or no misplaced page. -* errno - i.e. misplaced pages with MPOL_MF_STRICT specified (-EIO) or -* memory range specified by nodemask and maxnode points outside -* your accessible address space (-EFAULT) +* queue_pages_range() may return: +* 0 - all pages already on the right node, or successfully queued for moving +* (or neither strict checking nor moving requested: only range checking). +* >0 - this number of misplaced folios could not be queued for moving +* (a hugetlbfs page or a transparent huge page being counted as 1). +* -EIO - a misplaced page found, when MPOL_MF_STRICT specified without MOVEs. +* -EFAULT - a hole in the memory range, when MPOL_MF_DISCONTIG_OK unspecified. */ -static int +static long queue_pages_range (struct mm_struct *mm,unsigned long start,unsigned long end,nodemask_t *nodes,unsigned long flags, -struct list_head *pagelist,bool lock_vma) +struct list_head *pagelist) {int err;struct queue_pages qp = {
@@ -757,7 +740,7 @@ queue_pages_range(struct mm_struct *mm, unsigned long start, unsigned long end, .end = end, .first = NULL, }; -const struct mm_walk_ops *ops = lock_vma ? +const struct mm_walk_ops *ops = (flags &MPOL_MF_WRLOCK ) ? &queue_pages_lock_vma_walk_ops : &queue_pages_walk_ops; err =walk_page_range (mm, start, end, ops, &qp);
@@ -766,7 +749,7 @@ queue_pages_range(struct mm_struct *mm, unsigned long start, unsigned long end,/* whole range in hole */ err = -EFAULT ; -return err; +return err ? : qp.nr_failed; }/*
@@ -1029,16 +1012,16 @@ static long do_get_mempolicy(int *policy, nodemask_t *nmask, }#ifdef CONFIG_MIGRATION -static int migrate_folio_add (struct folio *folio,struct list_head *foliolist, +static bool migrate_folio_add (struct folio *folio,struct list_head *foliolist,unsigned long flags) {/* -* We try to migrate only unshared folios. If it is shared it -* is likely not worth migrating. +* Unless MPOL_MF_MOVE_ALL, we try to avoid migrating a shared folio. +* Choosing not to migrate a shared folio is not counted as a failure. * * To check if the folio is shared, ideally we want to make sure * every page is mapped to the same process. Doing that is very -* expensive, so check the estimated mapcount of the folio instead. +* expensive, so check the estimated sharers of the folio instead. */ if ((flags &MPOL_MF_MOVE_ALL ) ||folio_estimated_sharers (folio) ==1 ) {if (folio_isolate_lru (folio)) {
@@ -1046,32 +1029,31 @@ static int migrate_folio_add(struct folio *folio, struct list_head *foliolist,node_stat_mod_folio (folio,NR_ISOLATED_ANON +folio_is_file_lru (folio),folio_nr_pages (folio)); - }else if (flags &MPOL_MF_STRICT ) { + }else {/* * Non-movable folio may reach here. And, there may be * temporary off LRU folios or non-LRU movable folios. * Treat them as unmovable folios since they can't be -* isolated, so they can't be moved at the moment. It -* should return -EIO for this case too. +* isolated, so they can't be moved at the moment. */ -return -EIO ; +return false; } } - -return 0 ; +return true; }/* * Migrate pages from one node to a target node. * Returns error or the number of pages not migrated. */ -static int migrate_to_node (struct mm_struct *mm,int source,int dest, -int flags) +static long migrate_to_node (struct mm_struct *mm,int source,int dest, +int flags) {nodemask_t nmask;struct vm_area_struct *vma;LIST_HEAD (pagelist); -int err =0 ; +long nr_failed; +long err =0 ;struct migration_target_control mtc = { .nid = dest, .gfp_mask = GFP_HIGHUSER_MOVABLE | __GFP_THISNODE,
@@ -1080,23 +1062,27 @@ static int migrate_to_node(struct mm_struct *mm, int source, int dest,nodes_clear (nmask);node_set (source, nmask); -/* -* This does not "check" the range but isolates all pages that -* need migration. Between passing in the full user address -* space range and MPOL_MF_DISCONTIG_OK, this call can not fail. -*/ - vma =find_vma (mm,0 );VM_BUG_ON (!(flags & (MPOL_MF_MOVE |MPOL_MF_MOVE_ALL ))); -queue_pages_range (mm, vma->vm_start, mm->task_size, &nmask, - flags |MPOL_MF_DISCONTIG_OK , &pagelist, false); + vma =find_vma (mm,0 ); + +/* +* This does not migrate the range, but isolates all pages that +* need migration. Between passing in the full user address +* space range and MPOL_MF_DISCONTIG_OK, this call cannot fail, +* but passes back the count of pages which could not be isolated. +*/ + nr_failed =queue_pages_range (mm, vma->vm_start, mm->task_size, &nmask, + flags |MPOL_MF_DISCONTIG_OK , &pagelist);if (!list_empty (&pagelist)) { err =migrate_pages (&pagelist, alloc_migration_target,NULL , - (unsigned long )&mtc,MIGRATE_SYNC ,MR_SYSCALL ,NULL ); + (unsigned long )&mtc,MIGRATE_SYNC ,MR_SYSCALL ,NULL );if (err)putback_movable_pages (&pagelist); } +if (err >=0 ) + err += nr_failed;return err; }
@@ -1109,8 +1095,8 @@ static int migrate_to_node(struct mm_struct *mm, int source, int dest,int do_migrate_pages (struct mm_struct *mm,const nodemask_t *from,const nodemask_t *to,int flags) { -int busy =0 ; -int err =0 ; +long nr_failed =0 ; +long err =0 ;nodemask_t tmp;lru_cache_disable ();
@@ -1192,7 +1178,7 @@ int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,node_clear (source, tmp); err =migrate_to_node (mm, source, dest, flags);if (err >0 ) - busy += err; + nr_failed += err;if (err <0 )break ; }
@@ -1201,8 +1187,7 @@ int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,lru_cache_enable ();if (err <0 )return err; -return busy; - +return (nr_failed <INT_MAX ) ? nr_failed :INT_MAX ; }/*
@@ -1241,10 +1226,10 @@ static struct folio *new_folio(struct folio *src, unsigned long start) }#else -static int migrate_folio_add (struct folio *folio,struct list_head *foliolist, +static bool migrate_folio_add (struct folio *folio,struct list_head *foliolist,unsigned long flags) { -return -EIO ; +return false; } int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,
@@ -1268,8 +1253,8 @@ static long do_mbind(unsigned long start, unsigned long len,struct vma_iterator vmi;struct mempolicy *new;unsigned long end; -int err; -int ret; +long err; +long nr_failed;LIST_HEAD (pagelist);if (flags & ~(unsigned long )MPOL_MF_VALID )
@@ -1309,10 +1294,8 @@ static long do_mbind(unsigned long start, unsigned long len, start, start + len, mode, mode_flags, nmask ? nodes_addr(*nmask)[0] : NUMA_NO_NODE); -if (flags & (MPOL_MF_MOVE |MPOL_MF_MOVE_ALL )) { - +if (flags & (MPOL_MF_MOVE |MPOL_MF_MOVE_ALL ))lru_cache_disable (); - } {NODEMASK_SCRATCH (scratch);if (scratch) {
@@ -1328,44 +1311,37 @@ static long do_mbind(unsigned long start, unsigned long len, goto mpol_out;/* -* Lock the VMAs before scanning for pages to migrate, to ensure we don't -* miss a concurrently inserted page. +* Lock the VMAs before scanning for pages to migrate, +* to ensure we don't miss a concurrently inserted page. */ - ret =queue_pages_range (mm, start, end, nmask, - flags |MPOL_MF_INVERT , &pagelist, true); + nr_failed =queue_pages_range (mm, start, end, nmask, + flags |MPOL_MF_INVERT |MPOL_MF_WRLOCK , &pagelist); -if (ret <0 ) { - err = ret; - goto up_out; - } - -vma_iter_init (&vmi, mm, start); - prev =vma_prev (&vmi); -for_each_vma_range (vmi, vma, end) { - err =mbind_range (&vmi, vma, &prev, start, end, new); -if (err) -break ; +if (nr_failed <0 ) { + err = nr_failed; + }else { +vma_iter_init (&vmi, mm, start); + prev =vma_prev (&vmi); +for_each_vma_range (vmi, vma, end) { + err =mbind_range (&vmi, vma, &prev, start, end, new); +if (err) +break ; + } }if (!err) { -int nr_failed =0 ; -if (!list_empty (&pagelist)) {WARN_ON_ONCE (flags &MPOL_MF_LAZY ); - nr_failed =migrate_pages (&pagelist, new_folio,NULL , + nr_failed |=migrate_pages (&pagelist, new_folio,NULL , start,MIGRATE_SYNC ,MR_MEMPOLICY_MBIND ,NULL ); -if (nr_failed) -putback_movable_pages (&pagelist); } - -if ((ret >0 ) || (nr_failed && (flags &MPOL_MF_STRICT ))) +if (nr_failed && (flags &MPOL_MF_STRICT )) err = -EIO ; - }else { -up_out: -if (!list_empty (&pagelist)) -putback_movable_pages (&pagelist); } +if (!list_empty (&pagelist)) +putback_movable_pages (&pagelist); +mmap_write_unlock (mm); mpol_out:mpol_put (new);
Signature · 1 line
2.35.3
Re: [PATCH 03/12] mempolicy: fix migrate_pages(2) syscall return nr_failed
Quoted 7 lines · Hugh Dickins
"man 2
migrate_pages" says "On successmigrate_pages()returns the number of pages that could not be moved". Although 5.3 and 5.4 commits fixedmbind(MPOL_MF_STRICT|MPOL_MF_MOVE*)to fail with EIO when not all pages could be moved (because some could not be isolated for migration),migrate_pages(2)was left still reporting only those pages failing at the migration stage, forgetting those failing at the earlier isolation stage.
Quoted 5 lines
Fix that by accumulating a long
nr_failedcount in structqueue_pages, returned byqueue_pages_range()when it's not returning an error, for adding on to thenr_failedcount frommigrate_pages()inmm/migrate.c. A count of pages? It's more a count of folios, but changing it to pages would entail more work (also inmm/migrate.c): does not seem justified.
I certainly see what you're saying. If a folio is only partially mapped (in an extreme case, the VMA is PAGE_SIZE and maps one page of a 512-page folio), then setting nr_failed to folio_nr_pages() is misleading at best.
Quoted 32 lines
mm/mempolicy.c +4
-16+static void queue_folios_pmd (pmd_t *pmd,spinlock_t *ptl,unsigned long addr,unsigned long end,struct mm_walk *walk) - __releases(ptl) { - int ret = 0;struct folio *folio;struct queue_pages *qp = walk->private; -unsigned long flags;if (unlikely (is_pmd_migration_entry (*pmd))) { - ret = -EIO ; - goto unlock; + qp->nr_failed++; +return ; } folio =pfn_folio (pmd_pfn (*pmd));if (is_huge_zero_page (&folio->page)) { walk->action =ACTION_CONTINUE ; - goto unlock; +return ; }if (!queue_folio_required (folio, qp)) - goto unlock; - - flags = qp->flags; -/* go to folio migration */ -if (flags & (MPOL_MF_MOVE |MPOL_MF_MOVE_ALL )) { -if (!vma_migratable (walk->vma) || -migrate_folio_add (folio, qp->pagelist, flags)) { - ret =1 ; - goto unlock; - }
mm/mempolicy.c static inline bool queue_folio_required(struct folio *folio, · line 479
Quoted 10 lines
mm/mempolicy.c +5
-5- }else - ret = -EIO ; -unlock: -spin_unlock (ptl); -return ret; +return ; +if (!(qp->flags & (MPOL_MF_MOVE |MPOL_MF_MOVE_ALL )) || + !vma_migratable (walk->vma) || + !migrate_folio_add (folio, qp->pagelist, qp->flags)) + qp->nr_failed++;
However, I think here, we would do well to increment by HPAGE_PMD_NR. Or whatever equivalent is flavour of the week.
Bravo to the other changes.
Hugh Dickins
maintainerRe: [PATCH 03/12] mempolicy: fix migrate_pages(2) syscall return nr_failed
Quoted 14 lines · Matthew Wilcox
Quoted 7 lines · Hugh Dickins
"man 2
migrate_pages" says "On successmigrate_pages()returns the number of pages that could not be moved". Although 5.3 and 5.4 commits fixedmbind(MPOL_MF_STRICT|MPOL_MF_MOVE*)to fail with EIO when not all pages could be moved (because some could not be isolated for migration),migrate_pages(2)was left still reporting only those pages failing at the migration stage, forgetting those failing at the earlier isolation stage.Quoted 5 lines
Fix that by accumulating a long
nr_failedcount in structqueue_pages, returned byqueue_pages_range()when it's not returning an error, for adding on to thenr_failedcount frommigrate_pages()inmm/migrate.c. A count of pages? It's more a count of folios, but changing it to pages would entail more work (also inmm/migrate.c): does not seem justified.
Quoted 3 lines
I certainly see what you're saying. If a folio is only partially mapped (in an extreme case, the VMA is
PAGE_SIZEand maps one page of a 512-page folio), then settingnr_failedtofolio_nr_pages()is misleading at best.
Actually, that wasn't what I was thinking when I said that: but thank you for the comment, you've helped me to see that what I'm actually doing is not what is claimed there.
What I was thinking, something I'm taking as an axiom, is that the units of failure when isolating must match the units of failure when migrating, whatever they are.
And migrate_pages(), the internal one, has this helpfully explicit comment:
* Returns the number of {normal folio, large folio, hugetlb} that were not
* migrated, or an error code. The number of large folio splits will be
* considered as the number of non-migrated large folio, no matter how many
* split folios of the large folio are migrated successfully.
(TBH I haven't spent long enough to actually understand what the second sentence is saying: I do realize that splits complicate the issue, but the function wouldn't be expected to return a "number of large folio splits" anyway. One day, I should work out what the code is actually doing, and try to reword that sentence better.)
So above I was trying to say that migrate_pages(), the syscall, returns that quantity: totalling the failed-isolation and failed-migration folios.
But you've alerted me to how in fact I'm doing an nr_failed++ for each PTE of a failing-to-isolate folio, not as claimed. It looks like I need to record "qp->large" in the case of failure as well as success. (And then bother about when isolation fails on the first PTE, but succeeds by the time of a later PTE? maybe, or maybe that just gets silly.) I must fix that in v2.
Quoted 3 lines
Quoted 3 lines
mm/mempolicy.c +1
-0+static void queue_folios_pmd (pmd_t *pmd,spinlock_t *ptl,unsigned long addr,unsigned long end,struct mm_walk *walk)
...
Quoted 7 lines
mm/mempolicy.c static inline bool queue_folio_required(struct folio *folio, · line 479
Quoted 4 lines
mm/mempolicy.c +4
-0+if (!(qp->flags & (MPOL_MF_MOVE |MPOL_MF_MOVE_ALL )) || + !vma_migratable (walk->vma) || + !migrate_folio_add (folio, qp->pagelist, qp->flags)) + qp->nr_failed++;However, I think here, we would do well to increment by
HPAGE_PMD_NR. Or whatever equivalent is flavour of the week.
I *really* wanted to do that (and increment nr_failed PTE by PTE as I'm doing, rather than as I claimed), and gave it some thought: but I don't think it can be done - or not without abandoning the axiom (in which case it's impossible to say what migrate_pages(2) is counting), or adding a layer of complication which simply isn't justifiable.
Certainly we could change the definition of what migrate_pages(internal) returns (though I haven't researched who depends on it: IIRC-long-ago there's maybe only one other caller who cares, to update a stat); but that still would not help. Because whether migrate_pages(internal) returns 1 or HPAGE_PMD_NR for an unmigratable and unsplittable THP, it has no idea whether that THP got into the pagelist via a PMD or via one or some number more of PTEs. More info would have to be passed down separately, folio by folio: an auxiliary xarray perhaps, but let's not.
If it turns out that I'm deluded, and it can be easily done, please clarify one point: you made this comment on queue_folios_pmd(), but what about queue_folios_hugetlb()? Would you nowadays prefer hugetlb to count 1 or folio_nr_pages()? I think the latter.
Quoted 2 lines
Bravo to the other changes.
Thanks - I'm guessing your enthusiasm is mainly due to that "qp->large" realization, which we ought to have thought of before. I'm afraid it's going to get more complicated, once COWs are feeding on Ryan's ALFalfA - might need large[MAX_ORDER], or some better way. But no great hurry, nothing will crash if it's occasionally not-quite-right.
Quoted 2 lines
Reviewed-by Matthew Wilcox (Oracle) <[email protected]>
Many thanks for all these rapid and encouraging reviews.
Hugh
Re: [PATCH 03/12] mempolicy: fix migrate_pages(2) syscall return nr_failed
In reply to Hugh DickinsQuoted 90 lines · Hugh Dickins
"man 2
migrate_pages" says "On successmigrate_pages()returns the number of pages that could not be moved". Although 5.3 and 5.4 commits fixedmbind(MPOL_MF_STRICT|MPOL_MF_MOVE*)to fail with EIO when not all pages could be moved (because some could not be isolated for migration),migrate_pages(2)was left still reporting only those pages failing at the migration stage, forgetting those failing at the earlier isolation stage.Fix that by accumulating a long
nr_failedcount in structqueue_pages, returned byqueue_pages_range()when it's not returning an error, for adding on to thenr_failedcount frommigrate_pages()inmm/migrate.c. A count of pages? It's more a count of folios, but changing it to pages would entail more work (also inmm/migrate.c): does not seem justified.
queue_pages_range()itself should only return-EIOin the "strictly unmovable" case (STRICT without any MOVEs): in that case it's best to break out as soon asnr_failedgets set; but otherwise it should continue to isolate pages for MOVing even whennr_failed- as the mbind(2) manpage promises. This fixesmbind(MPOL_MF_STRICT|MPOL_MF_MOVE*)behavior left over from 5.3 and 5.4, and the recent syzbot need forvma_start_write()beforembind_range(): but both of those may be fixed by smaller patches.There's a case when
nr_failedshould be incremented when it was missed:queue_folios_pte_range()andqueue_folios_hugetlb()count the transient migration entries, likequeue_folios_pmd()already did. And there's a case whennr_failedshould not be incremented when it would have been:
in meeting later PTEs of the same large folio, which can only be isolated
once: fixed by recording the current large folio in structqueue_pages.Clean up the affected functions, fixing or updating many comments. Bool
migrate_folio_add(), without-EIO: true if adding, or if skipping shared (but its arguablefolio_estimated_sharers()heuristic left unchanged). UseMPOL_MF_WRLOCKflag toqueue_pages_range(), instead of boollock_vma. Use explicit STRICT|MOVE* flags wherequeue_pages_test_walk()checks for skipping, instead of hiding them behindMPOL_MF_VALID.Signed-off-by Hugh Dickins <[email protected]>--- mm/mempolicy.c | 322 +++++++++++++++++++++++-------------------------- 1 file changed, 149 insertions(+), 173 deletions(-)mm/mempolicy.c +15
-4@@ -111,7 +111,8 @@/* Internal flags */ #define MPOL_MF_DISCONTIG_OK (MPOL_MF_INTERNAL << 0)/* Skip checks for continuous vmas */ -#define MPOL_MF_INVERT (MPOL_MF_INTERNAL << 1)/* Invert check for nodemask */ +#define MPOL_MF_INVERT (MPOL_MF_INTERNAL << 1)/* Invert check for nodemask */ +#define MPOL_MF_WRLOCK (MPOL_MF_INTERNAL << 2)/* Write-lock walked vmas */ static struct kmem_cache *policy_cache;static struct kmem_cache *sn_cache;@@ -416,9 +417,19 @@ static const struct mempolicy_operations mpol_ops[MPOL_MAX] = { }, }; -static int migrate_folio_add (struct folio *folio,struct list_head *foliolist, +static bool migrate_folio_add (struct folio *folio,struct list_head *foliolist,unsigned long flags); +static bool strictly_unmovable (unsigned long flags) +{ +/* +* STRICT without MOVE flags lets do_mbind() fail immediately with -EIO +* if any misplaced page is found. +*/ +return (flags & (MPOL_MF_STRICT |MPOL_MF_MOVE |MPOL_MF_MOVE_ALL )) == +MPOL_MF_STRICT ; +} +struct queue_pages {struct list_head *pagelist;unsigned long flags;@@ -426,6 +437,8 @@ struct queue_pages {unsigned long start;unsigned long end;struct vm_area_struct *first; +struct folio *large;/* note last large folio on pagelist */ +long nr_failed;/* could not be isolated at this time */ };/* @@ -443,50 +456,27 @@ static inline bool queue_folio_required(struct folio *folio,return node_isset (nid, *qp->nmask) == !(flags &MPOL_MF_INVERT ); } -/* -* queue_folios_pmd() has three possible return values:
mm/mempolicy.c static inline bool queue_folio_required(struct folio *folio, · line 459
Quoted 10 lines
mm/mempolicy.c +1
-9-* 0 - folios are placed on the right node or queued successfully, or -* special page is met, i.e. huge zero page. -* 1 - there is unmovable folio, and MPOL_MF_MOVE* & MPOL_MF_STRICT were -* specified. -* -EIO - is migration entry or only MPOL_MF_STRICT was specified and an -* existing folio was already on a node that does not follow the -* policy. -*/ -static int queue_folios_pmd(pmd_t *pmd, spinlock_t *ptl, unsigned long addr, +static void queue_folios_pmd(pmd_t *pmd, spinlock_t *ptl, unsigned long addr,
I don't find that "ptl" is used in the function now. So, remove it?
Quoted 94 lines
mm/mempolicy.c +19
-29unsigned long end, struct mm_walk *walk) - __releases(ptl) { - int ret = 0; struct folio *folio;struct queue_pages *qp = walk->private; -unsigned long flags;if (unlikely (is_pmd_migration_entry (*pmd))) { - ret = -EIO ; - goto unlock; + qp->nr_failed++; +return ; } folio =pfn_folio (pmd_pfn (*pmd));if (is_huge_zero_page (&folio->page)) { walk->action =ACTION_CONTINUE ; - goto unlock; +return ; }if (!queue_folio_required (folio, qp)) - goto unlock; - - flags = qp->flags; -/* go to folio migration */ -if (flags & (MPOL_MF_MOVE |MPOL_MF_MOVE_ALL )) { -if (!vma_migratable (walk->vma) || -migrate_folio_add (folio, qp->pagelist, flags)) { - ret =1 ; - goto unlock; - } - }else - ret = -EIO ; -unlock: -spin_unlock (ptl); -return ret; +return ; +if (!(qp->flags & (MPOL_MF_MOVE |MPOL_MF_MOVE_ALL )) || + !vma_migratable (walk->vma) || + !migrate_folio_add (folio, qp->pagelist, qp->flags)) + qp->nr_failed++; }/* @@ -496,8 +486,6 @@ static int queue_folios_pmd(pmd_t *pmd, spinlock_t *ptl, unsigned long addr,* queue_folios_pte_range() has three possible return values: * 0 - folios are placed on the right node or queued successfully, or * special page is met, i.e. zero page. -* 1 - there is unmovable folio, and MPOL_MF_MOVE* & MPOL_MF_STRICT were -* specified. * -EIO - only MPOL_MF_STRICT was specified and an existing folio was already * on a node that does not follow the policy. */ @@ -508,14 +496,16 @@ static int queue_folios_pte_range(pmd_t *pmd, unsigned long addr,struct folio *folio;struct queue_pages *qp = walk->private;unsigned long flags = qp->flags; -bool has_unmovable = false;pte_t *pte, *mapped_pte;pte_t ptent;spinlock_t *ptl; ptl =pmd_trans_huge_lock (pmd, vma); -if (ptl) -return queue_folios_pmd (pmd, ptl, addr, end, walk); +if (ptl) { +queue_folios_pmd (pmd, ptl, addr, end, walk); +spin_unlock (ptl); + goto out; + } mapped_pte = pte =pte_offset_map_lock (walk->mm, pmd, addr, &ptl);if (!pte) {@@ -524,8 +514,13 @@ static int queue_folios_pte_range(pmd_t *pmd, unsigned long addr, }for (; addr != end; pte++, addr +=PAGE_SIZE ) { ptent =ptep_get (pte); -if (!pte_present (ptent)) +if (pte_none (ptent))continue ; +if (!pte_present (ptent)) { +if (is_migration_entry (pte_to_swp_entry (ptent))) + qp->nr_failed++; +continue ; + } folio =vm_normal_folio (vma, addr, ptent);if (!folio ||folio_is_zone_device (folio))continue ;@@ -537,97 +532,82 @@ static int queue_folios_pte_range(pmd_t *pmd, unsigned long addr,continue ;if (!queue_folio_required (folio, qp))continue ; -if (flags & (MPOL_MF_MOVE |MPOL_MF_MOVE_ALL )) { -/* MPOL_MF_STRICT must be specified if we get here */
mm/mempolicy.c static int queue_folios_pte_range(pmd_t *pmd, unsigned long addr, · line 540
Quoted 10 lines
mm/mempolicy.c +5
-4-if (!vma_migratable (vma)) { - has_unmovable = true; +if (!(flags & (MPOL_MF_MOVE |MPOL_MF_MOVE_ALL )) || + !vma_migratable (vma)) { + qp->nr_failed++; +if (strictly_unmovable (flags))break ; - } - + }
IIUC, even if !(flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) or !vma_migratable(vma), the folio will be isolated in migrate_folio_add() below. Is this the expected behavior?
Quoted 139 lines
mm/mempolicy.c +42
-53+if (migrate_folio_add (folio, qp->pagelist, flags)) {/* -* Do not abort immediately since there may be -* temporary off LRU pages in the range. Still -* need migrate other LRU pages. +* A large folio can only be isolated from LRU once, +* but may be mapped by many PTEs (and Copy-On-Write may +* intersperse PTEs of other folios). This is a common +* case, so don't mistake it for failure (but of course +* there can be other cases of multi-mapped pages which +* this quick check does not help to filter out - and a +* search of the pagelist might grow to be prohibitive). */ -if (migrate_folio_add (folio, qp->pagelist, flags)) - has_unmovable = true; - }else -break ; +if (folio_test_large (folio)) + qp->large = folio; + }else if (folio != qp->large) { + qp->nr_failed++; +if (strictly_unmovable (flags)) +break ; + } }pte_unmap_unlock (mapped_pte, ptl);cond_resched (); - -if (has_unmovable) -return 1 ; - -return addr != end ? -EIO :0 ; +out: +if (qp->nr_failed &&strictly_unmovable (flags)) +return -EIO ; +return 0 ; }static int queue_folios_hugetlb (pte_t *pte,unsigned long hmask,unsigned long addr,unsigned long end,struct mm_walk *walk) { -int ret =0 ;#ifdef CONFIG_HUGETLB_PAGE struct queue_pages *qp = walk->private; -unsigned long flags = (qp->flags &MPOL_MF_VALID ); +unsigned long flags = qp->flags;struct folio *folio;spinlock_t *ptl;pte_t entry; ptl =huge_pte_lock (hstate_vma (walk->vma), walk->mm, pte); entry =huge_ptep_get (pte); -if (!pte_present (entry)) +if (!pte_present (entry)) { +if (unlikely (is_hugetlb_entry_migration (entry))) + qp->nr_failed++; goto unlock; + } folio =pfn_folio (pte_pfn (entry));if (!queue_folio_required (folio, qp)) goto unlock; - -if (flags ==MPOL_MF_STRICT ) { -/* -* STRICT alone means only detecting misplaced folio and no -* need to further check other vma. -*/ - ret = -EIO ; +if (!(flags & (MPOL_MF_MOVE |MPOL_MF_MOVE_ALL )) || + !vma_migratable (walk->vma)) { + qp->nr_failed++; goto unlock; } - -if (!vma_migratable (walk->vma)) { -/* -* Must be STRICT with MOVE*, otherwise .test_walk() have -* stopped walking current vma. -* Detecting misplaced folio but allow migrating folios which -* have been queued. -*/ - ret =1 ; - goto unlock; - } -/* -* With MPOL_MF_MOVE, we try to migrate only unshared folios. If it -* is shared it is likely not worth migrating. +* Unless MPOL_MF_MOVE_ALL, we try to avoid migrating a shared folio. +* Choosing not to migrate a shared folio is not counted as a failure. * * To check if the folio is shared, ideally we want to make sure * every page is mapped to the same process. Doing that is very -* expensive, so check the estimated mapcount of the folio instead. +* expensive, so check the estimated sharers of the folio instead. */ -if (flags & (MPOL_MF_MOVE_ALL ) || - (flags &MPOL_MF_MOVE &&folio_estimated_sharers (folio) ==1 && - !hugetlb_pmd_shared (pte))) { -if (!isolate_hugetlb (folio, qp->pagelist) && - (flags &MPOL_MF_STRICT )) -/* -* Failed to isolate folio but allow migrating pages -* which have been queued. -*/ - ret =1 ; - } +if ((flags &MPOL_MF_MOVE_ALL ) || + (folio_estimated_sharers (folio) ==1 && !hugetlb_pmd_shared (pte))) +if (!isolate_hugetlb (folio, qp->pagelist)) + qp->nr_failed++; unlock:spin_unlock (ptl); -#else -BUG (); +if (qp->nr_failed &&strictly_unmovable (flags)) +return -EIO ;#endif -return ret; +return 0 ; }#ifdef CONFIG_NUMA_BALANCING @@ -708,8 +688,11 @@ static int queue_pages_test_walk(unsigned long start, unsigned long end,return 1 ; } -/* queue pages from current vma */ -if (flags &MPOL_MF_VALID ) +/* +* Check page nodes, and queue pages to move, in the current vma. +* But if no moving, and no strict checking, the scan can be skipped. +*/ +if (flags & (MPOL_MF_STRICT |MPOL_MF_MOVE |MPOL_MF_MOVE_ALL ))return 0 ;return 1 ; }
mm/mempolicy.c static const struct mm_walk_ops queue_pages_lock_vma_walk_ops = { · line 719
Quoted 9 lines
mm/mempolicy.c +3
-3@@ -731,22 +714,22 @@ static const struct mm_walk_ops queue_pages_lock_vma_walk_ops = {/* * Walk through page tables and collect pages to be migrated. * -* If pages found in a given range are on a set of nodes (determined by -* @nodes and @flags,) it's isolated and queued to the pagelist which is -* passed via @private. +* If pages found in a given range are not on the required set of @nodes, +* and migration is allowed, they are isolated and queued to the pagelist +* which is passed via @private.
s/@private/@pagelist/
Quoted 163 lines
mm/mempolicy.c +39
-36* -* queue_pages_range() has three possible return values: -* 1 - there is unmovable page, but MPOL_MF_MOVE* & MPOL_MF_STRICT were -* specified. -* 0 - queue pages successfully or no misplaced page. -* errno - i.e. misplaced pages with MPOL_MF_STRICT specified (-EIO) or -* memory range specified by nodemask and maxnode points outside -* your accessible address space (-EFAULT) +* queue_pages_range() may return: +* 0 - all pages already on the right node, or successfully queued for moving +* (or neither strict checking nor moving requested: only range checking). +* >0 - this number of misplaced folios could not be queued for moving +* (a hugetlbfs page or a transparent huge page being counted as 1). +* -EIO - a misplaced page found, when MPOL_MF_STRICT specified without MOVEs. +* -EFAULT - a hole in the memory range, when MPOL_MF_DISCONTIG_OK unspecified. */ -static int +static long queue_pages_range (struct mm_struct *mm,unsigned long start,unsigned long end,nodemask_t *nodes,unsigned long flags, -struct list_head *pagelist,bool lock_vma) +struct list_head *pagelist) {int err;struct queue_pages qp = {@@ -757,7 +740,7 @@ queue_pages_range(struct mm_struct *mm, unsigned long start, unsigned long end, .end = end, .first = NULL, }; -const struct mm_walk_ops *ops = lock_vma ? +const struct mm_walk_ops *ops = (flags &MPOL_MF_WRLOCK ) ? &queue_pages_lock_vma_walk_ops : &queue_pages_walk_ops; err =walk_page_range (mm, start, end, ops, &qp);@@ -766,7 +749,7 @@ queue_pages_range(struct mm_struct *mm, unsigned long start, unsigned long end,/* whole range in hole */ err = -EFAULT ; -return err; +return err ? : qp.nr_failed; }/* @@ -1029,16 +1012,16 @@ static long do_get_mempolicy(int *policy, nodemask_t *nmask, }#ifdef CONFIG_MIGRATION -static int migrate_folio_add (struct folio *folio,struct list_head *foliolist, +static bool migrate_folio_add (struct folio *folio,struct list_head *foliolist,unsigned long flags) {/* -* We try to migrate only unshared folios. If it is shared it -* is likely not worth migrating. +* Unless MPOL_MF_MOVE_ALL, we try to avoid migrating a shared folio. +* Choosing not to migrate a shared folio is not counted as a failure. * * To check if the folio is shared, ideally we want to make sure * every page is mapped to the same process. Doing that is very -* expensive, so check the estimated mapcount of the folio instead. +* expensive, so check the estimated sharers of the folio instead. */ if ((flags &MPOL_MF_MOVE_ALL ) ||folio_estimated_sharers (folio) ==1 ) {if (folio_isolate_lru (folio)) {@@ -1046,32 +1029,31 @@ static int migrate_folio_add(struct folio *folio, struct list_head *foliolist,node_stat_mod_folio (folio,NR_ISOLATED_ANON +folio_is_file_lru (folio),folio_nr_pages (folio)); - }else if (flags &MPOL_MF_STRICT ) { + }else {/* * Non-movable folio may reach here. And, there may be * temporary off LRU folios or non-LRU movable folios. * Treat them as unmovable folios since they can't be -* isolated, so they can't be moved at the moment. It -* should return -EIO for this case too. +* isolated, so they can't be moved at the moment. */ -return -EIO ; +return false; } } - -return 0 ; +return true; }/* * Migrate pages from one node to a target node. * Returns error or the number of pages not migrated. */ -static int migrate_to_node (struct mm_struct *mm,int source,int dest, -int flags) +static long migrate_to_node (struct mm_struct *mm,int source,int dest, +int flags) {nodemask_t nmask;struct vm_area_struct *vma;LIST_HEAD (pagelist); -int err =0 ; +long nr_failed; +long err =0 ;struct migration_target_control mtc = { .nid = dest, .gfp_mask = GFP_HIGHUSER_MOVABLE | __GFP_THISNODE,@@ -1080,23 +1062,27 @@ static int migrate_to_node(struct mm_struct *mm, int source, int dest,nodes_clear (nmask);node_set (source, nmask); -/* -* This does not "check" the range but isolates all pages that -* need migration. Between passing in the full user address -* space range and MPOL_MF_DISCONTIG_OK, this call can not fail. -*/ - vma =find_vma (mm,0 );VM_BUG_ON (!(flags & (MPOL_MF_MOVE |MPOL_MF_MOVE_ALL ))); -queue_pages_range (mm, vma->vm_start, mm->task_size, &nmask, - flags |MPOL_MF_DISCONTIG_OK , &pagelist, false); + vma =find_vma (mm,0 ); + +/* +* This does not migrate the range, but isolates all pages that +* need migration. Between passing in the full user address +* space range and MPOL_MF_DISCONTIG_OK, this call cannot fail, +* but passes back the count of pages which could not be isolated. +*/ + nr_failed =queue_pages_range (mm, vma->vm_start, mm->task_size, &nmask, + flags |MPOL_MF_DISCONTIG_OK , &pagelist);if (!list_empty (&pagelist)) { err =migrate_pages (&pagelist, alloc_migration_target,NULL , - (unsigned long )&mtc,MIGRATE_SYNC ,MR_SYSCALL ,NULL ); + (unsigned long )&mtc,MIGRATE_SYNC ,MR_SYSCALL ,NULL );if (err)putback_movable_pages (&pagelist); } +if (err >=0 ) + err += nr_failed;return err; }@@ -1109,8 +1095,8 @@ static int migrate_to_node(struct mm_struct *mm, int source, int dest,int do_migrate_pages (struct mm_struct *mm,const nodemask_t *from,const nodemask_t *to,int flags) { -int busy =0 ; -int err =0 ; +long nr_failed =0 ; +long err =0 ;nodemask_t tmp;lru_cache_disable ();@@ -1192,7 +1178,7 @@ int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,node_clear (source, tmp); err =migrate_to_node (mm, source, dest, flags);if (err >0 ) - busy += err; + nr_failed += err;if (err <0 )break ; }
mm/mempolicy.c int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from, · line 1190
Quoted 6 lines
mm/mempolicy.c +1
-2@@ -1201,8 +1187,7 @@ int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,lru_cache_enable ();if (err <0 )return err; -return busy; - +return (nr_failed <INT_MAX ) ? nr_failed :INT_MAX ;
return min_t(long, nr_failed, INT_MAX);
?
Quoted 93 lines
mm/mempolicy.c +21
-31}/* @@ -1241,10 +1226,10 @@ static struct folio *new_folio(struct folio *src, unsigned long start) }#else -static int migrate_folio_add (struct folio *folio,struct list_head *foliolist, +static bool migrate_folio_add (struct folio *folio,struct list_head *foliolist,unsigned long flags) { -return -EIO ; +return false; } int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,@@ -1268,8 +1253,8 @@ static long do_mbind(unsigned long start, unsigned long len,struct vma_iterator vmi;struct mempolicy *new;unsigned long end; -int err; -int ret; +long err; +long nr_failed;LIST_HEAD (pagelist);if (flags & ~(unsigned long )MPOL_MF_VALID )@@ -1309,10 +1294,8 @@ static long do_mbind(unsigned long start, unsigned long len, start, start + len, mode, mode_flags, nmask ? nodes_addr(*nmask)[0] : NUMA_NO_NODE); -if (flags & (MPOL_MF_MOVE |MPOL_MF_MOVE_ALL )) { - +if (flags & (MPOL_MF_MOVE |MPOL_MF_MOVE_ALL ))lru_cache_disable (); - } {NODEMASK_SCRATCH (scratch);if (scratch) {@@ -1328,44 +1311,37 @@ static long do_mbind(unsigned long start, unsigned long len, goto mpol_out;/* -* Lock the VMAs before scanning for pages to migrate, to ensure we don't -* miss a concurrently inserted page. +* Lock the VMAs before scanning for pages to migrate, +* to ensure we don't miss a concurrently inserted page. */ - ret =queue_pages_range (mm, start, end, nmask, - flags |MPOL_MF_INVERT , &pagelist, true); + nr_failed =queue_pages_range (mm, start, end, nmask, + flags |MPOL_MF_INVERT |MPOL_MF_WRLOCK , &pagelist); -if (ret <0 ) { - err = ret; - goto up_out; - } - -vma_iter_init (&vmi, mm, start); - prev =vma_prev (&vmi); -for_each_vma_range (vmi, vma, end) { - err =mbind_range (&vmi, vma, &prev, start, end, new); -if (err) -break ; +if (nr_failed <0 ) { + err = nr_failed; + }else { +vma_iter_init (&vmi, mm, start); + prev =vma_prev (&vmi); +for_each_vma_range (vmi, vma, end) { + err =mbind_range (&vmi, vma, &prev, start, end, new); +if (err) +break ; + } }if (!err) { -int nr_failed =0 ; -if (!list_empty (&pagelist)) {WARN_ON_ONCE (flags &MPOL_MF_LAZY ); - nr_failed =migrate_pages (&pagelist, new_folio,NULL , + nr_failed |=migrate_pages (&pagelist, new_folio,NULL , start,MIGRATE_SYNC ,MR_MEMPOLICY_MBIND ,NULL ); -if (nr_failed) -putback_movable_pages (&pagelist); } - -if ((ret >0 ) || (nr_failed && (flags &MPOL_MF_STRICT ))) +if (nr_failed && (flags &MPOL_MF_STRICT )) err = -EIO ; - }else { -up_out:
mm/mempolicy.c static long do_mbind(unsigned long start, unsigned long len, · line 1347
Quoted 10 lines
mm/mempolicy.c +3
-2-if (!list_empty (&pagelist)) -putback_movable_pages (&pagelist); } +if (!list_empty (&pagelist)) +putback_movable_pages (&pagelist); +mmap_write_unlock (mm); mpol_out:mpol_put (new);
--
Best Regards,
Huang, Ying
Hugh Dickins
maintainerRe: [PATCH 03/12] mempolicy: fix migrate_pages(2) syscall return nr_failed
Quoted 5 lines · Ying
mm/mempolicy.c static inline bool queue_folio_required(struct folio *folio, · line 459
Quoted 2 lines · Hugh Dickins
mm/mempolicy.c +1
-1-static int queue_folios_pmd(pmd_t *pmd, spinlock_t *ptl, unsigned long addr, +static void queue_folios_pmd(pmd_t *pmd, spinlock_t *ptl, unsigned long addr,I don't find that "ptl" is used in the function now. So, remove it?
Yes indeed, thanks for spotting that, I shall remove it. And I think addr, end are also unused: I'll remove them too.
...
Quoted 4 lines
Quoted 4 lines
mm/mempolicy.c +4
-0+if (!(flags & (MPOL_MF_MOVE |MPOL_MF_MOVE_ALL )) || + !vma_migratable (vma)) { + qp->nr_failed++; +if (strictly_unmovable (flags))
Quoted 8 lines
mm/mempolicy.c static int queue_folios_pte_range(pmd_t *pmd, unsigned long addr, · line 540
Quoted 4 lines
mm/mempolicy.c +1
-2break ; - } - + }IIUC, even if !(flags & (
MPOL_MF_MOVE|MPOL_MF_MOVE_ALL)) or !vma_migratable(vma), the folio will be isolated inmigrate_folio_add()below. Is this the expected behavior?
Yikes, I think you're right, thanks a lot: it was okay up until I realized the "qp->large" issue, and had to separate out the migrate_folio_add() failure case. Late changes...
Quoted 2 lines
Quoted 2 lines
mm/mempolicy.c +1
-0+if (migrate_folio_add (folio, qp->pagelist, flags)) {
...
Quoted 4 lines
Quoted 4 lines
mm/mempolicy.c +0
-0@@ -731,22 +714,22 @@ static const struct mm_walk_ops queue_pages_lock_vma_walk_ops = {/* * Walk through page tables and collect pages to be migrated. *
Quoted 8 lines
mm/mempolicy.c static const struct mm_walk_ops queue_pages_lock_vma_walk_ops = { · line 719
Quoted 6 lines
mm/mempolicy.c +3
-3-* If pages found in a given range are on a set of nodes (determined by -* @nodes and @flags,) it's isolated and queued to the pagelist which is -* passed via @private. +* If pages found in a given range are not on the required set of @nodes, +* and migration is allowed, they are isolated and queued to the pagelist +* which is passed via @private. s/@private/@pagelist/
Right: or even better, simply "queued to @pagelist."
...
Quoted 10 lines
mm/mempolicy.c int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from, · line 1190
Quoted 7 lines
mm/mempolicy.c +1
-2@@ -1201,8 +1187,7 @@ int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,lru_cache_enable ();if (err <0 )return err; -return busy; - +return (nr_failed <INT_MAX ) ? nr_failed :INT_MAX ;return min_t(long, nr_failed, INT_MAX);?
I may be the odd man out, but I read it more easily how it is:
so will stick with that, unless others object.
Very helpful comments: thank you,
Hugh
Hugh Dickins
maintainerPATCH 4/12 mempolicy trivia: delete those ancient pr_debug()s
In reply to Hugh DickinsDelete those ancient pr_debug()s - PDprintk()s in Andi Kleen's original submission of core NUMA API, and useful when debugging shared mempolicy lifetime back then, but not used recently.
--- mm/mempolicy.c | 21 --------------------- 1 file changed, 21 deletions(-)
mm/mempolicy.c +0 -21
@@ -264,9 +264,6 @@ static struct mempolicy *mpol_new(unsigned short mode, unsigned short flags, {struct mempolicy *policy; -pr_debug ("setting mode %d flags %d nodes[0] %lx\n" , - mode, flags, nodes ?nodes_addr (*nodes)[0 ] :NUMA_NO_NODE ); -if (mode ==MPOL_DEFAULT ) {if (nodes && !nodes_empty (*nodes))return ERR_PTR (-EINVAL );
@@ -765,11 +762,6 @@ static int vma_replace_policy(struct vm_area_struct *vma,vma_assert_write_locked (vma); -pr_debug ("vma %lx-%lx/%lx vm_ops %p vm_file %p set_policy %p\n" , - vma->vm_start, vma->vm_end, vma->vm_pgoff, - vma->vm_ops, vma->vm_file, - vma->vm_ops ? vma->vm_ops->set_policy :NULL ); - new =mpol_dup (pol);if (IS_ERR (new))return PTR_ERR (new);
@@ -1290,10 +1282,6 @@ static long do_mbind(unsigned long start, unsigned long len,if (!new) flags |=MPOL_MF_DISCONTIG_OK ; -pr_debug ("mbind %lx-%lx mode:%d flags:%d nodes:%lx\n" , - start, start + len, mode, mode_flags, - nmask ?nodes_addr (*nmask)[0 ] :NUMA_NO_NODE ); -if (flags & (MPOL_MF_MOVE |MPOL_MF_MOVE_ALL ))lru_cache_disable (); {
@@ -2511,8 +2499,6 @@ static void sp_insert(struct shared_policy *sp, struct sp_node *new) }rb_link_node (&new->nd, parent, p);rb_insert_color (&new->nd, &sp->root); -pr_debug ("inserting %lx-%lx: %d\n" , new->start, new->end, - new->policy ? new->policy->mode :0 ); }/* Find shared policy intersecting idx */
@@ -2649,7 +2635,6 @@ void mpol_put_task_policy(struct task_struct *task)static void sp_delete (struct shared_policy *sp,struct sp_node *n) { -pr_debug ("deleting %lx-l%lx\n" , n->start, n->end);rb_erase (&n->nd, &sp->root);sp_free (n); }
@@ -2806,12 +2791,6 @@ int mpol_set_shared_policy(struct shared_policy *info,struct sp_node *new =NULL ;unsigned long sz =vma_pages (vma); -pr_debug ("set_shared_policy %lx sz %lu %d %d %lx\n" , - vma->vm_pgoff, - sz, npol ? npol->mode :-1 , - npol ? npol->flags :-1 , - npol ?nodes_addr (npol->nodes)[0 ] :NUMA_NO_NODE ); -if (npol) { new =sp_alloc (vma->vm_pgoff, vma->vm_pgoff + sz, npol);if (!new)
Signature · 1 line
2.35.3
Re: [PATCH 04/12] mempolicy trivia: delete those ancient pr_debug()s
Quoted 3 lines · Hugh Dickins
Delete those ancient pr_debug()s - PDprintk()s in Andi Kleen's original submission of core NUMA API, and useful when debugging shared mempolicy lifetime back then, but not used recently.
Hugh Dickins
maintainerPATCH 5/12 mempolicy trivia: slightly more consistent naming
In reply to Hugh DickinsBefore getting down to work, do a little cleanup, mainly of inconsistent variable naming. I gave up trying to rationalize mpol versus pol versus policy, and node versus nid, but let's avoid p and nd. Remove a few superfluous blank lines, but add one; and here prefer vma->vm_policy to vma_policy(vma) - the latter being appropriate in other sources, which have to allow for !CONFIG_NUMA. That intriguing line about KERNEL_DS? should have gone in v2.6.15, when numa_policy_init() stopped using set_mempolicy(2)'s system call handler.
--- include/linux/mempolicy.h | 11 +++--- mm/mempolicy.c | 73 ++++++++++++++++++--------------------- 2 files changed, 38 insertions(+), 46 deletions(-)
include/linux/mempolicy.h +5 -6
@@ -126,10 +126,9 @@ struct shared_policy {int vma_dup_policy (struct vm_area_struct *src,struct vm_area_struct *dst);void mpol_shared_policy_init (struct shared_policy *sp,struct mempolicy *mpol); -int mpol_set_shared_policy (struct shared_policy *info, -struct vm_area_struct *vma, -struct mempolicy *new); -void mpol_free_shared_policy (struct shared_policy *p); +int mpol_set_shared_policy (struct shared_policy *sp, +struct vm_area_struct *vma,struct mempolicy *mpol); +void mpol_free_shared_policy (struct shared_policy *sp);struct mempolicy *mpol_shared_policy_lookup (struct shared_policy *sp,unsigned long idx);
@@ -193,7 +192,7 @@ static inline bool mpol_equal(struct mempolicy *a, struct mempolicy *b)return true; } -static inline void mpol_put (struct mempolicy *p) +static inline void mpol_put (struct mempolicy *pol) { }
@@ -212,7 +211,7 @@ static inline void mpol_shared_policy_init(struct shared_policy *sp, { } -static inline void mpol_free_shared_policy (struct shared_policy *p) +static inline void mpol_free_shared_policy (struct shared_policy *sp) { }
mm/mempolicy.c +33 -40
221 lines not on this page · raw message
Signature · 1 line
2.35.3
Re: [PATCH 05/12] mempolicy trivia: slightly more consistent naming
Quoted 8 lines · Hugh Dickins
Before getting down to work, do a little cleanup, mainly of inconsistent variable naming. I gave up trying to rationalize mpol versus pol versus policy, and node versus nid, but let's avoid p and nd. Remove a few superfluous blank lines, but add one; and here prefer
vma->vm_policytovma_policy(vma)- the latter being appropriate in other sources, which have to allow for !CONFIG_NUMA. That intriguing line aboutKERNEL_DS? should have gone in v2.6.15, whennuma_policy_init()stopped using set_mempolicy(2)'s system call handler.
Hugh Dickins
maintainerPATCH 6/12 mempolicy trivia: use pgoff_t in shared mempolicy tree
In reply to Hugh DickinsPrefer the more explicit "pgoff_t" to "unsigned long" when dealing with a shared mempolicy tree. Delete confusing comment about pseudo mm vmas.
--- include/linux/mempolicy.h | 12 +++--------- mm/mempolicy.c | 8 ++++---- 2 files changed, 7 insertions(+), 13 deletions(-)
include/linux/mempolicy.h +3 -9
@@ -107,18 +107,12 @@ static inline bool mpol_equal(struct mempolicy *a, struct mempolicy *b)/* * Tree of shared policies for a shared memory region. -* Maintain the policies in a pseudo mm that contains vmas. The vmas -* carry the policy. As a special twist the pseudo mm is indexed in pages, not -* bytes, so that we can work with shared memory segments bigger than -* unsigned long. */ -struct sp_node {struct rb_node nd; -unsigned long start, end; +pgoff_t start, end;struct mempolicy *policy; }; -struct shared_policy {struct rb_root root;rwlock_t lock;
@@ -130,7 +124,7 @@ int mpol_set_shared_policy(struct shared_policy *sp, struct vm_area_struct *vma, struct mempolicy *mpol);void mpol_free_shared_policy (struct shared_policy *sp);struct mempolicy *mpol_shared_policy_lookup (struct shared_policy *sp, -unsigned long idx); +pgoff_t idx);struct mempolicy *get_task_policy (struct task_struct *p); struct mempolicy *__get_vma_policy(struct vm_area_struct *vma,
@@ -216,7 +210,7 @@ static inline void mpol_free_shared_policy(struct shared_policy *sp) }static inline struct mempolicy * -mpol_shared_policy_lookup (struct shared_policy *sp,unsigned long idx) +mpol_shared_policy_lookup (struct shared_policy *sp,pgoff_t idx) {return NULL ; }
mm/mempolicy.c +4 -4
@@ -2444,7 +2444,7 @@ bool __mpol_equal(struct mempolicy *a, struct mempolicy *b)* reading or for writing */ static struct sp_node * -sp_lookup (struct shared_policy *sp,unsigned long start,unsigned long end) +sp_lookup (struct shared_policy *sp,pgoff_t start,pgoff_t end) {struct rb_node *n = sp->root.rb_node;
@@ -2499,7 +2499,7 @@ static void sp_insert(struct shared_policy *sp, struct sp_node *new)/* Find shared policy intersecting idx */ struct mempolicy * -mpol_shared_policy_lookup (struct shared_policy *sp,unsigned long idx) +mpol_shared_policy_lookup (struct shared_policy *sp,pgoff_t idx) {struct mempolicy *pol =NULL ;struct sp_node *sn;
@@ -2665,8 +2665,8 @@ static struct sp_node *sp_alloc(unsigned long start, unsigned long end, }/* Replace a policy range. */ -static int shared_policy_replace (struct shared_policy *sp,unsigned long start, -unsigned long end,struct sp_node *new) +static int shared_policy_replace (struct shared_policy *sp,pgoff_t start, +pgoff_t end,struct sp_node *new) {struct sp_node *n;struct sp_node *n_new =NULL ;
Signature · 1 line
2.35.3
Re: [PATCH 06/12] mempolicy trivia: use pgoff_t in shared mempolicy tree
Quoted 2 lines · Hugh Dickins
Prefer the more explicit "
pgoff_t" to "unsigned long" when dealing with a shared mempolicy tree. Delete confusing comment about pseudo mm vmas.
Yes, with three quibbles
include/linux/mempolicy.h static inline bool mpol_equal(struct mempolicy *a, struct mempolicy *b) · line 116
Quoted 8 lines
struct
sp_node{include/linux/mempolicy.h +1
-2struct rb_node nd; -unsigned long start, end; +pgoff_t start, end;struct mempolicy *policy; }; -struct shared_policy {
Did you intend to delete the blank line between these two structs? That's not our normal style.
mm/mempolicy.c bool __mpol_equal(struct mempolicy *a, struct mempolicy *b) · line 2447
Quoted 7 lines
+++
b/mm/mempolicy.cmm/mempolicy.c +1
-1@@ -2444,7 +2444,7 @@ bool __mpol_equal(struct mempolicy *a, struct mempolicy *b)* reading or for writing */ static struct sp_node * -sp_lookup (struct shared_policy *sp,unsigned long start,unsigned long end) +sp_lookup (struct shared_policy *sp,pgoff_t start,pgoff_t end)
While you're reformatting anyway, mind joining these two lines?
mm/mempolicy.c static void sp_insert(struct shared_policy *sp, struct sp_node *new) · line 2502
Quoted 6 lines
mm/mempolicy.c +1
-1@@ -2499,7 +2499,7 @@ static void sp_insert(struct shared_policy *sp, struct sp_node *new)/* Find shared policy intersecting idx */ struct mempolicy * -mpol_shared_policy_lookup (struct shared_policy *sp,unsigned long idx) +mpol_shared_policy_lookup (struct shared_policy *sp,pgoff_t idx)
Ditto
Re: [PATCH 06/12] mempolicy trivia: use pgoff_t in shared mempolicy tree
Quoted 5 lines · Matthew Wilcox
Quoted 2 lines · Hugh Dickins
Prefer the more explicit "
pgoff_t" to "unsigned long" when dealing with a shared mempolicy tree. Delete confusing comment about pseudo mm vmas.Yes, with three quibbles
Actually, a fourth has occurred to me
Quoted 6 lines
include/linux/mempolicy.h static inline bool mpol_equal(struct mempolicy *a, struct mempolicy *b) · line 115
Quoted 6 lines
struct
sp_node{include/linux/mempolicy.h +1
-1struct rb_node nd; -unsigned long start, end; +pgoff_t start, end;struct mempolicy *policy; };
This data structure is unused outside mempolicy.c today, and you don't add any. Perhaps we could move it from mempolicy.h to mempolicy.c?
Hugh Dickins
maintainerRe: [PATCH 06/12] mempolicy trivia: use pgoff_t in shared mempolicy tree
In reply to Matthew WilcoxQuoted 6 lines · Matthew Wilcox
Quoted 2 lines · Hugh Dickins
Prefer the more explicit "
pgoff_t" to "unsigned long" when dealing with a shared mempolicy tree. Delete confusing comment about pseudo mm vmas.Yes, with three quibbles
Quoted 11 lines
include/linux/mempolicy.h static inline bool mpol_equal(struct mempolicy *a, struct mempolicy *b) · line 116
Quoted 8 lines
struct
sp_node{include/linux/mempolicy.h +1
-2struct rb_node nd; -unsigned long start, end; +pgoff_t start, end;struct mempolicy *policy; }; -struct shared_policy {Did you intend to delete the blank line between these two structs? That's not our normal style.
I think I did intend it actually, to join both of those structs to the "Tree of shared policies" comment above them. But now that I'm looking again, I think what I'd most like to do (and going against all of your suggestions e.g. move sp_node to mempolicy.c: good observation, but isn't keeping them together more helpful to the reader?) is swap those structs around - struct shared_policy first for the root of the tree, then struct sp_node showing the nodes of the tree (and still without blank line). Wouldn't that be the most helpful way to present them?
I'll knuckle down and do exactly as you have suggested, if you say so:
but above is my own preference.
Quoted 11 lines
mm/mempolicy.c bool __mpol_equal(struct mempolicy *a, struct mempolicy *b) · line 2447
Quoted 8 lines
+++
b/mm/mempolicy.cmm/mempolicy.c +1
-1@@ -2444,7 +2444,7 @@ bool __mpol_equal(struct mempolicy *a, struct mempolicy *b)* reading or for writing */ static struct sp_node * -sp_lookup (struct shared_policy *sp,unsigned long start,unsigned long end) +sp_lookup (struct shared_policy *sp,pgoff_t start,pgoff_t end)While you're reformatting anyway, mind joining these two lines?
Quoted 8 lines
mm/mempolicy.c static void sp_insert(struct shared_policy *sp, struct sp_node *new) · line 2502
Quoted 6 lines
mm/mempolicy.c +1
-1@@ -2499,7 +2499,7 @@ static void sp_insert(struct shared_policy *sp, struct sp_node *new)/* Find shared policy intersecting idx */ struct mempolicy * -mpol_shared_policy_lookup (struct shared_policy *sp,unsigned long idx) +mpol_shared_policy_lookup (struct shared_policy *sp,pgoff_t idx)Ditto
Sure, will do (I recall Linus much prefers them as you suggest). But we have different indentation habits: I think it's spaced exactitude which irritates you the most, would the style below be okay? Or maybe you'd prefer to go over-80 with these two.
struct mempolicy *mpol_shared_policy_lookup(struct shared_policy *sp,
pgoff_t idx)
Hugh
Hugh Dickins
maintainerPATCH 7/12 mempolicy: mpol_shared_policy_init() without pseudo-vma
In reply to Hugh Dickinsmpol_shared_policy_init() does not need to use a pseudo-vma: it can use sp_alloc() and sp_insert() directly, since the object's shared policy tree is empty and inaccessible (needing no lock) at get_inode() time.
--- mm/mempolicy.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
mm/mempolicy.c +5 -6
@@ -2749,7 +2749,7 @@ void mpol_shared_policy_init(struct shared_policy *sp, struct mempolicy *mpol)rwlock_init (&sp->lock);if (mpol) { -struct vm_area_struct pvma; +struct sp_node *n;struct mempolicy *new;NODEMASK_SCRATCH (scratch);
@@ -2766,11 +2766,10 @@ void mpol_shared_policy_init(struct shared_policy *sp, struct mempolicy *mpol)if (ret) goto put_new; -/* Create pseudo-vma that contains just the policy */ -vma_init (&pvma,NULL ); - pvma.vm_end =TASK_SIZE ;/* policy covers entire file */ -mpol_set_shared_policy (sp, &pvma, new);/* adds ref */ - +/* alloc node covering entire file; adds ref to new */ + n =sp_alloc (0 ,MAX_LFS_FILESIZE >>PAGE_SHIFT , new); +if (n) +sp_insert (sp, n); put_new:mpol_put (new);/* drop initial ref */ free_scratch:
Signature · 1 line
2.35.3
Re: [PATCH 07/12] mempolicy: mpol_shared_policy_init() without pseudo-vma
mm/mempolicy.c void mpol_shared_policy_init(struct shared_policy *sp, struct mempolicy *mpol) · line 2769
Quoted 1 line · Hugh Dickins
mm/mempolicy.c +1
-0+/* alloc node covering entire file; adds ref to new */
This comment is confusing. sp_alloc initialises the refcount of 'n' to 1. Which is the same memory referred to by the name 'new' in __mpol_dup(), but in this function, the name "new" refers to the mempolicy called "old" in __mpol_dup().
mm/mempolicy.c void mpol_shared_policy_init(struct shared_policy *sp, struct mempolicy *mpol) · line 2775
Quoted 6 lines
mm/mempolicy.c +3
-0+ n =sp_alloc (0 ,MAX_LFS_FILESIZE >>PAGE_SHIFT , new); +if (n) +sp_insert (sp, n); put_new:mpol_put (new);/* drop initial ref */ free_scratch:
This is all a bit inefficient, really. We call mpol_new() to get a new mpol, then we set it up, then we dup it, then we free it. It'd be nice if we could donate it instead of copying it. Maybe you'll do something like that later.
Hugh Dickins
maintainerRe: [PATCH 07/12] mempolicy: mpol_shared_policy_init() without pseudo-vma
Quoted 7 lines · Matthew Wilcox
mm/mempolicy.c void mpol_shared_policy_init(struct shared_policy *sp, struct mempolicy *mpol) · line 2769
Quoted 1 line · Hugh Dickins
mm/mempolicy.c +1
-0+/* alloc node covering entire file; adds ref to new */ This comment is confusing.
sp_allocinitialises the refcount of 'n' to 1. Which is the same memory referred to by the name 'new' in__mpol_dup(), but in this function, the name "new" refers to the mempolicy called "old" in__mpol_dup().
No promises, but I'll see if I can make it look better in v2.
Quoted 12 lines
mm/mempolicy.c void mpol_shared_policy_init(struct shared_policy *sp, struct mempolicy *mpol) · line 2775
Quoted 7 lines
mm/mempolicy.c +3
-0+ n =sp_alloc (0 ,MAX_LFS_FILESIZE >>PAGE_SHIFT , new); +if (n) +sp_insert (sp, n); put_new:mpol_put (new);/* drop initial ref */ free_scratch:This is all a bit inefficient, really. We call
mpol_new()to get a new mpol, then we set it up, then we dup it, then we free it. It'd be nice if we could donate it instead of copying it. Maybe you'll do something like that later.
"later" is probably the operative word. I do have an unincluded 2017 patch where I had that same realization, and wrote "I suspect that this series of commits may be adding to an absurdity of over-mpol_dup()ing:
but that's for some other future cleanup, right now I'm just happy not to be corrupting or leaking mpols."
Hugh
Hugh Dickins
maintainerPATCH 8/12 mempolicy: remove confusing MPOL_MF_LAZY dead code
In reply to Hugh Dickinsv3.8 commit b24f53a0bea3 ("mm: mempolicy: Add MPOL_MF_LAZY") introduced MPOL_MF_LAZY, and included it in the MPOL_MF_VALID flags; but a720094ded8 ("mm: mempolicy: Hide MPOL_NOOP and MPOL_MF_LAZY from userspace for now") immediately removed it from MPOL_MF_VALID flags, pending further review. "This will need to be revisited", but it has not been reinstated.
The present state is confusing: there is dead code in mm/mempolicy.c to handle MPOL_MF_LAZY cases which can never occur. Remove that: it can be resurrected later if necessary. But keep the definition of MPOL_MF_LAZY, which must remain in the UAPI, even though it always fails with EINVAL.
https://lore.kernel.org/linux-mm/[email protected]/
links to a previous request to remove MPOL_MF_LAZY.
--- include/uapi/linux/mempolicy.h | 2 +- mm/mempolicy.c | 18 ------------------ 2 files changed, 1 insertion(+), 19 deletions(-)
include/uapi/linux/mempolicy.h +1 -1
@@ -48,7 +48,7 @@ enum {#define MPOL_MF_MOVE (1<<1)/* Move pages owned by this process to conform to policy */ #define MPOL_MF_MOVE_ALL (1<<2)/* Move every page to conform to policy */ -#define MPOL_MF_LAZY (1<<3)/* Modifies '_MOVE: lazy migrate on fault */ +#define MPOL_MF_LAZY (1<<3)/* UNSUPPORTED FLAG: Lazy migrate on fault */ #define MPOL_MF_INTERNAL (1<<4)/* Internal flags start here */ #define MPOL_MF_VALID (MPOL_MF_STRICT | \
mm/mempolicy.c +0 -18
@@ -632,12 +632,6 @@ unsigned long change_prot_numa(struct vm_area_struct *vma,return nr_updated; } -#else -static unsigned long change_prot_numa (struct vm_area_struct *vma, -unsigned long addr,unsigned long end) -{ -return 0 ; -}#endif /* CONFIG_NUMA_BALANCING */ static int queue_pages_test_walk(unsigned long start, unsigned long end,
@@ -676,14 +670,6 @@ static int queue_pages_test_walk(unsigned long start, unsigned long end,if (endvma > end) endvma = end; -if (flags &MPOL_MF_LAZY ) { -/* Similar to task_numa_work, skip inaccessible VMAs */ -if (!is_vm_hugetlb_page (vma) &&vma_is_accessible (vma) && - !(vma->vm_flags &VM_MIXEDMAP )) -change_prot_numa (vma, start, endvma); -return 1 ; - } -/* * Check page nodes, and queue pages to move, in the current vma. * But if no moving, and no strict checking, the scan can be skipped.
@@ -1271,9 +1257,6 @@ static long do_mbind(unsigned long start, unsigned long len,if (IS_ERR (new))return PTR_ERR (new); -if (flags &MPOL_MF_LAZY ) - new->flags |=MPOL_F_MOF ; -/* * If we are using the default policy then operation * on discontinuous address spaces is okay after all
@@ -1318,7 +1301,6 @@ static long do_mbind(unsigned long start, unsigned long len,if (!err) {if (!list_empty (&pagelist)) { -WARN_ON_ONCE (flags &MPOL_MF_LAZY ); nr_failed |=migrate_pages (&pagelist, new_folio,NULL , start,MIGRATE_SYNC ,MR_MEMPOLICY_MBIND ,NULL ); }
Signature · 1 line
2.35.3
Re: [PATCH 08/12] mempolicy: remove confusing MPOL_MF_LAZY dead code
Quoted 11 lines · Hugh Dickins
v3.8 commit
b24f53a0bea3("mm: mempolicy: AddMPOL_MF_LAZY") introducedMPOL_MF_LAZY, and included it in theMPOL_MF_VALIDflags; buta720094ded8("mm: mempolicy: HideMPOL_NOOPandMPOL_MF_LAZYfrom userspace for now") immediately removed it fromMPOL_MF_VALIDflags, pending further review. "This will need to be revisited", but it has not been reinstated.The present state is confusing: there is dead code in
mm/mempolicy.cto handleMPOL_MF_LAZYcases which can never occur. Remove that: it can be resurrected later if necessary. But keep the definition ofMPOL_MF_LAZY, which must remain in the UAPI, even though it always fails with EINVAL.
Quoted 4 lines
https://lore.kernel.org/linux-mm/[email protected]/
links to a previous request to removeMPOL_MF_LAZY.Signed-off-by Hugh Dickins <[email protected]>
Hugh Dickins
maintainerPATCH 9/12 mm: add page_rmappable_folio() wrapper
In reply to Hugh Dickinsfolio_prep_large_rmappable() is being used repeatedly along with a conversion from page to folio, a check non-NULL, a check order > 1:
wrap it all up into struct folio *page_rmappable_folio(struct page *).
--- include/linux/huge_mm.h | 13 +++++++++++++ mm/mempolicy.c | 17 +++-------------- mm/page_alloc.c | 8 ++------ 3 files changed, 18 insertions(+), 20 deletions(-)
include/linux/huge_mm.h +13 -0
@@ -141,6 +141,15 @@ unsigned long thp_get_unmapped_area(struct file *filp, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags);void folio_prep_large_rmappable (struct folio *folio); +static inline struct folio *page_rmappable_folio (struct page *page) +{ +struct folio *folio = (struct folio *)page; + +if (folio &&folio_order (folio) >1 ) +folio_prep_large_rmappable (folio); +return folio; +} +bool can_split_folio (struct folio *folio,int *pextra_pins);int split_huge_page_to_list (struct page *page,struct list_head *list);static inline int split_huge_page (struct page *page)
@@ -281,6 +290,10 @@ static inline bool hugepage_vma_check(struct vm_area_struct *vma, }static inline void folio_prep_large_rmappable (struct folio *folio) {} +static inline struct folio *page_rmappable_folio (struct page *page) +{ +return (struct folio *)page; +}#define transparent_hugepage_flags 0UL
mm/mempolicy.c +3 -14
@@ -2137,10 +2137,7 @@ struct folio *vma_alloc_folio(gfp_t gfp, int order, struct vm_area_struct *vma,mpol_cond_put (pol); gfp |= __GFP_COMP; page =alloc_page_interleave (gfp, order, nid); - folio = (struct folio *)page; -if (folio && order >1 ) -folio_prep_large_rmappable (folio); - goto out; +return page_rmappable_folio (page); }if (pol->mode ==MPOL_PREFERRED_MANY ) {
@@ -2150,10 +2147,7 @@ struct folio *vma_alloc_folio(gfp_t gfp, int order, struct vm_area_struct *vma, gfp |= __GFP_COMP; page =alloc_pages_preferred_many (gfp, order, node, pol);mpol_cond_put (pol); - folio = (struct folio *)page; -if (folio && order >1 ) -folio_prep_large_rmappable (folio); - goto out; +return page_rmappable_folio (page); }if (unlikely (IS_ENABLED (CONFIG_TRANSPARENT_HUGEPAGE ) && hugepage)) {
@@ -2247,12 +2241,7 @@ EXPORT_SYMBOL(alloc_pages);struct folio *folio_alloc (gfp_t gfp,unsigned order) { -struct page *page =alloc_pages (gfp | __GFP_COMP, order); -struct folio *folio = (struct folio *)page; - -if (folio && order >1 ) -folio_prep_large_rmappable (folio); -return folio; +return page_rmappable_folio (alloc_pages (gfp | __GFP_COMP, order)); }EXPORT_SYMBOL (folio_alloc);
mm/page_alloc.c +2 -6
@@ -4456,12 +4456,8 @@ struct folio *__folio_alloc(gfp_t gfp, unsigned int order, int preferred_nid, nodemask_t *nodemask) {struct page *page =__alloc_pages (gfp | __GFP_COMP, order, - preferred_nid, nodemask); -struct folio *folio = (struct folio *)page; - -if (folio && order >1 ) -folio_prep_large_rmappable (folio); -return folio; + preferred_nid, nodemask); +return page_rmappable_folio (page); }EXPORT_SYMBOL (__folio_alloc);
Signature · 1 line
2.35.3
Re: [PATCH 09/12] mm: add page_rmappable_folio() wrapper
mm/page_alloc.c struct folio *__folio_alloc(gfp_t gfp, unsigned int order, int preferred_nid, · line 4459
Quoted 4 lines · Hugh Dickins
{
mm/page_alloc.c +1
-1struct page *page =__alloc_pages (gfp | __GFP_COMP, order, - preferred_nid, nodemask); + preferred_nid, nodemask);
I really prefer not to do this "align arguments with opening bracket" style. As long as they're indented enough to make them visually distinct from indentation-for-if-blocks, I find it annoying when functions get renamed to something with a different length and somebody then wastes time reindenting all the arguments to match.
Review
Quoted 1 line
(quoted diff) +1
-0+ return page_rmappable_folio(page);
I don't particularly object to the main thrust of this patch. I'm not sure I like it in huge_mm.h though. Maybe in mm/internal.h? I wouldn't want anyone outside mm/ calling it.
Hugh Dickins
maintainerRe: [PATCH 09/12] mm: add page_rmappable_folio() wrapper
Quoted 11 lines · Matthew Wilcox
mm/page_alloc.c struct folio *__folio_alloc(gfp_t gfp, unsigned int order, int preferred_nid, · line 4459
Quoted 4 lines · Hugh Dickins
{
mm/page_alloc.c +1
-1struct page *page =__alloc_pages (gfp | __GFP_COMP, order, - preferred_nid, nodemask); + preferred_nid, nodemask);I really prefer not to do this "align arguments with opening bracket" style. As long as they're indented enough to make them visually distinct from indentation-for-if-blocks, I find it annoying when functions get renamed to something with a different length and somebody then wastes time reindenting all the arguments to match.
Okay, I don't care much about inserting spaces to align with the bracket, but didn't like those continuation args leftward of function name above. I'll adjust in v2, and eventually we reach a compromise.
Quoted 6 lines
Review
Quoted 2 lines
(quoted diff) +1
-0+ return page_rmappable_folio(page);I don't particularly object to the main thrust of this patch. I'm not sure I like it in
huge_mm.hthough. Maybe inmm/internal.h? I wouldn't want anyone outsidemm/calling it.
I was expecting more resistance :) Right, I put it in huge_mm.h to be next to your folio_prep_large_rmappable() declaration, but it doesn't have to be there. Ooh, there's a folio_undo_large_rmappable() in mm/internal.h, I'll move page_rmappable_folio() next to that.
Hugh
Hugh Dickins
maintainerPATCH 10/12 mempolicy: alloc_pages_mpol() for NUMA policy without vma
In reply to Hugh DickinsShrink shmem's stack usage by eliminating the pseudo-vma from its folio allocation. alloc_pages_mpol(gfp, order, pol, ilx, nid) becomes the principal actor for passing mempolicy choice down to __alloc_pages(), rather than vma_alloc_folio(gfp, order, vma, addr, hugepage).
vma_alloc_folio() and alloc_pages() remain, but as wrappers around alloc_pages_mpol(). alloc_pages_bulk_*() untouched, except to provide the additional args to policy_nodemask(), which subsumes policy_node(). Cleanup throughout, cutting out some unhelpful "helpers".
It would all be much simpler without MPOL_INTERLEAVE, but that adds a dynamic to the constant mpol: complicated by v3.6 commit 09c231cb8bfd ("tmpfs: distribute interleave better across nodes"), which added ino bias to the interleave, hidden from mm/mempolicy.c until this commit.
Hence "ilx" throughout, the "interleave index". Originally I thought it could be done just with nid, but that's wrong: the nodemask may come from the shared policy layer below a shmem vma, or it may come from the task layer above a shmem vma; and without the final nodemask then nodeid cannot be decided. And how ilx is applied depends also on page order.
The interleave index is almost always irrelevant unless MPOL_INTERLEAVE:
with one exception in alloc_pages_mpol(), where the NO_INTERLEAVE_INDEX passed down from vma-less alloc_pages() is also used as hint not to use THP-style hugepage allocation - to avoid the overhead of a hugepage arg (though I don't understand why we never just added a GFP bit for THP - if it actually needs a different allocation strategy from other pages of the same order). vma_alloc_folio() still carries its hugepage arg here, but it is not used, and should be removed when agreed.
get_vma_policy() no longer allows a NULL vma: over time I believe we've eradicated all the places which used to need it e.g. swapoff and madvise used to pass NULL vma to read_swap_cache_async(), but now know the vma.
--- fs/proc/task_mmu.c | 5 +- include/linux/gfp.h | 10 +- include/linux/mempolicy.h | 13 +- include/linux/mm.h | 2 +- ipc/shm.c | 21 +-- mm/mempolicy.c | 383 ++++++++++++++++---------------------- mm/shmem.c | 102 +++++----- mm/swap.h | 9 +- mm/swap_state.c | 86 +++++---- 9 files changed, 304 insertions(+), 327 deletions(-)
fs/proc/task_mmu.c +3 -2
@@ -1945,8 +1945,9 @@ static int show_numa_map(struct seq_file *m, void *v)struct numa_maps *md = &numa_priv->md;struct file *file = vma->vm_file;struct mm_struct *mm = vma->vm_mm; -struct mempolicy *pol;char buffer[64 ]; +struct mempolicy *pol; +pgoff_t ilx;int nid;if (!mm)
@@ -1955,7 +1956,7 @@ static int show_numa_map(struct seq_file *m, void *v)/* Ensure we start with an empty set of numa_maps statistics. */ memset (md,0 ,sizeof (*md)); - pol =__get_vma_policy (vma, vma->vm_start); + pol =__get_vma_policy (vma, vma->vm_start, &ilx);if (pol) {mpol_to_str (buffer,sizeof (buffer), pol);mpol_cond_put (pol);
include/linux/gfp.h +9 -1
@@ -8,6 +8,7 @@#include <linux/topology.h> struct vm_area_struct ; +struct mempolicy ;/* Convert GFP flags to their corresponding migrate type */ #define GFP_MOVABLE_MASK (__GFP_RECLAIMABLE|__GFP_MOVABLE)
@@ -262,7 +263,9 @@ static inline struct page *alloc_pages_node(int nid, gfp_t gfp_mask,#ifdef CONFIG_NUMA struct page *alloc_pages (gfp_t gfp,unsigned int order); -struct folio *folio_alloc (gfp_t gfp,unsigned order); +struct page *alloc_pages_mpol (gfp_t gfp,unsigned int order, +struct mempolicy *mpol,pgoff_t ilx,int nid); +struct folio *folio_alloc (gfp_t gfp,unsigned int order);struct folio *vma_alloc_folio (gfp_t gfp,int order,struct vm_area_struct *vma,unsigned long addr,bool hugepage);#else
@@ -270,6 +273,11 @@ static inline struct page *alloc_pages(gfp_t gfp_mask, unsigned int order) {return alloc_pages_node (numa_node_id (), gfp_mask, order); } +static inline struct page *alloc_pages_mpol (gfp_t gfp,unsigned int order, +struct mempolicy *mpol,pgoff_t ilx,int nid) +{ +return alloc_pages (gfp, order); +}static inline struct folio *folio_alloc (gfp_t gfp,unsigned int order) {return __folio_alloc_node (gfp, order,numa_node_id ());
include/linux/mempolicy.h +10 -3
@@ -128,7 +128,9 @@ struct mempolicy *mpol_shared_policy_lookup(struct shared_policy *sp,struct mempolicy *get_task_policy (struct task_struct *p);struct mempolicy *__get_vma_policy (struct vm_area_struct *vma, -unsigned long addr); +unsigned long addr,pgoff_t *ilx); +struct mempolicy *get_vma_policy (struct vm_area_struct *vma, +unsigned long addr,int order,pgoff_t *ilx);bool vma_policy_mof (struct vm_area_struct *vma);extern void numa_default_policy (void );
@@ -142,8 +144,6 @@ extern int huge_node(struct vm_area_struct *vma,extern bool init_nodemask_of_mempolicy (nodemask_t *mask);extern bool mempolicy_in_oom_domain (struct task_struct *tsk,const nodemask_t *mask); -extern nodemask_t *policy_nodemask (gfp_t gfp,struct mempolicy *policy); -extern unsigned int mempolicy_slab_node (void );extern enum zone_type policy_zone;
@@ -215,6 +215,13 @@ mpol_shared_policy_lookup(struct shared_policy *sp, pgoff_t idx)return NULL ; } +static inline struct mempolicy *get_vma_policy (struct vm_area_struct *vma, +unsigned long addr,int order,pgoff_t *ilx) +{ + *ilx =0 ; +return NULL ; +} +#define vma_policy (vma) NULL static inline int
include/linux/mm.h +1 -1
@@ -619,7 +619,7 @@ struct vm_operations_struct {* policy. */ struct mempolicy *(*get_policy)(struct vm_area_struct *vma, -unsigned long addr); +unsigned long addr,pgoff_t *ilx);#endif /* * Called by vm_normal_page() for special PTEs to find the
ipc/shm.c +8 -13
@@ -562,30 +562,25 @@ static unsigned long shm_pagesize(struct vm_area_struct *vma) }#ifdef CONFIG_NUMA -static int shm_set_policy (struct vm_area_struct *vma,struct mempolicy *new) +static int shm_set_policy (struct vm_area_struct *vma,struct mempolicy *mpol) { -struct file *file = vma->vm_file; -struct shm_file_data *sfd =shm_file_data (file); +struct shm_file_data *sfd =shm_file_data (vma->vm_file);int err =0 ;if (sfd->vm_ops->set_policy) - err = sfd->vm_ops->set_policy (vma, new); + err = sfd->vm_ops->set_policy (vma, mpol);return err; }static struct mempolicy *shm_get_policy (struct vm_area_struct *vma, -unsigned long addr) +unsigned long addr,pgoff_t *ilx) { -struct file *file = vma->vm_file; -struct shm_file_data *sfd =shm_file_data (file); -struct mempolicy *pol =NULL ; +struct shm_file_data *sfd =shm_file_data (vma->vm_file); +struct mempolicy *mpol = vma->vm_policy;if (sfd->vm_ops->get_policy) - pol = sfd->vm_ops->get_policy (vma, addr); -else if (vma->vm_policy) - pol = vma->vm_policy; - -return pol; + mpol = sfd->vm_ops->get_policy (vma, addr, ilx); +return mpol; }#endif
mm/mempolicy.c +158 -225
575 lines not on this page · raw message
mm/shmem.c +56 -46
151 lines not on this page · raw message
mm/swap.h +5 -4
@@ -2,6 +2,8 @@#ifndef _MM_SWAP_H#define _MM_SWAP_H +struct mempolicy ; +#ifdef CONFIG_SWAP #include <linux/blk_types.h> /* for bio_end_io_t */
@@ -48,11 +50,10 @@ struct page *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,unsigned long addr, struct swap_iocb **plug);struct page *__read_swap_cache_async (swp_entry_t entry,gfp_t gfp_mask, -struct vm_area_struct *vma, -unsigned long addr, +struct mempolicy *mpol,pgoff_t ilx,bool *new_page_allocated);struct page *swap_cluster_readahead (swp_entry_t entry,gfp_t flag, -struct vm_fault *vmf); +struct mempolicy *mpol,pgoff_t ilx);struct page *swapin_readahead (swp_entry_t entry,gfp_t flag,struct vm_fault *vmf);
@@ -80,7 +81,7 @@ static inline void show_swap_cache_info(void) }static inline struct page *swap_cluster_readahead (swp_entry_t entry, -gfp_t gfp_mask,struct vm_fault *vmf) +gfp_t gfp_mask,struct mempolicy *mpol,pgoff_t ilx) {return NULL ; }
mm/swap_state.c +54 -32
194 lines not on this page · raw message
Signature · 1 line
2.35.3
Hugh Dickins
maintainerPATCH 11/12 mempolicy: mmap_lock is not needed while migrating folios
In reply to Hugh Dickinsmbind(2) holds down_write of current task's mmap_lock throughout (exclusive because it needs to set the new mempolicy on the vmas); migrate_pages(2) holds down_read of pid's mmap_lock throughout.
They both hold mmap_lock across the internal migrate_pages(), under which all new page allocations (huge or small) are made. I'm nervous about it; and migrate_pages() certainly does not need mmap_lock itself. It's done this way for mbind(2), because its page allocator is vma_alloc_folio() or alloc_hugetlb_folio_vma(), both of which depend on vma and address.
Now that we have alloc_pages_mpol(), depending on (refcounted) memory policy and interleave index, mbind(2) can be modified to use that or alloc_hugetlb_folio_nodemask(), and then not need mmap_lock across the internal migrate_pages() at all: add alloc_migration_target_by_mpol() to replace mbind's new_page().
(After that change, alloc_hugetlb_folio_vma() is used by nothing but a userfaultfd function: move it out of hugetlb.h and into the #ifdef.)
migrate_pages(2) has chosen its target node before migrating, so can continue to use the standard alloc_migration_target(); but let it take and drop mmap_lock just around migrate_to_node()'s queue_pages_range():
neither the node-to-node calculations nor the page migrations need it.
It seems unlikely, but it is conceivable that some userspace depends on the kernel's mmap_lock exclusion here, instead of doing its own locking:
more likely in a testsuite than in real life. It is also possible, of course, that some pages on the list will be munmapped by another thread before they are migrated, or a newer memory policy applied to the range by that time: but such races could happen before, as soon as mmap_lock was dropped, so it does not appear to be a concern.
--- include/linux/hugetlb.h | 9 ----- mm/hugetlb.c | 38 +++++++++--------- mm/mempolicy.c | 85 +++++++++++++++++++++-------------------- 3 files changed, 64 insertions(+), 68 deletions(-)
include/linux/hugetlb.h +0 -9
@@ -714,8 +714,6 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, unsigned long addr, int avoid_reserve);struct folio *alloc_hugetlb_folio_nodemask (struct hstate *h,int preferred_nid,nodemask_t *nmask,gfp_t gfp_mask); -struct folio *alloc_hugetlb_folio_vma (struct hstate *h,struct vm_area_struct *vma, -unsigned long address);int hugetlb_add_to_page_cache (struct folio *folio,struct address_space *mapping,pgoff_t idx); void restore_reserve_on_error(struct hstate *h, struct vm_area_struct *vma,
@@ -1024,13 +1022,6 @@ alloc_hugetlb_folio_nodemask(struct hstate *h, int preferred_nid,return NULL ; } -static inline struct folio *alloc_hugetlb_folio_vma (struct hstate *h, -struct vm_area_struct *vma, -unsigned long address) -{ -return NULL ; -} -static inline int __alloc_bootmem_huge_page (struct hstate *h) {return 0 ;
mm/hugetlb.c +20 -18
@@ -2479,24 +2479,6 @@ struct folio *alloc_hugetlb_folio_nodemask(struct hstate *h, int preferred_nid,return alloc_migrate_hugetlb_folio (h, gfp_mask, preferred_nid, nmask); } -/* mempolicy aware migration callback */ -struct folio *alloc_hugetlb_folio_vma (struct hstate *h,struct vm_area_struct *vma, -unsigned long address) -{ -struct mempolicy *mpol; -nodemask_t *nodemask; -struct folio *folio; -gfp_t gfp_mask; -int node; - - gfp_mask =htlb_alloc_mask (h); - node =huge_node (vma, address, gfp_mask, &mpol, &nodemask); - folio =alloc_hugetlb_folio_nodemask (h, node, nodemask, gfp_mask); -mpol_cond_put (mpol); - -return folio; -} -/* * Increase the hugetlb pool such that it can accommodate a reservation * of size 'delta'.
@@ -6225,6 +6207,26 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, }#ifdef CONFIG_USERFAULTFD +/* +* Can probably be eliminated, but still used by hugetlb_mfill_atomic_pte(). +*/ +static struct folio *alloc_hugetlb_folio_vma (struct hstate *h, +struct vm_area_struct *vma,unsigned long address) +{ +struct mempolicy *mpol; +nodemask_t *nodemask; +struct folio *folio; +gfp_t gfp_mask; +int node; + + gfp_mask =htlb_alloc_mask (h); + node =huge_node (vma, address, gfp_mask, &mpol, &nodemask); + folio =alloc_hugetlb_folio_nodemask (h, node, nodemask, gfp_mask); +mpol_cond_put (mpol); + +return folio; +} +/* * Used by userfaultfd UFFDIO_* ioctls. Based on userfaultfd's mfill_atomic_pte * with modifications for hugetlb pages.
mm/mempolicy.c +44 -41
153 lines not on this page · raw message
Signature · 1 line
2.35.3
Hugh Dickins
maintainerPATCH 12/12 mempolicy: migration attempt to match interleave nodes
In reply to Hugh DickinsImprove alloc_migration_target_by_mpol()'s treatment of MPOL_INTERLEAVE.
Make an effort in do_mbind(), to identify the correct interleave index for the first page to be migrated, so that it and all subsequent pages from the same vma will be targeted to precisely their intended nodes. Pages from following vmas will still be interleaved from the requested nodemask, but perhaps starting from a different base.
Whether this is worth doing at all, or worth improving further, is
arguable: queue_folio_required() is right not to care about the precise placement on interleaved nodes; but this little effort seems appropriate.
--- mm/mempolicy.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-)
mm/mempolicy.c +49 -3
@@ -430,6 +430,11 @@ static bool strictly_unmovable(unsigned long flags)MPOL_MF_STRICT ; } +struct migration_mpol {/* for alloc_migration_target_by_mpol() */ +struct mempolicy *pol; +pgoff_t ilx; +}; +struct queue_pages {struct list_head *pagelist;unsigned long flags;
@@ -1175,8 +1180,9 @@ int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,static struct folio *alloc_migration_target_by_mpol (struct folio *src,unsigned long private) { -struct mempolicy *pol = (struct mempolicy *)private; -pgoff_t ilx =0 ;/* improve on this later */ +struct migration_mpol *mmpol = (struct migration_mpol *)private; +struct mempolicy *pol = mmpol->pol; +pgoff_t ilx = mmpol->ilx;struct page *page;unsigned int order;int nid =numa_node_id ();
@@ -1231,6 +1237,7 @@ static long do_mbind(unsigned long start, unsigned long len,struct mm_struct *mm = current->mm;struct vm_area_struct *vma, *prev;struct vma_iterator vmi; +struct migration_mpol mmpol;struct mempolicy *new;unsigned long end;long err;
@@ -1311,9 +1318,48 @@ static long do_mbind(unsigned long start, unsigned long len, new =get_task_policy (current);mpol_get (new); } + mmpol.pol = new; + mmpol.ilx =0 ; + +/* +* In the interleaved case, attempt to allocate on exactly the +* targeted nodes, for the first VMA to be migrated; for later +* VMAs, the nodes will still be interleaved from the targeted +* nodemask, but one by one may be selected differently. +*/ +if (new->mode ==MPOL_INTERLEAVE ) { +struct page *page; +unsigned int order; +unsigned long addr = -EFAULT ; + +list_for_each_entry (page, &pagelist, lru) { +if (!PageKsm (page)) +break ; + } +if (!list_entry_is_head (page, &pagelist, lru)) { +vma_iter_init (&vmi, mm, start); +for_each_vma_range (vmi, vma, end) { + addr =page_address_in_vma (page, vma); +if (addr != -EFAULT ) +break ; + } + } +if (addr != -EFAULT ) { + order =compound_order (page); +/* We already know the pol, but not the ilx */ +mpol_cond_put (get_vma_policy (vma, addr, order, + &mmpol.ilx)); +/* HugeTLBfs indexes in hpage_size */ +if (order &&PageHuge (page)) + order =0 ; +/* Set base from which to increment by index */ + mmpol.ilx -= page->index >> order; + } + } + nr_failed |=migrate_pages (&pagelist, alloc_migration_target_by_mpol,NULL , - (unsigned long )new,MIGRATE_SYNC , + (unsigned long )&mmpol,MIGRATE_SYNC ,MR_MEMPOLICY_MBIND ,NULL ); }
Signature · 1 line
2.35.3