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 70DEAC433F5 for ; Wed, 27 Oct 2021 01:22:09 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id AA13360C4B for ; Wed, 27 Oct 2021 01:22:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org AA13360C4B 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 3DB00940008; Tue, 26 Oct 2021 21:22:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 36204940007; Tue, 26 Oct 2021 21:22:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 22A1F940008; Tue, 26 Oct 2021 21:22:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0070.hostedemail.com [216.40.44.70]) by kanga.kvack.org (Postfix) with ESMTP id 0D2F5940007 for ; Tue, 26 Oct 2021 21:22:08 -0400 (EDT) Received: from smtpin11.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id A5BE88249980 for ; Wed, 27 Oct 2021 01:22:07 +0000 (UTC) X-FDA: 78740466294.11.A5866AD Received: from mail-lj1-f169.google.com (mail-lj1-f169.google.com [209.85.208.169]) by imf30.hostedemail.com (Postfix) with ESMTP id 50344E0019BA for ; Wed, 27 Oct 2021 01:21:57 +0000 (UTC) Received: by mail-lj1-f169.google.com with SMTP id o26so1945874ljj.2 for ; Tue, 26 Oct 2021 18:22:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=YXjf3eGpY1i6FTdK0pzrewu/J0t+i5gtLQ2TvcFFJOY=; b=PxVapz5Uqh/H1c4ohK8jJVIMexXh+95TCfq2vs9j8PwNACM/FvIfg5nt8P9lxazoor p25NImF9tbvqauK4fXRXGntjxBnJlNerDMLR0YcsfgPUiEIn63EWj9oU5OvcmtcOdRvn W35pn9w5/RVcCQW3Fw9EQvhNbaR1f7aqFHI30nMVxM1MWsQ/F+qUAZnW5Gmak4+VYzWu Etk+Fsn1i22vTM6A2vd5cpJ2lv7qwepnXknS002FXOyYpCz3rMY8Wo6ihBC8yXwz19+8 nyTf5y2UgCmA6EIaso4lx0b/b9J8kmxge9EiP0N9b+Ya/mCE8Jyrv8cghLK6jjB/2+oE 0Tow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=YXjf3eGpY1i6FTdK0pzrewu/J0t+i5gtLQ2TvcFFJOY=; b=Yhe/cmPLSq1UImqZKSoq8pkgatrY/n1hK/h6N3y++aQu+MRdiFTmXmlI29P0MB3ifu u0kGIgRb5bdA5Mgf90PdugEL3Na5oYN7/Bf3x+nIsJWAxyHO3Gpk2d6U0VleorVhJe72 VV0XQ6XFkvWqKzBhPB8nF5/dRHq9L7z1EieHPn6SnKRpJ+J42Dk+3MmV+ardy+//wDtz YmBrjPG+e84YzUxRFdvlndqYPNx8ls4H9liMCi8CwIqolD0NWNEq3lV/xTVdKlSL3n4p vIus+CmIvy7//kpzAA8366+488MjDybBmWtKN/2lPjLjnLlfE5h44DxjeJGTbO39JuC7 bC3Q== X-Gm-Message-State: AOAM532QEPChCjb1oOkpliP3a0IHBL39Bx22j05I03c2+OufjS8bH+kd 2nQnPPsd8k1cJSoE3N1YsELuzy9VaHlndsct3MeiyA== X-Google-Smtp-Source: ABdhPJzEFKcMzaHxg5KoZbQlEcu4egmXSM6hrKj50R2HpUoChXbgvFqtcAmo/5SY71whr717TbZXtKdEI7jkWOi8x84= X-Received: by 2002:a2e:810c:: with SMTP id d12mr30018141ljg.177.1635297725570; Tue, 26 Oct 2021 18:22:05 -0700 (PDT) MIME-Version: 1.0 References: <20211026173822.502506-1-pasha.tatashin@soleen.com> <20211026173822.502506-2-pasha.tatashin@soleen.com> In-Reply-To: From: Pasha Tatashin Date: Tue, 26 Oct 2021 21:21:29 -0400 Message-ID: Subject: Re: [RFC 1/8] mm: add overflow and underflow checks for page->_refcount To: Matthew Wilcox Cc: LKML , linux-mm , linux-m68k@lists.linux-m68k.org, Anshuman Khandual , Andrew Morton , william.kucharski@oracle.com, Mike Kravetz , Vlastimil Babka , Geert Uytterhoeven , schmitzmic@gmail.com, Steven Rostedt , Ingo Molnar , Johannes Weiner , Roman Gushchin , Muchun Song , weixugc@google.com, Greg Thelen Content-Type: text/plain; charset="UTF-8" X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 50344E0019BA X-Stat-Signature: xcip8kejf83bwy4dyuhmi6e4euerm41u Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=soleen.com header.s=google header.b=PxVapz5U; spf=pass (imf30.hostedemail.com: domain of pasha.tatashin@soleen.com designates 209.85.208.169 as permitted sender) smtp.mailfrom=pasha.tatashin@soleen.com; dmarc=none X-HE-Tag: 1635297717-259135 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: On Tue, Oct 26, 2021 at 5:34 PM Pasha Tatashin wrote: > > On Tue, Oct 26, 2021 at 3:50 PM Matthew Wilcox wrote: > > > > On Tue, Oct 26, 2021 at 05:38:15PM +0000, Pasha Tatashin wrote: > > > static inline void page_ref_add(struct page *page, int nr) > > > { > > > - atomic_add(nr, &page->_refcount); > > > + int ret; > > > + > > > + VM_BUG_ON(nr <= 0); > > > + ret = atomic_add_return(nr, &page->_refcount); > > > + VM_BUG_ON_PAGE(ret <= 0, page); > > > > This isn't right. _refcount is allowed to overflow into the negatives. > > See page_ref_zero_or_close_to_overflow() and the conversations that led > > to it being added. > > #define page_ref_zero_or_close_to_overflow(page) \ > 1204 ((unsigned int) page_ref_count(page) + 127u <= 127u) > > > Uh, right, I saw the macro but did not realize there was an (unsigned int) cast. > > OK, I think we can move this macro inside: > include/linux/page_ref.h > > modify it to something like this: > #define page_ref_zero_or_close_to_overflow(page) \ > ((unsigned int) page_ref_count(page) + v + 127u <= v + 127u) > > The sub/dec can also be fixed to ensure that we do not underflow but > still working with the fact that we use all 32bits of _refcount. I think we can do that by using: atomic_fetch_*() and check for overflow/underflow after operation. I will send the updated series soon. Pasha