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=-6.0 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_PASS, T_DKIMWL_WL_HIGH,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 1A246C43219 for ; Thu, 2 May 2019 15:45:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D515E204EC for ; Thu, 2 May 2019 15:45:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1556811937; bh=UKjT9EWMSlisdkdHMpuQfPtWIf4rtQi8MIWPWYdnC0U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=e6eMN0LxrtKtLpJ53gnjwD9bwFxZ8JU4OHIPkuWusTedsbeIGHgfSftAYIRDXqwR5 4y4hr6IFj4pKzQsJabyF2DrcNu3+n6jNKA2q0KyTTVEeMuo6H38+8qW1M6IS1kumz0 K3RQmJxS4CO/fewh/vvHUt6UZI4coQBfQNHUx8IQ= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727590AbfEBPZn (ORCPT ); Thu, 2 May 2019 11:25:43 -0400 Received: from mail.kernel.org ([198.145.29.99]:42106 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727587AbfEBPZl (ORCPT ); Thu, 2 May 2019 11:25:41 -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 4473820B7C; Thu, 2 May 2019 15:25:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1556810740; bh=UKjT9EWMSlisdkdHMpuQfPtWIf4rtQi8MIWPWYdnC0U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hbvK/CMH1XfQUYDAAxP+6pJMldpTp5ojZGN2bIw66rH+W9qD12YllTSOJ1PsjxGmq FvLqNif0I7NmiTN/BhioUld+JWxz27fMUqv+XNjWaOcfGAYN0ul9FOq0XJcypU6gfj Fh1PXKnKiGoTvnA1+L0++fTRF70KlpCmxkM7hihs= 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 Subject: [PATCH 4.19 04/72] mm: add try_get_page() helper function Date: Thu, 2 May 2019 17:20:26 +0200 Message-Id: <20190502143333.829739491@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190502143333.437607839@linuxfoundation.org> References: <20190502143333.437607839@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: stable-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Linus Torvalds commit 88b1a17dfc3ed7728316478fae0f5ad508f50397 upstream. 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: Greg Kroah-Hartman --- include/linux/mm.h | 9 +++++++++ 1 file changed, 9 insertions(+) --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -930,6 +930,15 @@ static inline void get_page(struct 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);