Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

[PATCH] compound page: use page[1].lru

If a compound page has its own put_page_testzero destructor (the only current
example is free_huge_page), that is noted in page[1].mapping of the compound
page. But that's rather a poor place to keep it: functions which call
set_page_dirty_lock after get_user_pages (e.g. Infiniband's
__ib_umem_release) ought to be checking first, otherwise set_page_dirty is
liable to crash on what's not the address of a struct address_space.

And now I'm about to make that worse: it turns out that every compound page
needs a destructor, so we can no longer rely on hugetlb pages going their own
special way, to avoid further problems of page->mapping reuse. For example,
not many people know that: on 50% of i386 -Os builds, the first tail page of a
compound page purports to be PageAnon (when its destructor has an odd
address), which surprises page_add_file_rmap.

Keep the compound page destructor in page[1].lru.next instead. And to free up
the common pairing of mapping and index, also move compound page order from
index to lru.prev. Slab reuses page->lru too: but if we ever need slab to use
compound pages, it can easily stack its use above this.

(akpm: decoded version of the above: the tail pages of a compound page now
have ->mapping==NULL, so there's no need for the set_page_dirty[_lock]()
caller to check that they're not compund pages before doing the dirty).

Signed-off-by: Hugh Dickins <hugh@veritas.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>

authored by

Hugh Dickins and committed by
Linus Torvalds
41d78ba5 72772323

+9 -12
+2 -2
mm/hugetlb.c
··· 85 85 BUG_ON(page_count(page)); 86 86 87 87 INIT_LIST_HEAD(&page->lru); 88 - page[1].mapping = NULL; 88 + page[1].lru.next = NULL; /* reset dtor */ 89 89 90 90 spin_lock(&hugetlb_lock); 91 91 enqueue_huge_page(page); ··· 105 105 } 106 106 spin_unlock(&hugetlb_lock); 107 107 set_page_count(page, 1); 108 - page[1].mapping = (void *)free_huge_page; 108 + page[1].lru.next = (void *)free_huge_page; /* set dtor */ 109 109 for (i = 0; i < (HPAGE_SIZE/PAGE_SIZE); ++i) 110 110 clear_user_highpage(&page[i], addr); 111 111 return page;
+6 -9
mm/page_alloc.c
··· 169 169 * All pages have PG_compound set. All pages have their ->private pointing at 170 170 * the head page (even the head page has this). 171 171 * 172 - * The first tail page's ->mapping, if non-zero, holds the address of the 173 - * compound page's put_page() function. 174 - * 175 - * The order of the allocation is stored in the first tail page's ->index 176 - * This is only for debug at present. This usage means that zero-order pages 177 - * may not be compound. 172 + * The first tail page's ->lru.next holds the address of the compound page's 173 + * put_page() function. Its ->lru.prev holds the order of allocation. 174 + * This usage means that zero-order pages may not be compound. 178 175 */ 179 176 static void prep_compound_page(struct page *page, unsigned long order) 180 177 { 181 178 int i; 182 179 int nr_pages = 1 << order; 183 180 184 - page[1].mapping = NULL; 185 - page[1].index = order; 181 + page[1].lru.next = NULL; /* set dtor */ 182 + page[1].lru.prev = (void *)order; 186 183 for (i = 0; i < nr_pages; i++) { 187 184 struct page *p = page + i; 188 185 ··· 193 196 int i; 194 197 int nr_pages = 1 << order; 195 198 196 - if (unlikely(page[1].index != order)) 199 + if (unlikely((unsigned long)page[1].lru.prev != order)) 197 200 bad_page(page); 198 201 199 202 for (i = 0; i < nr_pages; i++) {
+1 -1
mm/swap.c
··· 40 40 if (put_page_testzero(page)) { 41 41 void (*dtor)(struct page *page); 42 42 43 - dtor = (void (*)(struct page *))page[1].mapping; 43 + dtor = (void (*)(struct page *))page[1].lru.next; 44 44 (*dtor)(page); 45 45 } 46 46 }