Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3623149pxj; Tue, 15 Jun 2021 05:12:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwMd7aaZNkupqLsU2OqkdWr4R6mchaJse5XDB8uhOMNKkWyucrERjPxaYODRnoX0MX7kqyM X-Received: by 2002:a5d:9c88:: with SMTP id p8mr18591780iop.31.1623759144757; Tue, 15 Jun 2021 05:12:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623759144; cv=none; d=google.com; s=arc-20160816; b=bO1IN5Aev3WXy73C6xElT8AUn06IXnmgFN+vKqT1aagVRCkuycwn1d7WYIMuTduaod AJWh4ikA5NkJ4KOymosnIvcOP1wiT2nl25AQSnvJDIcXfuCVWKEBhEPYDwAHPz4T92hv LU9cdcFJ1woPnbVNFKj0oyalsN6BBQV6HLQw+bfcvPvuQYyoBc6IO5CuW0SRGq8MILSZ 3MlkiFBQdD66E6zl6a9D3/sS4Q1g+BUfepIjeVFB85FYstHkN/wWxb6xJ/fYa83p8llL tvDOn3R38nAj3SCnqStQ3Wc0gSTzOXEi8ah5dzq6lRZHE6WfwdYNWaCZuzfbY11T38J+ 4ImA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=WNpdr2S1NDbOx7bwbHaY+rxGjF1pZI1K0hiTte/Oqqs=; b=OBWU11k68Lr1xyKIQTft7jCbPZiaMRGiDRNjUkQEsjdQeSqkHs7hRQzcss77rXbkVP HL10DGnluXhdXsO1A0YOmnGdxGuDhyVE1i+3NypIOCUuImfzHkjmXar4qNQUOFdc8ark W4GTafPm3UDAUDZZBUFUG85fjISMCrp23IcBGsOa+pmz7j7FVaIpSxEg79LRyxHfuGWi eCVjhBt35ZUdmg4BBzHMm8lggQ15yNu3QuI/IgDgyIAGj1QGKoE+z3U+iBRetDSuVRlH aIb4L0bxVJ4h3AnbreiDD2iP2QkVfT7F11GPTqsGOzd+tZBZ9xYj6KLTCvYDja4kar0W ZnHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=hn6SsOwG; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j1si18201445jar.65.2021.06.15.05.12.12; Tue, 15 Jun 2021 05:12:24 -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=@google.com header.s=20161025 header.b=hn6SsOwG; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229943AbhFOMMN (ORCPT + 99 others); Tue, 15 Jun 2021 08:12:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35348 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229557AbhFOMMM (ORCPT ); Tue, 15 Jun 2021 08:12:12 -0400 Received: from mail-lj1-x22b.google.com (mail-lj1-x22b.google.com [IPv6:2a00:1450:4864:20::22b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6AA9DC06175F for ; Tue, 15 Jun 2021 05:10:08 -0700 (PDT) Received: by mail-lj1-x22b.google.com with SMTP id k8so7765937lja.4 for ; Tue, 15 Jun 2021 05:10:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=WNpdr2S1NDbOx7bwbHaY+rxGjF1pZI1K0hiTte/Oqqs=; b=hn6SsOwGYHR0CT0tDtiihzIzNVHWuJ5IrzWQY7kMPAPAkOcNK/166MPvX5oPF0j2qO 9T/1zTMuqXEJ4b6ZgOC6Knr2m2Yqb2HfB8NrNqGrd4izzEpDRZwQu15NaWtzVMNqbTZ3 7dJdgKBsPI3lfkwZM1wMH/372Y7ijszwqY2alGxa0ROsPoSE9JOK9BrBKfBi1ucrs+q6 Y7asH2E5+HafTOB16GdKR1Br5TfolOTf9iW970559LAuX/4rScPQ7EzCAw+yJ8GcPiNj XGtWxud65paFus1Ok5qmMCntw4zfOD+Qwpb0VTJslYHXyUYBkuT6DesWQVfZvQ4GEa/3 i25A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=WNpdr2S1NDbOx7bwbHaY+rxGjF1pZI1K0hiTte/Oqqs=; b=cXlQsWxwRBrN02jMG5Mw2pSB/ysd7Hynzj2sSINdsipl7FdjGbx/OSYARhFsm23H3Z pPJygaOarLkhbzFNPZN+HQl/pw/BiVIedvcUsRxX7IFxI6JAaUAoiq0Jw8z1MB2AeEeL 7Xnken7JC1Y91mO7H4Ez/TkcxnJATfKjsPFs8op6/gaEFFklHR1z5r1n+IOFUAer0o6b M4voZ6xQe5fcpS54+dLo0anUKpbA5hVy9VD0kGC0rdiAKFqkIEKGzlrm+B9AP1WlENyA dscOtjAJKGaf+Y4QgEN4YbrrF7ClrU26ufQUF7qwRP822xZnuOv9G65d74fuHoR9CtyA 0t6w== X-Gm-Message-State: AOAM531EfkRwo8Aah87KU1IuVHVLERp4+VDsENjLYnnq0qeGm1bynGfa XZlOEjWjfVUDSWjogQtCQfsEPghggfUkkJE5yLC2wg== X-Received: by 2002:a2e:b5ae:: with SMTP id f14mr17742867ljn.94.1623759005720; Tue, 15 Jun 2021 05:10:05 -0700 (PDT) MIME-Version: 1.0 References: <20210615012014.1100672-1-jannh@google.com> <50d828d1-2ce6-21b4-0e27-fb15daa77561@nvidia.com> In-Reply-To: <50d828d1-2ce6-21b4-0e27-fb15daa77561@nvidia.com> From: Jann Horn Date: Tue, 15 Jun 2021 14:09:38 +0200 Message-ID: Subject: Re: [PATCH v2] mm/gup: fix try_grab_compound_head() race with split_huge_page() To: John Hubbard , Matthew Wilcox Cc: Andrew Morton , Linux-MM , kernel list , "Kirill A . Shutemov" , Jan Kara , stable Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jun 15, 2021 at 8:37 AM John Hubbard wrote: > On 6/14/21 6:20 PM, Jann Horn wrote: > > try_grab_compound_head() is used to grab a reference to a page from > > get_user_pages_fast(), which is only protected against concurrent > > freeing of page tables (via local_irq_save()), but not against > > concurrent TLB flushes, freeing of data pages, or splitting of compound > > pages. [...] > Reviewed-by: John Hubbard Thanks! [...] > > @@ -55,8 +72,23 @@ static inline struct page *try_get_compound_head(struct page *page, int refs) > > if (WARN_ON_ONCE(page_ref_count(head) < 0)) > > return NULL; > > if (unlikely(!page_cache_add_speculative(head, refs))) > > return NULL; > > + > > + /* > > + * At this point we have a stable reference to the head page; but it > > + * could be that between the compound_head() lookup and the refcount > > + * increment, the compound page was split, in which case we'd end up > > + * holding a reference on a page that has nothing to do with the page > > + * we were given anymore. > > + * So now that the head page is stable, recheck that the pages still > > + * belong together. > > + */ > > + if (unlikely(compound_head(page) != head)) { > > I was just wondering about what all could happen here. Such as: page gets split, > reallocated into a different-sized compound page, one that still has page pointing > to head. I think that's OK, because we don't look at or change other huge page > fields. > > But I thought I'd mention the idea in case anyone else has any clever ideas about > how this simple check might be insufficient here. It seems fine to me, but I > routinely lack enough imagination about concurrent operations. :) Hmmm... I think the scariest aspect here is probably the interaction with concurrent allocation of a compound page on architectures with store-store reordering (like ARM). *If* the page allocator handled compound pages with lockless, non-atomic percpu freelists, I think it might be possible that the zeroing of tail_page->compound_head in put_page() could be reordered after the page has been freed, reallocated and set to refcount 1 again? That shouldn't be possible at the moment, but it is still a bit scary. I think the lockless page cache code also has to deal with somewhat similar ordering concerns when it uses page_cache_get_speculative(), e.g. in mapping_get_entry() - first it looks up a page pointer with xas_load(), and any access to the page later on would be a _dependent load_, but if the page then gets freed, reallocated, and inserted into the page cache again before the refcount increment and the re-check using xas_reload(), then there would be no data dependency from xas_reload() to the following use of the page...