Skip to content

Commit 18a2f37

Browse files
Mel Gormantorvalds
authored andcommitted
tmpfs: fix shared mempolicy leak
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> Cc: stable@vger.kernel.org Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent c702418 commit 18a2f37

File tree

3 files changed

+16
-48
lines changed

3 files changed

+16
-48
lines changed

include/linux/mempolicy.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,6 @@ static inline void mpol_cond_put(struct mempolicy *pol)
8282
__mpol_put(pol);
8383
}
8484

85-
extern struct mempolicy *__mpol_cond_copy(struct mempolicy *tompol,
86-
struct mempolicy *frompol);
87-
static inline struct mempolicy *mpol_cond_copy(struct mempolicy *tompol,
88-
struct mempolicy *frompol)
89-
{
90-
if (!frompol)
91-
return frompol;
92-
return __mpol_cond_copy(tompol, frompol);
93-
}
94-
9585
extern struct mempolicy *__mpol_dup(struct mempolicy *pol);
9686
static inline struct mempolicy *mpol_dup(struct mempolicy *pol)
9787
{
@@ -215,12 +205,6 @@ static inline void mpol_cond_put(struct mempolicy *pol)
215205
{
216206
}
217207

218-
static inline struct mempolicy *mpol_cond_copy(struct mempolicy *to,
219-
struct mempolicy *from)
220-
{
221-
return from;
222-
}
223-
224208
static inline void mpol_get(struct mempolicy *pol)
225209
{
226210
}

mm/mempolicy.c

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,28 +2037,6 @@ struct mempolicy *__mpol_dup(struct mempolicy *old)
20372037
return new;
20382038
}
20392039

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

mm/shmem.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -910,25 +910,29 @@ static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
910910
static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
911911
struct shmem_inode_info *info, pgoff_t index)
912912
{
913-
struct mempolicy mpol, *spol;
914913
struct vm_area_struct pvma;
915-
916-
spol = mpol_cond_copy(&mpol,
917-
mpol_shared_policy_lookup(&info->policy, index));
914+
struct page *page;
918915

919916
/* Create a pseudo vma that just contains the policy */
920917
pvma.vm_start = 0;
921918
/* Bias interleave by inode number to distribute better across nodes */
922919
pvma.vm_pgoff = index + info->vfs_inode.i_ino;
923920
pvma.vm_ops = NULL;
924-
pvma.vm_policy = spol;
925-
return swapin_readahead(swap, gfp, &pvma, 0);
921+
pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
922+
923+
page = swapin_readahead(swap, gfp, &pvma, 0);
924+
925+
/* Drop reference taken by mpol_shared_policy_lookup() */
926+
mpol_cond_put(pvma.vm_policy);
927+
928+
return page;
926929
}
927930

928931
static struct page *shmem_alloc_page(gfp_t gfp,
929932
struct shmem_inode_info *info, pgoff_t index)
930933
{
931934
struct vm_area_struct pvma;
935+
struct page *page;
932936

933937
/* Create a pseudo vma that just contains the policy */
934938
pvma.vm_start = 0;
@@ -937,10 +941,12 @@ static struct page *shmem_alloc_page(gfp_t gfp,
937941
pvma.vm_ops = NULL;
938942
pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
939943

940-
/*
941-
* alloc_page_vma() will drop the shared policy reference
942-
*/
943-
return alloc_page_vma(gfp, &pvma, 0);
944+
page = alloc_page_vma(gfp, &pvma, 0);
945+
946+
/* Drop reference taken by mpol_shared_policy_lookup() */
947+
mpol_cond_put(pvma.vm_policy);
948+
949+
return page;
944950
}
945951
#else /* !CONFIG_NUMA */
946952
#ifdef CONFIG_TMPFS

0 commit comments

Comments
 (0)