Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp294111pxb; Wed, 27 Oct 2021 03:15:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzMeTIAkQhSs0Uv3jjDIa6TvLdPgj/S/k5BWJQo47ZI/88jRaZW9em70jfSNk0LmHgNb4xc X-Received: by 2002:a17:906:c28d:: with SMTP id r13mr8988698ejz.102.1635329746001; Wed, 27 Oct 2021 03:15:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635329745; cv=none; d=google.com; s=arc-20160816; b=INMB/MPJSW1ZyNgRnjkTSwP537LjHHOSs+Z4YW9Dwwsrm19YuJq63HmmwiPVIYVaA8 xScT9hFbGWViVyv6ZibdGo/59CxiHz+xH8DB8KZl/QY1eIhLG8erHlIkEg4lM4Ndl0N/ O+XIpcRKU/3oz5StzpdUT5pHX9JUMfoqUyqG59SGkzACuP0U+C6GYlNmuNhEu8ubHYnU eiM/aiZ3udFpCwegDwngnf8jwjRln4x7c6YXtiO38W+4eLDzxV7PXreaUhRA8GLZOf/3 Qe6Axx1FIwtfKP++BjZftLNPRDpqASizJYWu5epJ59XLsuN5jsLpe8B1HZqJqIH9bXkS GkVQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=XpcCZaMDhTMnYyJSdOgPGsmV13N4Kdjo79sKD4gwqi4=; b=01fofPzLQm+zJ51EONCXv9xlPFpnTFMngvX5qBSAR3fTSwCZmH40HvBFHGv79hDHAz ZXz/ZRVJZdQQkQMjaKQGAOW1ilfIaUsNKN7J/pXLIW7jtbTkc+a+nRQ+9pv9M1U3AWqW qwgbfj+IDkdbjCACXszhHa8iwsCy9ojKQBzpt0Vb5T+Lq7gJsemk3clraPN2OTtVt367 J/wrcenhkz8R/K/RCsgGvNOoR5gGZxRdw+VCqHnwMX40oC1eeSZap9g/a8j/R0IVDwOy SThjkrseGm5J2omtswWrIE8dP9Xsf6ZpvWBoe5Z9eVbFmfQlhzXU2fBbMEn2GWW9pEDR 95AA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=g5HEEyPq; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id nd25si2757504ejc.391.2021.10.27.03.15.21; Wed, 27 Oct 2021 03:15:45 -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=@infradead.org header.s=casper.20170209 header.b=g5HEEyPq; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236088AbhJZUQw (ORCPT + 99 others); Tue, 26 Oct 2021 16:16:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51002 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234301AbhJZUQv (ORCPT ); Tue, 26 Oct 2021 16:16:51 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 14FA5C061570; Tue, 26 Oct 2021 13:14:27 -0700 (PDT) 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: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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.