Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp7373083yba; Thu, 2 May 2019 08:46:58 -0700 (PDT) X-Google-Smtp-Source: APXvYqzbNc8eFeWvDa91KWHqx3J+g2plMwzuxe/OEPzVtDF5U2XH0g1hNUmx5ioTcKS+Oz6EAFCx X-Received: by 2002:a65:654c:: with SMTP id a12mr4713093pgw.101.1556812018111; Thu, 02 May 2019 08:46:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556812018; cv=none; d=google.com; s=arc-20160816; b=0JZYqGxexvp5iiJUmym0u96Em71pPCNX+85FoxQfSlz0N8+c4Qp4DjQ59VlF1zHueI ZP346l1RZKXmSnexbaYrIwm6650I5vbqEkicZqAar/w0eOTYTRdl9NL65i2y9hkDe1NA C4eVXp58u/DaOyo5fUSTqBcgjZZ2LfsSWNzBfHJkFT0h1mFLhicLM5jt0UlorSUlanCq qFhteDyc+L2zvXMKy0h7aLuQuDNyvqiVimXQM4fCXmhB35Sgss4J8IiR2qGOqNE8V0+T bfikLzD53TIpcrQuZR77jHx3ym25aK2wiSyzTl4fFoZzavFxL6qX62ThoqXKDlRa1HXW g3Xg== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=pFTBYmtkhCNYBEMS1fiIo2E7kBvK+x0Cqf196Viz7Vk=; b=ttRLkKC8cQRodQSnNSoahOCV+D9JAcHNedyq83RUKWfePPCxjXs6L+r0g9qzmRntUj hD4ySzWn/BKdXl32e0376PbCU1doiqwvFt10o5VGkWEHUcVl9CdxreGQk0xPJj7+4nOg bahVmnoqQ3gNhx+ZqCXZ7lcGcnRfoUd+8Tk0V+eWajxiSOGm/R6+3xUWe+ivVjauHgnc /Y+QiRVuBu4g+WCgXtnO4OWl0NYRyCPllq2BZU6kSAQQaltRvZzGoVPwBQFOlllEAv1p mRzPmRMGDJ4pc+EKfP8koMi8Euh7xp82UjMfewyzgbscFwtlxqhKdFSM7wn1xkLHLlg8 j6RQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="hbvK/CMH"; 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 f7si24668372plo.204.2019.05.02.08.46.42; Thu, 02 May 2019 08:46:58 -0700 (PDT) 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; dkim=pass header.i=@kernel.org header.s=default header.b="hbvK/CMH"; 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 S1727597AbfEBPZq (ORCPT + 99 others); Thu, 2 May 2019 11:25:46 -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: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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);