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 10048C433EF for ; Tue, 26 Oct 2021 01:35:41 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E689961074 for ; Tue, 26 Oct 2021 01:35:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org E689961074 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 58AD980007; Mon, 25 Oct 2021 21:35:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 53A10940007; Mon, 25 Oct 2021 21:35:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 450D980007; Mon, 25 Oct 2021 21:35:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0225.hostedemail.com [216.40.44.225]) by kanga.kvack.org (Postfix) with ESMTP id 3584B940007 for ; Mon, 25 Oct 2021 21:35:39 -0400 (EDT) Received: from smtpin03.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id DD3E9183956F4 for ; Tue, 26 Oct 2021 01:35:38 +0000 (UTC) X-FDA: 78736871556.03.BBA6455 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf29.hostedemail.com (Postfix) with ESMTP id 1BA519000163 for ; Tue, 26 Oct 2021 01:35:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=+8di00+Tplg8eRJpLLNj0aaGgJcmrPNm4/bOBU/fBh4=; b=MZkVxrFIbgtxGDx2ehqwHCqMfA p5JCYrIfEqMQ83vkTJyVX+AcWTc6j0lgPDaAPYKTdND02ubnmxCidxMHtCyuNxIemxoDIPelZMHec IkfEP3LP6rToSk2pgmKz9cdhRzv4nAnuEpXQcNjkAIvxRJKGLhCPKF1DZCnPEd0WdLFYQBeb+FhTn ibk+9LAjgpOv+NJlT0SFmr+c3xUWm3bkZVOm2SeiyKzLYdu+2iykA1hBM4FIwFCOEGgD5BWFb5q1b zeYsqg2SxFtCKf1IpUDTcM5A6BJmya+YK0ocJ/3tFDhMGbrmgErK1KB3E3pX241cov4rFG7NT9T6s IQtB/FEw==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1mfBM6-00GYwf-9v; Tue, 26 Oct 2021 01:34:30 +0000 Date: Tue, 26 Oct 2021 02:34:22 +0100 From: Matthew Wilcox To: Linus Torvalds Cc: Kees Cook , Linux Kernel Mailing List , Linux-MM , Jordy Zomer , James Bottomley , Mike Rapoport , Andrew Morton Subject: Re: [PATCH] secretmem: Prevent secretmem_users from wrapping to zero Message-ID: References: <20211025181634.3889666-1-willy@infradead.org> <202110251225.D01841AE67@keescook> <202110251402.ADFA4D41BF@keescook> <202110251438.1762406A5@keescook> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 1BA519000163 X-Stat-Signature: sgis4i9mek7jnsjxrucsohrjk19q6may Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=MZkVxrFI; spf=none (imf29.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org; dmarc=none X-HE-Tag: 1635212136-938779 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 Mon, Oct 25, 2021 at 04:37:01PM -0700, Linus Torvalds wrote: > For example, traditionally, on x86, with atomic counting there are > three special situations: negative, 0 and positive. So if you use the > traditional x86 counting atomics (just add/sub/inc/dec, no xadd) then > there are situations where you can get more information about the > result in %eflags if you don't use zero as the initial value, but -1. > > Because then you can do "inc", and if ZF is set, you know you were the > _first_ person to increment it. And when you use "dec", and SF is set > afterwards, you know you are the _last_ person to decrement it. > > That was useful when things like "xadd" weren't available, and cmpxchg > loops are expensive. So we used to have counters where -1 was that > "zero point". Very similar to your "1 is the zero point". > > But was it _logical_? No. It was an implementation trick. I think > we've removed all those cases because it was so subtle and confusing > (but maybe we still have it somewhere - I did not check). We still do it for page->_mapcount; it's biased to -1 so that both "the page has exactly one mapping" and "the page has no mapping" are cheaply checkable conditions. But, as you say, that's invisible to the users. page_mapcount() is return atomic_read(&page->_mapcount) + 1;