Skip to content

Commit 8ca39e6

Browse files
Muchun Songtorvalds
authored andcommitted
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". [[email protected]: include sched.h for `current'] Reported-by: Jianchao Guo <[email protected]> Suggested-by: Michal Hocko <[email protected]> Signed-off-by: Muchun Song <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Reviewed-by: Mike Kravetz <[email protected]> Cc: David Rientjes <[email protected]> Cc: Mel Gorman <[email protected]> Cc: Michel Lespinasse <[email protected]> Cc: Baoquan He <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Linus Torvalds <[email protected]>
1 parent 90631e1 commit 8ca39e6

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

include/linux/mempolicy.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#ifndef _LINUX_MEMPOLICY_H
77
#define _LINUX_MEMPOLICY_H 1
88

9-
9+
#include <linux/sched.h>
1010
#include <linux/mmzone.h>
1111
#include <linux/dax.h>
1212
#include <linux/slab.h>
@@ -152,6 +152,15 @@ extern int huge_node(struct vm_area_struct *vma,
152152
extern bool init_nodemask_of_mempolicy(nodemask_t *mask);
153153
extern bool mempolicy_nodemask_intersects(struct task_struct *tsk,
154154
const nodemask_t *mask);
155+
extern nodemask_t *policy_nodemask(gfp_t gfp, struct mempolicy *policy);
156+
157+
static inline nodemask_t *policy_nodemask_current(gfp_t gfp)
158+
{
159+
struct mempolicy *mpol = get_task_policy(current);
160+
161+
return policy_nodemask(gfp, mpol);
162+
}
163+
155164
extern unsigned int mempolicy_slab_node(void);
156165

157166
extern enum zone_type policy_zone;
@@ -281,5 +290,10 @@ static inline int mpol_misplaced(struct page *page, struct vm_area_struct *vma,
281290
static inline void mpol_put_task_policy(struct task_struct *task)
282291
{
283292
}
293+
294+
static inline nodemask_t *policy_nodemask_current(gfp_t gfp)
295+
{
296+
return NULL;
297+
}
284298
#endif /* CONFIG_NUMA */
285299
#endif

mm/hugetlb.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3458,13 +3458,21 @@ static int __init default_hugepagesz_setup(char *s)
34583458
}
34593459
__setup("default_hugepagesz=", default_hugepagesz_setup);
34603460

3461-
static unsigned int cpuset_mems_nr(unsigned int *array)
3461+
static unsigned int allowed_mems_nr(struct hstate *h)
34623462
{
34633463
int node;
34643464
unsigned int nr = 0;
3465+
nodemask_t *mpol_allowed;
3466+
unsigned int *array = h->free_huge_pages_node;
3467+
gfp_t gfp_mask = htlb_alloc_mask(h);
3468+
3469+
mpol_allowed = policy_nodemask_current(gfp_mask);
34653470

3466-
for_each_node_mask(node, cpuset_current_mems_allowed)
3467-
nr += array[node];
3471+
for_each_node_mask(node, cpuset_current_mems_allowed) {
3472+
if (!mpol_allowed ||
3473+
(mpol_allowed && node_isset(node, *mpol_allowed)))
3474+
nr += array[node];
3475+
}
34683476

34693477
return nr;
34703478
}
@@ -3643,12 +3651,18 @@ static int hugetlb_acct_memory(struct hstate *h, long delta)
36433651
* we fall back to check against current free page availability as
36443652
* a best attempt and hopefully to minimize the impact of changing
36453653
* semantics that cpuset has.
3654+
*
3655+
* Apart from cpuset, we also have memory policy mechanism that
3656+
* also determines from which node the kernel will allocate memory
3657+
* in a NUMA system. So similar to cpuset, we also should consider
3658+
* the memory policy of the current task. Similar to the description
3659+
* above.
36463660
*/
36473661
if (delta > 0) {
36483662
if (gather_surplus_pages(h, delta) < 0)
36493663
goto out;
36503664

3651-
if (delta > cpuset_mems_nr(h->free_huge_pages_node)) {
3665+
if (delta > allowed_mems_nr(h)) {
36523666
return_unused_surplus_pages(h, delta);
36533667
goto out;
36543668
}

mm/mempolicy.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1890,7 +1890,7 @@ static int apply_policy_zone(struct mempolicy *policy, enum zone_type zone)
18901890
* Return a nodemask representing a mempolicy for filtering nodes for
18911891
* page allocation
18921892
*/
1893-
static nodemask_t *policy_nodemask(gfp_t gfp, struct mempolicy *policy)
1893+
nodemask_t *policy_nodemask(gfp_t gfp, struct mempolicy *policy)
18941894
{
18951895
/* Lower zones don't get a nodemask applied for MPOL_BIND */
18961896
if (unlikely(policy->mode == MPOL_BIND) &&

0 commit comments

Comments
 (0)