Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp2227439ybx; Fri, 8 Nov 2019 01:43:00 -0800 (PST) X-Google-Smtp-Source: APXvYqxKE+A0F4nbCnCrYSCB/jmcvWvbWaTZ+NjUzPoxICLtq5JQ1dNecxp8mA2p4A9WSmBTv3Q7 X-Received: by 2002:a17:906:6852:: with SMTP id a18mr7814892ejs.193.1573206180526; Fri, 08 Nov 2019 01:43:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573206180; cv=none; d=google.com; s=arc-20160816; b=ZmyG2VcIo1IM96X7ft0yBiShK3r7c3LuwFZn3wgvqtyQzXjOfB/yR7XcP8ivcmN/TL xx8euwIV+hEG/m0+pJF12LJtaf6u6r0w4qqvocaMdMxdgYmJdt58Ns/1rBvILopId2mM Qs/Iq8Z06As7NUMzJPYqRC0ri4UB0oaSSBI1vpJ/9Q81jpvjzh7cz2iWVm/o1UeVry0B 0t7hjDNQoiF2EFQ6K0KLDydE58gnSRTWqu6e6g03xJq/PNS9Tx6LmWLXrojadppTuSLF yezYzapcGxOSsCazxJXx642S1H4nEEQYA+brbAFiTemqglMeSZ6LKWYG75EcK2hcyW5D xzFg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=kE0nZy5fwJIq75m6BMJBAUol6kzaD7uoxDgIZqtJP7s=; b=ZmCxCW0HHoPQFOSLaOAogEe34Y+36CHD/Ef3uVCO+SdLSeSATkz8ozo9IHVWnaWqZV 9zED+0q6Gpz3cPxUscpR6be62zJ6TtT1VxkIkaCuPEnYUK7ewx57j9+WIiYAXsNC/zqm TYU0laofzwCMMOFmd7KzwVirrOEP58naCESL/tom7+0Py4sM//4uv7ZCYdT9yFP8eTCR bzHogJ4c3eGpGtBgqEVXpfl/2FOWKpV/32PlWm5Ra+1nOfr5DnLivOLKBBMkt5qtmfr0 4CDiti8Y657P3NVqqrZ5D/mddOhNWyJ7NJF7gjTJ8ytmzQq3OYMM0EO41k8Pqe/kptu9 kHfQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w29si3564007edd.270.2019.11.08.01.42.37; Fri, 08 Nov 2019 01:43:00 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731463AbfKHJi1 (ORCPT + 99 others); Fri, 8 Nov 2019 04:38:27 -0500 Received: from mx2.suse.de ([195.135.220.15]:39630 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1730622AbfKHJi0 (ORCPT ); Fri, 8 Nov 2019 04:38:26 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 56815AC67; Fri, 8 Nov 2019 09:38:24 +0000 (UTC) From: Vlastimil Babka To: stable@vger.kernel.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Ajay Kaher , Linus Torvalds , Matthew Wilcox , Jann Horn , stable@kernel.org, Vlastimil Babka Subject: [PATCH STABLE 4.4 4/8] mm: add 'try_get_page()' helper function Date: Fri, 8 Nov 2019 10:38:10 +0100 Message-Id: <20191108093814.16032-5-vbabka@suse.cz> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191108093814.16032-1-vbabka@suse.cz> References: <20191108093814.16032-1-vbabka@suse.cz> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Linus Torvalds commit 88b1a17dfc3ed7728316478fae0f5ad508f50397 upstream. [ 4.4 backport: get_page() is more complicated due to special handling of tail pages via __get_page_tail(). But in all cases, eventually the compound head page's refcount is incremented. So try_get_page() just checks compound head's refcount for overflow and then simply calls get_page(). ] This is the same as the traditional 'get_page()' function, but instead of unconditionally incrementing the reference count of the page, it only does so if the count was "safe". It returns whether the reference count was incremented (and is marked __must_check, since the caller obviously has to be aware of it). Also like 'get_page()', you can't use this function unless you already had a reference to the page. The intent is that you can use this exactly like get_page(), but in situations where you want to limit the maximum reference count. The code currently does an unconditional WARN_ON_ONCE() if we ever hit the reference count issues (either zero or negative), as a notification that the conditional non-increment actually happened. NOTE! The count access for the "safety" check is inherently racy, but that doesn't matter since the buffer we use is basically half the range of the reference count (ie we look at the sign of the count). Acked-by: Matthew Wilcox Cc: Jann Horn Cc: stable@kernel.org Signed-off-by: Linus Torvalds Signed-off-by: Vlastimil Babka --- include/linux/mm.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index 997edfcb0a30..78358aeb7732 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -510,6 +510,21 @@ static inline void get_page(struct page *page) atomic_inc(&page->_count); } +static inline __must_check bool try_get_page(struct page *page) +{ + struct page *head = compound_head(page); + + /* + * get_page() increases always head page's refcount, either directly or + * via __get_page_tail() for tail page, so we check that + */ + if (WARN_ON_ONCE(page_ref_count(head) <= 0)) + return false; + + get_page(page); + return true; +} + static inline struct page *virt_to_head_page(const void *x) { struct page *page = virt_to_page(x); -- 2.23.0