1. 20 Dec, 2023 35 commits
    • SeongJae Park's avatar
      selftests/damon: add a test for update_schemes_tried_regions sysfs command · b5906f5f
      SeongJae Park authored
      Add a selftest for verifying the accuracy of DAMON's access monitoring
      functionality.  The test starts a program of artificial access pattern,
      monitor the access pattern using DAMON, and check if DAMON finds expected
      amount of hot data region (working set size) with only acceptable error
      rate.
      
      Note that the acceptable error rate is set with only naive assumptions and
      small number of tests.  Hence failures of the test may not always mean
      DAMON is broken.  Rather than that, those could be a signal to better
      understand the real accuracy level of DAMON in wider environments.  Based
      on further finding, we could optimize DAMON or adjust the expectation of
      the test.
      
      Link: https://lkml.kernel.org/r/20231212194810.54457-5-sj@kernel.orgSigned-off-by: default avatarSeongJae Park <sj@kernel.org>
      Cc: Shuah Khan <shuah@kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      b5906f5f
    • SeongJae Park's avatar
      selftests/damon/_damon_sysfs: implement updat_schemes_tried_bytes command · 3402c6ce
      SeongJae Park authored
      Implement update_schemes_tried_bytes command of DAMON sysfs interface in
      _damon_sysfs.py.  It is not only making the update, but also read the
      updated value from the sysfs interface and store it in the Kdamond python
      objects so that the user of the module can easily get the value.
      
      Link: https://lkml.kernel.org/r/20231212194810.54457-4-sj@kernel.orgSigned-off-by: default avatarSeongJae Park <sj@kernel.org>
      Cc: Shuah Khan <shuah@kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      3402c6ce
    • SeongJae Park's avatar
      selftests/damon/_damon_sysfs: implement kdamonds start function · f5f0e5a2
      SeongJae Park authored
      Extend the tests-writing-purpose DAMON sysfs control module to support the
      kdamonds start functionality.
      
      Link: https://lkml.kernel.org/r/20231212194810.54457-3-sj@kernel.orgSigned-off-by: default avatarSeongJae Park <sj@kernel.org>
      Cc: Shuah Khan <shuah@kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      f5f0e5a2
    • SeongJae Park's avatar
      selftests/damon: implement a python module for test-purpose DAMON sysfs controls · 306abb63
      SeongJae Park authored
      Patch series "selftests/damon: add Python-written DAMON functionality
      tests", v2.
      
      DAMON exports most of its functionality via its sysfs interface.  Hence
      most DAMON functionality tests could be implemented using the interface. 
      However, because the interfaces require simple but multiple operations for
      many controls, writing all such tests from the scratch could be repetitive
      and time consuming.
      
      Implement a minimum DAMON sysfs control module, and a couple of DAMON
      functionality tests using the control module.  The first test is for
      ensuring minimum accuracy of data access monitoring, and the second test
      is for finding if a previously found and fixed bug is introduced again.
      
      Note that the DAMON sysfs control module is only for avoiding duplicating
      code in tests.  For convenient and general control of DAMON, users should
      use DAMON user-space tools that developed for the purpose, such as
      damo[1].
      
      [1] https://github.com/damonitor/damo
      
      Patches Sequence
      ----------------
      
      This patchset is constructed with five patches.  The first three patches
      implement a Python-written test implementation-purpose DAMON sysfs control
      module.  The implementation is incrementally done in the sequence of the
      basic data structure (first patch) first, kdamonds start command (second
      patch) next, and finally DAMOS tried bytes update command (third patch).
      
      Then two patches for implementing selftests using the module follows.  The
      fourth patch implements a basic functionality test of DAMON for working
      set estimation accuracy.  Finally, the fifth patch implements a corner
      case test for a previously found bug.
      
      
      This patch (of 5):
      
      Implement a python module for DAMON sysfs controls.  The module is aimed
      to be useful for writing DAMON functionality tests in future. 
      Nonetheless, this module is only representing a subset of DAMON sysfs
      files.  Following commits will implement more DAMON sysfs controls.
      
      Link: https://lkml.kernel.org/r/20231212194810.54457-1-sj@kernel.org
      Link: https://lkml.kernel.org/r/20231212194810.54457-2-sj@kernel.orgSigned-off-by: default avatarSeongJae Park <sj@kernel.org>
      Cc: Shuah Khan <shuah@kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      306abb63
    • Randy Dunlap's avatar
      maple_tree: fix typos/spellos etc · d5f6057c
      Randy Dunlap authored
      Fix typos/grammar and spellos in documentation.
      
      Link: https://lkml.kernel.org/r/20231210063839.29967-1-rdunlap@infradead.orgSigned-off-by: default avatarRandy Dunlap <rdunlap@infradead.org>
      Reviewed-by: default avatarMatthew Wilcox (Oracle) <willy@infradead.org>
      Cc: Liam R. Howlett <Liam.Howlett@oracle.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      d5f6057c
    • Jiapeng Chong's avatar
      maple_tree: fix warning comparing pointer to 0 · 03d69d49
      Jiapeng Chong authored
      Avoid pointer type value compared with 0 to make code clear.
      
      ./tools/testing/radix-tree/maple.c:34142:15-16: WARNING comparing pointer to 0.
      
      Link: https://lkml.kernel.org/r/20231208020450.7003-1-jiapeng.chong@linux.alibaba.comReported-by: default avatarAbaci Robot <abaci@linux.alibaba.com>
      Closes: https://bugzilla.openanolis.cn/show_bug.cgi?id=7696Signed-off-by: default avatarJiapeng Chong <jiapeng.chong@linux.alibaba.com>
      Cc: Liam R. Howlett <Liam.Howlett@oracle.com>
      Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      03d69d49
    • Ryan Roberts's avatar
      selftests/mm/cow: add tests for anonymous multi-size THP · c0f79103
      Ryan Roberts authored
      Add tests similar to the existing PMD-sized THP tests, but which operate
      on memory backed by (PTE-mapped) multi-size THP.  This reuses all the
      existing infrastructure.  If the test suite detects that multi-size THP is
      not supported by the kernel, the new tests are skipped.
      
      Link: https://lkml.kernel.org/r/20231207161211.2374093-11-ryan.roberts@arm.comSigned-off-by: default avatarRyan Roberts <ryan.roberts@arm.com>
      Reviewed-by: default avatarDavid Hildenbrand <david@redhat.com>
      Tested-by: default avatarKefeng Wang <wangkefeng.wang@huawei.com>
      Tested-by: default avatarJohn Hubbard <jhubbard@nvidia.com>
      Cc: Alistair Popple <apopple@nvidia.com>
      Cc: Anshuman Khandual <anshuman.khandual@arm.com>
      Cc: Barry Song <v-songbaohua@oppo.com>
      Cc: Catalin Marinas <catalin.marinas@arm.com>
      Cc: David Rientjes <rientjes@google.com>
      Cc: "Huang, Ying" <ying.huang@intel.com>
      Cc: Hugh Dickins <hughd@google.com>
      Cc: Itaru Kitayama <itaru.kitayama@gmail.com>
      Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
      Cc: Luis Chamberlain <mcgrof@kernel.org>
      Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
      Cc: Vlastimil Babka <vbabka@suse.cz>
      Cc: Yang Shi <shy828301@gmail.com>
      Cc: Yin Fengwei <fengwei.yin@intel.com>
      Cc: Yu Zhao <yuzhao@google.com>
      Cc: Zi Yan <ziy@nvidia.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      c0f79103
    • Ryan Roberts's avatar
      selftests/mm/cow: generalize do_run_with_thp() helper · 12dc16b3
      Ryan Roberts authored
      do_run_with_thp() prepares (PMD-sized) THP memory into different states
      before running tests.  With the introduction of multi-size THP, we would
      like to reuse this logic to also test those smaller THP sizes.  So let's
      add a thpsize parameter which tells the function what size THP it should
      operate on.
      
      A separate commit will utilize this change to add new tests for multi-size
      THP, where available.
      
      Link: https://lkml.kernel.org/r/20231207161211.2374093-10-ryan.roberts@arm.comSigned-off-by: default avatarRyan Roberts <ryan.roberts@arm.com>
      Reviewed-by: default avatarDavid Hildenbrand <david@redhat.com>
      Tested-by: default avatarKefeng Wang <wangkefeng.wang@huawei.com>
      Tested-by: default avatarJohn Hubbard <jhubbard@nvidia.com>
      Cc: Alistair Popple <apopple@nvidia.com>
      Cc: Anshuman Khandual <anshuman.khandual@arm.com>
      Cc: Barry Song <v-songbaohua@oppo.com>
      Cc: Catalin Marinas <catalin.marinas@arm.com>
      Cc: David Rientjes <rientjes@google.com>
      Cc: "Huang, Ying" <ying.huang@intel.com>
      Cc: Hugh Dickins <hughd@google.com>
      Cc: Itaru Kitayama <itaru.kitayama@gmail.com>
      Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
      Cc: Luis Chamberlain <mcgrof@kernel.org>
      Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
      Cc: Vlastimil Babka <vbabka@suse.cz>
      Cc: Yang Shi <shy828301@gmail.com>
      Cc: Yin Fengwei <fengwei.yin@intel.com>
      Cc: Yu Zhao <yuzhao@google.com>
      Cc: Zi Yan <ziy@nvidia.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      12dc16b3
    • Ryan Roberts's avatar
      selftests/mm/khugepaged: enlighten for multi-size THP · 9f0704ea
      Ryan Roberts authored
      The `collapse_max_ptes_none` test was previously failing when a THP size
      less than PMD-size had enabled="always".  The root cause is because the
      test faults in 1 page less than the threshold it set for collapsing.  But
      when THP is enabled always, we "over allocate" and therefore the threshold
      is passed, and collapse unexpectedly succeeds.
      
      Solve this by enlightening khugepaged selftest.  Add a command line option
      to pass in the desired THP size that should be used for all anonymous
      allocations.  The harness will then explicitly configure a THP size as
      requested and modify the `collapse_max_ptes_none` test so that it faults
      in the threshold minus the number of pages in the configured THP size.  If
      no command line option is provided, default to order 0, as per previous
      behaviour.
      
      I chose to use an order in the command line interface, since this makes
      the interface agnostic of base page size, making it easier to invoke from
      run_vmtests.sh.
      
      Link: https://lkml.kernel.org/r/20231207161211.2374093-9-ryan.roberts@arm.comSigned-off-by: default avatarRyan Roberts <ryan.roberts@arm.com>
      Tested-by: default avatarKefeng Wang <wangkefeng.wang@huawei.com>
      Tested-by: default avatarJohn Hubbard <jhubbard@nvidia.com>
      Cc: Alistair Popple <apopple@nvidia.com>
      Cc: Anshuman Khandual <anshuman.khandual@arm.com>
      Cc: Barry Song <v-songbaohua@oppo.com>
      Cc: Catalin Marinas <catalin.marinas@arm.com>
      Cc: David Hildenbrand <david@redhat.com>
      Cc: David Rientjes <rientjes@google.com>
      Cc: "Huang, Ying" <ying.huang@intel.com>
      Cc: Hugh Dickins <hughd@google.com>
      Cc: Itaru Kitayama <itaru.kitayama@gmail.com>
      Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
      Cc: Luis Chamberlain <mcgrof@kernel.org>
      Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
      Cc: Vlastimil Babka <vbabka@suse.cz>
      Cc: Yang Shi <shy828301@gmail.com>
      Cc: Yin Fengwei <fengwei.yin@intel.com>
      Cc: Yu Zhao <yuzhao@google.com>
      Cc: Zi Yan <ziy@nvidia.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      9f0704ea
    • Ryan Roberts's avatar
      selftests/mm: support multi-size THP interface in thp_settings · 4f5070a5
      Ryan Roberts authored
      Save and restore the new per-size hugepage enabled setting, if available
      on the running kernel.
      
      Since the number of per-size directories is not fixed, solve this as
      simply as possible by catering for a maximum number in the thp_settings
      struct (20).  Each array index is the order.  The value of THP_NEVER is
      changed to 0 so that all of these new settings default to THP_NEVER and
      the user only needs to fill in the ones they want to enable.
      
      Link: https://lkml.kernel.org/r/20231207161211.2374093-8-ryan.roberts@arm.comSigned-off-by: default avatarRyan Roberts <ryan.roberts@arm.com>
      Tested-by: default avatarKefeng Wang <wangkefeng.wang@huawei.com>
      Tested-by: default avatarJohn Hubbard <jhubbard@nvidia.com>
      Cc: Alistair Popple <apopple@nvidia.com>
      Cc: Anshuman Khandual <anshuman.khandual@arm.com>
      Cc: Barry Song <v-songbaohua@oppo.com>
      Cc: Catalin Marinas <catalin.marinas@arm.com>
      Cc: David Hildenbrand <david@redhat.com>
      Cc: David Rientjes <rientjes@google.com>
      Cc: "Huang, Ying" <ying.huang@intel.com>
      Cc: Hugh Dickins <hughd@google.com>
      Cc: Itaru Kitayama <itaru.kitayama@gmail.com>
      Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
      Cc: Luis Chamberlain <mcgrof@kernel.org>
      Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
      Cc: Vlastimil Babka <vbabka@suse.cz>
      Cc: Yang Shi <shy828301@gmail.com>
      Cc: Yin Fengwei <fengwei.yin@intel.com>
      Cc: Yu Zhao <yuzhao@google.com>
      Cc: Zi Yan <ziy@nvidia.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      4f5070a5
    • Ryan Roberts's avatar
      selftests/mm: factor out thp settings management · 00679a18
      Ryan Roberts authored
      The khugepaged test has a useful framework for save/restore/pop/push of
      all thp settings via the sysfs interface.  This will be useful to
      explicitly control multi-size THP settings in other tests, so let's move
      it out of khugepaged and into its own thp_settings.[c|h] utility.
      
      Link: https://lkml.kernel.org/r/20231207161211.2374093-7-ryan.roberts@arm.comSigned-off-by: default avatarRyan Roberts <ryan.roberts@arm.com>
      Tested-by: default avatarAlistair Popple <apopple@nvidia.com>
      Acked-by: default avatarDavid Hildenbrand <david@redhat.com>
      Tested-by: default avatarKefeng Wang <wangkefeng.wang@huawei.com>
      Tested-by: default avatarJohn Hubbard <jhubbard@nvidia.com>
      Cc: Anshuman Khandual <anshuman.khandual@arm.com>
      Cc: Barry Song <v-songbaohua@oppo.com>
      Cc: Catalin Marinas <catalin.marinas@arm.com>
      Cc: David Rientjes <rientjes@google.com>
      Cc: "Huang, Ying" <ying.huang@intel.com>
      Cc: Hugh Dickins <hughd@google.com>
      Cc: Itaru Kitayama <itaru.kitayama@gmail.com>
      Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
      Cc: Luis Chamberlain <mcgrof@kernel.org>
      Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
      Cc: Vlastimil Babka <vbabka@suse.cz>
      Cc: Yang Shi <shy828301@gmail.com>
      Cc: Yin Fengwei <fengwei.yin@intel.com>
      Cc: Yu Zhao <yuzhao@google.com>
      Cc: Zi Yan <ziy@nvidia.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      00679a18
    • Ryan Roberts's avatar
      selftests/mm/kugepaged: restore thp settings at exit · b6aab338
      Ryan Roberts authored
      Previously, the saved thp settings would be restored upon a signal or at
      the natural end of the test suite.  But there are some tests that directly
      call exit() upon failure.  In this case, the thp settings were not being
      restored, which could then influence other tests.
      
      Fix this by installing an atexit() handler to do the actual restore.  The
      signal handler can now just call exit() and the atexit handler is invoked.
      
      Link: https://lkml.kernel.org/r/20231207161211.2374093-6-ryan.roberts@arm.comSigned-off-by: default avatarRyan Roberts <ryan.roberts@arm.com>
      Reviewed-by: default avatarAlistair Popple <apopple@nvidia.com>
      Reviewed-by: default avatarDavid Hildenbrand <david@redhat.com>
      Tested-by: default avatarKefeng Wang <wangkefeng.wang@huawei.com>
      Tested-by: default avatarJohn Hubbard <jhubbard@nvidia.com>
      Cc: Anshuman Khandual <anshuman.khandual@arm.com>
      Cc: Barry Song <v-songbaohua@oppo.com>
      Cc: Catalin Marinas <catalin.marinas@arm.com>
      Cc: David Rientjes <rientjes@google.com>
      Cc: "Huang, Ying" <ying.huang@intel.com>
      Cc: Hugh Dickins <hughd@google.com>
      Cc: Itaru Kitayama <itaru.kitayama@gmail.com>
      Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
      Cc: Luis Chamberlain <mcgrof@kernel.org>
      Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
      Cc: Vlastimil Babka <vbabka@suse.cz>
      Cc: Yang Shi <shy828301@gmail.com>
      Cc: Yin Fengwei <fengwei.yin@intel.com>
      Cc: Yu Zhao <yuzhao@google.com>
      Cc: Zi Yan <ziy@nvidia.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      b6aab338
    • Ryan Roberts's avatar
      mm: thp: support allocation of anonymous multi-size THP · 19eaf449
      Ryan Roberts authored
      Introduce the logic to allow THP to be configured (through the new sysfs
      interface we just added) to allocate large folios to back anonymous
      memory, which are larger than the base page size but smaller than
      PMD-size.  We call this new THP extension "multi-size THP" (mTHP).
      
      mTHP continues to be PTE-mapped, but in many cases can still provide
      similar benefits to traditional PMD-sized THP: Page faults are
      significantly reduced (by a factor of e.g.  4, 8, 16, etc.  depending on
      the configured order), but latency spikes are much less prominent because
      the size of each page isn't as huge as the PMD-sized variant and there is
      less memory to clear in each page fault.  The number of per-page
      operations (e.g.  ref counting, rmap management, lru list management) are
      also significantly reduced since those ops now become per-folio.
      
      Some architectures also employ TLB compression mechanisms to squeeze more
      entries in when a set of PTEs are virtually and physically contiguous and
      approporiately aligned.  In this case, TLB misses will occur less often.
      
      The new behaviour is disabled by default, but can be enabled at runtime by
      writing to /sys/kernel/mm/transparent_hugepage/hugepage-XXkb/enabled (see
      documentation in previous commit).  The long term aim is to change the
      default to include suitable lower orders, but there are some risks around
      internal fragmentation that need to be better understood first.
      
      [ryan.roberts@arm.com: resolve some multi-size THP review nits]
        Link: https://lkml.kernel.org/r/20231214160251.3574571-1-ryan.roberts@arm.com
      Link: https://lkml.kernel.org/r/20231207161211.2374093-5-ryan.roberts@arm.comSigned-off-by: default avatarRyan Roberts <ryan.roberts@arm.com>
      Tested-by: default avatarKefeng Wang <wangkefeng.wang@huawei.com>
      Tested-by: default avatarJohn Hubbard <jhubbard@nvidia.com>
      Acked-by: default avatarDavid Hildenbrand <david@redhat.com>
      Cc: Alistair Popple <apopple@nvidia.com>
      Cc: Anshuman Khandual <anshuman.khandual@arm.com>
      Cc: Barry Song <v-songbaohua@oppo.com>
      Cc: Catalin Marinas <catalin.marinas@arm.com>
      Cc: David Rientjes <rientjes@google.com>
      Cc: "Huang, Ying" <ying.huang@intel.com>
      Cc: Hugh Dickins <hughd@google.com>
      Cc: Itaru Kitayama <itaru.kitayama@gmail.com>
      Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
      Cc: Luis Chamberlain <mcgrof@kernel.org>
      Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
      Cc: Vlastimil Babka <vbabka@suse.cz>
      Cc: Yang Shi <shy828301@gmail.com>
      Cc: Yin Fengwei <fengwei.yin@intel.com>
      Cc: Yu Zhao <yuzhao@google.com>
      Cc: Zi Yan <ziy@nvidia.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      19eaf449
    • Ryan Roberts's avatar
      mm: thp: introduce multi-size THP sysfs interface · 3485b883
      Ryan Roberts authored
      In preparation for adding support for anonymous multi-size THP, introduce
      new sysfs structure that will be used to control the new behaviours.  A
      new directory is added under transparent_hugepage for each supported THP
      size, and contains an `enabled` file, which can be set to "inherit" (to
      inherit the global setting), "always", "madvise" or "never".  For now, the
      kernel still only supports PMD-sized anonymous THP, so only 1 directory is
      populated.
      
      The first half of the change converts transhuge_vma_suitable() and
      hugepage_vma_check() so that they take a bitfield of orders for which the
      user wants to determine support, and the functions filter out all the
      orders that can't be supported, given the current sysfs configuration and
      the VMA dimensions.  The resulting functions are renamed to
      thp_vma_suitable_orders() and thp_vma_allowable_orders() respectively. 
      Convenience functions that take a single, unencoded order and return a
      boolean are also defined as thp_vma_suitable_order() and
      thp_vma_allowable_order().
      
      The second half of the change implements the new sysfs interface.  It has
      been done so that each supported THP size has a `struct thpsize`, which
      describes the relevant metadata and is itself a kobject.  This is pretty
      minimal for now, but should make it easy to add new per-thpsize files to
      the interface if needed in future (e.g.  per-size defrag).  Rather than
      keep the `enabled` state directly in the struct thpsize, I've elected to
      directly encode it into huge_anon_orders_[always|madvise|inherit]
      bitfields since this reduces the amount of work required in
      thp_vma_allowable_orders() which is called for every page fault.
      
      See Documentation/admin-guide/mm/transhuge.rst, as modified by this
      commit, for details of how the new sysfs interface works.
      
      [ryan.roberts@arm.com: fix build warning when CONFIG_SYSFS is disabled]
        Link: https://lkml.kernel.org/r/20231211125320.3997543-1-ryan.roberts@arm.com
      Link: https://lkml.kernel.org/r/20231207161211.2374093-4-ryan.roberts@arm.comSigned-off-by: default avatarRyan Roberts <ryan.roberts@arm.com>
      Reviewed-by: default avatarBarry Song <v-songbaohua@oppo.com>
      Tested-by: default avatarKefeng Wang <wangkefeng.wang@huawei.com>
      Tested-by: default avatarJohn Hubbard <jhubbard@nvidia.com>
      Acked-by: default avatarDavid Hildenbrand <david@redhat.com>
      Cc: Alistair Popple <apopple@nvidia.com>
      Cc: Anshuman Khandual <anshuman.khandual@arm.com>
      Cc: Catalin Marinas <catalin.marinas@arm.com>
      Cc: David Rientjes <rientjes@google.com>
      Cc: "Huang, Ying" <ying.huang@intel.com>
      Cc: Hugh Dickins <hughd@google.com>
      Cc: Itaru Kitayama <itaru.kitayama@gmail.com>
      Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
      Cc: Luis Chamberlain <mcgrof@kernel.org>
      Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
      Cc: Vlastimil Babka <vbabka@suse.cz>
      Cc: Yang Shi <shy828301@gmail.com>
      Cc: Yin Fengwei <fengwei.yin@intel.com>
      Cc: Yu Zhao <yuzhao@google.com>
      Cc: Zi Yan <ziy@nvidia.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      3485b883
    • Ryan Roberts's avatar
      mm: non-pmd-mappable, large folios for folio_add_new_anon_rmap() · 372cbd4d
      Ryan Roberts authored
      In preparation for supporting anonymous multi-size THP, improve
      folio_add_new_anon_rmap() to allow a non-pmd-mappable, large folio to be
      passed to it.  In this case, all contained pages are accounted using the
      order-0 folio (or base page) scheme.
      
      Link: https://lkml.kernel.org/r/20231207161211.2374093-3-ryan.roberts@arm.comSigned-off-by: default avatarRyan Roberts <ryan.roberts@arm.com>
      Reviewed-by: default avatarYu Zhao <yuzhao@google.com>
      Reviewed-by: default avatarYin Fengwei <fengwei.yin@intel.com>
      Reviewed-by: default avatarDavid Hildenbrand <david@redhat.com>
      Reviewed-by: default avatarBarry Song <v-songbaohua@oppo.com>
      Tested-by: default avatarKefeng Wang <wangkefeng.wang@huawei.com>
      Tested-by: default avatarJohn Hubbard <jhubbard@nvidia.com>
      Cc: Alistair Popple <apopple@nvidia.com>
      Cc: Anshuman Khandual <anshuman.khandual@arm.com>
      Cc: Catalin Marinas <catalin.marinas@arm.com>
      Cc: David Rientjes <rientjes@google.com>
      Cc: "Huang, Ying" <ying.huang@intel.com>
      Cc: Hugh Dickins <hughd@google.com>
      Cc: Itaru Kitayama <itaru.kitayama@gmail.com>
      Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
      Cc: Luis Chamberlain <mcgrof@kernel.org>
      Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
      Cc: Vlastimil Babka <vbabka@suse.cz>
      Cc: Yang Shi <shy828301@gmail.com>
      Cc: Zi Yan <ziy@nvidia.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      372cbd4d
    • Ryan Roberts's avatar
      mm: allow deferred splitting of arbitrary anon large folios · 7dc7c5ef
      Ryan Roberts authored
      Patch series "Multi-size THP for anonymous memory", v9.
      
      A series to implement multi-size THP (mTHP) for anonymous memory
      (previously called "small-sized THP" and "large anonymous folios").
      
      The objective of this is to improve performance by allocating larger
      chunks of memory during anonymous page faults:
      
      1) Since SW (the kernel) is dealing with larger chunks of memory than base
         pages, there are efficiency savings to be had; fewer page faults, batched PTE
         and RMAP manipulation, reduced lru list, etc. In short, we reduce kernel
         overhead. This should benefit all architectures.
      2) Since we are now mapping physically contiguous chunks of memory, we can take
         advantage of HW TLB compression techniques. A reduction in TLB pressure
         speeds up kernel and user space. arm64 systems have 2 mechanisms to coalesce
         TLB entries; "the contiguous bit" (architectural) and HPA (uarch).
      
      This version incorporates David's feedback on the core patches (#3, #4)
      and adds some RB and TB tags (see change log for details).
      
      By default, the existing behaviour (and performance) is maintained.  The
      user must explicitly enable multi-size THP to see the performance benefit.
      This is done via a new sysfs interface (as recommended by David
      Hildenbrand - thanks to David for the suggestion)!  This interface is
      inspired by the existing per-hugepage-size sysfs interface used by
      hugetlb, provides full backwards compatibility with the existing PMD-size
      THP interface, and provides a base for future extensibility.  See [9] for
      detailed discussion of the interface.
      
      This series is based on mm-unstable (715b67adf4c8).
      
      
      Prerequisites
      =============
      
      I'm removing this section on the basis that I don't believe what we were
      previously calling prerequisites are really prerequisites anymore.  We
      originally defined them when mTHP was a compile-time feature.  There is
      now a runtime control to opt-in to mTHP; when disabled, correctness and
      performance are as before.  When enabled, the code is still
      correct/robust, but in the absence of the one remaining item (compaction)
      there may be a performance impact in some corners.  See the old list in
      the v8 cover letter at [8].  And a longer explanation of my thinking here
      [10].
      
      SUMMARY: I don't think we should hold this series up, waiting for the
      items on the prerequisites list.  I believe this series should be ready
      now so hopefully can be added to mm-unstable for some testing, then
      fingers crossed for v6.8.
      
      
      Testing
      =======
      
      The series includes patches for mm selftests to enlighten the cow and
      khugepaged tests to explicitly test with multi-size THP, in the same way
      that PMD-sized THP is tested.  The new tests all pass, and no regressions
      are observed in the mm selftest suite.  I've also run my usual kernel
      compilation and java script benchmarks without any issues.
      
      Refer to my performance numbers posted with v6 [6].  (These are for
      multi-size THP only - they do not include the arm64 contpte follow-on
      series).
      
      John Hubbard at Nvidia has indicated dramatic 10x performance improvements
      for some workloads at [11].  (Observed using v6 of this series as well as
      the arm64 contpte series).
      
      Kefeng Wang at Huawei has also indicated he sees improvements at [12] although
      there are some latency regressions also.
      
      I've also checked that there is no regression in the write fault path when
      mTHP is disabled using a microbenchmark.  I ran it for a baseline kernel,
      as well as v8 and v9.  I repeated on Ampere Altra (bare metal) and Apple
      M2 (VM):
      
      |              |        m2 vm        |        altra        |
      |--------------|---------------------|---------------------|
      | kernel       |     mean |  std_rel |     mean |  std_rel |
      |--------------|----------|----------|----------|----------|
      | baseline     |   0.000% |   0.341% |   0.000% |   3.581% |
      | anonfolio-v8 |   0.005% |   0.272% |   5.068% |   1.128% |
      | anonfolio-v9 |  -0.013% |   0.442% |   0.107% |   1.788% |
      
      There is no measurable difference on M2, but altra has a slow down in v8
      which is fixed in v9 by moving the THP order check to be inline within
      thp_vma_allowable_orders(), as suggested by David.
      
      
      This patch (of 10):
      
      In preparation for the introduction of anonymous multi-size THP, we would
      like to be able to split them when they have unmapped subpages, in order
      to free those unused pages under memory pressure.  So remove the
      artificial requirement that the large folio needed to be at least
      PMD-sized.
      
      Link: https://lkml.kernel.org/r/20231207161211.2374093-1-ryan.roberts@arm.com
      Link: https://lkml.kernel.org/r/20231207161211.2374093-2-ryan.roberts@arm.comSigned-off-by: default avatarRyan Roberts <ryan.roberts@arm.com>
      Reviewed-by: default avatarYu Zhao <yuzhao@google.com>
      Reviewed-by: default avatarYin Fengwei <fengwei.yin@intel.com>
      Reviewed-by: default avatarMatthew Wilcox (Oracle) <willy@infradead.org>
      Reviewed-by: default avatarDavid Hildenbrand <david@redhat.com>
      Reviewed-by: default avatarBarry Song <v-songbaohua@oppo.com>
      Tested-by: default avatarKefeng Wang <wangkefeng.wang@huawei.com>
      Tested-by: default avatarJohn Hubbard <jhubbard@nvidia.com>
      Cc: Alistair Popple <apopple@nvidia.com>
      Cc: Anshuman Khandual <anshuman.khandual@arm.com>
      Cc: Catalin Marinas <catalin.marinas@arm.com>
      Cc: David Rientjes <rientjes@google.com>
      Cc: "Huang, Ying" <ying.huang@intel.com>
      Cc: Hugh Dickins <hughd@google.com>
      Cc: Itaru Kitayama <itaru.kitayama@gmail.com>
      Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
      Cc: Luis Chamberlain <mcgrof@kernel.org>
      Cc: Vlastimil Babka <vbabka@suse.cz>
      Cc: Yang Shi <shy828301@gmail.com>
      Cc: Zi Yan <ziy@nvidia.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      7dc7c5ef
    • Yosry Ahmed's avatar
      mm: memcg: restore subtree stats flushing · 7d7ef0a4
      Yosry Ahmed authored
      Stats flushing for memcg currently follows the following rules:
      - Always flush the entire memcg hierarchy (i.e. flush the root).
      - Only one flusher is allowed at a time. If someone else tries to flush
        concurrently, they skip and return immediately.
      - A periodic flusher flushes all the stats every 2 seconds.
      
      The reason this approach is followed is because all flushes are serialized
      by a global rstat spinlock.  On the memcg side, flushing is invoked from
      userspace reads as well as in-kernel flushers (e.g.  reclaim, refault,
      etc).  This approach aims to avoid serializing all flushers on the global
      lock, which can cause a significant performance hit under high
      concurrency.
      
      This approach has the following problems:
      - Occasionally a userspace read of the stats of a non-root cgroup will
        be too expensive as it has to flush the entire hierarchy [1].
      - Sometimes the stats accuracy are compromised if there is an ongoing
        flush, and we skip and return before the subtree of interest is
        actually flushed, yielding stale stats (by up to 2s due to periodic
        flushing). This is more visible when reading stats from userspace,
        but can also affect in-kernel flushers.
      
      The latter problem is particulary a concern when userspace reads stats
      after an event occurs, but gets stats from before the event. Examples:
      - When memory usage / pressure spikes, a userspace OOM handler may look
        at the stats of different memcgs to select a victim based on various
        heuristics (e.g. how much private memory will be freed by killing
        this). Reading stale stats from before the usage spike in this case
        may cause a wrongful OOM kill.
      - A proactive reclaimer may read the stats after writing to
        memory.reclaim to measure the success of the reclaim operation. Stale
        stats from before reclaim may give a false negative.
      - Reading the stats of a parent and a child memcg may be inconsistent
        (child larger than parent), if the flush doesn't happen when the
        parent is read, but happens when the child is read.
      
      As for in-kernel flushers, they will occasionally get stale stats.  No
      regressions are currently known from this, but if there are regressions,
      they would be very difficult to debug and link to the source of the
      problem.
      
      This patch aims to fix these problems by restoring subtree flushing, and
      removing the unified/coalesced flushing logic that skips flushing if there
      is an ongoing flush.  This change would introduce a significant regression
      with global stats flushing thresholds.  With per-memcg stats flushing
      thresholds, this seems to perform really well.  The thresholds protect the
      underlying lock from unnecessary contention.
      
      This patch was tested in two ways to ensure the latency of flushing is
      up to par, on a machine with 384 cpus:
      
      - A synthetic test with 5000 concurrent workers in 500 cgroups doing
        allocations and reclaim, as well as 1000 readers for memory.stat
        (variation of [2]). No regressions were noticed in the total runtime.
        Note that significant regressions in this test are observed with
        global stats thresholds, but not with per-memcg thresholds.
      
      - A synthetic stress test for concurrently reading memcg stats while
        memory allocation/freeing workers are running in the background,
        provided by Wei Xu [3]. With 250k threads reading the stats every
        100ms in 50k cgroups, 99.9% of reads take <= 50us. Less than 0.01%
        of reads take more than 1ms, and no reads take more than 100ms.
      
      [1] https://lore.kernel.org/lkml/CABWYdi0c6__rh-K7dcM_pkf9BJdTRtAU08M43KO9ME4-dsgfoQ@mail.gmail.com/
      [2] https://lore.kernel.org/lkml/CAJD7tka13M-zVZTyQJYL1iUAYvuQ1fcHbCjcOBZcz6POYTV-4g@mail.gmail.com/
      [3] https://lore.kernel.org/lkml/CAAPL-u9D2b=iF5Lf_cRnKxUfkiEe0AMDTu6yhrUAzX0b6a6rDg@mail.gmail.com/
      
      [akpm@linux-foundation.org: fix mm/zswap.c]
      [yosryahmed@google.com: remove stats flushing mutex]
        Link: https://lkml.kernel.org/r/CAJD7tkZgP3m-VVPn+fF_YuvXeQYK=tZZjJHj=dzD=CcSSpp2qg@mail.gmail.com
      Link: https://lkml.kernel.org/r/20231129032154.3710765-6-yosryahmed@google.comSigned-off-by: default avatarYosry Ahmed <yosryahmed@google.com>
      Tested-by: default avatarDomenico Cerasuolo <cerasuolodomenico@gmail.com>
      Acked-by: default avatarShakeel Butt <shakeelb@google.com>
      Cc: Chris Li <chrisl@kernel.org>
      Cc: Greg Thelen <gthelen@google.com>
      Cc: Ivan Babrou <ivan@cloudflare.com>
      Cc: Johannes Weiner <hannes@cmpxchg.org>
      Cc: Michal Hocko <mhocko@kernel.org>
      Cc: Michal Koutny <mkoutny@suse.com>
      Cc: Muchun Song <muchun.song@linux.dev>
      Cc: Roman Gushchin <roman.gushchin@linux.dev>
      Cc: Tejun Heo <tj@kernel.org>
      Cc: Waiman Long <longman@redhat.com>
      Cc: Wei Xu <weixugc@google.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      7d7ef0a4
    • Yosry Ahmed's avatar
      mm: workingset: move the stats flush into workingset_test_recent() · b0068472
      Yosry Ahmed authored
      The workingset code flushes the stats in workingset_refault() to get
      accurate stats of the eviction memcg.  In preparation for more scoped
      flushed and passing the eviction memcg to the flush call, move the call to
      workingset_test_recent() where we have a pointer to the eviction memcg.
      
      The flush call is sleepable, and cannot be made in an rcu read section. 
      Hence, minimize the rcu read section by also moving it into
      workingset_test_recent().  Furthermore, instead of holding the rcu read
      lock throughout workingset_test_recent(), only hold it briefly to get a
      ref on the eviction memcg.  This allows us to make the flush call after we
      get the eviction memcg.
      
      As for workingset_refault(), nothing else there appears to be protected by
      rcu.  The memcg of the faulted folio (which is not necessarily the same as
      the eviction memcg) is protected by the folio lock, which is held from all
      callsites.  Add a VM_BUG_ON() to make sure this doesn't change from under
      us.
      
      No functional change intended.
      
      Link: https://lkml.kernel.org/r/20231129032154.3710765-5-yosryahmed@google.comSigned-off-by: default avatarYosry Ahmed <yosryahmed@google.com>
      Tested-by: default avatarDomenico Cerasuolo <cerasuolodomenico@gmail.com>
      Acked-by: default avatarShakeel Butt <shakeelb@google.com>
      Cc: Chris Li <chrisl@kernel.org>
      Cc: Greg Thelen <gthelen@google.com>
      Cc: Ivan Babrou <ivan@cloudflare.com>
      Cc: Johannes Weiner <hannes@cmpxchg.org>
      Cc: Michal Hocko <mhocko@kernel.org>
      Cc: Michal Koutny <mkoutny@suse.com>
      Cc: Muchun Song <muchun.song@linux.dev>
      Cc: Roman Gushchin <roman.gushchin@linux.dev>
      Cc: Tejun Heo <tj@kernel.org>
      Cc: Waiman Long <longman@redhat.com>
      Cc: Wei Xu <weixugc@google.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      b0068472
    • Yosry Ahmed's avatar
      mm: memcg: make stats flushing threshold per-memcg · 8d59d221
      Yosry Ahmed authored
      A global counter for the magnitude of memcg stats update is maintained on
      the memcg side to avoid invoking rstat flushes when the pending updates
      are not significant.  This avoids unnecessary flushes, which are not very
      cheap even if there isn't a lot of stats to flush.  It also avoids
      unnecessary lock contention on the underlying global rstat lock.
      
      Make this threshold per-memcg.  The scheme is followed where percpu (now
      also per-memcg) counters are incremented in the update path, and only
      propagated to per-memcg atomics when they exceed a certain threshold.
      
      This provides two benefits: (a) On large machines with a lot of memcgs,
      the global threshold can be reached relatively fast, so guarding the
      underlying lock becomes less effective.  Making the threshold per-memcg
      avoids this.
      
      (b) Having a global threshold makes it hard to do subtree flushes, as we
      cannot reset the global counter except for a full flush.  Per-memcg
      counters removes this as a blocker from doing subtree flushes, which helps
      avoid unnecessary work when the stats of a small subtree are needed.
      
      Nothing is free, of course.  This comes at a cost: (a) A new per-cpu
      counter per memcg, consuming NR_CPUS * NR_MEMCGS * 4 bytes.  The extra
      memory usage is insigificant.
      
      (b) More work on the update side, although in the common case it will only
      be percpu counter updates.  The amount of work scales with the number of
      ancestors (i.e.  tree depth).  This is not a new concept, adding a cgroup
      to the rstat tree involves a parent loop, so is charging.  Testing results
      below show no significant regressions.
      
      (c) The error margin in the stats for the system as a whole increases from
      NR_CPUS * MEMCG_CHARGE_BATCH to NR_CPUS * MEMCG_CHARGE_BATCH * NR_MEMCGS. 
      This is probably fine because we have a similar per-memcg error in charges
      coming from percpu stocks, and we have a periodic flusher that makes sure
      we always flush all the stats every 2s anyway.
      
      This patch was tested to make sure no significant regressions are
      introduced on the update path as follows.  The following benchmarks were
      ran in a cgroup that is 2 levels deep (/sys/fs/cgroup/a/b/):
      
      (1) Running 22 instances of netperf on a 44 cpu machine with
      hyperthreading disabled. All instances are run in a level 2 cgroup, as
      well as netserver:
        # netserver -6
        # netperf -6 -H ::1 -l 60 -t TCP_SENDFILE -- -m 10K
      
      Averaging 20 runs, the numbers are as follows:
      Base: 40198.0 mbps
      Patched: 38629.7 mbps (-3.9%)
      
      The regression is minimal, especially for 22 instances in the same
      cgroup sharing all ancestors (so updating the same atomics).
      
      (2) will-it-scale page_fault tests. These tests (specifically
      per_process_ops in page_fault3 test) detected a 25.9% regression before
      for a change in the stats update path [1]. These are the
      numbers from 10 runs (+ is good) on a machine with 256 cpus:
      
                   LABEL            |     MEAN    |   MEDIAN    |   STDDEV   |
      ------------------------------+-------------+-------------+-------------
        page_fault1_per_process_ops |             |             |            |
        (A) base                    | 270249.164  | 265437.000  | 13451.836  |
        (B) patched                 | 261368.709  | 255725.000  | 13394.767  |
                                    | -3.29%      | -3.66%      |            |
        page_fault1_per_thread_ops  |             |             |            |
        (A) base                    | 242111.345  | 239737.000  | 10026.031  |
        (B) patched                 | 237057.109  | 235305.000  | 9769.687   |
                                    | -2.09%      | -1.85%      |            |
        page_fault1_scalability     |             |             |
        (A) base                    | 0.034387    | 0.035168    | 0.0018283  |
        (B) patched                 | 0.033988    | 0.034573    | 0.0018056  |
                                    | -1.16%      | -1.69%      |            |
        page_fault2_per_process_ops |             |             |
        (A) base                    | 203561.836  | 203301.000  | 2550.764   |
        (B) patched                 | 197195.945  | 197746.000  | 2264.263   |
                                    | -3.13%      | -2.73%      |            |
        page_fault2_per_thread_ops  |             |             |
        (A) base                    | 171046.473  | 170776.000  | 1509.679   |
        (B) patched                 | 166626.327  | 166406.000  | 768.753    |
                                    | -2.58%      | -2.56%      |            |
        page_fault2_scalability     |             |             |
        (A) base                    | 0.054026    | 0.053821    | 0.00062121 |
        (B) patched                 | 0.053329    | 0.05306     | 0.00048394 |
                                    | -1.29%      | -1.41%      |            |
        page_fault3_per_process_ops |             |             |
        (A) base                    | 1295807.782 | 1297550.000 | 5907.585   |
        (B) patched                 | 1275579.873 | 1273359.000 | 8759.160   |
                                    | -1.56%      | -1.86%      |            |
        page_fault3_per_thread_ops  |             |             |
        (A) base                    | 391234.164  | 390860.000  | 1760.720   |
        (B) patched                 | 377231.273  | 376369.000  | 1874.971   |
                                    | -3.58%      | -3.71%      |            |
        page_fault3_scalability     |             |             |
        (A) base                    | 0.60369     | 0.60072     | 0.0083029  |
        (B) patched                 | 0.61733     | 0.61544     | 0.009855   |
                                    | +2.26%      | +2.45%      |            |
      
      All regressions seem to be minimal, and within the normal variance for the
      benchmark.  The fix for [1] assumes that 3% is noise -- and there were no
      further practical complaints), so hopefully this means that such
      variations in these microbenchmarks do not reflect on practical workloads.
      
      (3) I also ran stress-ng in a nested cgroup and did not observe any
      obvious regressions.
      
      [1]https://lore.kernel.org/all/20190520063534.GB19312@shao2-debian/
      
      Link: https://lkml.kernel.org/r/20231129032154.3710765-4-yosryahmed@google.comSigned-off-by: default avatarYosry Ahmed <yosryahmed@google.com>
      Suggested-by: default avatarJohannes Weiner <hannes@cmpxchg.org>
      Tested-by: default avatarDomenico Cerasuolo <cerasuolodomenico@gmail.com>
      Acked-by: default avatarShakeel Butt <shakeelb@google.com>
      Cc: Chris Li <chrisl@kernel.org>
      Cc: Greg Thelen <gthelen@google.com>
      Cc: Ivan Babrou <ivan@cloudflare.com>
      Cc: Michal Hocko <mhocko@kernel.org>
      Cc: Michal Koutny <mkoutny@suse.com>
      Cc: Muchun Song <muchun.song@linux.dev>
      Cc: Roman Gushchin <roman.gushchin@linux.dev>
      Cc: Tejun Heo <tj@kernel.org>
      Cc: Waiman Long <longman@redhat.com>
      Cc: Wei Xu <weixugc@google.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      8d59d221
    • Yosry Ahmed's avatar
      mm: memcg: move vmstats structs definition above flushing code · e0bf1dc8
      Yosry Ahmed authored
      The following patch will make use of those structs in the flushing code,
      so move their definitions (and a few other dependencies) a little bit up
      to reduce the diff noise in the following patch.
      
      No functional change intended.
      
      Link: https://lkml.kernel.org/r/20231129032154.3710765-3-yosryahmed@google.comSigned-off-by: default avatarYosry Ahmed <yosryahmed@google.com>
      Tested-by: default avatarDomenico Cerasuolo <cerasuolodomenico@gmail.com>
      Acked-by: default avatarShakeel Butt <shakeelb@google.com>
      Cc: Chris Li <chrisl@kernel.org>
      Cc: Greg Thelen <gthelen@google.com>
      Cc: Ivan Babrou <ivan@cloudflare.com>
      Cc: Johannes Weiner <hannes@cmpxchg.org>
      Cc: Michal Hocko <mhocko@kernel.org>
      Cc: Michal Koutny <mkoutny@suse.com>
      Cc: Muchun Song <muchun.song@linux.dev>
      Cc: Roman Gushchin <roman.gushchin@linux.dev>
      Cc: Tejun Heo <tj@kernel.org>
      Cc: Waiman Long <longman@redhat.com>
      Cc: Wei Xu <weixugc@google.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      e0bf1dc8
    • Yosry Ahmed's avatar
      mm: memcg: change flush_next_time to flush_last_time · 508bed88
      Yosry Ahmed authored
      Patch series "mm: memcg: subtree stats flushing and thresholds", v4.
      
      This series attempts to address shortages in today's approach for memcg
      stats flushing, namely occasionally stale or expensive stat reads.  The
      series does so by changing the threshold that we use to decide whether to
      trigger a flush to be per memcg instead of global (patch 3), and then
      changing flushing to be per memcg (i.e.  subtree flushes) instead of
      global (patch 5).
      
      
      This patch (of 5):
      
      flush_next_time is an inaccurate name.  It's not the next time that
      periodic flushing will happen, it's rather the next time that ratelimited
      flushing can happen if the periodic flusher is late.
      
      Simplify its semantics by just storing the timestamp of the last flush
      instead, flush_last_time.  Move the 2*FLUSH_TIME addition to
      mem_cgroup_flush_stats_ratelimited(), and add a comment explaining it. 
      This way, all the ratelimiting semantics live in one place.
      
      No functional change intended.
      
      Link: https://lkml.kernel.org/r/20231129032154.3710765-1-yosryahmed@google.com
      Link: https://lkml.kernel.org/r/20231129032154.3710765-2-yosryahmed@google.comSigned-off-by: default avatarYosry Ahmed <yosryahmed@google.com>
      Tested-by: default avatarDomenico Cerasuolo <cerasuolodomenico@gmail.com>
      Acked-by: default avatarShakeel Butt <shakeelb@google.com>
      Acked-by: Chris Li <chrisl@kernel.org> (Google)
      Tested-by: default avatarBagas Sanjaya <bagasdotme@gmail.com>
      Cc: Greg Thelen <gthelen@google.com>
      Cc: Ivan Babrou <ivan@cloudflare.com>
      Cc: Johannes Weiner <hannes@cmpxchg.org>
      Cc: Michal Hocko <mhocko@kernel.org>
      Cc: Michal Koutny <mkoutny@suse.com>
      Cc: Muchun Song <muchun.song@linux.dev>
      Cc: Roman Gushchin <roman.gushchin@linux.dev>
      Cc: Tejun Heo <tj@kernel.org>
      Cc: Waiman Long <longman@redhat.com>
      Cc: Wei Xu <weixugc@google.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      508bed88
    • Andrew Morton's avatar
      mm/list_lru.c: remove unused list_lru_from_kmem() · 4a3bfbd1
      Andrew Morton authored
      Fixes: 0a97c01c ("list_lru: allow explicit memcg and NUMA node selection)
      Reported-by: default avatarkernel test robot <lkp@intel.com>
      Closes: https://lore.kernel.org/oe-kbuild-all/202312141318.q8b5yrAq-lkp@intel.com/
      Cc: Nhat Pham <nphamcs@gmail.com>
      Cc: Johannes Weiner <hannes@cmpxchg.org>
      Cc: Johannes Weiner <hannes@cmpxchg.org>
      Cc: Bagas Sanjaya <bagasdotme@gmail.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      4a3bfbd1
    • Andrew Morton's avatar
      lib/maple_tree.c: fix build error due to hotfix alteration · 5143eecd
      Andrew Morton authored
      Commit 0de56e38 ("maple_tree: use maple state end for write
      operations") was broken by a later patch "maple_tree: do not preallocate
      nodes for slot stores".  But the later patch was scheduled ahead of
      0de56e38, for 6.7-rc.
      
      This fixlet undoes the damage.
      
      Fixes: 0de56e38 ("maple_tree: use maple state end for write operations")
      Cc: Liam R. Howlett <Liam.Howlett@oracle.com>
      Cc: Sidhartha Kumar <sidhartha.kumar@oracle.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      5143eecd
    • Andrew Morton's avatar
    • Matthew Wilcox (Oracle)'s avatar
      mailmap: add an old address for Naoya Horiguchi · 1803d0c5
      Matthew Wilcox (Oracle) authored
      This address now bounces, remap it to a current address.
      
      Link: https://lkml.kernel.org/r/20231218140328.3313474-1-willy@infradead.orgSigned-off-by: default avatarMatthew Wilcox (Oracle) <willy@infradead.org>
      Cc: Dan Williams <dan.j.williams@intel.com>
      Cc: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      1803d0c5
    • Matthew Wilcox (Oracle)'s avatar
      mm/memory-failure: cast index to loff_t before shifting it · 39ebd6dc
      Matthew Wilcox (Oracle) authored
      On 32-bit systems, we'll lose the top bits of index because arithmetic
      will be performed in unsigned long instead of unsigned long long.  This
      affects files over 4GB in size.
      
      Link: https://lkml.kernel.org/r/20231218135837.3310403-4-willy@infradead.org
      Fixes: 6100e34b ("mm, memory_failure: Teach memory_failure() about dev_pagemap pages")
      Signed-off-by: default avatarMatthew Wilcox (Oracle) <willy@infradead.org>
      Cc: Dan Williams <dan.j.williams@intel.com>
      Cc: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
      Cc: <stable@vger.kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      39ebd6dc
    • Matthew Wilcox (Oracle)'s avatar
      mm/memory-failure: check the mapcount of the precise page · c79c5a0a
      Matthew Wilcox (Oracle) authored
      A process may map only some of the pages in a folio, and might be missed
      if it maps the poisoned page but not the head page.  Or it might be
      unnecessarily hit if it maps the head page, but not the poisoned page.
      
      Link: https://lkml.kernel.org/r/20231218135837.3310403-3-willy@infradead.org
      Fixes: 7af446a8 ("HWPOISON, hugetlb: enable error handling path for hugepage")
      Signed-off-by: default avatarMatthew Wilcox (Oracle) <willy@infradead.org>
      Cc: Dan Williams <dan.j.williams@intel.com>
      Cc: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
      Cc: <stable@vger.kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      c79c5a0a
    • Matthew Wilcox (Oracle)'s avatar
      mm/memory-failure: pass the folio and the page to collect_procs() · 376907f3
      Matthew Wilcox (Oracle) authored
      Patch series "Three memory-failure fixes".
      
      I've been looking at the memory-failure code and I believe I have found
      three bugs that need fixing -- one going all the way back to 2010!  I'll
      have more patches later to use folios more extensively but didn't want
      these bugfixes to get caught up in that.
      
      
      This patch (of 3):
      
      Both collect_procs_anon() and collect_procs_file() iterate over the VMA
      interval trees looking for a single pgoff, so it is wrong to look for the
      pgoff of the head page as is currently done.  However, it is also wrong to
      look at page->mapping of the precise page as this is invalid for tail
      pages.  Clear up the confusion by passing both the folio and the precise
      page to collect_procs().
      
      Link: https://lkml.kernel.org/r/20231218135837.3310403-1-willy@infradead.org
      Link: https://lkml.kernel.org/r/20231218135837.3310403-2-willy@infradead.org
      Fixes: 415c64c1 ("mm/memory-failure: split thp earlier in memory error handling")
      Signed-off-by: default avatarMatthew Wilcox (Oracle) <willy@infradead.org>
      Cc: Dan Williams <dan.j.williams@intel.com>
      Cc: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
      Cc: <stable@vger.kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      376907f3
    • Muhammad Usama Anjum's avatar
      selftests: secretmem: floor the memory size to the multiple of page_size · 0aac13ad
      Muhammad Usama Anjum authored
      The "locked-in-memory size" limit per process can be non-multiple of
      page_size.  The mmap() fails if we try to allocate locked-in-memory with
      same size as the allowed limit if it isn't multiple of the page_size
      because mmap() rounds off the memory size to be allocated to next multiple
      of page_size.
      
      Fix this by flooring the length to be allocated with mmap() to the
      previous multiple of the page_size.
      
      This was getting triggered on KernelCI regularly because of different
      ulimit settings which wasn't multiple of the page_size.  Find logs
      here: https://linux.kernelci.org/test/plan/id/657654bd8e81e654fae13532/
      The bug in was present from the time test was first added.
      
      Link: https://lkml.kernel.org/r/20231214101931.1155586-1-usama.anjum@collabora.com
      Fixes: 76fe17ef ("secretmem: test: add basic selftest for memfd_secret(2)")
      Signed-off-by: default avatarMuhammad Usama Anjum <usama.anjum@collabora.com>
      Reported-by: default avatar"kernelci.org bot" <bot@kernelci.org>
      Closes: https://linux.kernelci.org/test/plan/id/657654bd8e81e654fae13532/
      Cc: "James E.J. Bottomley" <James.Bottomley@HansenPartnership.com>
      Cc: Mike Rapoport (IBM) <rppt@kernel.org>
      Cc: Shuah Khan <shuah@kernel.org>
      Cc: <stable@vger.kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      0aac13ad
    • Charan Teja Kalla's avatar
      mm: migrate high-order folios in swap cache correctly · fc346d0a
      Charan Teja Kalla authored
      Large folios occupy N consecutive entries in the swap cache instead of
      using multi-index entries like the page cache.  However, if a large folio
      is re-added to the LRU list, it can be migrated.  The migration code was
      not aware of the difference between the swap cache and the page cache and
      assumed that a single xas_store() would be sufficient.
      
      This leaves potentially many stale pointers to the now-migrated folio in
      the swap cache, which can lead to almost arbitrary data corruption in the
      future.  This can also manifest as infinite loops with the RCU read lock
      held.
      
      [willy@infradead.org: modifications to the changelog & tweaked the fix]
      Fixes: 3417013e ("mm/migrate: Add folio_migrate_mapping()")
      Link: https://lkml.kernel.org/r/20231214045841.961776-1-willy@infradead.orgSigned-off-by: default avatarCharan Teja Kalla <quic_charante@quicinc.com>
      Signed-off-by: default avatarMatthew Wilcox (Oracle) <willy@infradead.org>
      Reported-by: default avatarCharan Teja Kalla <quic_charante@quicinc.com>
      Closes: https://lkml.kernel.org/r/1700569840-17327-1-git-send-email-quic_charante@quicinc.com
      Cc: David Hildenbrand <david@redhat.com>
      Cc: Johannes Weiner <hannes@cmpxchg.org>
      Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
      Cc: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
      Cc: Shakeel Butt <shakeelb@google.com>
      Cc: <stable@vger.kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      fc346d0a
    • Sidhartha Kumar's avatar
      maple_tree: do not preallocate nodes for slot stores · 4249f13c
      Sidhartha Kumar authored
      mas_preallocate() defaults to requesting 1 node for preallocation and then
      ,depending on the type of store, will update the request variable.  There
      isn't a check for a slot store type, so slot stores are preallocating the
      default 1 node.  Slot stores do not require any additional nodes, so add a
      check for the slot store case that will bypass node_count_gfp().  Update
      the tests to reflect that slot stores do not require allocations.
      
      User visible effects of this bug include increased memory usage from the
      unneeded node that was allocated.
      
      Link: https://lkml.kernel.org/r/20231213205058.386589-1-sidhartha.kumar@oracle.com
      Fixes: 0b8bb544 ("maple_tree: update mas_preallocate() testing")
      Signed-off-by: default avatarSidhartha Kumar <sidhartha.kumar@oracle.com>
      Cc: Liam R. Howlett <Liam.Howlett@oracle.com>
      Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
      Cc: Peng Zhang <zhangpeng.00@bytedance.com>
      Cc: <stable@vger.kernel.org>	[6.6+]
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      4249f13c
    • Baokun Li's avatar
      mm/filemap: avoid buffered read/write race to read inconsistent data · e2c27b80
      Baokun Li authored
      The following concurrency may cause the data read to be inconsistent with
      the data on disk:
      
                   cpu1                           cpu2
      ------------------------------|------------------------------
                                     // Buffered write 2048 from 0
                                     ext4_buffered_write_iter
                                      generic_perform_write
                                       copy_page_from_iter_atomic
                                       ext4_da_write_end
                                        ext4_da_do_write_end
                                         block_write_end
                                          __block_commit_write
                                           folio_mark_uptodate
      // Buffered read 4096 from 0          smp_wmb()
      ext4_file_read_iter                   set_bit(PG_uptodate, folio_flags)
       generic_file_read_iter            i_size_write // 2048
        filemap_read                     unlock_page(page)
         filemap_get_pages
          filemap_get_read_batch
          folio_test_uptodate(folio)
           ret = test_bit(PG_uptodate, folio_flags)
           if (ret)
            smp_rmb();
            // Ensure that the data in page 0-2048 is up-to-date.
      
                                     // New buffered write 2048 from 2048
                                     ext4_buffered_write_iter
                                      generic_perform_write
                                       copy_page_from_iter_atomic
                                       ext4_da_write_end
                                        ext4_da_do_write_end
                                         block_write_end
                                          __block_commit_write
                                           folio_mark_uptodate
                                            smp_wmb()
                                            set_bit(PG_uptodate, folio_flags)
                                         i_size_write // 4096
                                         unlock_page(page)
      
         isize = i_size_read(inode) // 4096
         // Read the latest isize 4096, but without smp_rmb(), there may be
         // Load-Load disorder resulting in the data in the 2048-4096 range
         // in the page is not up-to-date.
         copy_page_to_iter
         // copyout 4096
      
      In the concurrency above, we read the updated i_size, but there is no read
      barrier to ensure that the data in the page is the same as the i_size at
      this point, so we may copy the unsynchronized page out.  Hence adding the
      missing read memory barrier to fix this.
      
      This is a Load-Load reordering issue, which only occurs on some weak
      mem-ordering architectures (e.g.  ARM64, ALPHA), but not on strong
      mem-ordering architectures (e.g.  X86).  And theoretically the problem
      doesn't only happen on ext4, filesystems that call filemap_read() but
      don't hold inode lock (e.g.  btrfs, f2fs, ubifs ...) will have this
      problem, while filesystems with inode lock (e.g.  xfs, nfs) won't have
      this problem.
      
      Link: https://lkml.kernel.org/r/20231213062324.739009-1-libaokun1@huawei.comSigned-off-by: default avatarBaokun Li <libaokun1@huawei.com>
      Reviewed-by: default avatarJan Kara <jack@suse.cz>
      Cc: Andreas Dilger <adilger.kernel@dilger.ca>
      Cc: Christoph Hellwig <hch@infradead.org>
      Cc: Dave Chinner <david@fromorbit.com>
      Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
      Cc: Ritesh Harjani (IBM) <ritesh.list@gmail.com>
      Cc: Theodore Ts'o <tytso@mit.edu>
      Cc: yangerkun <yangerkun@huawei.com>
      Cc: Yu Kuai <yukuai3@huawei.com>
      Cc: Zhang Yi <yi.zhang@huawei.com>
      Cc: <stable@vger.kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      e2c27b80
    • Nico Pache's avatar
      kunit: kasan_test: disable fortify string checker on kmalloc_oob_memset · b2325bf8
      Nico Pache authored
      Similar to commit 09c6304e ("kasan: test: fix compatibility with
      FORTIFY_SOURCE") the kernel is panicing in kmalloc_oob_memset_*.
      
      This is due to the `ptr` not being hidden from the optimizer which would
      disable the runtime fortify string checker.
      
      kernel BUG at lib/string_helpers.c:1048!
      Call Trace:
      [<00000000272502e2>] fortify_panic+0x2a/0x30
      ([<00000000272502de>] fortify_panic+0x26/0x30)
      [<001bffff817045c4>] kmalloc_oob_memset_2+0x22c/0x230 [kasan_test]
      
      Hide the `ptr` variable from the optimizer to fix the kernel panic.  Also
      define a memset_size variable and hide that as well.  This cleans up the
      code and follows the same convention as other tests.
      
      [npache@redhat.com: address review comments from Andrey]
        Link: https://lkml.kernel.org/r/20231214164423.6202-1-npache@redhat.com
      Link: https://lkml.kernel.org/r/20231212232659.18839-1-npache@redhat.comSigned-off-by: default avatarNico Pache <npache@redhat.com>
      Reviewed-by: default avatarAndrey Konovalov <andreyknvl@gmail.com>
      Cc: Alexander Potapenko <glider@google.com>
      Cc: Andrey Konovalov <andreyknvl@gmail.com>
      Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com>
      Cc: Dmitry Vyukov <dvyukov@google.com>
      Cc: Vincenzo Frascino <vincenzo.frascino@arm.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      b2325bf8
    • Arnd Bergmann's avatar
      kexec: select CRYPTO from KEXEC_FILE instead of depending on it · e63bde3d
      Arnd Bergmann authored
      All other users of crypto code use 'select' instead of 'depends on', so do
      the same thing with KEXEC_FILE for consistency.
      
      In practice this makes very little difference as kernels with kexec
      support are very likely to also include some other feature that already
      selects both crypto and crypto_sha256, but being consistent here helps for
      usability as well as to avoid potential circular dependencies.
      
      This reverts the dependency back to what it was originally before commit
      74ca317c ("kexec: create a new config option CONFIG_KEXEC_FILE for
      new syscall"), which changed changed it with the comment "This should be
      safer as "select" is not recursive", but that appears to have been done in
      error, as "select" is indeed recursive, and there are no other
      dependencies that prevent CRYPTO_SHA256 from being selected here.
      
      Link: https://lkml.kernel.org/r/20231023110308.1202042-2-arnd@kernel.org
      Fixes: 74ca317c ("kexec: create a new config option CONFIG_KEXEC_FILE for new syscall")
      Signed-off-by: default avatarArnd Bergmann <arnd@arndb.de>
      Reviewed-by: default avatarEric DeVolder <eric_devolder@yahoo.com>
      Tested-by: default avatarEric DeVolder <eric_devolder@yahoo.com>
      Acked-by: default avatarBaoquan He <bhe@redhat.com>
      Cc: Herbert Xu <herbert@gondor.apana.org.au>
      Cc: "David S. Miller" <davem@davemloft.net>
      Cc: Albert Ou <aou@eecs.berkeley.edu>
      Cc: Alexander Gordeev <agordeev@linux.ibm.com>
      Cc: Ard Biesheuvel <ardb@kernel.org>
      Cc: Borislav Petkov <bp@alien8.de>
      Cc: Christian Borntraeger <borntraeger@linux.ibm.com>
      Cc: Christophe Leroy <christophe.leroy@csgroup.eu>
      Cc: Conor Dooley <conor@kernel.org>
      Cc: Dave Hansen <dave.hansen@linux.intel.com>
      Cc: Heiko Carstens <hca@linux.ibm.com>
      Cc: "H. Peter Anvin" <hpa@zytor.com>
      Cc: Ingo Molnar <mingo@redhat.com>
      Cc: Nicholas Piggin <npiggin@gmail.com>
      Cc: Palmer Dabbelt <palmer@dabbelt.com>
      Cc: Paul Walmsley <paul.walmsley@sifive.com>
      Cc: Peter Zijlstra <peterz@infradead.org>
      Cc: Sven Schnelle <svens@linux.ibm.com>
      Cc: Thomas Gleixner <tglx@linutronix.de>
      Cc: Vasily Gorbik <gor@linux.ibm.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      e63bde3d
    • Arnd Bergmann's avatar
      kexec: fix KEXEC_FILE dependencies · c1ad12ee
      Arnd Bergmann authored
      The cleanup for the CONFIG_KEXEC Kconfig logic accidentally changed the
      'depends on CRYPTO=y' dependency to a plain 'depends on CRYPTO', which
      causes a link failure when all the crypto support is in a loadable module
      and kexec_file support is built-in:
      
      x86_64-linux-ld: vmlinux.o: in function `__x64_sys_kexec_file_load':
      (.text+0x32e30a): undefined reference to `crypto_alloc_shash'
      x86_64-linux-ld: (.text+0x32e58e): undefined reference to `crypto_shash_update'
      x86_64-linux-ld: (.text+0x32e6ee): undefined reference to `crypto_shash_final'
      
      Both s390 and x86 have this problem, while ppc64 and riscv have the
      correct dependency already.  On riscv, the dependency is only used for the
      purgatory, not for the kexec_file code itself, which may be a bit
      surprising as it means that with CONFIG_CRYPTO=m, it is possible to enable
      KEXEC_FILE but then the purgatory code is silently left out.
      
      Move this into the common Kconfig.kexec file in a way that is correct
      everywhere, using the dependency on CRYPTO_SHA256=y only when the
      purgatory code is available.  This requires reversing the dependency
      between ARCH_SUPPORTS_KEXEC_PURGATORY and KEXEC_FILE, but the effect
      remains the same, other than making riscv behave like the other ones.
      
      On s390, there is an additional dependency on CRYPTO_SHA256_S390, which
      should technically not be required but gives better performance.  Remove
      this dependency here, noting that it was not present in the initial
      Kconfig code but was brought in without an explanation in commit
      71406883 ("s390/kexec_file: Add kexec_file_load system call").
      
      [arnd@arndb.de: fix riscv build]
        Link: https://lkml.kernel.org/r/67ddd260-d424-4229-a815-e3fcfb864a77@app.fastmail.com
      Link: https://lkml.kernel.org/r/20231023110308.1202042-1-arnd@kernel.org
      Fixes: 6af51380 ("x86/kexec: refactor for kernel/Kconfig.kexec")
      Signed-off-by: default avatarArnd Bergmann <arnd@arndb.de>
      Reviewed-by: default avatarEric DeVolder <eric_devolder@yahoo.com>
      Tested-by: default avatarEric DeVolder <eric_devolder@yahoo.com>
      Cc: Albert Ou <aou@eecs.berkeley.edu>
      Cc: Alexander Gordeev <agordeev@linux.ibm.com>
      Cc: Ard Biesheuvel <ardb@kernel.org>
      Cc: Borislav Petkov <bp@alien8.de>
      Cc: Christian Borntraeger <borntraeger@linux.ibm.com>
      Cc: Christophe Leroy <christophe.leroy@csgroup.eu>
      Cc: Conor Dooley <conor@kernel.org>
      Cc: Dave Hansen <dave.hansen@linux.intel.com>
      Cc: David S. Miller <davem@davemloft.net>
      Cc: Heiko Carstens <hca@linux.ibm.com>
      Cc: Herbert Xu <herbert@gondor.apana.org.au>
      Cc: "H. Peter Anvin" <hpa@zytor.com>
      Cc: Ingo Molnar <mingo@redhat.com>
      Cc: Nicholas Piggin <npiggin@gmail.com>
      Cc: Palmer Dabbelt <palmer@dabbelt.com>
      Cc: Paul Walmsley <paul.walmsley@sifive.com>
      Cc: Peter Zijlstra <peterz@infradead.org>
      Cc: Sven Schnelle <svens@linux.ibm.com>
      Cc: Thomas Gleixner <tglx@linutronix.de>
      Cc: Vasily Gorbik <gor@linux.ibm.com>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      c1ad12ee
  2. 13 Dec, 2023 5 commits
    • Yu Zhao's avatar
      mm/mglru: reclaim offlined memcgs harder · 4376807b
      Yu Zhao authored
      In the effort to reduce zombie memcgs [1], it was discovered that the
      memcg LRU doesn't apply enough pressure on offlined memcgs.  Specifically,
      instead of rotating them to the tail of the current generation
      (MEMCG_LRU_TAIL) for a second attempt, it moves them to the next
      generation (MEMCG_LRU_YOUNG) after the first attempt.
      
      Not applying enough pressure on offlined memcgs can cause them to build
      up, and this can be particularly harmful to memory-constrained systems.
      
      On Pixel 8 Pro, launching apps for 50 cycles:
                       Before  After  Change
        Zombie memcgs  45      35     -22%
      
      [1] https://lore.kernel.org/CABdmKX2M6koq4Q0Cmp_-=wbP0Qa190HdEGGaHfxNS05gAkUtPA@mail.gmail.com/
      
      Link: https://lkml.kernel.org/r/20231208061407.2125867-4-yuzhao@google.com
      Fixes: e4dde56c ("mm: multi-gen LRU: per-node lru_gen_folio lists")
      Signed-off-by: default avatarYu Zhao <yuzhao@google.com>
      Reported-by: default avatarT.J. Mercier <tjmercier@google.com>
      Tested-by: default avatarT.J. Mercier <tjmercier@google.com>
      Cc: Charan Teja Kalla <quic_charante@quicinc.com>
      Cc: Hillf Danton <hdanton@sina.com>
      Cc: Jaroslav Pulchart <jaroslav.pulchart@gooddata.com>
      Cc: Kairui Song <ryncsn@gmail.com>
      Cc: Kalesh Singh <kaleshsingh@google.com>
      Cc: <stable@vger.kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      4376807b
    • Yu Zhao's avatar
      mm/mglru: respect min_ttl_ms with memcgs · 8aa42061
      Yu Zhao authored
      While investigating kswapd "consuming 100% CPU" [1] (also see "mm/mglru:
      try to stop at high watermarks"), it was discovered that the memcg LRU can
      breach the thrashing protection imposed by min_ttl_ms.
      
      Before the memcg LRU:
        kswapd()
          shrink_node_memcgs()
            mem_cgroup_iter()
              inc_max_seq()  // always hit a different memcg
          lru_gen_age_node()
            mem_cgroup_iter()
              check the timestamp of the oldest generation
      
      After the memcg LRU:
        kswapd()
          shrink_many()
            restart:
              iterate the memcg LRU:
                inc_max_seq()  // occasionally hit the same memcg
                if raced with lru_gen_rotate_memcg():
                  goto restart
          lru_gen_age_node()
            mem_cgroup_iter()
              check the timestamp of the oldest generation
      
      Specifically, when the restart happens in shrink_many(), it needs to stick
      with the (memcg LRU) generation it began with.  In other words, it should
      neither re-read memcg_lru->seq nor age an lruvec of a different
      generation.  Otherwise it can hit the same memcg multiple times without
      giving lru_gen_age_node() a chance to check the timestamp of that memcg's
      oldest generation (against min_ttl_ms).
      
      [1] https://lore.kernel.org/CAK8fFZ4DY+GtBA40Pm7Nn5xCHy+51w3sfxPqkqpqakSXYyX+Wg@mail.gmail.com/
      
      Link: https://lkml.kernel.org/r/20231208061407.2125867-3-yuzhao@google.com
      Fixes: e4dde56c ("mm: multi-gen LRU: per-node lru_gen_folio lists")
      Signed-off-by: default avatarYu Zhao <yuzhao@google.com>
      Tested-by: default avatarT.J. Mercier <tjmercier@google.com>
      Cc: Charan Teja Kalla <quic_charante@quicinc.com>
      Cc: Hillf Danton <hdanton@sina.com>
      Cc: Jaroslav Pulchart <jaroslav.pulchart@gooddata.com>
      Cc: Kairui Song <ryncsn@gmail.com>
      Cc: Kalesh Singh <kaleshsingh@google.com>
      Cc: <stable@vger.kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      8aa42061
    • Yu Zhao's avatar
      mm/mglru: try to stop at high watermarks · 5095a2b2
      Yu Zhao authored
      The initial MGLRU patchset didn't include the memcg LRU support, and it
      relied on should_abort_scan(), added by commit f76c8337 ("mm:
      multi-gen LRU: optimize multiple memcgs"), to "backoff to avoid
      overshooting their aggregate reclaim target by too much".
      
      Later on when the memcg LRU was added, should_abort_scan() was deemed
      unnecessary, and the test results [1] showed no side effects after it was
      removed by commit a579086c ("mm: multi-gen LRU: remove eviction
      fairness safeguard").
      
      However, that test used memory.reclaim, which sets nr_to_reclaim to
      SWAP_CLUSTER_MAX.  So it can overshoot only by SWAP_CLUSTER_MAX-1 pages,
      i.e., from nr_reclaimed=nr_to_reclaim-1 to
      nr_reclaimed=nr_to_reclaim+SWAP_CLUSTER_MAX-1.  Compared with the batch
      size kswapd sets to nr_to_reclaim, SWAP_CLUSTER_MAX is tiny.  Therefore
      that test isn't able to reproduce the worst case scenario, i.e., kswapd
      overshooting GBs on large systems and "consuming 100% CPU" (see the Closes
      tag).
      
      Bring back a simplified version of should_abort_scan() on top of the memcg
      LRU, so that kswapd stops when all eligible zones are above their
      respective high watermarks plus a small delta to lower the chance of
      KSWAPD_HIGH_WMARK_HIT_QUICKLY.  Note that this only applies to order-0
      reclaim, meaning compaction-induced reclaim can still run wild (which is a
      different problem).
      
      On Android, launching 55 apps sequentially:
                 Before     After      Change
        pgpgin   838377172  802955040  -4%
        pgpgout  38037080   34336300   -10%
      
      [1] https://lore.kernel.org/20221222041905.2431096-1-yuzhao@google.com/
      
      Link: https://lkml.kernel.org/r/20231208061407.2125867-2-yuzhao@google.com
      Fixes: a579086c ("mm: multi-gen LRU: remove eviction fairness safeguard")
      Signed-off-by: default avatarYu Zhao <yuzhao@google.com>
      Reported-by: default avatarCharan Teja Kalla <quic_charante@quicinc.com>
      Reported-by: default avatarJaroslav Pulchart <jaroslav.pulchart@gooddata.com>
      Closes: https://lore.kernel.org/CAK8fFZ4DY+GtBA40Pm7Nn5xCHy+51w3sfxPqkqpqakSXYyX+Wg@mail.gmail.com/Tested-by: default avatarJaroslav Pulchart <jaroslav.pulchart@gooddata.com>
      Tested-by: default avatarKalesh Singh <kaleshsingh@google.com>
      Cc: Hillf Danton <hdanton@sina.com>
      Cc: Kairui Song <ryncsn@gmail.com>
      Cc: T.J. Mercier <tjmercier@google.com>
      Cc: <stable@vger.kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      5095a2b2
    • Yu Zhao's avatar
      mm/mglru: fix underprotected page cache · 08148805
      Yu Zhao authored
      Unmapped folios accessed through file descriptors can be underprotected. 
      Those folios are added to the oldest generation based on:
      
      1. The fact that they are less costly to reclaim (no need to walk the
         rmap and flush the TLB) and have less impact on performance (don't
         cause major PFs and can be non-blocking if needed again).
      2. The observation that they are likely to be single-use. E.g., for
         client use cases like Android, its apps parse configuration files
         and store the data in heap (anon); for server use cases like MySQL,
         it reads from InnoDB files and holds the cached data for tables in
         buffer pools (anon).
      
      However, the oldest generation can be very short lived, and if so, it
      doesn't provide the PID controller with enough time to respond to a surge
      of refaults.  (Note that the PID controller uses weighted refaults and
      those from evicted generations only take a half of the whole weight.) In
      other words, for a short lived generation, the moving average smooths out
      the spike quickly.
      
      To fix the problem:
      1. For folios that are already on LRU, if they can be beyond the
         tracking range of tiers, i.e., five accesses through file
         descriptors, move them to the second oldest generation to give them
         more time to age. (Note that tiers are used by the PID controller
         to statistically determine whether folios accessed multiple times
         through file descriptors are worth protecting.)
      2. When adding unmapped folios to LRU, adjust the placement of them so
         that they are not too close to the tail. The effect of this is
         similar to the above.
      
      On Android, launching 55 apps sequentially:
                                 Before     After      Change
        workingset_refault_anon  25641024   25598972   0%
        workingset_refault_file  115016834  106178438  -8%
      
      Link: https://lkml.kernel.org/r/20231208061407.2125867-1-yuzhao@google.com
      Fixes: ac35a490 ("mm: multi-gen LRU: minimal implementation")
      Signed-off-by: default avatarYu Zhao <yuzhao@google.com>
      Reported-by: default avatarCharan Teja Kalla <quic_charante@quicinc.com>
      Tested-by: default avatarKalesh Singh <kaleshsingh@google.com>
      Cc: T.J. Mercier <tjmercier@google.com>
      Cc: Kairui Song <ryncsn@gmail.com>
      Cc: Hillf Danton <hdanton@sina.com>
      Cc: Jaroslav Pulchart <jaroslav.pulchart@gooddata.com>
      Cc: <stable@vger.kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      08148805
    • David Stevens's avatar
      mm/shmem: fix race in shmem_undo_range w/THP · 55ac8bbe
      David Stevens authored
      Split folios during the second loop of shmem_undo_range.  It's not
      sufficient to only split folios when dealing with partial pages, since
      it's possible for a THP to be faulted in after that point.  Calling
      truncate_inode_folio in that situation can result in throwing away data
      outside of the range being targeted.
      
      [akpm@linux-foundation.org: tidy up comment layout]
      Link: https://lkml.kernel.org/r/20230418084031.3439795-1-stevensd@google.com
      Fixes: b9a8a419 ("truncate,shmem: Handle truncates that split large folios")
      Signed-off-by: default avatarDavid Stevens <stevensd@chromium.org>
      Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
      Cc: Suleiman Souhlal <suleiman@google.com>
      Cc: <stable@vger.kernel.org>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      55ac8bbe