From mboxrd@z Thu Jan 1 00:00:00 1970 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 CEB89C63686 for ; Wed, 26 Jan 2022 18:34:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 235D76B0074; Wed, 26 Jan 2022 13:34:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 171176B0078; Wed, 26 Jan 2022 13:34:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EDEE16B007D; Wed, 26 Jan 2022 13:34:37 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0184.hostedemail.com [216.40.44.184]) by kanga.kvack.org (Postfix) with ESMTP id D2DEF6B0074 for ; Wed, 26 Jan 2022 13:34:37 -0500 (EST) Received: from smtpin23.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 82E6B8249980 for ; Wed, 26 Jan 2022 18:34:37 +0000 (UTC) X-FDA: 79073288994.23.0332AC5 Received: from mail-qk1-f174.google.com (mail-qk1-f174.google.com [209.85.222.174]) by imf04.hostedemail.com (Postfix) with ESMTP id 5E3034002C for ; Wed, 26 Jan 2022 18:34:35 +0000 (UTC) Received: by mail-qk1-f174.google.com with SMTP id c189so357983qkg.11 for ; Wed, 26 Jan 2022 10:34:35 -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=6rktNgcbrbK1gQESupfA4dHtrh4dYlEFYGvtlevr0bg=; b=ZOPvyP2LJa1z3UAKsmFkGIvb9SGcukKRw1eJDnZ03Qy7wZ4AeqMOKXH3MMBQGrbIrD /K1gtEKq72kR4D2ZLluXJ7N65IkST6vYrjwupXq4pYplOy24Hwrbt019FVcqjyTfs4uM lsxV8J1HoLT678mTxAyiXcz4eRp1gSd1iPLhXoXTOfQioYdf5YHp5LPIvXTIzhL/PYZg tFa4DDUfUv2/nhf4ixyqPQOrpe0bQFsJHGraZDElfYhxqzwN7HggCEhbvvXk+a5O2CJ8 zuTcV5TSk5C5M3X8ND+XcwEmPXrCXMwHne1ra/PCaFUMgtU1czwB78aFNGC/vAvu+kVG NUuA== 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=6rktNgcbrbK1gQESupfA4dHtrh4dYlEFYGvtlevr0bg=; b=rBfOMNn8RB7WMbm08XG1e9kOQNOWc6yPVOH1yaXrI0k7CxxQhMCu8GJ5qfxjpkKem/ HbLsQDF14LA3RAHurahBXtTqvQ6bs+JlzpS1AUSkssBxZM+Op/LnyIhUohoPWi/5dgEV u5AoT2wIZByS+/2yTyVpuFIbqmfMVXAzWlh32rJF1pC+cMwIZReoeHuRMudlH/PAiabj MxhqjlDnB6womI3v2GUQaZw4jyyQVShwfFIfvKmkiOUJ90PlT6Utk5m7b6Jz+gu/U++c 2hbYCg+kdHQ8O0fNuVFj6a+taFFlRw9QLUZ/7MKWVJxXBK+Bh2ZAE+RoB3QnxT7fG41W FVBQ== X-Gm-Message-State: AOAM531YlcStYkA0Rp+cGqhAHc9Vqzc6iOxHNJxFh/eUaz1vuhloHGCv K4fN3k+ezZrJYTUxKD68Unz5bQ== X-Google-Smtp-Source: ABdhPJyOZcmiDwXUXWvvn+PHKnj59WKkQW/1KnbYA3kyw9zl35YOaxOviGVfeae+pb/kVHJjKl9WDQ== X-Received: by 2002:a05:620a:25ca:: with SMTP id y10mr59059qko.546.1643222074652; Wed, 26 Jan 2022 10:34:34 -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:34 -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, hughd@google.com Subject: [PATCH v3 3/9] mm: remove set_page_count() from page_frag_alloc_align Date: Wed, 26 Jan 2022 18:34:23 +0000 Message-Id: <20220126183429.1840447-4-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: rspam09 X-Rspamd-Queue-Id: 5E3034002C X-Stat-Signature: d5hi53r44j11knwcmugaiaiowsf8qi8o X-Rspam-User: nil Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=soleen.com header.s=google header.b=ZOPvyP2L; spf=pass (imf04.hostedemail.com: domain of pasha.tatashin@soleen.com designates 209.85.222.174 as permitted sender) smtp.mailfrom=pasha.tatashin@soleen.com; dmarc=none X-HE-Tag: 1643222075-2031 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000005, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: set_page_count() unconditionally resets the value of _ref_count and that is dangerous, as it is not programmatically verified. Instead we rely on comments like: "OK, page count is 0, we can safely set it". Add a new refcount function: page_ref_add_return() to return the new refcount value after adding to it. Use the return value to verify that the _ref_count was indeed the expected one. Signed-off-by: Pasha Tatashin --- include/linux/page_ref.h | 11 +++++++++++ mm/page_alloc.c | 6 ++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/include/linux/page_ref.h b/include/linux/page_ref.h index fe4864f7f69c..03e21ce2f1bd 100644 --- a/include/linux/page_ref.h +++ b/include/linux/page_ref.h @@ -115,6 +115,17 @@ static inline void init_page_count(struct page *page= ) set_page_count(page, 1); } =20 +static inline int page_ref_add_return(struct page *page, int nr) +{ + int old_val =3D atomic_fetch_add(nr, &page->_refcount); + int new_val =3D old_val + nr; + + VM_BUG_ON_PAGE((unsigned int)new_val < (unsigned int)old_val, page); + if (page_ref_tracepoint_active(page_ref_mod_and_return)) + __page_ref_mod_and_return(page, nr, new_val); + return new_val; +} + static inline void page_ref_add(struct page *page, int nr) { int old_val =3D atomic_fetch_add(nr, &page->_refcount); diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 8dd6399bafb5..5a9167bda279 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -5528,6 +5528,7 @@ void *page_frag_alloc_align(struct page_frag_cache = *nc, unsigned int size =3D PAGE_SIZE; struct page *page; int offset; + int refcnt; =20 if (unlikely(!nc->va)) { refill: @@ -5566,8 +5567,9 @@ void *page_frag_alloc_align(struct page_frag_cache = *nc, /* if size can vary use size else just use PAGE_SIZE */ size =3D nc->size; #endif - /* OK, page count is 0, we can safely set it */ - set_page_count(page, PAGE_FRAG_CACHE_MAX_SIZE + 1); + /* page count is 0, set it to PAGE_FRAG_CACHE_MAX_SIZE + 1 */ + refcnt =3D page_ref_add_return(page, PAGE_FRAG_CACHE_MAX_SIZE + 1); + VM_BUG_ON_PAGE(refcnt !=3D PAGE_FRAG_CACHE_MAX_SIZE + 1, page); =20 /* reset page count bias and offset to start of new frag */ nc->pagecnt_bias =3D PAGE_FRAG_CACHE_MAX_SIZE + 1; --=20 2.35.0.rc0.227.g00780c9af4-goog