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 X-Spam-Level: X-Spam-Status: No, score=-9.1 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8BAF2C43218 for ; Sat, 27 Apr 2019 01:52:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 4DBDA206BA for ; Sat, 27 Apr 2019 01:52:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1556329929; bh=R2rpl+9GTkX7UfwhQa+rgLmWIhB1wYRoWJDewNXmuRM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=2T1x8WQG9PIsK1SJtQI0nU695OPNRn8GgY56hT3jShg6PiFqrr1TTeZnySfVVh7qf sHy53XJzwgJhENCBvMUIcUBtYjulHxo/luEvIYUKHX0ip9JyGtVt4tL8ZOLah1BUai Wg2m3dNujvz5RSVRUdpmOp2Wkre7SZkUzmc7Lkg8= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727503AbfD0Bkp (ORCPT ); Fri, 26 Apr 2019 21:40:45 -0400 Received: from mail.kernel.org ([198.145.29.99]:44460 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727502AbfD0Bkp (ORCPT ); Fri, 26 Apr 2019 21:40:45 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 936F120873; Sat, 27 Apr 2019 01:40:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1556329244; bh=R2rpl+9GTkX7UfwhQa+rgLmWIhB1wYRoWJDewNXmuRM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=1GBxLr6sp6zvcdMMZNKcw8TUcb94l8vShN01ZWKA7w6rjnQsIaXTt/4JrrXkLkiQU nbTT7CXjfxhuUwNM24EraNPovBG4tfH6zvQ6pEy+PgfIw13MoeDdlN7UGBDHAxNL0T o7I4+ENdFjz7VZaveVijLimj8uCsDQBGRAjWdXYs= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Linus Torvalds , Jann Horn , stable@kernel.org, Sasha Levin , linux-mm@kvack.org Subject: [PATCH AUTOSEL 5.0 77/79] mm: add 'try_get_page()' helper function Date: Fri, 26 Apr 2019 21:38:36 -0400 Message-Id: <20190427013838.6596-77-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190427013838.6596-1-sashal@kernel.org> References: <20190427013838.6596-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: stable-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Linus Torvalds [ Upstream commit 88b1a17dfc3ed7728316478fae0f5ad508f50397 ] 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: Sasha Levin --- include/linux/mm.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index 541d99b86aea..7000ddd807e0 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -980,6 +980,15 @@ static inline void get_page(struct page *page) page_ref_inc(page); } +static inline __must_check bool try_get_page(struct page *page) +{ + page = compound_head(page); + if (WARN_ON_ONCE(page_ref_count(page) <= 0)) + return false; + page_ref_inc(page); + return true; +} + static inline void put_page(struct page *page) { page = compound_head(page); -- 2.19.1