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=-8.9 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_PASS,T_DKIMWL_WL_HIGH,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham 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 80953C04AB2 for ; Thu, 9 May 2019 18:44:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 589BE2183F for ; Thu, 9 May 2019 18:44:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1557427458; bh=tNjYUTZ1DOGAjleLkG3kpghjYkmPPY5p4WMIEZcqOlI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=WPDAg6FASVx5SsK+QkuwjWHYcehbyFgQLqvrfF7cdi2pqZSzR1i5Q3NJNACS/7Sdb AKohlEu26MksPGM4uLdgZXu7ELusD04eO5Sb/A3eLeIVMXeLClgJHyUqD5r4gECZ/p Ya9SHKVesxL7ELDmBYBHop2T5yovwLjW+gdWBFQU= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726966AbfEISoR (ORCPT ); Thu, 9 May 2019 14:44:17 -0400 Received: from mail.kernel.org ([198.145.29.99]:35506 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726620AbfEISoP (ORCPT ); Thu, 9 May 2019 14:44:15 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id C81212183E; Thu, 9 May 2019 18:44:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1557427455; bh=tNjYUTZ1DOGAjleLkG3kpghjYkmPPY5p4WMIEZcqOlI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GyEf5MGQJCdnJflZts/co78p519k7bNn/ldJJ0WuzhY+IQr5mQ57/TbxgH7FpGLrR HIDt4c3xhg0Lr5CcdF6TQyYBeFPnyNxO0h31KSxqcB8TKgEMqk2OX8JNCLldsfcR1B tjyhAhQt1Y+kjb3FDr5/bKsGIVmoWRagCXAgjRkg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Matthew Wilcox , Jann Horn , stable@kernel.org, Linus Torvalds , Sasha Levin Subject: [PATCH 4.9 17/28] mm: add try_get_page() helper function Date: Thu, 9 May 2019 20:42:09 +0200 Message-Id: <20190509181253.918151808@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190509181247.647767531@linuxfoundation.org> References: <20190509181247.647767531@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org [ 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 11a5a46ce72be..e3c8d40a18b5b 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -777,6 +777,15 @@ static inline void get_page(struct page *page) get_zone_device_page(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.20.1