Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp2225954ybx; Fri, 8 Nov 2019 01:41:30 -0800 (PST) X-Google-Smtp-Source: APXvYqwMHVPUtAawO8iKRW9NpPjkeqHJkmWybsayynjz8iGdAWkrFU5nDRDNISWZPul3FydYEuP7 X-Received: by 2002:a50:9555:: with SMTP id v21mr9255407eda.90.1573206089903; Fri, 08 Nov 2019 01:41:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573206089; cv=none; d=google.com; s=arc-20160816; b=DALHdlhHRepTYnhnsjFsIdcuvTEv8bLjNowskmFMVcSmjpKw2PGvqrDC+DR/AIvPI8 sfcTl3JsT1oFBxaP5e8npkny6DycLwf+9UY0tsZhrgrO5ULob1J/kPxBOyxb6J1eLXM5 tz9TyXbHzeOwCDxOuzx3bnaqk4ZVQmyvNiyNsPyqKhxp4XHQLMYaiDykr60H60ZxzZNa 2nxj479/ArD8F1TO28ZbWhPre/3ZJFT+Pn7K6gj5HKKxSmUa4tqBfID36CWdcZzEZsK0 CgK7NQg+k6HhKfeQ2kmIO/Qvh0ujIYrB55gFPlUr3Sl2+SiVfSYFN/FS1BY8SBda+wK5 VpXw== 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=gByi40YOOssesMvv2k6O/fmCIGsJIz+vQW5UlIpcNXU=; b=YEfgtnLODlpK7mlPnAKo/ZBu8rk/+B6U4qNT4KPHqgPjcszRJBteDJ9q7f1HrekWj7 nQRSSl4IGJkT5jkGxKL6Jaj3z0Jkmgp6edBEWDBDLXwE5cxfmlTMW9O0n4Ti4cgom5Ew nuD20W/FVTkmS3LqcNT+gmVtNSGMhD5xFMnroJILkLRHHB+bWiBlCixooG4ven7Cc+DO Wo4QDwRgSd3ifMJrc5utAzw2z/5GcNiDMgRTsT3t1t9D5n1mmsnJL/6HRNKMw+vbtpr1 7lYP6anDk4y9HQA+USHSvanrqUkkg5LcKAZWiOEs3VLmYGpMe4YZUdS1UQwCaeVUHBE6 VFZQ== 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 n6si2929395edi.177.2019.11.08.01.41.06; Fri, 08 Nov 2019 01:41:29 -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 S1731299AbfKHJi0 (ORCPT + 99 others); Fri, 8 Nov 2019 04:38:26 -0500 Received: from mx2.suse.de ([195.135.220.15]:39608 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1730005AbfKHJi0 (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 56883AC79; 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 3/8] mm: make page ref count overflow check tighter and more explicit Date: Fri, 8 Nov 2019 10:38:09 +0100 Message-Id: <20191108093814.16032-4-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 f958d7b528b1b40c44cfda5eabe2d82760d868c3 upstream. [ 4.4 backport: page_ref_count() doesn't exist, introduce it to reduce churn. Change also two similar checks in mm/internal.h ] We have a VM_BUG_ON() to check that the page reference count doesn't underflow (or get close to overflow) by checking the sign of the count. That's all fine, but we actually want to allow people to use a "get page ref unless it's already very high" helper function, and we want that one to use the sign of the page ref (without triggering this VM_BUG_ON). Change the VM_BUG_ON to only check for small underflows (or _very_ close to overflowing), and ignore overflows which have strayed into negative territory. 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 | 11 ++++++++++- mm/internal.h | 5 +++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index ed653ba47c46..997edfcb0a30 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -488,6 +488,15 @@ static inline void get_huge_page_tail(struct page *page) extern bool __get_page_tail(struct page *page); +static inline int page_ref_count(struct page *page) +{ + return atomic_read(&page->_count); +} + +/* 127: arbitrary random number, small enough to assemble well */ +#define page_ref_zero_or_close_to_overflow(page) \ + ((unsigned int) page_ref_count(page) + 127u <= 127u) + static inline void get_page(struct page *page) { if (unlikely(PageTail(page))) @@ -497,7 +506,7 @@ static inline void get_page(struct page *page) * Getting a normal page or the head of a compound page * requires to already have an elevated page->_count. */ - VM_BUG_ON_PAGE(atomic_read(&page->_count) <= 0, page); + VM_BUG_ON_PAGE(page_ref_zero_or_close_to_overflow(page), page); atomic_inc(&page->_count); } diff --git a/mm/internal.h b/mm/internal.h index f63f4393d633..a6639c72780a 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -81,7 +81,8 @@ static inline void __get_page_tail_foll(struct page *page, * speculative page access (like in * page_cache_get_speculative()) on tail pages. */ - VM_BUG_ON_PAGE(atomic_read(&compound_head(page)->_count) <= 0, page); + VM_BUG_ON_PAGE(page_ref_zero_or_close_to_overflow(compound_head(page)), + page); if (get_page_head) atomic_inc(&compound_head(page)->_count); get_huge_page_tail(page); @@ -106,7 +107,7 @@ static inline void get_page_foll(struct page *page) * Getting a normal page or the head of a compound page * requires to already have an elevated page->_count. */ - VM_BUG_ON_PAGE(atomic_read(&page->_count) <= 0, page); + VM_BUG_ON_PAGE(page_ref_zero_or_close_to_overflow(page), page); atomic_inc(&page->_count); } } -- 2.23.0