Received: by 2002:a05:6a10:6006:0:0:0:0 with SMTP id w6csp1633191pxa; Fri, 28 Aug 2020 20:07:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyw0Bg5EGgB5Su4b6pNGS0KzN9hkke9ZjBBitMP9rGn0ZZQsCeCrc2iDS87QQTEVGNq01n6 X-Received: by 2002:aa7:dcc6:: with SMTP id w6mr1709979edu.273.1598670434049; Fri, 28 Aug 2020 20:07:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598670434; cv=none; d=google.com; s=arc-20160816; b=RQbPW75zCwQt1sFnmZor9W1xuNRPE2FPs7CWK/ZYp3VSxaJD1KA3ISRrC2U+wMD5hp iK2ZpQOqojI9n3TOk5qMQHLwYPEdEtDpc3AY/+upXOPOFeTPVLmud2jjY2+hzUkNbbB8 V+e+tvaoIz+moYFHa5kyLl6bU8wMz8DkcYvQKwfiDkETtPbXfMGhyI2k1xRmq5n+twd+ mkJjyEUCl7xyfzDG5oL79uU8/7C+fxJlPa/+lxIxygwYPyuUBSQ4zs/XAGkGSdvKpsqd VJdiXoswnSu6nP+ZquiHzBKAhxaxZASoWlvQVYX6UIrGUy+L1Ga+LdcZwA9Mbf4m9kV0 uxpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version; bh=l7xUaI995Ekm5CK+TxZkIt4YFPavrrAEtoUbXr4ArNs=; b=NTHZuLfkh+4W2m20dN8TS36eYSx5I8o5ql1hkSFWGQ2nAKN/+HntwXRi7R2WA/Nro/ /Gtm7emVbZZQog13jJCy3aCsWl4ZBLncdMCS20fe/MF3GJ7x9dqilOnv7Qlu3kJPso3c Rf/ayYZ4l2kidgXdmyTS98hgvWAcaFha3s+yAGlK8mugPCPb2Z9yOu/WczlkUmMFDnPH O9Jb7xVnqo5O5atg3XWaVsUyacy01pbLecYZ+KwZZ8BBmu3opShF59UpgyeWwgLpxg0f xNwEPuMO54GFi/EmP5jNRoJq/mIOoRCZLi66+a2rmnb0H40z5NxHD6BUGHt3OxHwKpil n/kA== ARC-Authentication-Results: i=1; mx.google.com; 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 h12si749445edw.108.2020.08.28.20.06.36; Fri, 28 Aug 2020 20:07:14 -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; 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 S1726798AbgH2DFj (ORCPT + 99 others); Fri, 28 Aug 2020 23:05:39 -0400 Received: from mail-io1-f67.google.com ([209.85.166.67]:34751 "EHLO mail-io1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726395AbgH2DFh (ORCPT ); Fri, 28 Aug 2020 23:05:37 -0400 Received: by mail-io1-f67.google.com with SMTP id w20so1049606iom.1; Fri, 28 Aug 2020 20:05:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=l7xUaI995Ekm5CK+TxZkIt4YFPavrrAEtoUbXr4ArNs=; b=SKfbW6mLkCNVeg3PyztUiSMc+4CdpetkAQHV5x6BnYhm5YYGHdn9RM7TvNEPD/rvAj d5s6ODnlYwfQxYwbhjq2HoeRRmjF/Ze5/n/1sV4It4bD7gONXIIvKAF16fBVLxd8WIwp 51qsQUNPYR0NhqY0/1qkrGj1sBRCNQTfeQTRWs4pIvNM6xYumWNd0/2jy2ygd2c59/Fn E8B7N1W+U4Dhnr2Vzb5HfMFXLBOuIpCop+mNtV/xllHTg92jK23SY/Zxlu3cA4bqaf7T 85StxZz+FujzDeTAWKKg/Zqu5WIr8VQzo5hiaDvYFXMtnUbtSPYBXCgk1V74UhQTriep dI/g== X-Gm-Message-State: AOAM533HaFhSftmSeF6BfoU56THUlRkwlRz77enVx3xPbQv2w5L3ABfh qqPjsjTL4pZIHFX5h8dfFNJUqd/ZPjJyQIAnprU= X-Received: by 2002:a02:840f:: with SMTP id k15mr3841034jah.100.1598670336665; Fri, 28 Aug 2020 20:05:36 -0700 (PDT) MIME-Version: 1.0 References: <20200827161237.889877377@infradead.org> <20200827161754.535381269@infradead.org> <20200828144650.GF28468@redhat.com> <20200828152946.GG1362448@hirez.programming.kicks-ass.net> In-Reply-To: <20200828152946.GG1362448@hirez.programming.kicks-ass.net> From: Cameron Date: Fri, 28 Aug 2020 23:05:20 -0400 Message-ID: Subject: Re: [RFC][PATCH 6/7] freelist: Lock less freelist To: Peter Zijlstra Cc: Oleg Nesterov , linux-kernel@vger.kernel.org, Masami Hiramatsu , Eddy_Wu@trendmicro.com, x86@kernel.org, davem@davemloft.net, rostedt@goodmis.org, naveen.n.rao@linux.ibm.com, anil.s.keshavamurthy@intel.com, linux-arch@vger.kernel.org, will@kernel.org, paulmck@kernel.org Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > I'm curious whether it is correct to just set the prev->refs to zero and return > @prev? So that it can remove an unneeded "add()&get()" pair (although in > an unlikely branch) and __freelist_add() can be folded into freelist_add() > for tidier code. That is a very good question. I believe it would be correct, yes, and would certainly simplify the code. The reference count is zero, so nobody can increment it again, and REFS_ON_FREELIST is set (the should-be-on-freelist flag), so instead of adding it to the freelist to be removed later, it can be returned directly. > On Fri, Aug 28, 2020 at 04:46:52PM +0200, Oleg Nesterov wrote: > > 129 lines! And I spent more than 2 hours trying to understand these > > 129 lines ;) looks correct... Hahaha. Sorry about that. Some of the most mind-bending code I've ever written. :-) > > + /* > > + * Hmm, the add failed, but we can only try again when > > + * the refcount goes back to zero. > > + */ > > + if (atomic_fetch_add_release(REFS_ON_FREELIST - 1, &node->refs) == 1) > > + continue; > Do we really need _release ? Why can't atomic_fetch_add_relaxed() work? The release is to synchronize with the acquire in the compare-exchange of try_get. However, I think you're right, between the previous release-write to the refs and that point, there are no memory effects that need to be synchronized, and so it could be safely replaced with a relaxed operation. > Cosmetic, but why not atomic_fetch_dec() ? This is just one of my idiosyncrasies. I prefer exclusively using atomic add, I find it easier to read. Feel free to change them all to subs! Cameron > > > > Do we the barriers implied by _fetch_? Why can't atomic_sub(2, refs) work? > > I think we can, the original has std::memory_order_relaxed here. So I > should've used atomic_fetch_add_relaxed() but since we don't use the > return value, atomic_sub() would work just fine too. > > > > + /* > > > + * OK, the head must have changed on us, but we still need to decrement > > > + * the refcount we increased. > > > + */ > > > + refs = atomic_fetch_add(-1, &prev->refs); > > > > Cosmetic, but why not atomic_fetch_dec() ? > > The original had that, I didn't want to risk more bugs by 'improving' > things. But yes, that can definitely become dec().