Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp657343pxv; Fri, 9 Jul 2021 06:20:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxnfZqH25JCqYe3d3GMhNgRjEwpEvNQ26kh6yX+VaE3KPYwV1TGFM+6ivKv1uJvNHavBCxx X-Received: by 2002:a5d:8c9a:: with SMTP id g26mr20474309ion.121.1625836805454; Fri, 09 Jul 2021 06:20:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625836805; cv=none; d=google.com; s=arc-20160816; b=xFhWOxwvb/7gaDF85zRLDzTA5+SXV51BI3Q8fQ74HsXogqSmkSn9bKb5LlflkBswMX m0mvO2rdqPUxJx65wmNBo+h8aOLo/UV3c4WE0yf5eMuK32w4YWXVaqE9hZ9tCArhyBnV AAjWpMLvvM7/r8uJl5bANGp6ZPUX52WWUL/N0R7fzpGdga3EMgI+HFuOGUzn0VAHTl0S /nwTSoQXTYJ4XlGXJq2/7Y7g5cGg6aWrtewOx+6wmxU/GbDOYe3YoGpq+ykjNy4MkSy7 wyKUFAgQzEmSJH2uJjAmTdMy//xSU6pMKDTBle06Fz6eJxdNH6jfnMU2GfhyP2QNsvI5 JmQQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=prV861kZ3muNjwgg/krd55JCpnSTPLMACpNF0vid3mA=; b=cbbFor8+Vj2CyBoKNI+I5DrjMmDCRFXFxn8m2SazGAPAZwD1G6lJyugBy7U5ED+0Y7 FCua5szsVW08x9F7insKEPXsTGHGdeUQVRPjdnLoFh8nXbqtJLC79spgCSqWJ8dyxDVz AUV3+TtFzVP+f9O8kh4G8ukPkAqM49TMieUFgnE+3dKoO1nOXeXvZW2nIuzJ2qitTPWk nUgVeGTfSuMnhZEN14Eu5/MzhXTYFrpyMMKCQxR46WfqpLyDacAcnrSE1UjihcdwzN2q m2O+E7GxwQuST25YAKVNUz6mopTeydLqASrgQoElYQ/OcbQIXqALi+Q6B6bQhlrZMmiK EjVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=fHnE2HTr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v9si7505651ilu.9.2021.07.09.06.19.52; Fri, 09 Jul 2021 06:20:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=fHnE2HTr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231963AbhGINVk (ORCPT + 99 others); Fri, 9 Jul 2021 09:21:40 -0400 Received: from mail.kernel.org ([198.145.29.99]:51762 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231989AbhGINVf (ORCPT ); Fri, 9 Jul 2021 09:21:35 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 252F2613BC; Fri, 9 Jul 2021 13:18:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1625836731; bh=r5VTxxUV2wnwQ/Dp2kYxFOUMIagFMh88Pe94OKBEJgg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fHnE2HTrdUtqoQrgyLzu9tJ00LcYgVpwBjax7P8nEq385BiwB1IgYgxhb0xPH26vU o3vWG2+LgptolpDBvbWXCy+j63Jx68Wd5eBtH5Bx23hXOuNvgxc3f9k5COSZFUsrSQ ixxleb3tfTHXiWoZgXRwYjIyi/ZzYWx2GqLPzXsI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Neel Natu , Hugh Dickins , "Matthew Wilcox (Oracle)" , Thomas Gleixner , "Kirill A. Shutemov" , Zhang Yi , Mel Gorman , Mike Kravetz , Ingo Molnar , Peter Zijlstra , Darren Hart , Davidlohr Bueso , Andrew Morton , Linus Torvalds , Sasha Levin Subject: [PATCH 4.9 4/9] mm, futex: fix shared futex pgoff on shmem huge page Date: Fri, 9 Jul 2021 15:18:31 +0200 Message-Id: <20210709131548.799185576@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210709131542.410636747@linuxfoundation.org> References: <20210709131542.410636747@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Hugh Dickins [ Upstream commit fe19bd3dae3d15d2fbfdb3de8839a6ea0fe94264 ] If more than one futex is placed on a shmem huge page, it can happen that waking the second wakes the first instead, and leaves the second waiting: the key's shared.pgoff is wrong. When 3.11 commit 13d60f4b6ab5 ("futex: Take hugepages into account when generating futex_key"), the only shared huge pages came from hugetlbfs, and the code added to deal with its exceptional page->index was put into hugetlb source. Then that was missed when 4.8 added shmem huge pages. page_to_pgoff() is what others use for this nowadays: except that, as currently written, it gives the right answer on hugetlbfs head, but nonsense on hugetlbfs tails. Fix that by calling hugetlbfs-specific hugetlb_basepage_index() on PageHuge tails as well as on head. Yes, it's unconventional to declare hugetlb_basepage_index() there in pagemap.h, rather than in hugetlb.h; but I do not expect anything but page_to_pgoff() ever to need it. [akpm@linux-foundation.org: give hugetlb_basepage_index() prototype the correct scope] Link: https://lkml.kernel.org/r/b17d946b-d09-326e-b42a-52884c36df32@google.com Fixes: 800d8c63b2e9 ("shmem: add huge pages support") Reported-by: Neel Natu Signed-off-by: Hugh Dickins Reviewed-by: Matthew Wilcox (Oracle) Acked-by: Thomas Gleixner Cc: "Kirill A. Shutemov" Cc: Zhang Yi Cc: Mel Gorman Cc: Mike Kravetz Cc: Ingo Molnar Cc: Peter Zijlstra Cc: Darren Hart Cc: Davidlohr Bueso Cc: Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Note on stable backport: leave redundant #include in kernel/futex.c, to avoid conflict over the header files included. Resolved trivial conflicts in include/linux/hugetlb.h. Signed-off-by: Hugh Dickins Signed-off-by: Sasha Levin --- include/linux/hugetlb.h | 15 --------------- include/linux/pagemap.h | 13 +++++++------ kernel/futex.c | 2 +- mm/hugetlb.c | 5 +---- 4 files changed, 9 insertions(+), 26 deletions(-) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 8dd365c65478..6417bc845db5 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -451,17 +451,6 @@ static inline int hstate_index(struct hstate *h) return h - hstates; } -pgoff_t __basepage_index(struct page *page); - -/* Return page->index in PAGE_SIZE units */ -static inline pgoff_t basepage_index(struct page *page) -{ - if (!PageCompound(page)) - return page->index; - - return __basepage_index(page); -} - extern int dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn); static inline bool hugepage_migration_supported(struct hstate *h) @@ -529,10 +518,6 @@ static inline unsigned int pages_per_huge_page(struct hstate *h) #define hstate_index_to_shift(index) 0 #define hstate_index(h) 0 -static inline pgoff_t basepage_index(struct page *page) -{ - return page->index; -} #define dissolve_free_huge_pages(s, e) 0 #define hugepage_migration_supported(h) false diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index 35f4c4d9c405..8672291633dd 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -374,7 +374,7 @@ static inline struct page *read_mapping_page(struct address_space *mapping, } /* - * Get index of the page with in radix-tree + * Get index of the page within radix-tree (but not for hugetlb pages). * (TODO: remove once hugetlb pages will have ->index in PAGE_SIZE) */ static inline pgoff_t page_to_index(struct page *page) @@ -393,15 +393,16 @@ static inline pgoff_t page_to_index(struct page *page) return pgoff; } +extern pgoff_t hugetlb_basepage_index(struct page *page); + /* - * Get the offset in PAGE_SIZE. - * (TODO: hugepage should have ->index in PAGE_SIZE) + * Get the offset in PAGE_SIZE (even for hugetlb pages). + * (TODO: hugetlb pages should have ->index in PAGE_SIZE) */ static inline pgoff_t page_to_pgoff(struct page *page) { - if (unlikely(PageHeadHuge(page))) - return page->index << compound_order(page); - + if (unlikely(PageHuge(page))) + return hugetlb_basepage_index(page); return page_to_index(page); } diff --git a/kernel/futex.c b/kernel/futex.c index 324fb85c8904..b3823736af6f 100644 --- a/kernel/futex.c +++ b/kernel/futex.c @@ -717,7 +717,7 @@ get_futex_key(u32 __user *uaddr, int fshared, union futex_key *key, int rw) key->both.offset |= FUT_OFF_INODE; /* inode-based key */ key->shared.i_seq = get_inode_sequence_number(inode); - key->shared.pgoff = basepage_index(tail); + key->shared.pgoff = page_to_pgoff(tail); rcu_read_unlock(); } diff --git a/mm/hugetlb.c b/mm/hugetlb.c index b7215b0807ca..de89e9295f6c 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -1380,15 +1380,12 @@ int PageHeadHuge(struct page *page_head) return get_compound_page_dtor(page_head) == free_huge_page; } -pgoff_t __basepage_index(struct page *page) +pgoff_t hugetlb_basepage_index(struct page *page) { struct page *page_head = compound_head(page); pgoff_t index = page_index(page_head); unsigned long compound_idx; - if (!PageHuge(page_head)) - return page_index(page); - if (compound_order(page_head) >= MAX_ORDER) compound_idx = page_to_pfn(page) - page_to_pfn(page_head); else -- 2.30.2