Skip to content

Commit 49ed8b4

Browse files
Mel Gormangregkh
Mel Gorman
authored andcommitted
tmpfs: fix shared mempolicy leak
commit 18a2f37 upstream. This fixes a regression in 3.7-rc, which has since gone into stable. Commit 00442ad ("mempolicy: fix a memory corruption by refcount imbalance in alloc_pages_vma()") changed get_vma_policy() to raise the refcount on a shmem shared mempolicy; whereas shmem_alloc_page() went on expecting alloc_page_vma() to drop the refcount it had acquired. This deserves a rework: but for now fix the leak in shmem_alloc_page(). Hugh: shmem_swapin() did not need a fix, but surely it's clearer to use the same refcounting there as in shmem_alloc_page(), delete its onstack mempolicy, and the strange mpol_cond_copy() and __mpol_cond_copy() - those were invented to let swapin_readahead() make an unknown number of calls to alloc_pages_vma() with one mempolicy; but since 00442ad, alloc_pages_vma() has kept refcount in balance, so now no problem. Reported-and-tested-by: Tommi Rantala <tt.rantala@gmail.com> Signed-off-by: Mel Gorman <mgorman@suse.de> Signed-off-by: Hugh Dickins <hughd@google.com> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent a63a7cf commit 49ed8b4

File tree

3 files changed

+16
-48
lines changed

3 files changed

+16
-48
lines changed

include/linux/mempolicy.h

-16
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,6 @@ static inline void mpol_cond_put(struct mempolicy *pol)
137137
__mpol_put(pol);
138138
}
139139

140-
extern struct mempolicy *__mpol_cond_copy(struct mempolicy *tompol,
141-
struct mempolicy *frompol);
142-
static inline struct mempolicy *mpol_cond_copy(struct mempolicy *tompol,
143-
struct mempolicy *frompol)
144-
{
145-
if (!frompol)
146-
return frompol;
147-
return __mpol_cond_copy(tompol, frompol);
148-
}
149-
150140
extern struct mempolicy *__mpol_dup(struct mempolicy *pol);
151141
static inline struct mempolicy *mpol_dup(struct mempolicy *pol)
152142
{
@@ -270,12 +260,6 @@ static inline void mpol_cond_put(struct mempolicy *pol)
270260
{
271261
}
272262

273-
static inline struct mempolicy *mpol_cond_copy(struct mempolicy *to,
274-
struct mempolicy *from)
275-
{
276-
return from;
277-
}
278-
279263
static inline void mpol_get(struct mempolicy *pol)
280264
{
281265
}

mm/mempolicy.c

-22
Original file line numberDiff line numberDiff line change
@@ -2035,28 +2035,6 @@ struct mempolicy *__mpol_dup(struct mempolicy *old)
20352035
return new;
20362036
}
20372037

2038-
/*
2039-
* If *frompol needs [has] an extra ref, copy *frompol to *tompol ,
2040-
* eliminate the * MPOL_F_* flags that require conditional ref and
2041-
* [NOTE!!!] drop the extra ref. Not safe to reference *frompol directly
2042-
* after return. Use the returned value.
2043-
*
2044-
* Allows use of a mempolicy for, e.g., multiple allocations with a single
2045-
* policy lookup, even if the policy needs/has extra ref on lookup.
2046-
* shmem_readahead needs this.
2047-
*/
2048-
struct mempolicy *__mpol_cond_copy(struct mempolicy *tompol,
2049-
struct mempolicy *frompol)
2050-
{
2051-
if (!mpol_needs_cond_ref(frompol))
2052-
return frompol;
2053-
2054-
*tompol = *frompol;
2055-
tompol->flags &= ~MPOL_F_SHARED; /* copy doesn't need unref */
2056-
__mpol_put(frompol);
2057-
return tompol;
2058-
}
2059-
20602038
/* Slow path of a mempolicy comparison */
20612039
bool __mpol_equal(struct mempolicy *a, struct mempolicy *b)
20622040
{

mm/shmem.c

+16-10
Original file line numberDiff line numberDiff line change
@@ -921,25 +921,29 @@ static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
921921
static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
922922
struct shmem_inode_info *info, pgoff_t index)
923923
{
924-
struct mempolicy mpol, *spol;
925924
struct vm_area_struct pvma;
926-
927-
spol = mpol_cond_copy(&mpol,
928-
mpol_shared_policy_lookup(&info->policy, index));
925+
struct page *page;
929926

930927
/* Create a pseudo vma that just contains the policy */
931928
pvma.vm_start = 0;
932929
/* Bias interleave by inode number to distribute better across nodes */
933930
pvma.vm_pgoff = index + info->vfs_inode.i_ino;
934931
pvma.vm_ops = NULL;
935-
pvma.vm_policy = spol;
936-
return swapin_readahead(swap, gfp, &pvma, 0);
932+
pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
933+
934+
page = swapin_readahead(swap, gfp, &pvma, 0);
935+
936+
/* Drop reference taken by mpol_shared_policy_lookup() */
937+
mpol_cond_put(pvma.vm_policy);
938+
939+
return page;
937940
}
938941

939942
static struct page *shmem_alloc_page(gfp_t gfp,
940943
struct shmem_inode_info *info, pgoff_t index)
941944
{
942945
struct vm_area_struct pvma;
946+
struct page *page;
943947

944948
/* Create a pseudo vma that just contains the policy */
945949
pvma.vm_start = 0;
@@ -948,10 +952,12 @@ static struct page *shmem_alloc_page(gfp_t gfp,
948952
pvma.vm_ops = NULL;
949953
pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
950954

951-
/*
952-
* alloc_page_vma() will drop the shared policy reference
953-
*/
954-
return alloc_page_vma(gfp, &pvma, 0);
955+
page = alloc_page_vma(gfp, &pvma, 0);
956+
957+
/* Drop reference taken by mpol_shared_policy_lookup() */
958+
mpol_cond_put(pvma.vm_policy);
959+
960+
return page;
955961
}
956962
#else /* !CONFIG_NUMA */
957963
#ifdef CONFIG_TMPFS

0 commit comments

Comments
 (0)