From patchwork Tue Dec 21 15:01:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pasha Tatashin X-Patchwork-Id: 12689811 Return-Path: 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 66A35C43219 for ; Tue, 21 Dec 2021 15:01:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F340A6B0075; Tue, 21 Dec 2021 10:01:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EE3586B0078; Tue, 21 Dec 2021 10:01:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DABA16B007B; Tue, 21 Dec 2021 10:01:58 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0054.hostedemail.com [216.40.44.54]) by kanga.kvack.org (Postfix) with ESMTP id CD4E86B0075 for ; Tue, 21 Dec 2021 10:01:58 -0500 (EST) Received: from smtpin26.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 8FF59805C1 for ; Tue, 21 Dec 2021 15:01:58 +0000 (UTC) X-FDA: 78942116316.26.1CD6195 Received: from mail-qk1-f175.google.com (mail-qk1-f175.google.com [209.85.222.175]) by imf17.hostedemail.com (Postfix) with ESMTP id 1F13440087 for ; Tue, 21 Dec 2021 15:01:35 +0000 (UTC) Received: by mail-qk1-f175.google.com with SMTP id t6so12792599qkg.1 for ; Tue, 21 Dec 2021 07:01:46 -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=8OLKCLK70MdlV7lY3Fm4nJPND3+4SBjM5tfYfr3at+o=; b=J7/VHH0JOS8YGb7cCfiDWVyL9L52RvtQXQMwPLoGbG4THf4d7TZ+nr3GN+lZxvr3gt DR+3zRsmjf8qmyeuY98lOO6DG0APhdsS3T9PbV7zB70pmM2ErA+pPzEESJOwAv5Fi81w KtsaENFiwW8dBAVRiZ7W+I6iah2RetGECM6ejHg3WUYuyUdI9g0vOUEOBGedrKnZPVaQ rSd6Yw4YzULM473hI0xFGj7CPWywVbUSzXVqRK4jNNnUPE9VstMypT1I0KCXBUKyb9WL V/LJtDyszKXavEJ85nP31HaFTBUoUKK4UZeO7tPUy+R2KUqthGLCG7zbzK/iMb56yo+U SmxQ== 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=8OLKCLK70MdlV7lY3Fm4nJPND3+4SBjM5tfYfr3at+o=; b=4P86xaWnQPReo5/InvU1w/GaPAny+PThEsX3xX5pUr0Qu7tlCjprL3WdZX0VWKp3dX I64GMLGWxPKnSTsA5W1Wee6cKWTYbWm+aKTPj6ZccXIKoCK3ktRYnn8XebUtkoZcxQnU b3CUGRu1WpbRLefRw3Zy8L1MntaBatCKbsDAMSTwR1UM5kOFhlfNXgxNT4M0+Tqug85b W4xaY2c0rmt97cRuBjwKzWXDbiovNHImBBU0qyyHQ4NAh9eL2I4tV0llbkU5n2rKo8Y1 gNzhxxy11WgxTAu+raQWvueM4BM11QStsXtQw5Mg52TGSjPtSoJmgPRTru4YJHBWDpJp OZzA== X-Gm-Message-State: AOAM5319EQrG9KYEAl91uwqSOum5YBgGlsHnjvHj4jy630/jG0FEwBhT JuIighwkRNIlJsK1oI5dngdXFA== X-Google-Smtp-Source: ABdhPJzN9SRSZZItQKCqO558WN2Y7tM5Mh2bBAZZpVvE6EZpGwsiLYjKEfjD3vbJIkTtFd7w2FLsYQ== X-Received: by 2002:a05:620a:c4f:: with SMTP id u15mr2250058qki.565.1640098905805; Tue, 21 Dec 2021 07:01:45 -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 d4sm1991371qkn.79.2021.12.21.07.01.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Dec 2021 07:01:45 -0800 (PST) From: Pasha Tatashin 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 v2 2/9] mm: Avoid using set_page_count() in set_page_recounted() Date: Tue, 21 Dec 2021 15:01:33 +0000 Message-Id: <20211221150140.988298-3-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.34.1.307.g9b7440fafd-goog In-Reply-To: <20211221150140.988298-1-pasha.tatashin@soleen.com> References: <20211221150140.988298-1-pasha.tatashin@soleen.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 1F13440087 X-Stat-Signature: yonp54derpjxahre55o8hs7u5mnpns6z Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=soleen.com header.s=google header.b="J7/VHH0J"; dmarc=none; spf=pass (imf17.hostedemail.com: domain of pasha.tatashin@soleen.com designates 209.85.222.175 as permitted sender) smtp.mailfrom=pasha.tatashin@soleen.com X-Rspamd-Server: rspam02 X-HE-Tag: 1640098895-828409 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: 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 --- mm/internal.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index deb9bda18e59..4d45ef2ffea6 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -162,9 +162,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;