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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 84543C433F5 for ; Tue, 26 Oct 2021 17:38:28 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 185AC60F9D for ; Tue, 26 Oct 2021 17:38:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 185AC60F9D 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 8F7FC940009; Tue, 26 Oct 2021 13:38:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 766D3940007; Tue, 26 Oct 2021 13:38:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 56B38940009; Tue, 26 Oct 2021 13:38:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0108.hostedemail.com [216.40.44.108]) by kanga.kvack.org (Postfix) with ESMTP id 42FF8940007 for ; Tue, 26 Oct 2021 13:38:27 -0400 (EDT) Received: from smtpin36.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 0469C3A8E5 for ; Tue, 26 Oct 2021 17:38:27 +0000 (UTC) X-FDA: 78739297854.36.3F4FA31 Received: from mail-qk1-f180.google.com (mail-qk1-f180.google.com [209.85.222.180]) by imf23.hostedemail.com (Postfix) with ESMTP id E697590000A7 for ; Tue, 26 Oct 2021 17:38:18 +0000 (UTC) Received: by mail-qk1-f180.google.com with SMTP id bj31so16109378qkb.2 for ; Tue, 26 Oct 2021 10:38:26 -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=1MjJIbJlIahDU+iLwwAfOaikWhB3foL3f/r8kdQBxNc=; b=lB5z28Fcx2ZBMKy/ZYv4hZAv3ZLMd31iPtufXn202o9yLe6MI1x3qw9X/RNSvtYIew uv6uXGo0OC2+G7hCGULnU85/pCy9QKRW8qJD/dytf8dC8XCK36AeNyUL7UHpm53+O9CA DU0MsN9PMMOUZ7wItqiRbkpzXWoaFQinJVqxz2VSB2ahBqqcdWGJObKx7fm9OrBZ9vip QCHDJiSw2Ez0E746AlEObFg6cd26FPxxQTLmX3S9ktxx0mK0SaOUfz3vFmtZz0YTb4DT vMlgFvpJtDayjOOWDAGJZBb7Wmzh8KWypWtb/cHBXLavKpY7RcnSuXRHZy3ygu0/Ttqi Y62A== 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=1MjJIbJlIahDU+iLwwAfOaikWhB3foL3f/r8kdQBxNc=; b=HUErd4f5XTVz55mH/LAFhYCRSOx5Nq36Erm1Wj0YexXoLOPgbGkOjEyQnd1UeDW6UL sbxYrF8HZbIlpBPs16Z4ZhnD5aQRE5jkXPXfAvGh2voK0OCuZumcUr+3HGLE72GFSbnm QGCxoxEtRbEWvaHWEPVsP5Xj3RZbiLooUUljkaZDnOnZTSxPtBB4Cp7G9EmcFSEnikQR OW5C7rykkcydMEAX4+Jah5cnTya1QQwFl8JhTpSuUkrDYFIxOwyKmVFhP+xzWXzgf8zA J3UZ4FrrVLQvlu0oG0zdZ6TcAlk+IQdgq4RM1nAtiyEdBrxuIaAbXWXzYvVLkPpZ3Me6 LGUQ== X-Gm-Message-State: AOAM532N8KDhBojsBYafE6ONf5g2EWxwTiqhF7F8IgwcQw8IRPBfdGUo LPcW8xRUcFqgmknPUzyQWDL7HQ== X-Google-Smtp-Source: ABdhPJzM+k476AduvT9DqDoOET4gfPzY9MByvHSnUBjP5xwl3bvdX3hONs7MKtegCmUHu0t+iVRQrA== X-Received: by 2002:a37:2ec6:: with SMTP id u189mr20159144qkh.466.1635269905910; Tue, 26 Oct 2021 10:38:25 -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.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Oct 2021 10:38:25 -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 1/8] mm: add overflow and underflow checks for page->_refcount Date: Tue, 26 Oct 2021 17:38:15 +0000 Message-Id: <20211026173822.502506-2-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: rspam05 X-Rspamd-Queue-Id: E697590000A7 X-Stat-Signature: g7tpmry9xz4dpnku9ucuqf1pe3tstr49 Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=soleen.com header.s=google header.b=lB5z28Fc; spf=pass (imf23.hostedemail.com: domain of pasha.tatashin@soleen.com designates 209.85.222.180 as permitted sender) smtp.mailfrom=pasha.tatashin@soleen.com; dmarc=none X-HE-Tag: 1635269898-651043 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: The problems with page->_refcount are hard to debug, because usually when they are detected, the damage has occurred a long time ago. Yet, the problems with invalid page refcount may be catastrophic and lead to memory corruptions. Reduce the scope of when the _refcount problems manifest themselves by adding checks for underflows and overflows into functions that modify _refcount. Signed-off-by: Pasha Tatashin --- include/linux/page_ref.h | 61 ++++++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 12 deletions(-) diff --git a/include/linux/page_ref.h b/include/linux/page_ref.h index 7ad46f45df39..b3ec2b231fc7 100644 --- a/include/linux/page_ref.h +++ b/include/linux/page_ref.h @@ -90,21 +90,35 @@ static inline void init_page_count(struct page *page) =20 static inline void page_ref_add(struct page *page, int nr) { - atomic_add(nr, &page->_refcount); + int ret; + + VM_BUG_ON(nr <=3D 0); + ret =3D atomic_add_return(nr, &page->_refcount); + VM_BUG_ON_PAGE(ret <=3D 0, page); + if (page_ref_tracepoint_active(page_ref_mod)) __page_ref_mod(page, nr); } =20 static inline void page_ref_sub(struct page *page, int nr) { - atomic_sub(nr, &page->_refcount); + int ret; + + VM_BUG_ON(nr <=3D 0); + ret =3D atomic_sub_return(nr, &page->_refcount); + VM_BUG_ON_PAGE(ret < 0, page); + if (page_ref_tracepoint_active(page_ref_mod)) __page_ref_mod(page, -nr); } =20 static inline int page_ref_sub_return(struct page *page, int nr) { - int ret =3D atomic_sub_return(nr, &page->_refcount); + int ret; + + VM_BUG_ON(nr <=3D 0); + ret =3D atomic_sub_return(nr, &page->_refcount); + VM_BUG_ON_PAGE(ret < 0, page); =20 if (page_ref_tracepoint_active(page_ref_mod_and_return)) __page_ref_mod_and_return(page, -nr, ret); @@ -113,31 +127,43 @@ static inline int page_ref_sub_return(struct page *= page, int nr) =20 static inline void page_ref_inc(struct page *page) { - atomic_inc(&page->_refcount); + int ret =3D atomic_inc_return(&page->_refcount); + + VM_BUG_ON_PAGE(ret <=3D 0, page); + if (page_ref_tracepoint_active(page_ref_mod)) __page_ref_mod(page, 1); } =20 static inline void page_ref_dec(struct page *page) { - atomic_dec(&page->_refcount); + int ret =3D atomic_dec_return(&page->_refcount); + + VM_BUG_ON_PAGE(ret < 0, page); + if (page_ref_tracepoint_active(page_ref_mod)) __page_ref_mod(page, -1); } =20 static inline int page_ref_sub_and_test(struct page *page, int nr) { - int ret =3D atomic_sub_and_test(nr, &page->_refcount); + int ret; + + VM_BUG_ON(nr <=3D 0); + ret =3D atomic_sub_return(nr, &page->_refcount); + VM_BUG_ON_PAGE(ret < 0, page); =20 if (page_ref_tracepoint_active(page_ref_mod_and_test)) __page_ref_mod_and_test(page, -nr, ret); - return ret; + return ret =3D=3D 0; } =20 static inline int page_ref_inc_return(struct page *page) { int ret =3D atomic_inc_return(&page->_refcount); =20 + VM_BUG_ON_PAGE(ret <=3D 0, page); + if (page_ref_tracepoint_active(page_ref_mod_and_return)) __page_ref_mod_and_return(page, 1, ret); return ret; @@ -145,17 +171,21 @@ static inline int page_ref_inc_return(struct page *= page) =20 static inline int page_ref_dec_and_test(struct page *page) { - int ret =3D atomic_dec_and_test(&page->_refcount); + int ret =3D atomic_dec_return(&page->_refcount); + + VM_BUG_ON_PAGE(ret < 0, page); =20 if (page_ref_tracepoint_active(page_ref_mod_and_test)) __page_ref_mod_and_test(page, -1, ret); - return ret; + return ret =3D=3D 0; } =20 static inline int page_ref_dec_return(struct page *page) { int ret =3D atomic_dec_return(&page->_refcount); =20 + VM_BUG_ON_PAGE(ret < 0, page); + if (page_ref_tracepoint_active(page_ref_mod_and_return)) __page_ref_mod_and_return(page, -1, ret); return ret; @@ -163,16 +193,23 @@ static inline int page_ref_dec_return(struct page *= page) =20 static inline int page_ref_add_unless(struct page *page, int nr, int u) { - int ret =3D atomic_add_unless(&page->_refcount, nr, u); + int ret; + + VM_BUG_ON(nr <=3D 0 || u < 0); + ret =3D atomic_fetch_add_unless(&page->_refcount, nr, u); + VM_BUG_ON_PAGE(ret < 0, page); =20 if (page_ref_tracepoint_active(page_ref_mod_unless)) __page_ref_mod_unless(page, nr, ret); - return ret; + return ret !=3D u; } =20 static inline int page_ref_freeze(struct page *page, int count) { - int ret =3D likely(atomic_cmpxchg(&page->_refcount, count, 0) =3D=3D co= unt); + int ret; + + VM_BUG_ON(count <=3D 0); + ret =3D likely(atomic_cmpxchg(&page->_refcount, count, 0) =3D=3D count)= ; =20 if (page_ref_tracepoint_active(page_ref_freeze)) __page_ref_freeze(page, count, ret); --=20 2.33.0.1079.g6e70778dc9-goog