Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp3747431pxv; Mon, 28 Jun 2021 11:47:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzdQBX0RrQwCgpLbCEl90G/A2q4qqW60TvaOw0aTCVJWoTwAhbBOCDyLwhorDdh7QgCaw2V X-Received: by 2002:a17:906:e03:: with SMTP id l3mr24963130eji.6.1624906053254; Mon, 28 Jun 2021 11:47:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624906053; cv=none; d=google.com; s=arc-20160816; b=k+YkwYogUOjHIdEfpkJdpc9ZdO6ZzDRPqSoS2TTzaKdq0q/K2dDS1wdYOvN9U9zd/I KIXah0EMilDjyaJk6gz+Ww2umlHp/e7SdKKcr8XXjGaX4EJgk8n8M0PZvYFnfqXk7KHW 3mDeQG42t2uQWQDkIfmAqJtjvNwnvkPVW9KIWiX+4VELPJXboe5JFVJf3j016LWWzJ+y T9UBMJYYkaJPw9I8KZCoTfE5SW29+giEb6asRdm4iLWuPMCEqwjFes3ewR/TmtvXBOo4 L1hRhnW9ztjsB5hVk6lF5qzrklUErfZokKmu4qNPVbinisToWiHQDkJJbXja3dcvz5ck ACAg== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=AaoIPrvA2zJnqjWv+Qdd+5X8K/tb9uI5yvlJu9t3F2I=; b=A3uvf5sx7sxrFVHuuHIysSFeHyRJU5oUARhfPxL81LnE3y33V6ktN2aDYmv0ufEWxN a6kT2ddCRI00Wg17CcpM90dSefKY+eW78lXbqkyQWLCd98oXQVkKLqpF0ZV4qXNZmhrl KQ0RVPCMjT2fc8i+xtpJqTVoXWLdJ893/9DIxhI9e3MPhAoB0dwrmfP0UvciPzeOz1/r rSawSMgCvkapnz0EAoc4M/+bKq+jjpzjQCYeRLEIaYqkwBstaA0efoT0QO5QXGNJeopd Lt8l40Kmdp00BzbzySpj3lUhefkda05W8KVdoILJWq3UCh8zKfS+Nmvkq7sR9qiw2kJ9 947Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=OvFI8uCN; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id la20si842478ejc.685.2021.06.28.11.47.09; Mon, 28 Jun 2021 11:47:33 -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=@kernel.org header.s=k20201202 header.b=OvFI8uCN; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234186AbhF1O3Q (ORCPT + 99 others); Mon, 28 Jun 2021 10:29:16 -0400 Received: from mail.kernel.org ([198.145.29.99]:55014 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232593AbhF1OXT (ORCPT ); Mon, 28 Jun 2021 10:23:19 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id A693E619AD; Mon, 28 Jun 2021 14:20:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1624890015; bh=hZM4slfU/fL5G4L7VUgdEfsyIL/aCjNcBMSBZ83Hc8g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OvFI8uCN+OMAWY/X8sKl5jCecgWNZgYtzIfmJ2xHk6BH0BQ6PTTUGHAi0Gf4VR0o8 VmIIUpC3HMPZOT0cd2w2dMqVSP1W5Tj5U365HrLXCF20iB/o433L3+0xkXa70yapAO m8cU3NK461XivHUoVvZmyYKfieHjsbG6JsxdYQi6VfTWxroQQ9y9iOsbUFoIjT8tVg ZAbfyWJ1bSe6NZMUJ0a4DXl+elyqAYwXEcAgqJqXHbt3UVocDbST87pTwFsTJBc8b/ b0vsjl1RPqkXHsg6FeMrhN33zXddwpgSzp8yMCj9UwEAVYG2SwXJU4EtHfnN6gheUi 9I2BQx0T8OIiw== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Hugh Dickins , Neel Natu , Matthew Wilcox , Thomas Gleixner , "Kirill A. Shutemov" , Zhang Yi , Mel Gorman , Mike Kravetz , Ingo Molnar , Peter Zijlstra , Darren Hart , Davidlohr Bueso , Andrew Morton , Linus Torvalds , Greg Kroah-Hartman Subject: [PATCH 5.12 100/110] mm, futex: fix shared futex pgoff on shmem huge page Date: Mon, 28 Jun 2021 10:18:18 -0400 Message-Id: <20210628141828.31757-101-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210628141828.31757-1-sashal@kernel.org> References: <20210628141828.31757-1-sashal@kernel.org> MIME-Version: 1.0 X-KernelTest-Patch: http://kernel.org/pub/linux/kernel/v5.x/stable-review/patch-5.12.14-rc1.gz X-KernelTest-Tree: git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.git X-KernelTest-Branch: linux-5.12.y X-KernelTest-Patches: git://git.kernel.org/pub/scm/linux/kernel/git/stable/stable-queue.git X-KernelTest-Version: 5.12.14-rc1 X-KernelTest-Deadline: 2021-06-30T14:18+00:00 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Hugh Dickins commit fe19bd3dae3d15d2fbfdb3de8839a6ea0fe94264 upstream. 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 Signed-off-by: Greg Kroah-Hartman --- include/linux/hugetlb.h | 16 ---------------- include/linux/pagemap.h | 13 +++++++------ kernel/futex.c | 3 +-- mm/hugetlb.c | 5 +---- 4 files changed, 9 insertions(+), 28 deletions(-) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 5dae4187210d..28fa3f9bbbfd 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -728,17 +728,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_page(struct page *page); extern int dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn); @@ -969,11 +958,6 @@ static inline int hstate_index(struct hstate *h) return 0; } -static inline pgoff_t basepage_index(struct page *page) -{ - return page->index; -} - static inline int dissolve_free_huge_page(struct page *page) { return 0; diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index 8c9947fd62f3..e0023e5f9aa6 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -501,7 +501,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) @@ -520,15 +520,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 a8629b695d38..5aa6d0a6c767 100644 --- a/kernel/futex.c +++ b/kernel/futex.c @@ -35,7 +35,6 @@ #include #include #include -#include #include #include #include @@ -650,7 +649,7 @@ again: 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 3da4817190f3..7ba7d9b20494 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -1584,15 +1584,12 @@ struct address_space *hugetlb_page_mapping_lock_write(struct page *hpage) return NULL; } -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