mm-commits.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* + mm-hugetlb-add-mempolicy-check-in-the-reservation-routine.patch added to -mm tree
@ 2020-08-07  1:23 akpm
  0 siblings, 0 replies; 2+ messages in thread
From: akpm @ 2020-08-07  1:23 UTC (permalink / raw)
  To: bhe, guojianchao, mgorman, mhocko, mike.kravetz, mm-commits,
	rientjes, songmuchun, walken


The patch titled
     Subject: mm/hugetlb: add mempolicy check in the reservation routine
has been added to the -mm tree.  Its filename is
     mm-hugetlb-add-mempolicy-check-in-the-reservation-routine.patch

This patch should soon appear at
    http://ozlabs.org/~akpm/mmots/broken-out/mm-hugetlb-add-mempolicy-check-in-the-reservation-routine.patch
and later at
    http://ozlabs.org/~akpm/mmotm/broken-out/mm-hugetlb-add-mempolicy-check-in-the-reservation-routine.patch

Before you just go and hit "reply", please:
   a) Consider who else should be cc'ed
   b) Prefer to cc a suitable mailing list as well
   c) Ideally: find the original patch on the mailing list and do a
      reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***

The -mm tree is included into linux-next and is updated
there every 3-4 working days

------------------------------------------------------
From: Muchun Song <songmuchun@bytedance.com>
Subject: mm/hugetlb: add mempolicy check in the reservation routine

In the reservation routine, we only check whether the cpuset meets the
memory allocation requirements.  But we ignore the mempolicy of MPOL_BIND
case.  If someone mmap hugetlb succeeds, but the subsequent memory
allocation may fail due to mempolicy restrictions and receives the SIGBUS
signal.  This can be reproduced by the follow steps.

 1) Compile the test case.
    cd tools/testing/selftests/vm/
    gcc map_hugetlb.c -o map_hugetlb

 2) Pre-allocate huge pages. Suppose there are 2 numa nodes in the
    system. Each node will pre-allocate one huge page.
    echo 2 > /proc/sys/vm/nr_hugepages

 3) Run test case(mmap 4MB). We receive the SIGBUS signal.
    numactl --membind=3D0 ./map_hugetlb 4

With this patch applied, the mmap will fail in the step 3) and throw
"mmap: Cannot allocate memory".

Link: http://lkml.kernel.org/r/20200728034938.14993-1-songmuchun@bytedance.com
Signed-off-by: Muchun Song <songmuchun@bytedance.com>
Reported-by: Jianchao Guo <guojianchao@bytedance.com>
Suggested-by: Michal Hocko <mhocko@kernel.org>
Reviewed-by: Mike Kravetz <mike.kravetz@oracle.com>
Cc: David Rientjes <rientjes@google.com>
Cc: Mel Gorman <mgorman@suse.de>
Cc: Michel Lespinasse <walken@google.com>
Cc: Baoquan He <bhe@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
---

 include/linux/mempolicy.h |   14 ++++++++++++++
 mm/hugetlb.c              |   24 +++++++++++++++++++-----
 mm/mempolicy.c            |    2 +-
 3 files changed, 34 insertions(+), 6 deletions(-)

--- a/include/linux/mempolicy.h~mm-hugetlb-add-mempolicy-check-in-the-reservation-routine
+++ a/include/linux/mempolicy.h
@@ -152,6 +152,15 @@ extern int huge_node(struct vm_area_stru
 extern bool init_nodemask_of_mempolicy(nodemask_t *mask);
 extern bool mempolicy_nodemask_intersects(struct task_struct *tsk,
 				const nodemask_t *mask);
+extern nodemask_t *policy_nodemask(gfp_t gfp, struct mempolicy *policy);
+
+static inline nodemask_t *policy_nodemask_current(gfp_t gfp)
+{
+	struct mempolicy *mpol = get_task_policy(current);
+
+	return policy_nodemask(gfp, mpol);
+}
+
 extern unsigned int mempolicy_slab_node(void);
 
 extern enum zone_type policy_zone;
@@ -281,5 +290,10 @@ static inline int mpol_misplaced(struct
 static inline void mpol_put_task_policy(struct task_struct *task)
 {
 }
+
+static inline nodemask_t *policy_nodemask_current(gfp_t gfp)
+{
+	return NULL;
+}
 #endif /* CONFIG_NUMA */
 #endif
--- a/mm/hugetlb.c~mm-hugetlb-add-mempolicy-check-in-the-reservation-routine
+++ a/mm/hugetlb.c
@@ -3458,13 +3458,21 @@ static int __init default_hugepagesz_set
 }
 __setup("default_hugepagesz=", default_hugepagesz_setup);
 
-static unsigned int cpuset_mems_nr(unsigned int *array)
+static unsigned int allowed_mems_nr(struct hstate *h)
 {
 	int node;
 	unsigned int nr = 0;
-
-	for_each_node_mask(node, cpuset_current_mems_allowed)
-		nr += array[node];
+	nodemask_t *mpol_allowed;
+	unsigned int *array = h->free_huge_pages_node;
+	gfp_t gfp_mask = htlb_alloc_mask(h);
+
+	mpol_allowed = policy_nodemask_current(gfp_mask);
+
+	for_each_node_mask(node, cpuset_current_mems_allowed) {
+		if (!mpol_allowed ||
+		    (mpol_allowed && node_isset(node, *mpol_allowed)))
+			nr += array[node];
+	}
 
 	return nr;
 }
@@ -3643,12 +3651,18 @@ static int hugetlb_acct_memory(struct hs
 	 * we fall back to check against current free page availability as
 	 * a best attempt and hopefully to minimize the impact of changing
 	 * semantics that cpuset has.
+	 *
+	 * Apart from cpuset, we also have memory policy mechanism that
+	 * also determines from which node the kernel will allocate memory
+	 * in a NUMA system. So similar to cpuset, we also should consider
+	 * the memory policy of the current task. Similar to the description
+	 * above.
 	 */
 	if (delta > 0) {
 		if (gather_surplus_pages(h, delta) < 0)
 			goto out;
 
-		if (delta > cpuset_mems_nr(h->free_huge_pages_node)) {
+		if (delta > allowed_mems_nr(h)) {
 			return_unused_surplus_pages(h, delta);
 			goto out;
 		}
--- a/mm/mempolicy.c~mm-hugetlb-add-mempolicy-check-in-the-reservation-routine
+++ a/mm/mempolicy.c
@@ -1890,7 +1890,7 @@ static int apply_policy_zone(struct memp
  * Return a nodemask representing a mempolicy for filtering nodes for
  * page allocation
  */
-static nodemask_t *policy_nodemask(gfp_t gfp, struct mempolicy *policy)
+nodemask_t *policy_nodemask(gfp_t gfp, struct mempolicy *policy)
 {
 	/* Lower zones don't get a nodemask applied for MPOL_BIND */
 	if (unlikely(policy->mode == MPOL_BIND) &&
_

Patches currently in -mm which might be from songmuchun@bytedance.com are

mm-page_alloc-skip-setting-nodemask-when-we-are-in-interrupt.patch
mm-hugetlb-add-mempolicy-check-in-the-reservation-routine.patch
kprobes-fix-compiler-warning-for-config_kprobes_on_ftrace.patch


^ permalink raw reply	[flat|nested] 2+ messages in thread
* incoming
@ 2020-07-24  4:14 Andrew Morton
  2020-07-27 20:51 ` + mm-hugetlb-add-mempolicy-check-in-the-reservation-routine.patch added to -mm tree Andrew Morton
  0 siblings, 1 reply; 2+ messages in thread
From: Andrew Morton @ 2020-07-24  4:14 UTC (permalink / raw)
  To: Linus Torvalds; +Cc: mm-commits, linux-mm


15 patches, based on f37e99aca03f63aa3f2bd13ceaf769455d12c4b0.

Subsystems affected by this patch series:

  mm/pagemap
  mm/shmem
  mm/hotfixes
  mm/memcg
  mm/hugetlb
  mailmap
  squashfs
  scripts
  io-mapping
  MAINTAINERS
  gdb

Subsystem: mm/pagemap

    Yang Shi <yang.shi@linux.alibaba.com>:
      mm/memory.c: avoid access flag update TLB flush for retried page fault

    "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>:
      mm/mmap.c: close race between munmap() and expand_upwards()/downwards()

Subsystem: mm/shmem

    Chengguang Xu <cgxu519@mykernel.net>:
      vfs/xattr: mm/shmem: kernfs: release simple xattr entry in a right way

Subsystem: mm/hotfixes

    Tom Rix <trix@redhat.com>:
      mm: initialize return of vm_insert_pages

    Bhupesh Sharma <bhsharma@redhat.com>:
      mm/memcontrol: fix OOPS inside mem_cgroup_get_nr_swap_pages()

Subsystem: mm/memcg

    Hugh Dickins <hughd@google.com>:
      mm/memcg: fix refcount error while moving and swapping

    Muchun Song <songmuchun@bytedance.com>:
      mm: memcg/slab: fix memory leak at non-root kmem_cache destroy

Subsystem: mm/hugetlb

    Barry Song <song.bao.hua@hisilicon.com>:
      mm/hugetlb: avoid hardcoding while checking if cma is enabled

    "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>:
      khugepaged: fix null-pointer dereference due to race

Subsystem: mailmap

    Mike Rapoport <rppt@linux.ibm.com>:
      mailmap: add entry for Mike Rapoport

Subsystem: squashfs

    Phillip Lougher <phillip@squashfs.org.uk>:
      squashfs: fix length field overlap check in metadata reading

Subsystem: scripts

    Pi-Hsun Shih <pihsun@chromium.org>:
      scripts/decode_stacktrace: strip basepath from all paths

Subsystem: io-mapping

    "Michael J. Ruhl" <michael.j.ruhl@intel.com>:
      io-mapping: indicate mapping failure

Subsystem: MAINTAINERS

    Andrey Konovalov <andreyknvl@google.com>:
      MAINTAINERS: add KCOV section

Subsystem: gdb

    Stefano Garzarella <sgarzare@redhat.com>:
      scripts/gdb: fix lx-symbols 'gdb.error' while loading modules

 .mailmap                     |    3 +++
 MAINTAINERS                  |   11 +++++++++++
 fs/squashfs/block.c          |    2 +-
 include/linux/io-mapping.h   |    5 ++++-
 include/linux/xattr.h        |    3 ++-
 mm/hugetlb.c                 |   15 ++++++++++-----
 mm/khugepaged.c              |    3 +++
 mm/memcontrol.c              |   13 ++++++++++---
 mm/memory.c                  |    9 +++++++--
 mm/mmap.c                    |   16 ++++++++++++++--
 mm/shmem.c                   |    2 +-
 mm/slab_common.c             |   35 ++++++++++++++++++++++++++++-------
 scripts/decode_stacktrace.sh |    4 ++--
 scripts/gdb/linux/symbols.py |    2 +-
 14 files changed, 97 insertions(+), 26 deletions(-)


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-08-07  1:23 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-07  1:23 + mm-hugetlb-add-mempolicy-check-in-the-reservation-routine.patch added to -mm tree akpm
  -- strict thread matches above, loose matches on Subject: below --
2020-07-24  4:14 incoming Andrew Morton
2020-07-27 20:51 ` + mm-hugetlb-add-mempolicy-check-in-the-reservation-routine.patch added to -mm tree Andrew Morton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).