Message ID | 20211208203544.2297121-4-pasha.tatashin@soleen.com (mailing list archive) |
---|---|
State | New |
Headers | show
Return-Path: <owner-linux-mm@kvack.org> X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 37A90C433F5 for <linux-mm@archiver.kernel.org>; Wed, 8 Dec 2021 20:37:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 845BC6B0075; Wed, 8 Dec 2021 15:36:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7CCE96B0078; Wed, 8 Dec 2021 15:36:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6954A6B007B; Wed, 8 Dec 2021 15:36:03 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0051.hostedemail.com [216.40.44.51]) by kanga.kvack.org (Postfix) with ESMTP id 5C11A6B0075 for <linux-mm@kvack.org>; Wed, 8 Dec 2021 15:36:03 -0500 (EST) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 22A688249980 for <linux-mm@kvack.org>; Wed, 8 Dec 2021 20:35:53 +0000 (UTC) X-FDA: 78895783386.12.CA4EFF5 Received: from mail-qv1-f45.google.com (mail-qv1-f45.google.com [209.85.219.45]) by imf09.hostedemail.com (Postfix) with ESMTP id 9CE4E3000109 for <linux-mm@kvack.org>; Wed, 8 Dec 2021 20:35:52 +0000 (UTC) Received: by mail-qv1-f45.google.com with SMTP id m17so3293454qvx.8 for <linux-mm@kvack.org>; Wed, 08 Dec 2021 12:35:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=foMj7rdrqKExpuW/5b2/9AtW6rfu8q0fYE86wv94MNg=; b=Usqr6WHfajDNAQvGjwiUgRjegcPmoT8iAYMVVkdTi17iBAWK6NqycYpldReeX4Kp/X CLophKDJ0Fzd92y2a34OoBrtYFRDQQ+v2E/Y73DMFzLVtH9dZ59Qalshv02NEdQtVF2f +unp2bAFPXD4y8jz4D1R08C2Vbp7ZeWRtuxqkn2NJX1QMqw4fOzaRmIGAGXldbZXm2nU nkeiZhh/VTopJ2LwKrEb0RyOL933F/SdVt9trRx8fexaJjRvxKvvRPBYCNef9558SVt3 oYU/C2wyyUTd4VFb7fBh9j68V+Ctwww/JRUiEQXzzjHLK2VKyWGEuUa2l5gxHKOlHYeu nqow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=foMj7rdrqKExpuW/5b2/9AtW6rfu8q0fYE86wv94MNg=; b=tsDvsPTJRyzAIqMFQhtNHxBXSnkViHSsT35X/Nj24h44E8hRdMZ7zK1Er8HldAwG5m vn7MCYzFHkktdONrsDrsT3KmeuZSDREPEbMsFKFb5Fn1N1CR5lpKc0TaELS9u2lPF01V ErDJaMvxiv9gEN2oP2XatN3POylv3IU7QFTuFuzRzAhWKKxTd0jhr4uv7/KNMxQonRKC et7pTi03IAAKLUtV/wbA5KPxAwshy/7RpPqgwSBR1lPfex2AC0CFbSlHupZCf8+6lct3 /J0DZEyYC5jpMjLqzq+rNRzLtlKxKFsruBdxNbJtsLbGKncj16igvt4YSx2Lh3B2zhYE x52Q== X-Gm-Message-State: AOAM532tHbz6e3aDgXwO/yFaMmeiUNsr6zCQOAwdNnz7WdWvJR3FhMFk J/5Uv/DlHDlyeRpHpL0Zn//SJQ== X-Google-Smtp-Source: ABdhPJyH976re0ArjKDAQ3EQSjXztAKBneGw7A3SAjIUjHZgPDdUMU9iIx+yXRn1v6AZEDQ6IjlXfw== X-Received: by 2002:a05:6214:20e4:: with SMTP id 4mr10695770qvk.95.1638995751606; Wed, 08 Dec 2021 12:35:51 -0800 (PST) Received: from soleen.c.googlers.com.com (189.216.85.34.bc.googleusercontent.com. [34.85.216.189]) by smtp.gmail.com with ESMTPSA id 143sm1898710qkg.87.2021.12.08.12.35.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Dec 2021 12:35:51 -0800 (PST) From: Pasha Tatashin <pasha.tatashin@soleen.com> To: pasha.tatashin@soleen.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-m68k@lists.linux-m68k.org, anshuman.khandual@arm.com, willy@infradead.org, akpm@linux-foundation.org, william.kucharski@oracle.com, mike.kravetz@oracle.com, vbabka@suse.cz, geert@linux-m68k.org, schmitzmic@gmail.com, rostedt@goodmis.org, mingo@redhat.com, hannes@cmpxchg.org, guro@fb.com, songmuchun@bytedance.com, weixugc@google.com, gthelen@google.com, rientjes@google.com, pjt@google.com Subject: [PATCH 03/10] mm: Avoid using set_page_count() in set_page_recounted() Date: Wed, 8 Dec 2021 20:35:37 +0000 Message-Id: <20211208203544.2297121-4-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.34.1.400.ga245620fadb-goog In-Reply-To: <20211208203544.2297121-1-pasha.tatashin@soleen.com> References: <20211208203544.2297121-1-pasha.tatashin@soleen.com> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 9CE4E3000109 X-Stat-Signature: a554nisnmc5dyexsyoqrcge43gfa7him Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=soleen.com header.s=google header.b=Usqr6WHf; spf=pass (imf09.hostedemail.com: domain of pasha.tatashin@soleen.com designates 209.85.219.45 as permitted sender) smtp.mailfrom=pasha.tatashin@soleen.com; dmarc=none X-HE-Tag: 1638995752-472399 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: <linux-mm.kvack.org> |
Series |
Hardening page _refcount
|
expand
|
diff --git a/mm/internal.h b/mm/internal.h index 94909fcce671..47d1d3c892fb 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -132,9 +132,11 @@ static inline bool page_evictable(struct page *page) */ static inline void set_page_refcounted(struct page *page) { + int refcnt; + VM_BUG_ON_PAGE(PageTail(page), page); - VM_BUG_ON_PAGE(page_ref_count(page), page); - set_page_count(page, 1); + refcnt = page_ref_inc_return(page); + VM_BUG_ON_PAGE(refcnt != 1, page); } extern unsigned long highest_memmap_pfn;
set_page_refcounted() converts a non-refcounted page that has (page->_refcount == 0) into a refcounted page by setting _refcount to 1. The current apporach uses the following logic: VM_BUG_ON_PAGE(page_ref_count(page), page); set_page_count(page, 1); However, if _refcount changes from 0 to 1 between the VM_BUG_ON_PAGE() and set_page_count() we can break _refcount, which can cause other problems such as memory corruptions. Instead, use a safer method: increment _refcount first and verify that at increment time it was indeed 1. refcnt = page_ref_inc_return(page); VM_BUG_ON_PAGE(refcnt != 1, page); Use page_ref_inc_return() to avoid unconditionally overwriting the _refcount value with set_page_count(), and check the return value. Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com> --- mm/internal.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)