Message ID | 20220126183429.1840447-3-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 EEA16C2BA4C for <linux-mm@archiver.kernel.org>; Wed, 26 Jan 2022 18:34:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 69D546B0073; Wed, 26 Jan 2022 13:34:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5C2606B0078; Wed, 26 Jan 2022 13:34:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3690B6B0078; Wed, 26 Jan 2022 13:34:35 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0005.hostedemail.com [216.40.44.5]) by kanga.kvack.org (Postfix) with ESMTP id 233846B0073 for <linux-mm@kvack.org>; Wed, 26 Jan 2022 13:34:35 -0500 (EST) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id DA9FF1816C8DC for <linux-mm@kvack.org>; Wed, 26 Jan 2022 18:34:34 +0000 (UTC) X-FDA: 79073288868.16.9EC2F4F Received: from mail-qv1-f51.google.com (mail-qv1-f51.google.com [209.85.219.51]) by imf09.hostedemail.com (Postfix) with ESMTP id 7CAB6140019 for <linux-mm@kvack.org>; Wed, 26 Jan 2022 18:34:34 +0000 (UTC) Received: by mail-qv1-f51.google.com with SMTP id b12so627260qvz.5 for <linux-mm@kvack.org>; Wed, 26 Jan 2022 10:34:34 -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=4mw64hU1ZpbwSPVpRDpj2TUUxEzGleIpOUNlQVlyvBg=; b=F3hPkfc5vl0lCp4Fc8VE6hdllDFjQ/KJ4ChxD81oXql/kI+Wqg+5Eg6mJp84q5ARi0 bwTA2AeDywIsvmiI0bsQwfYhuw5oHutsV5TEGXWen0Fswl2QklzgqTABOo6pfqUX6H4A 9iw9QJC5YKH4ek5QFHZ7USKm0q6pF0Hv/5ucF13RYA0PqDHQMv+uzuEM91G9R/l4ehP2 FKA2+uQkBZtYXRHaOK3gVvGZ6YGKSt/5ZzAzKnPdoMy8jM7rcvPnHIKlRB/z3n0fM5tC qw27euVm+4l4GXriH7OI+46l5IztN1E0Q7uHd4+RVdjriFD0RFh1sJb4k12yDj0T6ywZ gvCQ== 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=4mw64hU1ZpbwSPVpRDpj2TUUxEzGleIpOUNlQVlyvBg=; b=lBWhpOqbOtatWJNKMFNffbD6ldOaxLEjEnXeOjRDzudTejA9wwjgXuaOeYxwKAywmy Q3QApXJzs8l/6yuyvMk8BhkIIF3jEE8NS1Vcs2FB8LwZylD95paRXrDvE8y4fRe5flxT 1uaxwfQJk+g2pp5Czmrx2Wh6sQYvZ+FfcAUpYqYSH/hq4rRWZu/Zh67Q19Nc+jUf7hVv +sWpqwVqVznfaRcloZ6/n0r6GJKnwWCHCR8xLWpHHahl15q/YqFgcot7neLQdI7dkQkN 8yIaTB0zr9NKPhW7ZDMj8U6FVvMbcuzQSwW+evuDeaK8BmJCtw4C2aOeXDWkuI2YKKie bCbg== X-Gm-Message-State: AOAM530qxFXVpaO3yNU/bw612r8DaAhoywpmHOKTObt3LtIpV81LGdmU ZyuvdiZhYTg0ouwZkMAhb1reSA== X-Google-Smtp-Source: ABdhPJwb0TuIX2qSbyMJVsMfaoSMdbbLW4f4sRMAfDNGo3TL8yjF/ORtCOVu1ucqYV6uSVM24X6+vg== X-Received: by 2002:a05:6214:401b:: with SMTP id kd27mr77991qvb.22.1643222073781; Wed, 26 Jan 2022 10:34:33 -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 u17sm35886qki.12.2022.01.26.10.34.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jan 2022 10:34:33 -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, hughd@google.com Subject: [PATCH v3 2/9] mm: Avoid using set_page_count() in set_page_recounted() Date: Wed, 26 Jan 2022 18:34:22 +0000 Message-Id: <20220126183429.1840447-3-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.35.0.rc0.227.g00780c9af4-goog In-Reply-To: <20220126183429.1840447-1-pasha.tatashin@soleen.com> References: <20220126183429.1840447-1-pasha.tatashin@soleen.com> MIME-Version: 1.0 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 7CAB6140019 X-Rspam-User: nil Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=soleen.com header.s=google header.b=F3hPkfc5; dmarc=none; spf=pass (imf09.hostedemail.com: domain of pasha.tatashin@soleen.com designates 209.85.219.51 as permitted sender) smtp.mailfrom=pasha.tatashin@soleen.com X-Stat-Signature: bx314xyayjesg9pmmc1kosy88xq7n6ub X-HE-Tag: 1643222074-575398 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 4c2d06a2f50b..6b74f7f32613 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -141,9 +141,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(-)