1. 12 Sep, 2022 40 commits
    • Miaohe Lin's avatar
      hugetlb_cgroup: remove unneeded return value · 736a8ccc
      Miaohe Lin authored
      The return value of set_hugetlb_cgroup and set_hugetlb_cgroup_rsvd are
      always ignored. Remove them to clean up the code.
      
      Link: https://lkml.kernel.org/r/20220729080106.12752-4-linmiaohe@huawei.comSigned-off-by: default avatarMiaohe Lin <linmiaohe@huawei.com>
      Reviewed-by: default avatarMike Kravetz <mike.kravetz@oracle.com>
      Cc: Mina Almasry <almasrymina@google.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      736a8ccc
    • Miaohe Lin's avatar
      hugetlb_cgroup: hugetlbfs: use helper macro SZ_1{K,M,G} · abfb09e2
      Miaohe Lin authored
      Use helper macro SZ_1K, SZ_1M and SZ_1G to do the size conversion. Minor
      readability improvement.
      
      Link: https://lkml.kernel.org/r/20220729080106.12752-3-linmiaohe@huawei.comSigned-off-by: default avatarMiaohe Lin <linmiaohe@huawei.com>
      Reviewed-by: default avatarMina Almasry <almasrymina@google.com>
      Cc: Mike Kravetz <mike.kravetz@oracle.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      abfb09e2
    • Miaohe Lin's avatar
      hugetlb_cgroup: remove unneeded nr_pages > 0 check · 862f7f65
      Miaohe Lin authored
      Patch series "A few cleanup patches for hugetlb_cgroup", v2.
      
      This series contains a few cleaup patches to remove unneeded check, use
      helper macro, remove unneeded return value and so on.  More details can be
      found in the respective changelogs.
      
      
      This patch (of 5):
      
      When code reaches here, nr_pages must be > 0. Remove unneeded nr_pages > 0
      check to simplify the code.
      
      Link: https://lkml.kernel.org/r/20220729080106.12752-1-linmiaohe@huawei.com
      Link: https://lkml.kernel.org/r/20220729080106.12752-2-linmiaohe@huawei.comSigned-off-by: default avatarMiaohe Lin <linmiaohe@huawei.com>
      Reviewed-by: default avatarMina Almasry <almasrymina@google.com>
      Cc: Mike Kravetz <mike.kravetz@oracle.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      862f7f65
    • Tarun Sahu's avatar
      Kselftests: remove support of libhugetlbfs from kselftests · 6f83d6c7
      Tarun Sahu authored
      libhugetlbfs, the user side utitlity to work with hugepages, does not have
      any active support.  There are only 2 selftests which are part of in
      vm/hmm_test.c that depends on libhugetlbfs.
      
      This patch modifies the tests so that they will not require libhugetlb
      library.
      
      [axelrasmussen@google.com: : remove orphaned references to local_config.{h,mk}]
        Link: https://lkml.kernel.org/r/20220831211526.2743216-1-axelrasmussen@google.com
      Link: https://lkml.kernel.org/r/20220801070231.13831-1-tsahu@linux.ibm.comSigned-off-by: default avatarTarun Sahu <tsahu@linux.ibm.com>
      Signed-off-by: default avatarAxel Rasmussen <axelrasmussen@google.com>
      Tested-by: default avatarZach O'Keefe <zokeefe@google.com>
      Cc: "Aneesh Kumar K.V" <aneesh.kumar@linux.ibm.com>
      Cc: Jerome Glisse <jglisse@redhat.com>
      Cc: Shivaprasad G Bhat <sbhat@linux.ibm.com>
      Cc: Shuah Khan <shuah@kernel.org>
      Cc: Vaibhav Jain <vaibhav@linux.ibm.com>
      Cc: Axel Rasmussen <axelrasmussen@google.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      6f83d6c7
    • Imran Khan's avatar
      kfence: add sysfs interface to disable kfence for selected slabs. · b84e04f1
      Imran Khan authored
      By default kfence allocation can happen for any slab object, whose size is
      up to PAGE_SIZE, as long as that allocation is the first allocation after
      expiration of kfence sample interval.  But in certain debugging scenarios
      we may be interested in debugging corruptions involving some specific slub
      objects like dentry or ext4_* etc.  In such cases limiting kfence for
      allocations involving only specific slub objects will increase the
      probablity of catching the issue since kfence pool will not be consumed by
      other slab objects.
      
      This patch introduces a sysfs interface
      '/sys/kernel/slab/<name>/skip_kfence' to disable kfence for specific
      slabs.  Having the interface work in this way does not impact
      current/default behavior of kfence and allows us to use kfence for
      specific slabs (when needed) as well.  The decision to skip/use kfence is
      taken depending on whether kmem_cache.flags has (newly introduced)
      SLAB_SKIP_KFENCE flag set or not.
      
      Link: https://lkml.kernel.org/r/20220814195353.2540848-1-imran.f.khan@oracle.comSigned-off-by: default avatarImran Khan <imran.f.khan@oracle.com>
      Reviewed-by: default avatarVlastimil Babka <vbabka@suse.cz>
      Reviewed-by: default avatarMarco Elver <elver@google.com>
      Reviewed-by: default avatarHyeonggon Yoo <42.hyeyoo@gmail.com>
      Cc: Alexander Potapenko <glider@google.com>
      Cc: Dmitry Vyukov <dvyukov@google.com>
      Cc: Christoph Lameter <cl@linux.com>
      Cc: Pekka Enberg <penberg@kernel.org>
      Cc: David Rientjes <rientjes@google.com>
      Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
      Cc: Roman Gushchin <roman.gushchin@linux.dev>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      b84e04f1
    • Haiyue Wang's avatar
      mm: migration: fix the FOLL_GET failure on following huge page · 83156821
      Haiyue Wang authored
      Not all huge page APIs support FOLL_GET option, so move_pages() syscall
      will fail to get the page node information for some huge pages.
      
      Like x86 on linux 5.19 with 1GB huge page API follow_huge_pud(), it will
      return NULL page for FOLL_GET when calling move_pages() syscall with the
      NULL 'nodes' parameter, the 'status' parameter has '-2' error in array.
      
      Note: follow_huge_pud() now supports FOLL_GET in linux 6.0.
            Link: https://lore.kernel.org/all/20220714042420.1847125-3-naoya.horiguchi@linux.dev
      
      But these huge page APIs don't support FOLL_GET:
        1. follow_huge_pud() in arch/s390/mm/hugetlbpage.c
        2. follow_huge_addr() in arch/ia64/mm/hugetlbpage.c
           It will cause WARN_ON_ONCE for FOLL_GET.
        3. follow_huge_pgd() in mm/hugetlb.c
      
      This is an temporary solution to mitigate the side effect of the race
      condition fix by calling follow_page() with FOLL_GET set for huge pages.
      
      After supporting follow huge page by FOLL_GET is done, this fix can be
      reverted safely.
      
      Link: https://lkml.kernel.org/r/20220823135841.934465-2-haiyue.wang@intel.com
      Link: https://lkml.kernel.org/r/20220812084921.409142-1-haiyue.wang@intel.com
      Fixes: 4cd61484 ("mm: migration: fix possible do_pages_stat_array racing with memory offline")
      Signed-off-by: default avatarHaiyue Wang <haiyue.wang@intel.com>
      Reviewed-by: default avatarMiaohe Lin <linmiaohe@huawei.com>
      Reviewed-by: default avatar"Huang, Ying" <ying.huang@intel.com>
      Reviewed-by: default avatarBaolin Wang <baolin.wang@linux.alibaba.com>
      Cc: David Hildenbrand <david@redhat.com>
      Cc: Muchun Song <songmuchun@bytedance.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      83156821
    • Yang Yang's avatar
      mm/vmscan: make the annotations of refaults code at the right place · d3629af5
      Yang Yang authored
      After patch "mm/workingset: prepare the workingset detection
      infrastructure for anon LRU", we can handle the refaults of anonymous
      pages too.  So the annotations of refaults should cover both of anonymous
      pages and file pages.
      
      Link: https://lkml.kernel.org/r/20220813080757.59131-1-yang.yang29@zte.com.cn
      Fixes: 170b04b7 ("mm/workingset: prepare the workingset detection infrastructure for anon LRU")
      Signed-off-by: default avatarYang Yang <yang.yang29@zte.com.cn>
      Signed-off-by: default avatarCGEL ZTE <cgel.zte@gmail.com>
      Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
      Cc: Johannes Weiner <hannes@cmpxchg.org>
      Cc: Vlastimil Babka <vbabka@suse.cz>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      d3629af5
    • Kaixu Xia's avatar
      mm/damon/core: simplify the parameter passing for region split operation · 4ed98243
      Kaixu Xia authored
      The parameter 'struct damon_ctx *ctx' is unnecessary in damon region split
      operation, so we can remove it.
      
      Link: https://lkml.kernel.org/r/1660403943-29124-1-git-send-email-kaixuxia@tencent.comSigned-off-by: default avatarKaixu Xia <kaixuxia@tencent.com>
      Reviewed-by: default avatarSeongJae Park <sj@kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      4ed98243
    • Yixuan Cao's avatar
      tools/vm/page_owner_sort: fix -f option · 57eb60c0
      Yixuan Cao authored
      The -f option is to filter out the information of blocks whose memory has
      not been released, I noticed some blocks should not be filtered out.
      
      Commit 9cc7e96a ("mm/page_owner: record timestamp and pid") records
      the allocation timestamp (ts_nsec) of all pages.
      
      Commit 866b4852 ("mm/page_owner: record the timestamp of all pages
      during free") records the free timestamp (free_ts_nsec) of all pages. 
      When the page is allocated for the first time, the initial value of
      free_ts_nsec is 0, and the corresponding time will be obtained when the
      page is released.  But during reallocation the free_ts_nsec will not reset
      to 0 again.  In particular, when page migration occurs, these two
      timestamps will be the same.
      
      Now page_owner_sort removes all text blocks whose free_ts_nsec is not 0
      when using -f option.  However, this way can only select pages allocated
      for the first time.  If a freed page is reallocated, free_ts_nsec will be
      less than ts_nsec; if page migration occurs, the two timestamps will be
      equal.  These cases should be considered as pages are not released.
      
      So I fix the function is_need() to keep text blocks that meet the above
      two conditions when using -f option.
      
      Link: https://lkml.kernel.org/r/20220812155515.30846-1-caoyixuan2019@email.szu.edu.cnSigned-off-by: default avatarYixuan Cao <caoyixuan2019@email.szu.edu.cn>
      Cc: Chongxi Zhao <zhaochongxi2019@email.szu.edu.cn>
      Cc: Jiajian Ye <yejiajian2018@email.szu.edu.cn>
      Cc: Yuhong Feng <yuhongf@szu.edu.cn>
      Cc: Liam Mark <lmark@codeaurora.org>
      Cc: Georgi Djakov <georgi.djakov@linaro.org>
      Cc: Vlastimil Babka <vbabka@suse.cz>
      Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      57eb60c0
    • Kairui Song's avatar
      mm/util: reduce stack usage of folio_mapcount · 2fd86a07
      Kairui Song authored
      folio_test_hugetlb() will call PageHeadHuge which is a function call,
      and blocks the compiler from recognizing this redundant load.
      
      After rearranging the code, stack usage is dropped from 32 to 24, and
      the function size is smaller (tested on GCC 12):
      
      Before:
      Stack usage:
      mm/util.c:845:5:folio_mapcount  32      static
      Size:
      0000000000000ea0 00000000000000c7 T folio_mapcount
      
      After:
      Stack usage:
      mm/util.c:845:5:folio_mapcount  24      static
      Size:
      0000000000000ea0 00000000000000b0 T folio_mapcount
      
      Link: https://lkml.kernel.org/r/20220801173155.92008-1-ryncsn@gmail.comSigned-off-by: default avatarKairui Song <kasong@tencent.com>
      Cc: Matthew Wilcox <willy@infradead.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      2fd86a07
    • Abel Wu's avatar
      mm/page_alloc: only search higher order when fallback · e933dc4a
      Abel Wu authored
      It seems unnecessary to search pages with order < alloc_order in
      fallback allocation.
      
      This can currently happen with ALLOC_NOFRAGMENT and alloc_order >
      pageblock_order, so add a test to prevent it.
      
      [vbabka@suse.cz: changelog addition]
      Link: https://lkml.kernel.org/r/20220803025121.47018-1-wuyun.abel@bytedance.comSigned-off-by: default avatarAbel Wu <wuyun.abel@bytedance.com>
      Acked-by: default avatarVlastimil Babka <vbabka@suse.cz>
      Acked-by: default avatarMel Gorman <mgorman@techsingularity.net>
      Reviewed-by: default avatarMuchun Song <songmuchun@bytedance.com>
      Cc: Michal Hocko <mhocko@kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      e933dc4a
    • Li kunyu's avatar
      page_alloc: remove inactive initialization · 97bab178
      Li kunyu authored
      The allocation address of the table pointer variable is first performed in
      the function, no initialization assignment is required, and no invalid
      pointer will appear.
      
      Link: https://lkml.kernel.org/r/20220803064118.3664-1-kunyu@nfschina.comSigned-off-by: default avatarLi kunyu <kunyu@nfschina.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      97bab178
    • Feng Tang's avatar
      mm/hugetlb: add dedicated func to get 'allowed' nodemask for current process · d2226ebd
      Feng Tang authored
      Muchun Song found that after MPOL_PREFERRED_MANY policy was introduced in
      commit b27abacc ("mm/mempolicy: add MPOL_PREFERRED_MANY for multiple
      preferred nodes"), the policy_nodemask_current()'s semantics for this new
      policy has been changed, which returns 'preferred' nodes instead of
      'allowed' nodes.
      
      With the changed semantic of policy_nodemask_current, a task with
      MPOL_PREFERRED_MANY policy could fail to get its reservation even though
      it can fall back to other nodes (either defined by cpusets or all online
      nodes) for that reservation failing mmap calles unnecessarily early.
      
      The fix is to not consider MPOL_PREFERRED_MANY for reservations at all
      because they, unlike MPOL_MBIND, do not pose any actual hard constrain.
      
      Michal suggested the policy_nodemask_current() is only used by hugetlb,
      and could be moved to hugetlb code with more explicit name to enforce the
      'allowed' semantics for which only MPOL_BIND policy matters.
      
      apply_policy_zone() is made extern to be called in hugetlb code and its
      return value is changed to bool.
      
      [1]. https://lore.kernel.org/lkml/20220801084207.39086-1-songmuchun@bytedance.com/t/
      
      Link: https://lkml.kernel.org/r/20220805005903.95563-1-feng.tang@intel.com
      Fixes: b27abacc ("mm/mempolicy: add MPOL_PREFERRED_MANY for multiple preferred nodes")
      Signed-off-by: default avatarFeng Tang <feng.tang@intel.com>
      Reported-by: default avatarMuchun Song <songmuchun@bytedance.com>
      Suggested-by: default avatarMichal Hocko <mhocko@suse.com>
      Acked-by: default avatarMichal Hocko <mhocko@suse.com>
      Reviewed-by: default avatarMuchun Song <songmuchun@bytedance.com>
      Cc: Mike Kravetz <mike.kravetz@oracle.com>
      Cc: Dave Hansen <dave.hansen@intel.com>
      Cc: Ben Widawsky <bwidawsk@kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      d2226ebd
    • Abel Wu's avatar
      mm/mempolicy: fix lock contention on mems_allowed · 12c1dc8e
      Abel Wu authored
      The mems_allowed field can be modified by other tasks, so it isn't safe to
      access it with alloc_lock unlocked even in the current process context.
      
      Say there are two tasks: A from cpusetA is performing set_mempolicy(2),
      and B is changing cpusetA's cpuset.mems:
      
        A (set_mempolicy)		B (echo xx > cpuset.mems)
        -------------------------------------------------------
        pol = mpol_new();
      				update_tasks_nodemask(cpusetA) {
      				  foreach t in cpusetA {
      				    cpuset_change_task_nodemask(t) {
        mpol_set_nodemask(pol) {
      				      task_lock(t); // t could be A
          new = f(A->mems_allowed);
      				      update t->mems_allowed;
          pol.create(pol, new);
      				      task_unlock(t);
        }
      				    }
      				  }
      				}
        task_lock(A);
        A->mempolicy = pol;
        task_unlock(A);
      
      In this case A's pol->nodes is computed by old mems_allowed, and could
      be inconsistent with A's new mems_allowed.
      
      While it is different when replacing vmas' policy: the pol->nodes is
      gone wild only when current_cpuset_is_being_rebound():
      
        A (mbind)			B (echo xx > cpuset.mems)
        -------------------------------------------------------
        pol = mpol_new();
        mmap_write_lock(A->mm);
      				cpuset_being_rebound = cpusetA;
      				update_tasks_nodemask(cpusetA) {
      				  foreach t in cpusetA {
      				    cpuset_change_task_nodemask(t) {
        mpol_set_nodemask(pol) {
      				      task_lock(t); // t could be A
          mask = f(A->mems_allowed);
      				      update t->mems_allowed;
          pol.create(pol, mask);
      				      task_unlock(t);
        }
      				    }
        foreach v in A->mm {
          if (cpuset_being_rebound == cpusetA)
            pol.rebind(pol, cpuset.mems);
          v->vma_policy = pol;
        }
        mmap_write_unlock(A->mm);
      				    mmap_write_lock(t->mm);
      				    mpol_rebind_mm(t->mm);
      				    mmap_write_unlock(t->mm);
      				  }
      				}
      				cpuset_being_rebound = NULL;
      
      In this case, the cpuset.mems, which has already done updating, is finally
      used for calculating pol->nodes, rather than A->mems_allowed.  So it is OK
      to call mpol_set_nodemask() with alloc_lock unlocked when doing mbind(2).
      
      Link: https://lkml.kernel.org/r/20220811124157.74888-1-wuyun.abel@bytedance.com
      Fixes: 78b132e9 ("mm/mempolicy: remove or narrow the lock on current")
      Signed-off-by: default avatarAbel Wu <wuyun.abel@bytedance.com>
      Acked-by: default avatarMichal Hocko <mhocko@suse.com>
      Reviewed-by: default avatarWei Yang <richard.weiyang@gmail.com>
      Reviewed-by: default avatarMuchun Song <songmuchun@bytedance.com>
      Cc: Vlastimil Babka <vbabka@suse.cz>
      Cc: Mel Gorman <mgorman@techsingularity.net>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      12c1dc8e
    • Charan Teja Kalla's avatar
      mm/cma_debug: show complete cma name in debugfs directories · 9a79443d
      Charan Teja Kalla authored
      Currently only 12 characters of the cma name is being used as the debug
      directories where as the cma name can be of length CMA_MAX_NAME(=64)
      characters.  One side problem with this is having 2 cma's with first
      common 12 characters would end up in trying to create directories with
      same name and fails with -EEXIST thus can limit cma debug functionality.
      
      The 'cma-' prefix is used initially where cma areas don't have any names
      and are represented by simple integer values.  Since now each cma would be
      having its own name, drop 'cma-' prefix for the cma debug directories as
      they are clearly evident that they are for cma debug through creating them
      in /sys/kernel/debug/cma/ path.
      
      Link: https://lkml.kernel.org/r/1660223729-22461-1-git-send-email-quic_charante@quicinc.comSigned-off-by: default avatarCharan Teja Kalla <quic_charante@quicinc.com>
      Cc: David Hildenbrand <david@redhat.com>
      Cc: Vlastimil Babka <vbabka@suse.cz>
      Cc: Pavan Kondeti <quic_pkondeti@quicinc.com>
      Cc: Minchan Kim <minchan@kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      9a79443d
    • Christoph Hellwig's avatar
      mm/swap: remove the end_write_func argument to __swap_writepage · cf1e3fe4
      Christoph Hellwig authored
      The argument is always set to end_swap_bio_write, so remove the argument
      and mark end_swap_bio_write static.
      
      Link: https://lkml.kernel.org/r/20220811141741.660214-1-hch@lst.deSigned-off-by: default avatarChristoph Hellwig <hch@lst.de>
      Cc: Seth Jennings <sjenning@redhat.com>
      Cc: Dan Streetman <ddstreet@ieee.org>
      Cc: Vitaly Wool <vitaly.wool@konsulko.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      cf1e3fe4
    • Alexey Romanov's avatar
      zsmalloc: remove unnecessary size_class NULL check · f24263a5
      Alexey Romanov authored
      pool->size_class array elements can't be NULL, so this check
      is not needed.
      
      In the whole code, we assign pool->size_class[i] values that are
      not NULL. Releasing memory for these values occurs in the
      zs_destroy_pool() function, which also releases and destroys the pool.
      
      In addition, in the zs_stats_size_show() and async_free_zspage(),
      with similar iterations over the array, we don't check it for NULL
      pointer.
      
      Link: https://lkml.kernel.org/r/20220811153755.16102-3-avromanov@sberdevices.ruSigned-off-by: default avatarAlexey Romanov <avromanov@sberdevices.ru>
      Reviewed-by: default avatarSergey Senozhatsky <senozhatsky@chromium.org>
      Cc: Minchan Kim <minchan@kernel.org>
      Cc: Nitin Gupta <ngupta@vflare.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      f24263a5
    • Alexey Romanov's avatar
      zsmalloc: zs_object_copy: add clarifying comment · 050a388b
      Alexey Romanov authored
      Patch series "tidy up zsmalloc implementation"
      
      This patchset remove some unnecessary checks and adds a clarifying
      comment.  While analysing zs_object_copy() function code, I spent some
      time to understand what the call kunmap_atomic(d_addr) is for.  It seems
      that this point is not trivial and it is worth adding a comment.
      
      
      This patch (of 2):
      
      It's not obvious why kunmap_atomic(d_addr) call is needed.
      
      [akpm@linux-foundation.org: tweak comment layout]
      Link: https://lkml.kernel.org/r/20220811153755.16102-1-avromanov@sberdevices.ru
      Link: https://lkml.kernel.org/r/20220811153755.16102-2-avromanov@sberdevices.ruSigned-off-by: default avatarAlexey Romanov <avromanov@sberdevices.ru>
      Cc: Minchan Kim <minchan@kernel.org>
      Cc: Sergey Senozhatsky <senozhatsky@chromium.org>
      Cc: Nitin Gupta <ngupta@vflare.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      050a388b
    • Yang Yang's avatar
      mm/vmscan: define macros for refaults in struct lruvec · e9c2dbc8
      Yang Yang authored
      The magic number 0 and 1 are used in several places in vmscan.c.
      Define macros for them to improve code readability.
      
      Link: https://lkml.kernel.org/r/20220808005644.1721066-1-yang.yang29@zte.com.cnSigned-off-by: default avatarYang Yang <yang.yang29@zte.com.cn>
      Cc: Johannes Weiner <hannes@cmpxchg.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      e9c2dbc8
    • Axel Rasmussen's avatar
      selftests: vm: add /dev/userfaultfd test cases to run_vmtests.sh · 4a7e9225
      Axel Rasmussen authored
      This new mode was recently added to the userfaultfd selftest. We want to
      exercise both userfaultfd(2) as well as /dev/userfaultfd, so add both
      test cases to the script.
      
      Link: https://lkml.kernel.org/r/20220808175614.3885028-6-axelrasmussen@google.comSigned-off-by: default avatarAxel Rasmussen <axelrasmussen@google.com>
      Reviewed-by: default avatarShuah Khan <skhan@linuxfoundation.org>
      Acked-by: default avatarPeter Xu <peterx@redhat.com>
      Cc: Al Viro <viro@zeniv.linux.org.uk>
      Cc: Dave Hansen <dave.hansen@linux.intel.com>
      Cc: Dmitry V. Levin <ldv@altlinux.org>
      Cc: Gleb Fotengauer-Malinovskiy <glebfm@altlinux.org>
      Cc: Hugh Dickins <hughd@google.com>
      Cc: Jan Kara <jack@suse.cz>
      Cc: Jonathan Corbet <corbet@lwn.net>
      Cc: Mel Gorman <mgorman@techsingularity.net>
      Cc: Mike Kravetz <mike.kravetz@oracle.com>
      Cc: Mike Rapoport <rppt@kernel.org>
      Cc: Nadav Amit <namit@vmware.com>
      Cc: Shuah Khan <shuah@kernel.org>
      Cc: Suren Baghdasaryan <surenb@google.com>
      Cc: Vlastimil Babka <vbabka@suse.cz>
      Cc: Zhang Yi <yi.zhang@huawei.com>
      Cc: Mike Rapoport <rppt@linux.ibm.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      4a7e9225
    • Axel Rasmussen's avatar
      userfaultfd: update documentation to describe /dev/userfaultfd · 816284a3
      Axel Rasmussen authored
      Explain the different ways to create a new userfaultfd, and how access
      control works for each way.
      
      [axelrasmussen@google.com: improve wording in documentation, per Mike]
        Link: https://lkml.kernel.org/r/20220819205201.658693-5-axelrasmussen@google.com
      Link: https://lkml.kernel.org/r/20220808175614.3885028-5-axelrasmussen@google.comSigned-off-by: default avatarAxel Rasmussen <axelrasmussen@google.com>
      Acked-by: default avatarPeter Xu <peterx@redhat.com>
      Reviewed-by: default avatarShuah Khan <skhan@linuxfoundation.org>
      Cc: Al Viro <viro@zeniv.linux.org.uk>
      Cc: Dave Hansen <dave.hansen@linux.intel.com>
      Cc: Dmitry V. Levin <ldv@altlinux.org>
      Cc: Gleb Fotengauer-Malinovskiy <glebfm@altlinux.org>
      Cc: Hugh Dickins <hughd@google.com>
      Cc: Jan Kara <jack@suse.cz>
      Cc: Jonathan Corbet <corbet@lwn.net>
      Cc: Mel Gorman <mgorman@techsingularity.net>
      Cc: Mike Kravetz <mike.kravetz@oracle.com>
      Cc: Mike Rapoport <rppt@kernel.org>
      Cc: Nadav Amit <namit@vmware.com>
      Cc: Suren Baghdasaryan <surenb@google.com>
      Cc: Vlastimil Babka <vbabka@suse.cz>
      Cc: Zhang Yi <yi.zhang@huawei.com>
      Cc: Mike Rapoport <rppt@linux.ibm.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      816284a3
    • Axel Rasmussen's avatar
      userfaultfd: selftests: modify selftest to use /dev/userfaultfd · 77c07f7c
      Axel Rasmussen authored
      We clearly want to ensure both userfaultfd(2) and /dev/userfaultfd keep
      working into the future, so just run the test twice, using each interface.
      
      Instead of always testing both userfaultfd(2) and /dev/userfaultfd, let
      the user choose which to test.
      
      As with other test features, change the behavior based on a new command
      line flag.  Introduce the idea of "test mods", which are generic (not
      specific to a test type) modifications to the behavior of the test.  This
      is sort of borrowed from this RFC patch series [1], but simplified a bit.
      
      The benefit is, in "typical" configurations this test is somewhat slow
      (say, 30sec or something).  Testing both clearly doubles it, so it may not
      always be desirable, as users are likely to use one or the other, but
      never both, in the "real world".
      
      [1]: https://patchwork.kernel.org/project/linux-mm/patch/20201129004548.1619714-14-namit@vmware.com/
      
      [axelrasmussen@google.com: modify selftest to exit with KSFT_SKIP *only* when features are unsupported, per Mike]
        Link: https://lkml.kernel.org/r/20220819205201.658693-4-axelrasmussen@google.com
      Link: https://lkml.kernel.org/r/20220808175614.3885028-4-axelrasmussen@google.comSigned-off-by: default avatarAxel Rasmussen <axelrasmussen@google.com>
      Acked-by: default avatarPeter Xu <peterx@redhat.com>
      Acked-by: default avatarMike Rapoport <rppt@linux.ibm.com>
      Cc: Al Viro <viro@zeniv.linux.org.uk>
      Cc: Dave Hansen <dave.hansen@linux.intel.com>
      Cc: Dmitry V. Levin <ldv@altlinux.org>
      Cc: Gleb Fotengauer-Malinovskiy <glebfm@altlinux.org>
      Cc: Hugh Dickins <hughd@google.com>
      Cc: Jan Kara <jack@suse.cz>
      Cc: Jonathan Corbet <corbet@lwn.net>
      Cc: Mel Gorman <mgorman@techsingularity.net>
      Cc: Mike Kravetz <mike.kravetz@oracle.com>
      Cc: Shuah Khan <shuah@kernel.org>
      Cc: Shuah Khan <skhan@linuxfoundation.org>
      Cc: Suren Baghdasaryan <surenb@google.com>
      Cc: Vlastimil Babka <vbabka@suse.cz>
      Cc: Zhang Yi <yi.zhang@huawei.com>
      Cc: Mike Rapoport <rppt@kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      77c07f7c
    • Axel Rasmussen's avatar
      userfaultfd: add /dev/userfaultfd for fine grained access control · 2d5de004
      Axel Rasmussen authored
      Historically, it has been shown that intercepting kernel faults with
      userfaultfd (thereby forcing the kernel to wait for an arbitrary amount of
      time) can be exploited, or at least can make some kinds of exploits
      easier.  So, in 37cd0575 "userfaultfd: add UFFD_USER_MODE_ONLY" we
      changed things so, in order for kernel faults to be handled by
      userfaultfd, either the process needs CAP_SYS_PTRACE, or this sysctl must
      be configured so that any unprivileged user can do it.
      
      In a typical implementation of a hypervisor with live migration (take
      QEMU/KVM as one such example), we do indeed need to be able to handle
      kernel faults.  But, both options above are less than ideal:
      
      - Toggling the sysctl increases attack surface by allowing any
        unprivileged user to do it.
      
      - Granting the live migration process CAP_SYS_PTRACE gives it this
        ability, but *also* the ability to "observe and control the
        execution of another process [...], and examine and change [its]
        memory and registers" (from ptrace(2)). This isn't something we need
        or want to be able to do, so granting this permission violates the
        "principle of least privilege".
      
      This is all a long winded way to say: we want a more fine-grained way to
      grant access to userfaultfd, without granting other additional permissions
      at the same time.
      
      To achieve this, add a /dev/userfaultfd misc device.  This device provides
      an alternative to the userfaultfd(2) syscall for the creation of new
      userfaultfds.  The idea is, any userfaultfds created this way will be able
      to handle kernel faults, without the caller having any special
      capabilities.  Access to this mechanism is instead restricted using e.g. 
      standard filesystem permissions.
      
      [axelrasmussen@google.com: Handle misc_register() failure properly]
        Link: https://lkml.kernel.org/r/20220819205201.658693-3-axelrasmussen@google.com
      Link: https://lkml.kernel.org/r/20220808175614.3885028-3-axelrasmussen@google.comSigned-off-by: default avatarAxel Rasmussen <axelrasmussen@google.com>
      Acked-by: default avatarNadav Amit <namit@vmware.com>
      Acked-by: default avatarPeter Xu <peterx@redhat.com>
      Acked-by: default avatarMike Rapoport <rppt@linux.ibm.com>
      Cc: Al Viro <viro@zeniv.linux.org.uk>
      Cc: Dave Hansen <dave.hansen@linux.intel.com>
      Cc: Dmitry V. Levin <ldv@altlinux.org>
      Cc: Gleb Fotengauer-Malinovskiy <glebfm@altlinux.org>
      Cc: Hugh Dickins <hughd@google.com>
      Cc: Jan Kara <jack@suse.cz>
      Cc: Jonathan Corbet <corbet@lwn.net>
      Cc: Mel Gorman <mgorman@techsingularity.net>
      Cc: Mike Kravetz <mike.kravetz@oracle.com>
      Cc: Shuah Khan <shuah@kernel.org>
      Cc: Shuah Khan <skhan@linuxfoundation.org>
      Cc: Suren Baghdasaryan <surenb@google.com>
      Cc: Vlastimil Babka <vbabka@suse.cz>
      Cc: Zhang Yi <yi.zhang@huawei.com>
      Cc: Mike Rapoport <rppt@kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      2d5de004
    • Axel Rasmussen's avatar
      selftests: vm: add hugetlb_shared userfaultfd test to run_vmtests.sh · a722d705
      Axel Rasmussen authored
      Patch series "userfaultfd: add /dev/userfaultfd for fine grained access
      control", v7.
      
      Why not ...?
      ============
      
      - Why not /proc/[pid]/userfaultfd? Two main points (additional discussion [1]):
      
          - /proc/[pid]/* files are all owned by the user/group of the process, and
            they don't really support chmod/chown. So, without extending procfs it
            doesn't solve the problem this series is trying to solve.
      
          - The main argument *for* this was to support creating UFFDs for remote
            processes. But, that use case clearly calls for CAP_SYS_PTRACE, so to
            support this we could just use the UFFD syscall as-is.
      
      - Why not use a syscall? Access to syscalls is generally controlled by
        capabilities. We don't have a capability which is used for userfaultfd access
        without also granting more / other permissions as well, and adding a new
        capability was rejected [2].
      
          - It's possible a LSM could be used to control access instead, but I have
            some concerns. I don't think this approach would be as easy to use,
            particularly if we were to try to solve this with something heavyweight
            like SELinux. Maybe we could pursue adding a new LSM specifically for
            this user case, but it may be too narrow of a case to justify that.
      
      [1]: https://patchwork.kernel.org/project/linux-mm/cover/20220719195628.3415852-1-axelrasmussen@google.com/
      [2]: https://lore.kernel.org/lkml/686276b9-4530-2045-6bd8-170e5943abe4@schaufler-ca.com/T/
      
      
      This patch (of 5):
      
      This not being included was just a simple oversight.  There are certain
      features (like minor fault support) which are only enabled on shared
      mappings, so without including hugetlb_shared we actually lose a
      significant amount of test coverage.
      
      Link: https://lkml.kernel.org/r/20220808175614.3885028-1-axelrasmussen@google.com
      Link: https://lkml.kernel.org/r/20220808175614.3885028-2-axelrasmussen@google.comSigned-off-by: default avatarAxel Rasmussen <axelrasmussen@google.com>
      Reviewed-by: default avatarShuah Khan <skhan@linuxfoundation.org>
      Reviewed-by: default avatarPeter Xu <peterx@redhat.com>
      Cc: Al Viro <viro@zeniv.linux.org.uk>
      Cc: Dave Hansen <dave.hansen@linux.intel.com>
      Cc: Dmitry V. Levin <ldv@altlinux.org>
      Cc: Gleb Fotengauer-Malinovskiy <glebfm@altlinux.org>
      Cc: Hugh Dickins <hughd@google.com>
      Cc: Jan Kara <jack@suse.cz>
      Cc: Jonathan Corbet <corbet@lwn.net>
      Cc: Mel Gorman <mgorman@techsingularity.net>
      Cc: Mike Kravetz <mike.kravetz@oracle.com>
      Cc: Mike Rapoport <rppt@kernel.org>
      Cc: Nadav Amit <namit@vmware.com>
      Cc: Shuah Khan <shuah@kernel.org>
      Cc: Suren Baghdasaryan <surenb@google.com>
      Cc: Vlastimil Babka <vbabka@suse.cz>
      Cc: Zhang Yi <yi.zhang@huawei.com>
      Cc: Mike Rapoport <rppt@linux.ibm.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      a722d705
    • Kenneth Lee's avatar
      mm/damon/dbgfs: use kmalloc for allocating only one element · b2d4c646
      Kenneth Lee authored
      Use kmalloc(...) rather than kmalloc_array(1, ...) because the number of
      elements we are specifying in this case is 1, kmalloc would accomplish the
      same thing and we can simplify.
      
      Link: https://lkml.kernel.org/r/20220808220019.1680469-1-klee33@uw.eduSigned-off-by: default avatarKenneth Lee <klee33@uw.edu>
      Reviewed-by: default avatarSeongJae Park <sj@kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      b2d4c646
    • Shaoqin Huang's avatar
      mm/filemap.c: convert page_endio() to use a folio · 223ce491
      Shaoqin Huang authored
      Replace three calls to compound_head() with one.
      
      Link: https://lkml.kernel.org/r/20220809023256.178194-1-shaoqin.huang@intel.comSigned-off-by: default avatarShaoqin Huang <shaoqin.huang@intel.com>
      Cc: Matthew Wilcox <willy@infradead.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      223ce491
    • Kefeng Wang's avatar
      mm: memory-failure: cleanup try_to_split_thp_page() · 2ace36f0
      Kefeng Wang authored
      Since commit 5d1fd5dc ("mm,hwpoison: introduce MF_MSG_UNSPLIT_THP"),
      the action_result(,MF_MSG_UNSPLIT_THP,) called to show memory error event
      in memory_failure(), so the pr_info() in try_to_split_thp_page() is only
      needed in soft_offline_in_use_page().
      
      Meanwhile this could also fix the unexpected prefix for "thp split failed"
      due to commit 96f96763 ("mm: memory-failure: convert to pr_fmt()").
      
      Link: https://lkml.kernel.org/r/20220809111813.139690-1-wangkefeng.wang@huawei.comSigned-off-by: default avatarKefeng Wang <wangkefeng.wang@huawei.com>
      Acked-by: default avatarNaoya Horiguchi <naoya.horiguchi@nec.com>
      Cc: Miaohe Lin <linmiaohe@huawei.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      2ace36f0
    • Rik van Riel's avatar
      mm: align larger anonymous mappings on THP boundaries · f35b5d7d
      Rik van Riel authored
      Align larger anonymous memory mappings on THP boundaries by going through
      thp_get_unmapped_area if THPs are enabled for the current process.
      
      With this patch, larger anonymous mappings are now THP aligned.  When a
      malloc library allocates a 2MB or larger arena, that arena can now be
      mapped with THPs right from the start, which can result in better TLB hit
      rates and execution time.
      
      Link: https://lkml.kernel.org/r/20220809142457.4751229f@imladris.surriel.comSigned-off-by: default avatarRik van Riel <riel@surriel.com>
      Reviewed-by: default avatarYang Shi <shy828301@gmail.com>
      Cc: Matthew Wilcox <willy@infradead.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      f35b5d7d
    • Charan Teja Kalla's avatar
      mm/page_ext: remove unused variable in offline_page_ext · 7b5a0b66
      Charan Teja Kalla authored
      Remove unused variable 'nid' in offline_page_ext().  This is not used
      since the page_ext code inception.
      
      Link: https://lkml.kernel.org/r/1659330397-11817-1-git-send-email-quic_charante@quicinc.comSigned-off-by: default avatarCharan Teja Kalla <quic_charante@quicinc.com>
      Acked-by: default avatarMichal Hocko <mhocko@suse.com>
      Reviewed-by: default avatarAnshuman Khandual <anshuman.khandual@arm.com>
      Cc: Roman Gushchin <roman.gushchin@linux.dev>
      Cc: Johannes Weiner <hannes@cmpxchg.org>
      Cc: Shakeel Butt <shakeelb@google.com>
      Cc: Muchun Song <songmuchun@bytedance.com>
      Cc: Pavan Kondeti <quic_pkondeti@quicinc.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      7b5a0b66
    • Zach O'Keefe's avatar
      selftests/vm: add selftest to verify multi THP collapse · 9d0d9468
      Zach O'Keefe authored
      Add support to allocate and verify collapse of multiple hugepage-sized
      regions into multiple THPs.
      
      Add "nr" argument to check_huge() that instructs check_huge() to check for
      exactly "nr_hpages" THPs.  This has the added benefit of now being able to
      check for exactly 0 THPs, and so callsites that previously checked the
      negation of exactly 1 THP are now more correct.
      
      ->collapse struct collapse_context hook has been expanded with a
      "nr_hpages" argument to collapse "nr_hpages" hugepages.  The
      collapse_full() test has been repurposed to collapse 4 THPs at once.  It
      is expected more tests will want to test multi THP collapse (e.g. 
      file/shmem).
      
      This is of particular benefit to madvise collapse context given that it
      may do many THP collapses during a single syscall.
      
      Link: https://lkml.kernel.org/r/20220706235936.2197195-19-zokeefe@google.comSigned-off-by: default avatarZach O'Keefe <zokeefe@google.com>
      Cc: Alex Shi <alex.shi@linux.alibaba.com>
      Cc: Andrea Arcangeli <aarcange@redhat.com>
      Cc: Arnd Bergmann <arnd@arndb.de>
      Cc: Axel Rasmussen <axelrasmussen@google.com>
      Cc: Chris Kennelly <ckennelly@google.com>
      Cc: Chris Zankel <chris@zankel.net>
      Cc: David Hildenbrand <david@redhat.com>
      Cc: David Rientjes <rientjes@google.com>
      Cc: Helge Deller <deller@gmx.de>
      Cc: Hugh Dickins <hughd@google.com>
      Cc: Ivan Kokshaysky <ink@jurassic.park.msu.ru>
      Cc: James Bottomley <James.Bottomley@HansenPartnership.com>
      Cc: Jens Axboe <axboe@kernel.dk>
      Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
      Cc: Matthew Wilcox <willy@infradead.org>
      Cc: Matt Turner <mattst88@gmail.com>
      Cc: Max Filippov <jcmvbkbc@gmail.com>
      Cc: Miaohe Lin <linmiaohe@huawei.com>
      Cc: Michal Hocko <mhocko@suse.com>
      Cc: Minchan Kim <minchan@kernel.org>
      Cc: Pasha Tatashin <pasha.tatashin@soleen.com>
      Cc: Pavel Begunkov <asml.silence@gmail.com>
      Cc: Peter Xu <peterx@redhat.com>
      Cc: Rongwei Wang <rongwei.wang@linux.alibaba.com>
      Cc: SeongJae Park <sj@kernel.org>
      Cc: Song Liu <songliubraving@fb.com>
      Cc: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
      Cc: Vlastimil Babka <vbabka@suse.cz>
      Cc: Yang Shi <shy828301@gmail.com>
      Cc: Zi Yan <ziy@nvidia.com>
      Cc: Dan Carpenter <dan.carpenter@oracle.com>
      Cc: "Souptick Joarder (HPE)" <jrdr.linux@gmail.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      9d0d9468
    • Zach O'Keefe's avatar
      selftests/vm: add selftest to verify recollapse of THPs · 1370a21f
      Zach O'Keefe authored
      Add selftest specific to madvise collapse context that tests MADV_COLLAPSE
      is "successful" if a hugepage-aligned/sized region is already pmd-mapped.
      
      This test also verifies that MADV_COLLAPSE can collapse memory into THPs
      even in "madvise" THP mode and the memory isn't marked VM_HUGEPAGE.
      
      Link: https://lkml.kernel.org/r/20220706235936.2197195-18-zokeefe@google.comSigned-off-by: default avatarZach O'Keefe <zokeefe@google.com>
      Cc: Alex Shi <alex.shi@linux.alibaba.com>
      Cc: Andrea Arcangeli <aarcange@redhat.com>
      Cc: Arnd Bergmann <arnd@arndb.de>
      Cc: Axel Rasmussen <axelrasmussen@google.com>
      Cc: Chris Kennelly <ckennelly@google.com>
      Cc: Chris Zankel <chris@zankel.net>
      Cc: David Hildenbrand <david@redhat.com>
      Cc: David Rientjes <rientjes@google.com>
      Cc: Helge Deller <deller@gmx.de>
      Cc: Hugh Dickins <hughd@google.com>
      Cc: Ivan Kokshaysky <ink@jurassic.park.msu.ru>
      Cc: James Bottomley <James.Bottomley@HansenPartnership.com>
      Cc: Jens Axboe <axboe@kernel.dk>
      Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
      Cc: Matthew Wilcox <willy@infradead.org>
      Cc: Matt Turner <mattst88@gmail.com>
      Cc: Max Filippov <jcmvbkbc@gmail.com>
      Cc: Miaohe Lin <linmiaohe@huawei.com>
      Cc: Michal Hocko <mhocko@suse.com>
      Cc: Minchan Kim <minchan@kernel.org>
      Cc: Pasha Tatashin <pasha.tatashin@soleen.com>
      Cc: Pavel Begunkov <asml.silence@gmail.com>
      Cc: Peter Xu <peterx@redhat.com>
      Cc: Rongwei Wang <rongwei.wang@linux.alibaba.com>
      Cc: SeongJae Park <sj@kernel.org>
      Cc: Song Liu <songliubraving@fb.com>
      Cc: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
      Cc: Vlastimil Babka <vbabka@suse.cz>
      Cc: Yang Shi <shy828301@gmail.com>
      Cc: Zi Yan <ziy@nvidia.com>
      Cc: Dan Carpenter <dan.carpenter@oracle.com>
      Cc: "Souptick Joarder (HPE)" <jrdr.linux@gmail.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      1370a21f
    • Zach O'Keefe's avatar
      selftests/vm: add MADV_COLLAPSE collapse context to selftests · 9330694d
      Zach O'Keefe authored
      Add madvise collapse context to hugepage collapse selftests.  This context
      is tested with /sys/kernel/mm/transparent_hugepage/enabled set to "never"
      in order to avoid unwanted interaction with khugepaged during testing.
      
      Also, refactor updates to sysfs THP settings using a stack so that the THP
      settings from nested callers can be restored.
      
      Link: https://lkml.kernel.org/r/20220706235936.2197195-17-zokeefe@google.comSigned-off-by: default avatarZach O'Keefe <zokeefe@google.com>
      Cc: Alex Shi <alex.shi@linux.alibaba.com>
      Cc: Andrea Arcangeli <aarcange@redhat.com>
      Cc: Arnd Bergmann <arnd@arndb.de>
      Cc: Axel Rasmussen <axelrasmussen@google.com>
      Cc: Chris Kennelly <ckennelly@google.com>
      Cc: Chris Zankel <chris@zankel.net>
      Cc: David Hildenbrand <david@redhat.com>
      Cc: David Rientjes <rientjes@google.com>
      Cc: Helge Deller <deller@gmx.de>
      Cc: Hugh Dickins <hughd@google.com>
      Cc: Ivan Kokshaysky <ink@jurassic.park.msu.ru>
      Cc: James Bottomley <James.Bottomley@HansenPartnership.com>
      Cc: Jens Axboe <axboe@kernel.dk>
      Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
      Cc: Matthew Wilcox <willy@infradead.org>
      Cc: Matt Turner <mattst88@gmail.com>
      Cc: Max Filippov <jcmvbkbc@gmail.com>
      Cc: Miaohe Lin <linmiaohe@huawei.com>
      Cc: Michal Hocko <mhocko@suse.com>
      Cc: Minchan Kim <minchan@kernel.org>
      Cc: Pasha Tatashin <pasha.tatashin@soleen.com>
      Cc: Pavel Begunkov <asml.silence@gmail.com>
      Cc: Peter Xu <peterx@redhat.com>
      Cc: Rongwei Wang <rongwei.wang@linux.alibaba.com>
      Cc: SeongJae Park <sj@kernel.org>
      Cc: Song Liu <songliubraving@fb.com>
      Cc: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
      Cc: Vlastimil Babka <vbabka@suse.cz>
      Cc: Yang Shi <shy828301@gmail.com>
      Cc: Zi Yan <ziy@nvidia.com>
      Cc: Dan Carpenter <dan.carpenter@oracle.com>
      Cc: "Souptick Joarder (HPE)" <jrdr.linux@gmail.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      9330694d
    • Zach O'Keefe's avatar
      selftests/vm: dedup hugepage allocation logic · be6667b0
      Zach O'Keefe authored
      The code
      
      	p = alloc_mapping();
      	printf("Allocate huge page...");
      	madvise(p, hpage_pmd_size, MADV_HUGEPAGE);
      	fill_memory(p, 0, hpage_pmd_size);
      	if (check_huge(p))
      		success("OK");
      	else
      		fail("Fail");
      
      Is repeated many times in different tests.  Add a helper, alloc_hpage()
      to handle this.
      
      Link: https://lkml.kernel.org/r/20220706235936.2197195-16-zokeefe@google.comSigned-off-by: default avatarZach O'Keefe <zokeefe@google.com>
      Cc: Alex Shi <alex.shi@linux.alibaba.com>
      Cc: Andrea Arcangeli <aarcange@redhat.com>
      Cc: Arnd Bergmann <arnd@arndb.de>
      Cc: Axel Rasmussen <axelrasmussen@google.com>
      Cc: Chris Kennelly <ckennelly@google.com>
      Cc: Chris Zankel <chris@zankel.net>
      Cc: David Hildenbrand <david@redhat.com>
      Cc: David Rientjes <rientjes@google.com>
      Cc: Helge Deller <deller@gmx.de>
      Cc: Hugh Dickins <hughd@google.com>
      Cc: Ivan Kokshaysky <ink@jurassic.park.msu.ru>
      Cc: James Bottomley <James.Bottomley@HansenPartnership.com>
      Cc: Jens Axboe <axboe@kernel.dk>
      Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
      Cc: Matthew Wilcox <willy@infradead.org>
      Cc: Matt Turner <mattst88@gmail.com>
      Cc: Max Filippov <jcmvbkbc@gmail.com>
      Cc: Miaohe Lin <linmiaohe@huawei.com>
      Cc: Michal Hocko <mhocko@suse.com>
      Cc: Minchan Kim <minchan@kernel.org>
      Cc: Pasha Tatashin <pasha.tatashin@soleen.com>
      Cc: Pavel Begunkov <asml.silence@gmail.com>
      Cc: Peter Xu <peterx@redhat.com>
      Cc: Rongwei Wang <rongwei.wang@linux.alibaba.com>
      Cc: SeongJae Park <sj@kernel.org>
      Cc: Song Liu <songliubraving@fb.com>
      Cc: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
      Cc: Vlastimil Babka <vbabka@suse.cz>
      Cc: Yang Shi <shy828301@gmail.com>
      Cc: Zi Yan <ziy@nvidia.com>
      Cc: Dan Carpenter <dan.carpenter@oracle.com>
      Cc: "Souptick Joarder (HPE)" <jrdr.linux@gmail.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      be6667b0
    • Zach O'Keefe's avatar
      selftests/vm: modularize collapse selftests · 61c2c676
      Zach O'Keefe authored
      Modularize the collapse action of khugepaged collapse selftests by
      introducing a struct collapse_context which specifies how to collapse a
      given memory range and the expected semantics of the collapse.  This can
      be reused later to test other collapse contexts.
      
      Additionally, all tests have logic that checks if a collapse occurred via
      reading /proc/self/smaps, and report if this is different than expected. 
      Move this logic into the per-context ->collapse() hook instead of
      repeating it in every test.
      
      Link: https://lkml.kernel.org/r/20220706235936.2197195-15-zokeefe@google.comSigned-off-by: default avatarZach O'Keefe <zokeefe@google.com>
      Cc: Alex Shi <alex.shi@linux.alibaba.com>
      Cc: Andrea Arcangeli <aarcange@redhat.com>
      Cc: Arnd Bergmann <arnd@arndb.de>
      Cc: Axel Rasmussen <axelrasmussen@google.com>
      Cc: Chris Kennelly <ckennelly@google.com>
      Cc: Chris Zankel <chris@zankel.net>
      Cc: David Hildenbrand <david@redhat.com>
      Cc: David Rientjes <rientjes@google.com>
      Cc: Helge Deller <deller@gmx.de>
      Cc: Hugh Dickins <hughd@google.com>
      Cc: Ivan Kokshaysky <ink@jurassic.park.msu.ru>
      Cc: James Bottomley <James.Bottomley@HansenPartnership.com>
      Cc: Jens Axboe <axboe@kernel.dk>
      Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
      Cc: Matthew Wilcox <willy@infradead.org>
      Cc: Matt Turner <mattst88@gmail.com>
      Cc: Max Filippov <jcmvbkbc@gmail.com>
      Cc: Miaohe Lin <linmiaohe@huawei.com>
      Cc: Michal Hocko <mhocko@suse.com>
      Cc: Minchan Kim <minchan@kernel.org>
      Cc: Pasha Tatashin <pasha.tatashin@soleen.com>
      Cc: Pavel Begunkov <asml.silence@gmail.com>
      Cc: Peter Xu <peterx@redhat.com>
      Cc: Rongwei Wang <rongwei.wang@linux.alibaba.com>
      Cc: SeongJae Park <sj@kernel.org>
      Cc: Song Liu <songliubraving@fb.com>
      Cc: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
      Cc: Vlastimil Babka <vbabka@suse.cz>
      Cc: Yang Shi <shy828301@gmail.com>
      Cc: Zi Yan <ziy@nvidia.com>
      Cc: Dan Carpenter <dan.carpenter@oracle.com>
      Cc: "Souptick Joarder (HPE)" <jrdr.linux@gmail.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      61c2c676
    • Zach O'Keefe's avatar
      mm/madvise: add MADV_COLLAPSE to process_madvise() · 876b4a18
      Zach O'Keefe authored
      Allow MADV_COLLAPSE behavior for process_madvise(2) if caller has
      CAP_SYS_ADMIN or is requesting collapse of it's own memory.
      
      This is useful for the development of userspace agents that seek to
      optimize THP utilization system-wide by using userspace signals to
      prioritize what memory is most deserving of being THP-backed.
      
      [zokeefe@google.com: remove CAP_SYS_ADMIN requirement for process_madvise(MADV_COLLAPSE)]
        Link: https://lkml.kernel.org/r/20220801210946.3069083-1-zokeefe@google.com
      Link: https://lkml.kernel.org/r/20220706235936.2197195-13-zokeefe@google.comSigned-off-by: default avatarZach O'Keefe <zokeefe@google.com>
      Acked-by: default avatarDavid Rientjes <rientjes@google.com>
      Cc: Alex Shi <alex.shi@linux.alibaba.com>
      Cc: Andrea Arcangeli <aarcange@redhat.com>
      Cc: Arnd Bergmann <arnd@arndb.de>
      Cc: Axel Rasmussen <axelrasmussen@google.com>
      Cc: Chris Kennelly <ckennelly@google.com>
      Cc: Chris Zankel <chris@zankel.net>
      Cc: David Hildenbrand <david@redhat.com>
      Cc: Helge Deller <deller@gmx.de>
      Cc: Hugh Dickins <hughd@google.com>
      Cc: Ivan Kokshaysky <ink@jurassic.park.msu.ru>
      Cc: James Bottomley <James.Bottomley@HansenPartnership.com>
      Cc: Jens Axboe <axboe@kernel.dk>
      Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
      Cc: Matthew Wilcox <willy@infradead.org>
      Cc: Matt Turner <mattst88@gmail.com>
      Cc: Max Filippov <jcmvbkbc@gmail.com>
      Cc: Miaohe Lin <linmiaohe@huawei.com>
      Cc: Michal Hocko <mhocko@suse.com>
      Cc: Minchan Kim <minchan@kernel.org>
      Cc: Pasha Tatashin <pasha.tatashin@soleen.com>
      Cc: Pavel Begunkov <asml.silence@gmail.com>
      Cc: Peter Xu <peterx@redhat.com>
      Cc: Rongwei Wang <rongwei.wang@linux.alibaba.com>
      Cc: SeongJae Park <sj@kernel.org>
      Cc: Song Liu <songliubraving@fb.com>
      Cc: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
      Cc: Vlastimil Babka <vbabka@suse.cz>
      Cc: Yang Shi <shy828301@gmail.com>
      Cc: Zi Yan <ziy@nvidia.com>
      Cc: Dan Carpenter <dan.carpenter@oracle.com>
      Cc: "Souptick Joarder (HPE)" <jrdr.linux@gmail.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      876b4a18
    • Zach O'Keefe's avatar
      mm/khugepaged: rename prefix of shared collapse functions · 7d2c4385
      Zach O'Keefe authored
      The following functions are shared between khugepaged and madvise collapse
      contexts.  Replace the "khugepaged_" prefix with generic "hpage_collapse_"
      prefix in such cases:
      
      khugepaged_test_exit() -> hpage_collapse_test_exit()
      khugepaged_scan_abort() -> hpage_collapse_scan_abort()
      khugepaged_scan_pmd() -> hpage_collapse_scan_pmd()
      khugepaged_find_target_node() -> hpage_collapse_find_target_node()
      khugepaged_alloc_page() -> hpage_collapse_alloc_page()
      
      The kerenel ABI (e.g.  huge_memory:mm_khugepaged_scan_pmd tracepoint) is
      unaltered.
      
      Link: https://lkml.kernel.org/r/20220706235936.2197195-11-zokeefe@google.comSigned-off-by: default avatarZach O'Keefe <zokeefe@google.com>
      Reviewed-by: default avatarYang Shi <shy828301@gmail.com>
      Cc: Alex Shi <alex.shi@linux.alibaba.com>
      Cc: Andrea Arcangeli <aarcange@redhat.com>
      Cc: Arnd Bergmann <arnd@arndb.de>
      Cc: Axel Rasmussen <axelrasmussen@google.com>
      Cc: Chris Kennelly <ckennelly@google.com>
      Cc: Chris Zankel <chris@zankel.net>
      Cc: David Hildenbrand <david@redhat.com>
      Cc: David Rientjes <rientjes@google.com>
      Cc: Helge Deller <deller@gmx.de>
      Cc: Hugh Dickins <hughd@google.com>
      Cc: Ivan Kokshaysky <ink@jurassic.park.msu.ru>
      Cc: James Bottomley <James.Bottomley@HansenPartnership.com>
      Cc: Jens Axboe <axboe@kernel.dk>
      Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
      Cc: Matthew Wilcox <willy@infradead.org>
      Cc: Matt Turner <mattst88@gmail.com>
      Cc: Max Filippov <jcmvbkbc@gmail.com>
      Cc: Miaohe Lin <linmiaohe@huawei.com>
      Cc: Michal Hocko <mhocko@suse.com>
      Cc: Minchan Kim <minchan@kernel.org>
      Cc: Pasha Tatashin <pasha.tatashin@soleen.com>
      Cc: Pavel Begunkov <asml.silence@gmail.com>
      Cc: Peter Xu <peterx@redhat.com>
      Cc: Rongwei Wang <rongwei.wang@linux.alibaba.com>
      Cc: SeongJae Park <sj@kernel.org>
      Cc: Song Liu <songliubraving@fb.com>
      Cc: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
      Cc: Vlastimil Babka <vbabka@suse.cz>
      Cc: Zi Yan <ziy@nvidia.com>
      Cc: Dan Carpenter <dan.carpenter@oracle.com>
      Cc: "Souptick Joarder (HPE)" <jrdr.linux@gmail.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      7d2c4385
    • Zach O'Keefe's avatar
      mm/madvise: introduce MADV_COLLAPSE sync hugepage collapse · 7d8faaf1
      Zach O'Keefe authored
      This idea was introduced by David Rientjes[1].
      
      Introduce a new madvise mode, MADV_COLLAPSE, that allows users to request
      a synchronous collapse of memory at their own expense.
      
      The benefits of this approach are:
      
      * CPU is charged to the process that wants to spend the cycles for the
        THP
      * Avoid unpredictable timing of khugepaged collapse
      
      Semantics
      
      This call is independent of the system-wide THP sysfs settings, but will
      fail for memory marked VM_NOHUGEPAGE.  If the ranges provided span
      multiple VMAs, the semantics of the collapse over each VMA is independent
      from the others.  This implies a hugepage cannot cross a VMA boundary.  If
      collapse of a given hugepage-aligned/sized region fails, the operation may
      continue to attempt collapsing the remainder of memory specified.
      
      The memory ranges provided must be page-aligned, but are not required to
      be hugepage-aligned.  If the memory ranges are not hugepage-aligned, the
      start/end of the range will be clamped to the first/last hugepage-aligned
      address covered by said range.  The memory ranges must span at least one
      hugepage-sized region.
      
      All non-resident pages covered by the range will first be
      swapped/faulted-in, before being internally copied onto a freshly
      allocated hugepage.  Unmapped pages will have their data directly
      initialized to 0 in the new hugepage.  However, for every eligible
      hugepage aligned/sized region to-be collapsed, at least one page must
      currently be backed by memory (a PMD covering the address range must
      already exist).
      
      Allocation for the new hugepage may enter direct reclaim and/or
      compaction, regardless of VMA flags.  When the system has multiple NUMA
      nodes, the hugepage will be allocated from the node providing the most
      native pages.  This operation operates on the current state of the
      specified process and makes no persistent changes or guarantees on how
      pages will be mapped, constructed, or faulted in the future
      
      Return Value
      
      If all hugepage-sized/aligned regions covered by the provided range were
      either successfully collapsed, or were already PMD-mapped THPs, this
      operation will be deemed successful.  On success, process_madvise(2)
      returns the number of bytes advised, and madvise(2) returns 0.  Else, -1
      is returned and errno is set to indicate the error for the most-recently
      attempted hugepage collapse.  Note that many failures might have occurred,
      since the operation may continue to collapse in the event a single
      hugepage-sized/aligned region fails.
      
      	ENOMEM	Memory allocation failed or VMA not found
      	EBUSY	Memcg charging failed
      	EAGAIN	Required resource temporarily unavailable.  Try again
      		might succeed.
      	EINVAL	Other error: No PMD found, subpage doesn't have Present
      		bit set, "Special" page no backed by struct page, VMA
      		incorrectly sized, address not page-aligned, ...
      
      Most notable here is ENOMEM and EBUSY (new to madvise) which are intended
      to provide the caller with actionable feedback so they may take an
      appropriate fallback measure.
      
      Use Cases
      
      An immediate user of this new functionality are malloc() implementations
      that manage memory in hugepage-sized chunks, but sometimes subrelease
      memory back to the system in native-sized chunks via MADV_DONTNEED;
      zapping the pmd.  Later, when the memory is hot, the implementation could
      madvise(MADV_COLLAPSE) to re-back the memory by THPs to regain hugepage
      coverage and dTLB performance.  TCMalloc is such an implementation that
      could benefit from this[2].
      
      Only privately-mapped anon memory is supported for now, but additional
      support for file, shmem, and HugeTLB high-granularity mappings[2] is
      expected.  File and tmpfs/shmem support would permit:
      
      * Backing executable text by THPs.  Current support provided by
        CONFIG_READ_ONLY_THP_FOR_FS may take a long time on a large system which
        might impair services from serving at their full rated load after
        (re)starting.  Tricks like mremap(2)'ing text onto anonymous memory to
        immediately realize iTLB performance prevents page sharing and demand
        paging, both of which increase steady state memory footprint.  With
        MADV_COLLAPSE, we get the best of both worlds: Peak upfront performance
        and lower RAM footprints.
      * Backing guest memory by hugapages after the memory contents have been
        migrated in native-page-sized chunks to a new host, in a
        userfaultfd-based live-migration stack.
      
      [1] https://lore.kernel.org/linux-mm/d098c392-273a-36a4-1a29-59731cdf5d3d@google.com/
      [2] https://github.com/google/tcmalloc/tree/master/tcmalloc
      
      [jrdr.linux@gmail.com: avoid possible memory leak in failure path]
        Link: https://lkml.kernel.org/r/20220713024109.62810-1-jrdr.linux@gmail.com
      [zokeefe@google.com add missing kfree() to madvise_collapse()]
        Link: https://lore.kernel.org/linux-mm/20220713024109.62810-1-jrdr.linux@gmail.com/
        Link: https://lkml.kernel.org/r/20220713161851.1879439-1-zokeefe@google.com
      [zokeefe@google.com: delay computation of hpage boundaries until use]]
        Link: https://lkml.kernel.org/r/20220720140603.1958773-4-zokeefe@google.com
      Link: https://lkml.kernel.org/r/20220706235936.2197195-10-zokeefe@google.comSigned-off-by: default avatarZach O'Keefe <zokeefe@google.com>
      Signed-off-by: default avatar"Souptick Joarder (HPE)" <jrdr.linux@gmail.com>
      Suggested-by: default avatarDavid Rientjes <rientjes@google.com>
      Cc: Alex Shi <alex.shi@linux.alibaba.com>
      Cc: Andrea Arcangeli <aarcange@redhat.com>
      Cc: Arnd Bergmann <arnd@arndb.de>
      Cc: Axel Rasmussen <axelrasmussen@google.com>
      Cc: Chris Kennelly <ckennelly@google.com>
      Cc: Chris Zankel <chris@zankel.net>
      Cc: David Hildenbrand <david@redhat.com>
      Cc: Helge Deller <deller@gmx.de>
      Cc: Hugh Dickins <hughd@google.com>
      Cc: Ivan Kokshaysky <ink@jurassic.park.msu.ru>
      Cc: James Bottomley <James.Bottomley@HansenPartnership.com>
      Cc: Jens Axboe <axboe@kernel.dk>
      Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
      Cc: Matthew Wilcox <willy@infradead.org>
      Cc: Matt Turner <mattst88@gmail.com>
      Cc: Max Filippov <jcmvbkbc@gmail.com>
      Cc: Miaohe Lin <linmiaohe@huawei.com>
      Cc: Michal Hocko <mhocko@suse.com>
      Cc: Minchan Kim <minchan@kernel.org>
      Cc: Pasha Tatashin <pasha.tatashin@soleen.com>
      Cc: Pavel Begunkov <asml.silence@gmail.com>
      Cc: Peter Xu <peterx@redhat.com>
      Cc: Rongwei Wang <rongwei.wang@linux.alibaba.com>
      Cc: SeongJae Park <sj@kernel.org>
      Cc: Song Liu <songliubraving@fb.com>
      Cc: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
      Cc: Vlastimil Babka <vbabka@suse.cz>
      Cc: Yang Shi <shy828301@gmail.com>
      Cc: Zi Yan <ziy@nvidia.com>
      Cc: Dan Carpenter <dan.carpenter@oracle.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      7d8faaf1
    • Zach O'Keefe's avatar
      mm/khugepaged: record SCAN_PMD_MAPPED when scan_pmd() finds hugepage · 50722804
      Zach O'Keefe authored
      When scanning an anon pmd to see if it's eligible for collapse, return
      SCAN_PMD_MAPPED if the pmd already maps a hugepage.  Note that
      SCAN_PMD_MAPPED is different from SCAN_PAGE_COMPOUND used in the
      file-collapse path, since the latter might identify pte-mapped compound
      pages.  This is required by MADV_COLLAPSE which necessarily needs to know
      what hugepage-aligned/sized regions are already pmd-mapped.
      
      In order to determine if a pmd already maps a hugepage, refactor
      mm_find_pmd():
      
      Return mm_find_pmd() to it's pre-commit f72e7dcd ("mm: let mm_find_pmd
      fix buggy race with THP fault") behavior.  ksm was the only caller that
      explicitly wanted a pte-mapping pmd, so open code the pte-mapping logic
      there (pmd_present() and pmd_trans_huge() checks).
      
      Undo revert change in commit f72e7dcd ("mm: let mm_find_pmd fix buggy
      race with THP fault") that open-coded split_huge_pmd_address() pmd lookup
      and use mm_find_pmd() instead.
      
      Link: https://lkml.kernel.org/r/20220706235936.2197195-9-zokeefe@google.comSigned-off-by: default avatarZach O'Keefe <zokeefe@google.com>
      Reviewed-by: default avatarYang Shi <shy828301@gmail.com>
      Cc: Alex Shi <alex.shi@linux.alibaba.com>
      Cc: Andrea Arcangeli <aarcange@redhat.com>
      Cc: Arnd Bergmann <arnd@arndb.de>
      Cc: Axel Rasmussen <axelrasmussen@google.com>
      Cc: Chris Kennelly <ckennelly@google.com>
      Cc: Chris Zankel <chris@zankel.net>
      Cc: David Hildenbrand <david@redhat.com>
      Cc: David Rientjes <rientjes@google.com>
      Cc: Helge Deller <deller@gmx.de>
      Cc: Hugh Dickins <hughd@google.com>
      Cc: Ivan Kokshaysky <ink@jurassic.park.msu.ru>
      Cc: James Bottomley <James.Bottomley@HansenPartnership.com>
      Cc: Jens Axboe <axboe@kernel.dk>
      Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
      Cc: Matthew Wilcox <willy@infradead.org>
      Cc: Matt Turner <mattst88@gmail.com>
      Cc: Max Filippov <jcmvbkbc@gmail.com>
      Cc: Miaohe Lin <linmiaohe@huawei.com>
      Cc: Michal Hocko <mhocko@suse.com>
      Cc: Minchan Kim <minchan@kernel.org>
      Cc: Pasha Tatashin <pasha.tatashin@soleen.com>
      Cc: Pavel Begunkov <asml.silence@gmail.com>
      Cc: Peter Xu <peterx@redhat.com>
      Cc: Rongwei Wang <rongwei.wang@linux.alibaba.com>
      Cc: SeongJae Park <sj@kernel.org>
      Cc: Song Liu <songliubraving@fb.com>
      Cc: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
      Cc: Vlastimil Babka <vbabka@suse.cz>
      Cc: Zi Yan <ziy@nvidia.com>
      Cc: Dan Carpenter <dan.carpenter@oracle.com>
      Cc: "Souptick Joarder (HPE)" <jrdr.linux@gmail.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      50722804
    • Zach O'Keefe's avatar
      mm/thp: add flag to enforce sysfs THP in hugepage_vma_check() · a7f4e6e4
      Zach O'Keefe authored
      MADV_COLLAPSE is not coupled to the kernel-oriented sysfs THP settings[1].
      
      hugepage_vma_check() is the authority on determining if a VMA is eligible
      for THP allocation/collapse, and currently enforces the sysfs THP
      settings.  Add a flag to disable these checks.  For now, only apply this
      arg to anon and file, which use /sys/kernel/transparent_hugepage/enabled. 
      We can expand this to shmem, which uses
      /sys/kernel/transparent_hugepage/shmem_enabled, later.
      
      Use this flag in collapse_pte_mapped_thp() where previously the VMA flags
      passed to hugepage_vma_check() were OR'd with VM_HUGEPAGE to elide the
      VM_HUGEPAGE check in "madvise" THP mode.  Prior to "mm: khugepaged: check
      THP flag in hugepage_vma_check()", this check also didn't check "never"
      THP mode.  As such, this restores the previous behavior of
      collapse_pte_mapped_thp() where sysfs THP settings are ignored.  See
      comment in code for justification why this is OK.
      
      [1] https://lore.kernel.org/linux-mm/CAAa6QmQxay1_=Pmt8oCX2-Va18t44FV-Vs-WsQt_6+qBks4nZA@mail.gmail.com/
      
      Link: https://lkml.kernel.org/r/20220706235936.2197195-8-zokeefe@google.comSigned-off-by: default avatarZach O'Keefe <zokeefe@google.com>
      Reviewed-by: default avatarYang Shi <shy828301@gmail.com>
      Cc: Alex Shi <alex.shi@linux.alibaba.com>
      Cc: Andrea Arcangeli <aarcange@redhat.com>
      Cc: Arnd Bergmann <arnd@arndb.de>
      Cc: Axel Rasmussen <axelrasmussen@google.com>
      Cc: Chris Kennelly <ckennelly@google.com>
      Cc: Chris Zankel <chris@zankel.net>
      Cc: David Hildenbrand <david@redhat.com>
      Cc: David Rientjes <rientjes@google.com>
      Cc: Helge Deller <deller@gmx.de>
      Cc: Hugh Dickins <hughd@google.com>
      Cc: Ivan Kokshaysky <ink@jurassic.park.msu.ru>
      Cc: James Bottomley <James.Bottomley@HansenPartnership.com>
      Cc: Jens Axboe <axboe@kernel.dk>
      Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
      Cc: Matthew Wilcox <willy@infradead.org>
      Cc: Matt Turner <mattst88@gmail.com>
      Cc: Max Filippov <jcmvbkbc@gmail.com>
      Cc: Miaohe Lin <linmiaohe@huawei.com>
      Cc: Michal Hocko <mhocko@suse.com>
      Cc: Minchan Kim <minchan@kernel.org>
      Cc: Pasha Tatashin <pasha.tatashin@soleen.com>
      Cc: Pavel Begunkov <asml.silence@gmail.com>
      Cc: Peter Xu <peterx@redhat.com>
      Cc: Rongwei Wang <rongwei.wang@linux.alibaba.com>
      Cc: SeongJae Park <sj@kernel.org>
      Cc: Song Liu <songliubraving@fb.com>
      Cc: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
      Cc: Vlastimil Babka <vbabka@suse.cz>
      Cc: Zi Yan <ziy@nvidia.com>
      Cc: Dan Carpenter <dan.carpenter@oracle.com>
      Cc: "Souptick Joarder (HPE)" <jrdr.linux@gmail.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      a7f4e6e4
    • Zach O'Keefe's avatar
      mm/khugepaged: add flag to predicate khugepaged-only behavior · d8ea7cc8
      Zach O'Keefe authored
      Add .is_khugepaged flag to struct collapse_control so khugepaged-specific
      behavior can be elided by MADV_COLLAPSE context.
      
      Start by protecting khugepaged-specific heuristics by this flag.  In
      MADV_COLLAPSE, the user presumably has reason to believe the collapse will
      be beneficial and khugepaged heuristics shouldn't prevent the user from
      doing so:
      
      1) sysfs-controlled knobs khugepaged_max_ptes_[none|swap|shared]
      
      2) requirement that some pages in region being collapsed be young or
         referenced
      
      [zokeefe@google.com: consistently order cc->is_khugepaged and pte_* checks]
        Link: https://lkml.kernel.org/r/20220720140603.1958773-3-zokeefe@google.com
        Link: https://lore.kernel.org/linux-mm/Ys2qJm6FaOQcxkha@google.com/
      Link: https://lkml.kernel.org/r/20220706235936.2197195-7-zokeefe@google.comSigned-off-by: default avatarZach O'Keefe <zokeefe@google.com>
      Reviewed-by: default avatarYang Shi <shy828301@gmail.com>
      Cc: Alex Shi <alex.shi@linux.alibaba.com>
      Cc: Andrea Arcangeli <aarcange@redhat.com>
      Cc: Arnd Bergmann <arnd@arndb.de>
      Cc: Axel Rasmussen <axelrasmussen@google.com>
      Cc: Chris Kennelly <ckennelly@google.com>
      Cc: Chris Zankel <chris@zankel.net>
      Cc: David Hildenbrand <david@redhat.com>
      Cc: David Rientjes <rientjes@google.com>
      Cc: Helge Deller <deller@gmx.de>
      Cc: Hugh Dickins <hughd@google.com>
      Cc: Ivan Kokshaysky <ink@jurassic.park.msu.ru>
      Cc: James Bottomley <James.Bottomley@HansenPartnership.com>
      Cc: Jens Axboe <axboe@kernel.dk>
      Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
      Cc: Matthew Wilcox <willy@infradead.org>
      Cc: Matt Turner <mattst88@gmail.com>
      Cc: Max Filippov <jcmvbkbc@gmail.com>
      Cc: Miaohe Lin <linmiaohe@huawei.com>
      Cc: Michal Hocko <mhocko@suse.com>
      Cc: Minchan Kim <minchan@kernel.org>
      Cc: Pasha Tatashin <pasha.tatashin@soleen.com>
      Cc: Pavel Begunkov <asml.silence@gmail.com>
      Cc: Peter Xu <peterx@redhat.com>
      Cc: Rongwei Wang <rongwei.wang@linux.alibaba.com>
      Cc: SeongJae Park <sj@kernel.org>
      Cc: Song Liu <songliubraving@fb.com>
      Cc: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
      Cc: Vlastimil Babka <vbabka@suse.cz>
      Cc: Zi Yan <ziy@nvidia.com>
      Cc: Dan Carpenter <dan.carpenter@oracle.com>
      Cc: "Souptick Joarder (HPE)" <jrdr.linux@gmail.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      d8ea7cc8