From patchwork Sat Jun 12 04:31:16 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hugh Dickins X-Patchwork-Id: 12316859 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-23.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 47819C48BE5 for ; Sat, 12 Jun 2021 04:31:32 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B02D4611CD for ; Sat, 12 Jun 2021 04:31:31 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B02D4611CD Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id DDD7E6B006C; Sat, 12 Jun 2021 00:31:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D65596B006E; Sat, 12 Jun 2021 00:31:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B69556B0070; Sat, 12 Jun 2021 00:31:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0159.hostedemail.com [216.40.44.159]) by kanga.kvack.org (Postfix) with ESMTP id 7FA426B006C for ; Sat, 12 Jun 2021 00:31:30 -0400 (EDT) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 019E3D224 for ; Sat, 12 Jun 2021 04:31:30 +0000 (UTC) X-FDA: 78243797940.28.570C6BF Received: from mail-qt1-f181.google.com (mail-qt1-f181.google.com [209.85.160.181]) by imf07.hostedemail.com (Postfix) with ESMTP id E9669A0001A9 for ; Sat, 12 Jun 2021 04:31:22 +0000 (UTC) Received: by mail-qt1-f181.google.com with SMTP id r20so4279895qtp.3 for ; Fri, 11 Jun 2021 21:31:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:mime-version; bh=9rUn5z6J1oCf/hH7go9J9pBuFJfFwvGBOLnsQiWyXHM=; b=brXn7LLXuCAf1V+/rlD8RQj33kCogvu/48KwLJsYVPKgbkEU6Jl5yuHyQdKWLGwsT5 64loB1QJetvvFVt6BAkM84sgL0XM2poqfd6vS96/UD0H+MUO3xeT36Vg1X9R3kIN1RJh sYKe9lCFgw9cYV+eaRJa9pbyTBXQjyWn2wrP/HS4teG6IcOGIBxGm3cBRLKyUY2IvUDb ajhlC/L91JWeGsjYmj0VrQml1DJQRgY7dk61BK+JFhPKjSh12ftYp0ihhsKJxVTrXLgz G2mHWwfMgyejnys03ESHJ/vFs+UCfwLJYOsHXdYVpeD1sdioIrij8ddcoVSMqbcH5rbv T9xA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version; bh=9rUn5z6J1oCf/hH7go9J9pBuFJfFwvGBOLnsQiWyXHM=; b=gfSo+1bYBRvzvKAyLC7GIyXegU4YwN6Pn/BiFiAD0MkVAB0dYWgEGxhSLrqxa4KrAw r3kcawYH8C1ZOdCdePTiYJe18SOpL1ma0MP42PYY5xFKLfwHsKfBqPF291dus/kyBlm/ 3kmJm0wm3Wo+vPh3ARpQGtVFOMBHYA8k5MP5lMhtdTt3ozJyfLjNljzBUlEy1+ZpFcYK 6i4Z4brlq8VracykRRiJoslQHs1tO7WfG73FmW9sZw2DREvIDbJdumOhHNmB/htWSyBA tkcJwdMhtbpW72mSHQh22Xm6iqfhPv0za4r7bBx7qi4Rc8hMj0Jg1R9/9ztP4eSlmYQK AbOw== X-Gm-Message-State: AOAM532Nor5UkDdRfAFtytkcv/hW5aC5FExwjIXcOsPROkA6occyiEI+ cMP6v/yLvdu/8uYs0FBdFHfOzw== X-Google-Smtp-Source: ABdhPJzEcHIJiqN09yuESLHWZGpbiaiD+oHi5hCzMhr2sIJSSwCv9LdUFiRO8T032wOfRpBcdrWjGQ== X-Received: by 2002:ac8:709a:: with SMTP id y26mr6922294qto.315.1623472288778; Fri, 11 Jun 2021 21:31:28 -0700 (PDT) Received: from ripple.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id j14sm3781764qtq.56.2021.06.11.21.31.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Jun 2021 21:31:28 -0700 (PDT) Date: Fri, 11 Jun 2021 21:31:16 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@ripple.attlocal.net To: Andrew Morton cc: "Kirill A. Shutemov" , Zhang Yi , Mel Gorman , Mike Kravetz , Matthew Wilcox , Thomas Gleixner , Ingo Molnar , Peter Zijlstra , Darren Hart , Davidlohr Bueso , Neel Natu , Hugh Dickins , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH] mm, futex: Fix shared futex pgoff on shmem huge page Message-ID: <45e8fd67-51fd-7828-fe43-d261d6c33727@google.com> MIME-Version: 1.0 Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20161025 header.b=brXn7LLX; spf=pass (imf07.hostedemail.com: domain of hughd@google.com designates 209.85.160.181 as permitted sender) smtp.mailfrom=hughd@google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: E9669A0001A9 X-Stat-Signature: b3gcut5o7a63xsud1b463trf7ekpngm9 X-HE-Tag: 1623472282-570622 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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. Fixes: 800d8c63b2e9 ("shmem: add huge pages support") Reported-by: Neel Natu Signed-off-by: Hugh Dickins Cc: --- include/linux/hugetlb.h | 16 ---------------- include/linux/pagemap.h | 13 ++++++------- kernel/futex.c | 3 +-- mm/hugetlb.c | 5 +---- 4 files changed, 8 insertions(+), 29 deletions(-) --- 5.13-rc5/include/linux/hugetlb.h 2021-05-09 17:25:09.278703159 -0700 +++ linux/include/linux/hugetlb.h 2021-06-11 17:30:28.726720252 -0700 @@ -733,17 +733,6 @@ static inline int hstate_index(struct hs 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); @@ -980,11 +969,6 @@ static inline int hstate_index(struct hs 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; --- 5.13-rc5/include/linux/pagemap.h 2021-05-16 22:49:30.036176843 -0700 +++ linux/include/linux/pagemap.h 2021-06-11 17:30:28.726720252 -0700 @@ -516,8 +516,7 @@ static inline struct page *read_mapping_ } /* - * Get index of the page with in radix-tree - * (TODO: remove once hugetlb pages will have ->index in PAGE_SIZE) + * Get index of the page within radix-tree (but not for hugetlb pages). */ static inline pgoff_t page_to_index(struct page *page) { @@ -536,14 +535,14 @@ static inline pgoff_t page_to_index(stru } /* - * Get the offset in PAGE_SIZE. - * (TODO: hugepage should have ->index in PAGE_SIZE) + * Get the offset in PAGE_SIZE (even for hugetlb pages). */ static inline pgoff_t page_to_pgoff(struct page *page) { - if (unlikely(PageHeadHuge(page))) - return page->index << compound_order(page); - + if (unlikely(PageHuge(page))) { + extern pgoff_t hugetlb_basepage_index(struct page *page); + return hugetlb_basepage_index(page); + } return page_to_index(page); } --- 5.13-rc5/kernel/futex.c 2021-05-09 17:25:09.670705811 -0700 +++ linux/kernel/futex.c 2021-06-11 17:30:28.726720252 -0700 @@ -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(); } --- 5.13-rc5/mm/hugetlb.c 2021-06-06 16:57:26.263006733 -0700 +++ linux/mm/hugetlb.c 2021-06-11 17:30:28.730720276 -0700 @@ -1588,15 +1588,12 @@ struct address_space *hugetlb_page_mappi 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