From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E03BEC433F5 for ; Tue, 26 Oct 2021 20:14:37 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 7F79E60F0F for ; Tue, 26 Oct 2021 20:14:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 7F79E60F0F Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id E0B8A80007; Tue, 26 Oct 2021 16:14:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DBA8A940007; Tue, 26 Oct 2021 16:14:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C820E80007; Tue, 26 Oct 2021 16:14:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0034.hostedemail.com [216.40.44.34]) by kanga.kvack.org (Postfix) with ESMTP id B8B6D940007 for ; Tue, 26 Oct 2021 16:14:36 -0400 (EDT) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 6D78F3A7DA for ; Tue, 26 Oct 2021 20:14:36 +0000 (UTC) X-FDA: 78739691352.16.D347943 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf04.hostedemail.com (Postfix) with ESMTP id D57315000305 for ; Tue, 26 Oct 2021 20:14:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=XpcCZaMDhTMnYyJSdOgPGsmV13N4Kdjo79sKD4gwqi4=; b=g5HEEyPqoYTrvCHhJurluWH14d GhrqbBsrMvPDxy93l7ms1zJyXKSFlQCa/LcV8U0RTMKWGuWnxQI24FGbLMu8zHwvrMLrnqUi7tFT6 x7c0HQwcj3ReCJzyafrRF61hUCM6Z4S1ZD4MhfSXT9l7BRR3TXlibO9ctHctzH3sjvUMB2boWHSee uZcOLIZsHsWvanVlKVUUCC/IDuooM92qmnVaFSUIqoSUdfV1U6MCo5rfV0RHQ1FKjHge/6ODqa2K8 RvfTG4GNCkq3Xf8EQmS24cUsJU7l2oKbS40/FVt0pUD0/whQdI/hSSpNkAz/8SOFI+XCHw89P006k YKvVEp4Q==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1mfSpB-00H8Hv-3p; Tue, 26 Oct 2021 20:13:45 +0000 Date: Tue, 26 Oct 2021 21:13:33 +0100 From: Matthew Wilcox To: Pasha Tatashin Cc: LKML , linux-mm , linux-m68k@lists.linux-m68k.org, Anshuman Khandual , Andrew Morton , william.kucharski@oracle.com, Mike Kravetz , Vlastimil Babka , Geert Uytterhoeven , schmitzmic@gmail.com, Steven Rostedt , Ingo Molnar , Johannes Weiner , Roman Gushchin , songmuchun@bytedance.com, weixugc@google.com, Greg Thelen Subject: Re: [RFC 0/8] Hardening page _refcount Message-ID: References: <20211026173822.502506-1-pasha.tatashin@soleen.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Queue-Id: D57315000305 Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=g5HEEyPq; spf=none (imf04.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org; dmarc=none X-Stat-Signature: 5unh4348jf1xk781gu6c1j1p9mqpytnk X-Rspamd-Server: rspam06 X-HE-Tag: 1635279270-244325 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: On Tue, Oct 26, 2021 at 02:30:25PM -0400, Pasha Tatashin wrote: > On Tue, Oct 26, 2021 at 2:24 PM Matthew Wilcox wrote: > > I think this is overkill. Won't we get exactly the same protection > > by simply testing that page->_refcount == 0 in set_page_count()? > > Anything which triggers that BUG_ON would already be buggy because > > it can race with speculative gets. > > We can't because set_page_count(v) is used for > 1. changing _refcount form a current value to unconstrained v > 2. initialize _refcount from undefined state to v. > > In this work we forbid the first case, and reduce the second case to > initialize only to 1. Anything that is calling set_page_refcount() on something which is not 0 is buggy today. There are several ways to increment the page refcount speculatively if it is not 0. eg lockless GUP and page cache reads. So we could have: CPU 0: alloc_page() (refcount now 1) CPU 1: get_page_unless_zero() (refcount now 2) CPU 0: set_page_refcount(5) (refcount now 5) CPU 1: put_page() (refcount now 4) Now the refcount is wrong. So it is *only* safe to call set_page_refcount() if the refcount is 0. If you can find somewhere that's calling set_page_refcount() on a non-0 refcount, that's a bug that needs to be fixed.