Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp665111pxb; Thu, 19 Aug 2021 08:23:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxlWfSUhZ1r08cBuZA607wbFMeGh8HnPvX3Wh6XJOOvSOjBclAE3RNwj/uuEsIct0N3mNLr X-Received: by 2002:a50:e789:: with SMTP id b9mr16848798edn.101.1629386602392; Thu, 19 Aug 2021 08:23:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629386602; cv=none; d=google.com; s=arc-20160816; b=SExlMLFSBj56ajP306St+Q8Ptwed09IiCml8OTV3cI+DXKMKErpwu471YGluSpC0Yz KPNSa0LtyVpLGHJMjOp4WgXDJ08rnXlboec31Y8d58DItwCZuNjDRKAT/vGuEj4du52b sWC/Odi4Y7Cd1MPNrFGEtZEOb48MGjxFLho9M1FRB5SONWdgFRrUTos6kT80KIDA+vTb 2b1N1BmF2wriLjZVFFZTDclktYJIeNfS9EbN/Jf1gYBzY9tDM/OveB+/IAR0QAsrki/k ZpeevhoJZLqwGRPNN8ppeII9FIW35gd6vukUgnl3UwPO56nC2ZjGOzfMD6GDk3dU7ryE gtAA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=vKpJrX+WIWY9oRj8Y9x18hpmhdn4wB2b3WzzcTX0gC0=; b=iAo857OKDZhz18XavwXTriYTs6Uerfxe5MJW8Ny5wFylcYVSkVnOlJroXPnQeXVohs y2msxg2KsGFGjOA709897hwT8M5urldWOUefHG0Z3BZBBE1b2Q+hcfhDPrKqWZvP4Lq7 9VrTtuhoVLx+JWYHHgeFnA6nK9SGqU4Dx+e2c47ckmmiDkQwlVQoaBWYpKqbpmZCUwjB e3rkyPGn5uGLW2eQR98BpkU9Fc6LQjnv7VdtQAOTcO764NeNzMTQVSAPozdCS6WbFRmx WjbxcOQenu525SQ02WpN9YARafy44I699xtLzkD39SeSGeJUQkfevICZEnfYN7dd2rr7 VMxQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=iRnWocda; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id eg15si3393493edb.434.2021.08.19.08.22.52; Thu, 19 Aug 2021 08:23:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=iRnWocda; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238427AbhHSPVp (ORCPT + 99 others); Thu, 19 Aug 2021 11:21:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55604 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232821AbhHSPVo (ORCPT ); Thu, 19 Aug 2021 11:21:44 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5787EC061575 for ; Thu, 19 Aug 2021 08:21:08 -0700 (PDT) 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=vKpJrX+WIWY9oRj8Y9x18hpmhdn4wB2b3WzzcTX0gC0=; b=iRnWocda0X8g3D+NKPaswqcI3j WHn9NnZZ4CgorMeNqTGgkR39y1RLlpH6elLst6kb1Hb63uTM4WJCgMHpSiKssszjISMZLwPSl8FZp 7X/7yTHNZIz8F5UJtxtiqV3eAIwt8BBQCuFshxRXSxWoKXCikVe6+sLM9US/Y0/T2aR0keQtK3cav K7XtIguJo/UwAQPO3dvSm4hCo/l+CARRf8Pgwm+s+KJqCY5AxMbCcKL1e/v6+Dg3Xe+z4ZiR+Q3Jq A4bO+zVI3W0fHz1z/UmbS+9eJggicY+FoVtmteGUtZbIfQRDZ8iG+iGANLfKuVTw1F30JazeNM0bj WPnqIjqQ==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1mGjpV-005F79-5t; Thu, 19 Aug 2021 15:19:47 +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 942043004B2; Thu, 19 Aug 2021 17:19:38 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 7B42820242605; Thu, 19 Aug 2021 17:19:38 +0200 (CEST) Date: Thu, 19 Aug 2021 17:19:38 +0200 From: Peter Zijlstra To: Will Deacon Cc: Andrew Morton , bp@alien8.de, Xiyu Yang , Alistair Popple , Yang Shi , Shakeel Butt , Hugh Dickins , Miaohe Lin , linux-kernel@vger.kernel.org, linux-mm@kvack.org, yuanxzhang@fudan.edu.cn, Xin Tan , Will Deacon , Linus Torvalds , dhowells@redhat.com 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: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Aug 19, 2021 at 04:06:45PM +0200, Peter Zijlstra wrote: > > > > * We can implement (1) by checking if we hit zero (ZF=1) > > * We can implement (2) by checking if the new value is < 0 (SF=1). > > We then need to catch the case where the old value was < 0 but the > > new value is 0. I think this is (SF=0 && OF=1). > > > > So maybe the second check is actually SF != OF? I could benefit from some > > x86 expertise here, but hopefully you get the idea. > > Right, so the first condition is ZF=1, we hit zero. > The second condition is SF=1, the result is negative. > > I'm not sure we need OF, if we hit that condition we've already lost. > But it's easy enough to add I suppose. If we can skip the OF... we can do something like this: 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" "ud1 %[var], %%ebx\n\t" "1:" : : [var] "m" (r->refs.counter) : "memory" : cc_zero); return false; cc_zero: smp_acquire__after_ctrl_dep(); return true; } where we encode the whole refcount_warn_saturate() thing into UD1. The first argument is @r and the second argument the REFCOUNT_* thing encoded in register space. It would mean adding something 'clever' to the #UD handler that decodes the trapping instruction and extracts these arguments, but this is the smallest I could get it.