From patchwork Sun Jun 13 03:16:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hugh Dickins X-Patchwork-Id: 12317281 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 C1744C48BCF for ; Sun, 13 Jun 2021 03:17:06 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 361BB611B0 for ; Sun, 13 Jun 2021 03:17:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 361BB611B0 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 9135E6B006C; Sat, 12 Jun 2021 23:17:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8C2AC6B006E; Sat, 12 Jun 2021 23:17:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 715536B0070; Sat, 12 Jun 2021 23:17:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0135.hostedemail.com [216.40.44.135]) by kanga.kvack.org (Postfix) with ESMTP id 3FD5F6B006C for ; Sat, 12 Jun 2021 23:17:05 -0400 (EDT) Received: from smtpin24.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id C78AF181AF5C1 for ; Sun, 13 Jun 2021 03:17:04 +0000 (UTC) X-FDA: 78247239168.24.D7F2FBF Received: from mail-oo1-f45.google.com (mail-oo1-f45.google.com [209.85.161.45]) by imf04.hostedemail.com (Postfix) with ESMTP id C417A36D for ; Sun, 13 Jun 2021 03:16:59 +0000 (UTC) Received: by mail-oo1-f45.google.com with SMTP id v17-20020a4aa5110000b0290249d63900faso1795034ook.0 for ; Sat, 12 Jun 2021 20:17:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:in-reply-to:message-id:references :mime-version; bh=NQEk3B/D9DQqdbqI2jqXdPIDfvBL0SmgQfWxdc0tj48=; b=J3T0NdWAGgeAJ2pSB1pbIn1dBxJe2lBUZoYhkqV735mwHyxeul41xMoGX/MX0uaCWr SuMBu2Gp1ryVQOe/LAdjZmeTJN9iNiQwVGA8h8TpEQwPTwa/Kp24uebk4qRgjDMiApxi yS7baEthtnZLGygqQErWmmzlCnMNF31x5Opz95L1Ml7vW54Lk6upzjzC0/EMrEitfxUm XlLHgd0P7pgqqQ+IGpQuQOBjneWh6eDoeOJ8d1g2Ena4Fn3xqIOziNXf2kycAaIjcnO7 G5K/+QNzQTRUUxaMT5w2wvKYgAJbpF667K5LQIB3imResymR1E8wvIEEWmcwFGb/OiWB cAUg== 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:in-reply-to:message-id :references:mime-version; bh=NQEk3B/D9DQqdbqI2jqXdPIDfvBL0SmgQfWxdc0tj48=; b=dkfb1yO2aLLGgpITo3qsfLhkuKt+Ql4zEtq0lVqgFGl0Ob0wbS6khN+H07sThkWd0R ODIGrpPdz/yzQDWky7aeRUtqr7xQYe0pQFGLnWlTdTTE6K4lp9tTZfr0b1BDN4V7L0pQ vynS6rW3nsAUrPWI+4IGHsE69ScArSLA8pZor15ECOgUHrbF9SKm7QK7KVWn18JuwmnP uWpj/OdSVWlgj7F+atyGzh0igEZ/XwjxHya66EQIlB/Qtm7Np/1RD0DPVI0EwgbXcwXg mlUoFJaMmsZs92HtyLob3P/87ArUoBWujf2hhJF8cUzvI3QpHQixmYw8L104p/9BcXYN xpCA== X-Gm-Message-State: AOAM532YFFUAmaYl0C+D7auDhwY+sBg6vR2+T2jzimGp4QwAUbHbyv5N 7eY7AhqkPAlUVZaKirEm2kdgBw== X-Google-Smtp-Source: ABdhPJwpuOaHt86XVL/vsr2gc5S0e7/tSsfFr5jAwgEk7ve6yi4Xn+CgsuUogZTf5spoyyGSxCB6Og== X-Received: by 2002:a4a:a802:: with SMTP id o2mr8721061oom.91.1623554221876; Sat, 12 Jun 2021 20:17:01 -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 p203sm1120452oib.40.2021.06.12.20.17.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 12 Jun 2021 20:17:01 -0700 (PDT) Date: Sat, 12 Jun 2021 20:16:58 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@ripple.attlocal.net To: Andrew Morton cc: Hugh Dickins , Matthew Wilcox , "Kirill A. Shutemov" , Zhang Yi , Mel Gorman , Mike Kravetz , Thomas Gleixner , Ingo Molnar , Peter Zijlstra , Darren Hart , Davidlohr Bueso , Neel Natu , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] mm, futex: Fix shared futex pgoff on shmem huge page In-Reply-To: Message-ID: References: <45e8fd67-51fd-7828-fe43-d261d6c33727@google.com> MIME-Version: 1.0 Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20161025 header.b=J3T0NdWA; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf04.hostedemail.com: domain of hughd@google.com designates 209.85.161.45 as permitted sender) smtp.mailfrom=hughd@google.com X-Stat-Signature: ew8tq8ps6zejnz9kk9t8uuxe7frj13rj X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: C417A36D X-HE-Tag: 1623554219-551955 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: Reviewed-by: Matthew Wilcox (Oracle) Acked-by: Thomas Gleixner --- v2: restored TODO comments, per Matthew; make "hugetlb pages" explicit. 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(-) --- 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-12 19:29:23.364387191 -0700 @@ -516,7 +516,7 @@ static inline struct page *read_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) @@ -536,14 +536,15 @@ 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). + * (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))) { + 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