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 X-Spam-Level: X-Spam-Status: No, score=-3.5 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0F973C432BE for ; Fri, 20 Aug 2021 06:44:12 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 81C3B61053 for ; Fri, 20 Aug 2021 06:44:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 81C3B61053 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 050026B0071; Fri, 20 Aug 2021 02:44:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F418B6B0072; Fri, 20 Aug 2021 02:44:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E0A736B0073; Fri, 20 Aug 2021 02:44:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0142.hostedemail.com [216.40.44.142]) by kanga.kvack.org (Postfix) with ESMTP id C3E3D6B0071 for ; Fri, 20 Aug 2021 02:44:10 -0400 (EDT) Received: from smtpin02.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 69C072A037 for ; Fri, 20 Aug 2021 06:44:10 +0000 (UTC) X-FDA: 78494519460.02.B195129 Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) by imf08.hostedemail.com (Postfix) with ESMTP id 92A8930000A8 for ; Fri, 20 Aug 2021 06:44:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; 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=z8KFItNI7mVsJ+C1MoNIJsoXQ10X1syrvXI2kfF+1oU=; b=l43CxEtXJhMMYRI0aDC2rF5ZzB TN47illQ2FfshWt7MAXfTjaTikzcg6U94BkVCtc51tBPw26gM7qZYaeUwhe0Ap/zfeQ1273Bf2Vcs Gsc9XxuuwdUgQdGw6coCXlcw2Q3r1kxGSX3TqGNIwTzGM8EUoo7sxn49VCI6WE1LB6Lj0Yz4YAudw eIG0tVzWCtKXUtnGR6o9+HrzMqDu33WwA2WlelI7+OhZT55CneBaauPDlK2MPm4bw7IfAO0vpcC/p oTVhTQ6Saapsg+btW0uMdBQddylsQb13l8DCp6goC8ALS01TEVEpuHFqe2LO9xO/kFdQ6tLfiQPks gwFyv9Qg==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1mGyFi-00BLrB-Up; Fri, 20 Aug 2021 06:43:43 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 26B59300233; Fri, 20 Aug 2021 08:43:40 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 15F692015BC65; Fri, 20 Aug 2021 08:43:40 +0200 (CEST) Date: Fri, 20 Aug 2021 08:43:40 +0200 From: Peter Zijlstra To: Linus Torvalds Cc: Will Deacon , Andrew Morton , Borislav Petkov , Xiyu Yang , Alistair Popple , Yang Shi , Shakeel Butt , Hugh Dickins , Miaohe Lin , Linux Kernel Mailing List , Linux-MM , yuanxzhang@fudan.edu.cn, Xin Tan , Will Deacon , David Howells , keescook@chromium.org Subject: Re: [PATCH] mm/rmap: Convert from atomic_t to refcount_t on anon_vma->refcount Message-ID: References: <1626665029-49104-1-git-send-email-xiyuyang19@fudan.edu.cn> <20210720160127.ac5e76d1e03a374b46f25077@linux-foundation.org> <20210819132131.GA15779@willie-the-truck> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 92A8930000A8 X-Stat-Signature: pqjadej15ftoxedujfkj1yan31krntfr Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=infradead.org header.s=desiato.20200630 header.b=l43CxEtX; dmarc=none; spf=none (imf08.hostedemail.com: domain of peterz@infradead.org has no SPF policy when checking 90.155.92.199) smtp.mailfrom=peterz@infradead.org X-HE-Tag: 1629441849-102656 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 Thu, Aug 19, 2021 at 12:09:37PM -0700, Linus Torvalds wrote: > On Thu, Aug 19, 2021 at 8:21 AM Peter Zijlstra wrote: > > > > If we can skip the OF... we can do something like this: > > Honestly, I think a lot of the refcount code is questionable. It was > absolutely written with no care for performance AT ALL. That's a bit unfair I feel. Will's last rewrite of the stuff was specifically to address performance issues. > I'm not sure it helps to then add arch-specific code for it without > thinking it through a _lot_ first. > > It might be better to just have a "atomic_t with overflow handling" in > general, exactly because the refcount_t was designed and written > without any regard for code that cares about performance. The primary concern was to use a single unconditional atomic op where possible (mostly fetch_add), as the atomic op dominates whatever else it does. The rest is just because C absolutely sucks at conditions. Doing atomic_t with overflow handling would require doing the whole thing in arch asm. > > static inline bool refcount_dec_and_test(refcount_t *r) > > { > > asm_volatile_goto (LOCK_PREFIX "decl %[var]\n\t" > > "jz %l[cc_zero]\n\t" > > "jns 1f\n\t" > > I think you can use "jl" for the bad case. Duh yes. I clearly didn't have my head on straight. > I think it's better to handle that case out-of-line than play games > with UD, though - this is going to be the rare case, the likelihood > that we get the fixup wrong is just too big. Once it's out-of-line > it's not as critical any more, even if it does add to the size of the > code. Fine with me; although the immediate complaint from Andrew was about size, hence my UD1 hackery. > So if we do this, I think it should be something like > > static inline __must_check bool refcount_dec_and_test(refcount_t *r) > { > asm_volatile_goto (LOCK_PREFIX "decl %[var]\n\t" > "jz %l[cc_zero]\n\t" > "jl %l[cc_error]" > : : [var] "m" (r->refs.counter) > : "memory" : cc_zero, cc_error); > > return false; > > cc_zero: > return true; > cc_error: > refcount_warn_saturate(r, REFCOUNT_SUB_UAF); > return false; > } > > and we can discuss whether we could improve on the > refcount_warn_saturate() separately. I can do the refcount_warn_saturate() change separately. Let me go check how small I can get it... > But see above: maybe just make this a separate "careful atomic_t", > with the option to panic-on-overflow. So then we could get rid of > refcount_warn_saturate() enmtirely above, and instead just have a > (compile-time option) BUG() case, with the non-careful version just > being our existing atomic_dec_and_test. We used to have that option; the argument was made that everybody cares about security and as long as this doesn't show up on benchmarks we good. Also, I don't think most people want the overflow to go BUG, WARN is mostly the right thing and only the super paranoid use panic-on-warn or something.