Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp194060pxb; Wed, 13 Jan 2021 00:56:08 -0800 (PST) X-Google-Smtp-Source: ABdhPJzvVgglmVr41tm6TuoQ0uyXkjELNodH+ab4CFrr2bAaGsnps9djS0s4/Up1v578cB2MeIqd X-Received: by 2002:a17:906:e212:: with SMTP id gf18mr819435ejb.551.1610528168403; Wed, 13 Jan 2021 00:56:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610528168; cv=none; d=google.com; s=arc-20160816; b=XRZajw0ujF8x/zS0w9YsSK4TcxTm0IoRNsma+ekxn4AO3h4icb+GHeo8H+ESeCxnrP 1pGW/4X7sa3WmPnYpFLQAUlRzmSXVann3tQXBr5TkK1bTbI6S81vzAO/9wrcwVYb6lAm pvKiNA8B9Cr1ZFQPX1jrv06T9oz1S/bYl59XeKiwvYUsiE4dSX7y/0D68OgFtRbW2mGj /vsu5eXYgZ+dobcSYumr7OsiTlcYF0sYgiYcJrwSrpOvONJIJ6GMRG08/BSCF9Hle/86 fjllzT1ksRqKxWwjNEkSNJVBzAlkMhg9mmixBTZ3K4F+V/HazB/nUAmR5fXrZRpL5SVj aY/w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:organization :from:references:cc:to:subject:dkim-signature; bh=SOjJkqULa4ZLAAYrZfKYkDgM6Fbu5xv0gKR0egm7r+0=; b=AWOjMad0zJ+3JDht0BmX9Xgx99qRYG2lla4TxX1Z4vNoCiBOjKiOicwz359SXD0jCa FL1DmTd6ucHP4zLG6AyJXintFGL+0z4NCfjI60cQHAVT0WpSABAiZFegm+EXdLHLwVY/ hGlfAQBEFcj3RkVxH3VvQOGsgTWB5hBYxm7k6P4S+Rd+HP7DCEum2catcUorKd+a2uGf tpBaO8miS39A/NbzKYaQsJTKdWQwcz9j8jK5YP5S8Y798V6mFnoG7OWSvO/eG7zVbb4l s1fxZABOiumEZm/jFMGySjRoIJvjit1ZnivJr9QmM544ZhwK+msDBd2yQ1n09CJy8K8n 8CKg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=A8tdLQvJ; 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=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t13si207645edi.21.2021.01.13.00.55.45; Wed, 13 Jan 2021 00:56:08 -0800 (PST) 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=@redhat.com header.s=mimecast20190719 header.b=A8tdLQvJ; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727164AbhAMIyf (ORCPT + 99 others); Wed, 13 Jan 2021 03:54:35 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:33851 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727053AbhAMIye (ORCPT ); Wed, 13 Jan 2021 03:54:34 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1610527987; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=SOjJkqULa4ZLAAYrZfKYkDgM6Fbu5xv0gKR0egm7r+0=; b=A8tdLQvJkqQ4MCJWD/XISBfjXok45Y+LG9MDvUPzZB6e1sWtAQV/dCSLz8P/qm8DqK/SJV iXt5Wdi6mDTnW+w0Rbr7duLP4z0LMSSvqMyf//ZNkkJkxBu1xNQy7yYnwuRqTUewSoM0dQ T9ZnWDAuWUG7CUy0o9GhmzBaxSr3+88= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-97-ejCqgblAN5GlsaoRrl4inw-1; Wed, 13 Jan 2021 03:53:04 -0500 X-MC-Unique: ejCqgblAN5GlsaoRrl4inw-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B1B7F806660; Wed, 13 Jan 2021 08:53:00 +0000 (UTC) Received: from [10.36.114.135] (ovpn-114-135.ams2.redhat.com [10.36.114.135]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1CB0E6A911; Wed, 13 Jan 2021 08:52:48 +0000 (UTC) Subject: Re: [PATCH 0/1] mm: restore full accuracy in COW page reuse To: Linus Torvalds , Matthew Wilcox Cc: John Hubbard , Andrea Arcangeli , Andrew Morton , Linux-MM , Linux Kernel Mailing List , Yu Zhao , Andy Lutomirski , Peter Xu , Pavel Emelyanov , Mike Kravetz , Mike Rapoport , Minchan Kim , Will Deacon , Peter Zijlstra , Hugh Dickins , "Kirill A. Shutemov" , Oleg Nesterov , Jann Horn , Kees Cook , Leon Romanovsky , Jason Gunthorpe , Jan Kara , Kirill Tkhai , Nadav Amit , Jens Axboe References: <20210110004435.26382-1-aarcange@redhat.com> <45806a5a-65c2-67ce-fc92-dc8c2144d766@nvidia.com> <20210113021619.GL35215@casper.infradead.org> From: David Hildenbrand Organization: Red Hat GmbH Message-ID: <0cbefee2-70e9-9666-2d0c-ee2807e0fef9@redhat.com> Date: Wed, 13 Jan 2021 09:52:47 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.5.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 13.01.21 04:31, Linus Torvalds wrote: > On Tue, Jan 12, 2021 at 6:16 PM Matthew Wilcox wrote: >> >> The thing about the speculative page cache references is that they can >> temporarily bump a refcount on a page which _used_ to be in the page >> cache and has now been reallocated as some other kind of page. > > Oh, and thinking about this made me think we might actually have a > serious bug here, and it has nothing what-so-ever to do with COW, GUP, > or even the page count itself. > > It's unlikely enough that I think it's mostly theoretical, but tell me > I'm wrong. > > PLEASE tell me I'm wrong: > > CPU1 does page_cache_get_speculative under RCU lock > > CPU2 frees and re-uses the page > > CPU1 CPU2 > ---- ---- > > page = xas_load(&xas); > if (!page_cache_get_speculative(page)) > goto repeat; > .. succeeds .. > > remove page from XA > release page > reuse for something else > > .. and then re-check .. > if (unlikely(page != xas_reload(&xas))) { > put_page(page); > goto repeat; > } > > ok, the above all looks fine. We got the speculative ref, but then we > noticed that its' not valid any more, so we put it again. All good, > right? > > Wrong. > > What if that "reuse for something else" was actually really quick, and > both allocated and released it? > > That still sounds good, right? Yes, now the "put_page()" will be the > one that _actually_ releases the page, but we're still fine, right? > > Very very wrong. > > The "reuse for something else" on CPU2 might have gotten not an > order-0 page, but a *high-order* page. So it allocated (and then > immediately free'd) maybe an order-2 allocation with _four_ pages, and > the re-use happened when we had coalesced the buddy pages. > > But when we release the page on CPU1, we will release just _one_ page, > and the other three pages will be lost forever. > > IOW, we restored the page count perfectly fine, but we screwed up the > page sizes and buddy information. > > Ok, so the above is so unlikely from a timing standpoint that I don't > think it ever happens, but I don't see why it couldn't happen in > theory. > > Please somebody tell me I'm missing some clever thing we do to make > sure this can actually not happen.. Wasn't that tackled by latest (not merged AFAIKs) __free_pages() changes? I'm only able to come up with the doc update, not with the oroginal fix/change https://lkml.kernel.org/r/20201027025523.3235-1-willy@infradead.org -- Thanks, David / dhildenb