Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp25881459rwd; Mon, 3 Jul 2023 02:17:14 -0700 (PDT) X-Google-Smtp-Source: APBJJlE86eGKzhDSliB9hwn9FZnBd+Qq3ae63bET3BZyRHRMfr/boVx8hJKXRWvLYuLgu2QY23d5 X-Received: by 2002:a17:90a:42:b0:263:16f3:f04a with SMTP id 2-20020a17090a004200b0026316f3f04amr8580164pjb.1.1688375833920; Mon, 03 Jul 2023 02:17:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688375833; cv=none; d=google.com; s=arc-20160816; b=IYngloo2hbx8rSbp3chUMWZfMLCKw/SjQH/ADJXhvULkg/Jz+c7YT+DYJJgOKf7uLR X6xjqG2HUmpG7ykBjEKbtvALwgO4kp5pVRQjMJvTdCuYRdL0591yxV/6DGAvwm1NHy6Z lUK+XP3Ya9c6CPW92kdVYWymkLkYAMfFb6R0+We9X5o8spXglVU/EhmYSkcpjAcsZt+q CI3s2GIJA6FmKtqdozMJ9aC2U88x04C48CKnh1AiazvC+AxNg2aE6ncHcETDVa3u8gPD 1Lf+PH6pox8CYbdF2U1HIEBOmpL4xd3R2v03uXnigocKP6u3kWXPJ+Kpx/KyFg8odOnj j8qQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:subject:user-agent:mime-version:date:message-id; bh=WBhoBBx3iLWPPQttbV99j2CksQvWTQ45ukXMmdKNxKc=; fh=5BuvHrlly8iv3N6Z6UUSLM8jXtvnPRemjCVjHddYLgY=; b=vJSMR/uwVe/n8G+GTlPywXxIsDGPnCdZXIvFgxbqVX9sDGoccfR1s3tww9KpUPNSfJ PQz+8UGAivvPbRrxithg0qmjPMC1XljzelRGq7IhS/CW7z23Y0lt2GIBxGR33dNPSXhO puyh39Wp9+xkFsPzvs/D+ULIYZxYnTBOWyqaKoX2CN3kk247+GdAfSZsAG3Mv77D/C2p mvCyigy1sr9/U7+g3t+EM84Rhk4jxdu8gRMND9u1W4ysj3O9v4jXUNMu1xvqpoP8SGZu ptxa2aF1ZsSUErH8xweeJbdLBSKH1AV1XEsFRxhR+2FPA1fU/CigHqj3LXsyJAnI5b6d Vt9g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id lp6-20020a17090b4a8600b002639ae468d4si5785880pjb.162.2023.07.03.02.16.59; Mon, 03 Jul 2023 02:17:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229928AbjGCIx5 (ORCPT + 99 others); Mon, 3 Jul 2023 04:53:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229581AbjGCIx4 (ORCPT ); Mon, 3 Jul 2023 04:53:56 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 25C6E94 for ; Mon, 3 Jul 2023 01:53:55 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 6A04C1FB; Mon, 3 Jul 2023 01:54:37 -0700 (PDT) Received: from [10.57.76.103] (unknown [10.57.76.103]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id C8B1F3F762; Mon, 3 Jul 2023 01:53:47 -0700 (PDT) Message-ID: <8b2df98c-f3d4-a1ab-b3c0-ed456da6f404@arm.com> Date: Mon, 3 Jul 2023 09:53:45 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.12.0 Subject: Re: [PATCH v2] mm/hugetlb.c: fix a bug within a BUG(): inconsistent pte comparison To: John Hubbard , Andrew Morton Cc: LKML , linux-mm@kvack.org, James Houghton , Muchun Song , Adrian Hunter , Al Viro , Alex Williamson , Alexander Potapenko , Alexander Shishkin , Andrey Konovalov , Andrey Ryabinin , Christian Brauner , Christoph Hellwig , Daniel Vetter , Dave Airlie , Dimitri Sivanich , Dmitry Vyukov , Ian Rogers , Jason Gunthorpe , Jiri Olsa , Johannes Weiner , "Kirill A . Shutemov" , Lorenzo Stoakes , Mark Rutland , Matthew Wilcox , Miaohe Lin , Michal Hocko , Mike Kravetz , Mike Rapoport , Namhyung Kim , Naoya Horiguchi , Oleksandr Tyshchenko , Pavel Tatashin , Roman Gushchin , SeongJae Park , Shakeel Butt , Uladzislau Rezki , Vincenzo Frascino , Yu Zhao References: <20230701010442.2041858-1-jhubbard@nvidia.com> From: Ryan Roberts In-Reply-To: <20230701010442.2041858-1-jhubbard@nvidia.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 01/07/2023 02:04, John Hubbard wrote: > The following crash happens for me when running the -mm selftests > (below). Specifically, it happens while running the uffd-stress > subtests: > > kernel BUG at mm/hugetlb.c:7249! > invalid opcode: 0000 [#1] PREEMPT SMP NOPTI > CPU: 0 PID: 3238 Comm: uffd-stress Not tainted 6.4.0-hubbard-github+ #109 > Hardware name: ASUS X299-A/PRIME X299-A, BIOS 1503 08/03/2018 > RIP: 0010:huge_pte_alloc+0x12c/0x1a0 > ... > Call Trace: > > ? __die_body+0x63/0xb0 > ? die+0x9f/0xc0 > ? do_trap+0xab/0x180 > ? huge_pte_alloc+0x12c/0x1a0 > ? do_error_trap+0xc6/0x110 > ? huge_pte_alloc+0x12c/0x1a0 > ? handle_invalid_op+0x2c/0x40 > ? huge_pte_alloc+0x12c/0x1a0 > ? exc_invalid_op+0x33/0x50 > ? asm_exc_invalid_op+0x16/0x20 > ? __pfx_put_prev_task_idle+0x10/0x10 > ? huge_pte_alloc+0x12c/0x1a0 > hugetlb_fault+0x1a3/0x1120 > ? finish_task_switch+0xb3/0x2a0 > ? lock_is_held_type+0xdb/0x150 > handle_mm_fault+0xb8a/0xd40 > ? find_vma+0x5d/0xa0 > do_user_addr_fault+0x257/0x5d0 > exc_page_fault+0x7b/0x1f0 > asm_exc_page_fault+0x22/0x30 > > That happens because a BUG() statement in huge_pte_alloc() attempts to > check that a pte, if present, is a hugetlb pte, but it does so in a > non-lockless-safe manner that leads to a false BUG() report. > > We got here due to a couple of bugs, each of which by itself was not > quite enough to cause a problem: > > First of all, before commit c33c794828f2("mm: ptep_get() conversion"), > the BUG() statement in huge_pte_alloc() was itself fragile: it relied > upon compiler behavior to only read the pte once, despite using it twice > in the same conditional. > > Next, commit c33c794828f2 ("mm: ptep_get() conversion") broke that > delicate situation, by causing all direct pte reads to be done via > READ_ONCE(). And so READ_ONCE() got called twice within the same BUG() > conditional, leading to comparing (potentially, occasionally) different > versions of the pte, and thus to false BUG() reports. > > Fix this by taking a single snapshot of the pte before using it in the > BUG conditional. > > Now, that commit is only partially to blame here but, people doing > bisections will invariably land there, so this will help them find a fix > for a real crash. And also, the previous behavior was unlikely to ever > expose this bug--it was fragile, yet not actually broken. > > So that's why I chose this commit for the Fixes tag, rather than the > commit that created the original BUG() statement. > > Fixes: c33c794828f2 ("mm: ptep_get() conversion") > Acked-by: James Houghton > Acked-by: Muchun Song > Cc: Adrian Hunter > Cc: Al Viro > Cc: Alex Williamson > Cc: Alexander Potapenko > Cc: Alexander Shishkin > Cc: Andrew Morton > Cc: Andrey Konovalov > Cc: Andrey Ryabinin > Cc: Christian Brauner > Cc: Christoph Hellwig > Cc: Daniel Vetter > Cc: Dave Airlie > Cc: Dimitri Sivanich > Cc: Dmitry Vyukov > Cc: Ian Rogers > Cc: Jason Gunthorpe > Cc: Jiri Olsa > Cc: Johannes Weiner > Cc: Kirill A. Shutemov > Cc: Lorenzo Stoakes > Cc: Mark Rutland > Cc: Matthew Wilcox > Cc: Miaohe Lin > Cc: Michal Hocko > Cc: Mike Kravetz > Cc: Mike Rapoport (IBM) > Cc: Namhyung Kim > Cc: Naoya Horiguchi > Cc: Oleksandr Tyshchenko > Cc: Pavel Tatashin > Cc: Roman Gushchin > Cc: Ryan Roberts > Cc: SeongJae Park > Cc: Shakeel Butt > Cc: Uladzislau Rezki (Sony) > Cc: Vincenzo Frascino > Cc: Yu Zhao > Signed-off-by: John Hubbard Reviewed-by: Ryan Roberts > --- > > Changes since v1: > > Added Acked-by's. > > Fixed as per Ryan Roberts (thanks!): changed to ptep_get_lockless(). > > > mm/hugetlb.c | 7 ++++++- > 1 file changed, 6 insertions(+), 1 deletion(-) > > diff --git a/mm/hugetlb.c b/mm/hugetlb.c > index bce28cca73a1..64a3239b6407 100644 > --- a/mm/hugetlb.c > +++ b/mm/hugetlb.c > @@ -7246,7 +7246,12 @@ pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma, > pte = (pte_t *)pmd_alloc(mm, pud, addr); > } > } > - BUG_ON(pte && pte_present(ptep_get(pte)) && !pte_huge(ptep_get(pte))); > + > + if (pte) { > + pte_t pteval = ptep_get_lockless(pte); > + > + BUG_ON(pte_present(pteval) && !pte_huge(pteval)); > + } > > return pte; > } > > base-commit: bf1fa6f15553df04f2bdd06190ccd5f388ab0777