From patchwork Tue Oct 26 17:38:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pasha Tatashin X-Patchwork-Id: 12585329 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A023AC433F5 for ; Tue, 26 Oct 2021 17:38:34 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 522A36109D for ; Tue, 26 Oct 2021 17:38:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 522A36109D Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=soleen.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 2626E94000C; Tue, 26 Oct 2021 13:38:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2117A940007; Tue, 26 Oct 2021 13:38:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 03B2A94000C; Tue, 26 Oct 2021 13:38:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0193.hostedemail.com [216.40.44.193]) by kanga.kvack.org (Postfix) with ESMTP id E8994940007 for ; Tue, 26 Oct 2021 13:38:29 -0400 (EDT) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id AD38582499B9 for ; Tue, 26 Oct 2021 17:38:29 +0000 (UTC) X-FDA: 78739297938.04.CCBD2A0 Received: from mail-qt1-f182.google.com (mail-qt1-f182.google.com [209.85.160.182]) by imf26.hostedemail.com (Postfix) with ESMTP id 4C30120019C9 for ; Tue, 26 Oct 2021 17:38:30 +0000 (UTC) Received: by mail-qt1-f182.google.com with SMTP id t40so1959801qtc.6 for ; Tue, 26 Oct 2021 10:38:29 -0700 (PDT) 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=amB34lO2XSg7TLKdijE9Lv8Nvadz5QaO2Wd8mVuWFPM=; b=UvkQGJH2xUki3bT8TP/u4V57Jx6YNVYOhlR8XwaZpYU/cLXky/a+Odx+QEHRAwKZP4 5tYqZn2x2c5ykd+SfCswrBnOWsUHwScQRSclAnkFdKauHvPc8cXV75EtqNhxHJh3Ny3W S4u8E/0j9mKiuFwnUwWYcXrYRTeBBH7FDBt6kOs6nbtiGvYM6ImHoN7ldfieIWTYdpto /NTuu9yahMryb90AwSlKtEWgljomhEWbYV4oPPdWqDEzilTmSlUfS1wkPQeX0mzI9hJK VSHT9awQHLM4F3LDCBqavcVaebKl0cwF4q1bd6Xp7W5xDDtd7NyQmlbXOrG8KiagHBqH Nj7g== 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=amB34lO2XSg7TLKdijE9Lv8Nvadz5QaO2Wd8mVuWFPM=; b=z3l9uPupeEezXNfF148PsaNCblU0RS8f7QjFQk1leGjejgpPXQ7lb5c5JxJgtitAx9 IYhQraqk51QYTOQ/DTtm0N0WXBFf7fBtrOf1h7F0AH2yTugHvaEZwsI8yx0sX0Zn/7Y3 +uAWwO007fhHg6dnbUZyZ15cp2YzTExtmzE75N90StVMBHtVM7khtnSeEF9p2iVXM3C3 MAl3TE9vqPvP9cJTdiVwcIEhyJk8roXPkOM8ORhajRzC1RqTC6JZQhnyyTMjdo9YH7gM P8UF3rb/lgK2APOrKzL/JxtSTaBBC/2IPm2GAIYo57UITKXwzDkt8+iOSaQWlFKXbB15 kk/A== X-Gm-Message-State: AOAM533kD8CxeO/bZAHQ5kUqVB7xURIkZPUS+0Mnr8vymNwuWSRxnvcx xtMrD17rE4xtKyRg7Gl/ssR+aw== X-Google-Smtp-Source: ABdhPJyVeZK2MwFq2jYs4GVzJmbEbYXlrpRJehMXT5vxhwOW3NH+YWuXHJQV1KbM6WrKRXcAWA05mw== X-Received: by 2002:ac8:584d:: with SMTP id h13mr26002147qth.41.1635269908771; Tue, 26 Oct 2021 10:38:28 -0700 (PDT) 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 bj37sm11001939qkb.49.2021.10.26.10.38.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Oct 2021 10:38:28 -0700 (PDT) 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 Subject: [RFC 4/8] mm: remove set_page_count() from page_frag_alloc_align Date: Tue, 26 Oct 2021 17:38:18 +0000 Message-Id: <20211026173822.502506-5-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.33.0.1079.g6e70778dc9-goog In-Reply-To: <20211026173822.502506-1-pasha.tatashin@soleen.com> References: <20211026173822.502506-1-pasha.tatashin@soleen.com> MIME-Version: 1.0 X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 4C30120019C9 X-Stat-Signature: snn95dtbza5beoudszxf98m8wsemh3cx Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=soleen.com header.s=google header.b=UvkQGJH2; dmarc=none; spf=pass (imf26.hostedemail.com: domain of pasha.tatashin@soleen.com designates 209.85.160.182 as permitted sender) smtp.mailfrom=pasha.tatashin@soleen.com X-HE-Tag: 1635269910-488572 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_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 what we expected. Signed-off-by: Pasha Tatashin --- include/linux/page_ref.h | 13 +++++++++++++ mm/page_alloc.c | 6 ++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/include/linux/page_ref.h b/include/linux/page_ref.h index b3ec2b231fc7..db7ccb461c3e 100644 --- a/include/linux/page_ref.h +++ b/include/linux/page_ref.h @@ -88,6 +88,19 @@ static inline void init_page_count(struct page *page) set_page_count(page, 1); } +static inline int page_ref_add_return(struct page *page, int nr) +{ + int ret; + + VM_BUG_ON(nr <= 0); + ret = atomic_add_return(nr, &page->_refcount); + VM_BUG_ON_PAGE(ret <= 0, page); + + if (page_ref_tracepoint_active(page_ref_mod_and_return)) + __page_ref_mod_and_return(page, nr, ret); + return ret; +} + static inline void page_ref_add(struct page *page, int nr) { int ret; diff --git a/mm/page_alloc.c b/mm/page_alloc.c index b37435c274cf..6af4596bddc2 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -5510,6 +5510,7 @@ void *page_frag_alloc_align(struct page_frag_cache *nc, unsigned int size = PAGE_SIZE; struct page *page; int offset; + int refcnt; if (unlikely(!nc->va)) { refill: @@ -5548,8 +5549,9 @@ void *page_frag_alloc_align(struct page_frag_cache *nc, /* if size can vary use size else just use PAGE_SIZE */ size = 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 = page_ref_add_return(page, PAGE_FRAG_CACHE_MAX_SIZE + 1); + VM_BUG_ON_PAGE(refcnt != PAGE_FRAG_CACHE_MAX_SIZE + 1, page); /* reset page count bias and offset to start of new frag */ nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1;