Commit 8ca39e68 authored by Muchun Song's avatar Muchun Song Committed by Linus Torvalds

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".

[akpm@linux-foundation.org: include sched.h for `current']
Reported-by: default avatarJianchao Guo <guojianchao@bytedance.com>
Suggested-by: default avatarMichal Hocko <mhocko@kernel.org>
Signed-off-by: default avatarMuchun Song <songmuchun@bytedance.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Reviewed-by: default avatarMike 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>
Link: http://lkml.kernel.org/r/20200728034938.14993-1-songmuchun@bytedance.comSigned-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 90631e1d
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
#ifndef _LINUX_MEMPOLICY_H #ifndef _LINUX_MEMPOLICY_H
#define _LINUX_MEMPOLICY_H 1 #define _LINUX_MEMPOLICY_H 1
#include <linux/sched.h>
#include <linux/mmzone.h> #include <linux/mmzone.h>
#include <linux/dax.h> #include <linux/dax.h>
#include <linux/slab.h> #include <linux/slab.h>
...@@ -152,6 +152,15 @@ extern int huge_node(struct vm_area_struct *vma, ...@@ -152,6 +152,15 @@ extern int huge_node(struct vm_area_struct *vma,
extern bool init_nodemask_of_mempolicy(nodemask_t *mask); extern bool init_nodemask_of_mempolicy(nodemask_t *mask);
extern bool mempolicy_nodemask_intersects(struct task_struct *tsk, extern bool mempolicy_nodemask_intersects(struct task_struct *tsk,
const nodemask_t *mask); 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 unsigned int mempolicy_slab_node(void);
extern enum zone_type policy_zone; extern enum zone_type policy_zone;
...@@ -281,5 +290,10 @@ static inline int mpol_misplaced(struct page *page, struct vm_area_struct *vma, ...@@ -281,5 +290,10 @@ static inline int mpol_misplaced(struct page *page, struct vm_area_struct *vma,
static inline void mpol_put_task_policy(struct task_struct *task) 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 /* CONFIG_NUMA */
#endif #endif
...@@ -3458,13 +3458,21 @@ static int __init default_hugepagesz_setup(char *s) ...@@ -3458,13 +3458,21 @@ static int __init default_hugepagesz_setup(char *s)
} }
__setup("default_hugepagesz=", default_hugepagesz_setup); __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; int node;
unsigned int nr = 0; unsigned int nr = 0;
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) for_each_node_mask(node, cpuset_current_mems_allowed) {
nr += array[node]; if (!mpol_allowed ||
(mpol_allowed && node_isset(node, *mpol_allowed)))
nr += array[node];
}
return nr; return nr;
} }
...@@ -3643,12 +3651,18 @@ static int hugetlb_acct_memory(struct hstate *h, long delta) ...@@ -3643,12 +3651,18 @@ static int hugetlb_acct_memory(struct hstate *h, long delta)
* we fall back to check against current free page availability as * we fall back to check against current free page availability as
* a best attempt and hopefully to minimize the impact of changing * a best attempt and hopefully to minimize the impact of changing
* semantics that cpuset has. * 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 (delta > 0) {
if (gather_surplus_pages(h, delta) < 0) if (gather_surplus_pages(h, delta) < 0)
goto out; goto out;
if (delta > cpuset_mems_nr(h->free_huge_pages_node)) { if (delta > allowed_mems_nr(h)) {
return_unused_surplus_pages(h, delta); return_unused_surplus_pages(h, delta);
goto out; goto out;
} }
......
...@@ -1890,7 +1890,7 @@ static int apply_policy_zone(struct mempolicy *policy, enum zone_type zone) ...@@ -1890,7 +1890,7 @@ static int apply_policy_zone(struct mempolicy *policy, enum zone_type zone)
* Return a nodemask representing a mempolicy for filtering nodes for * Return a nodemask representing a mempolicy for filtering nodes for
* page allocation * 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 */ /* Lower zones don't get a nodemask applied for MPOL_BIND */
if (unlikely(policy->mode == MPOL_BIND) && if (unlikely(policy->mode == MPOL_BIND) &&
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment