Received: by 2002:a05:6a10:6006:0:0:0:0 with SMTP id w6csp481524pxa; Thu, 27 Aug 2020 07:35:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz1jzAMV0GsxxftRjeY2wOwqqUbVCGXlB9eyDLio3OFI6entM84VobtqggMPWo1LSIyeiw9 X-Received: by 2002:a17:906:5811:: with SMTP id m17mr20916949ejq.40.1598538902831; Thu, 27 Aug 2020 07:35:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598538902; cv=none; d=google.com; s=arc-20160816; b=GfMOPrNBL5Jgnq4Dz0EfDwpPmFgLP0hXC7qfGVebPtRowaS9kW3ED+oyO+mlu8Xex1 O4RMm1pjqmpZjdrkmxBtbHoJY4klVjJP4IiH77TExEr+5heOaCC9Y0EX5KrMJLc/cfOt aBVN4Rcr/+htPfGg02wnnr5mHXGY6IHhZ9unOrxlqJVZDv69WeHy2zc6Z05+wf5iIOHw xn9lFJv5uuziLKsYOLgWAmwwWkhJXq5AXSNa3fwc20akdKWJdgqB6/bkeA+1xZlfG806 0SYNG8TX8DMU0Dbkr3OaeBUtSbtreO4p9Z5ouwrpEllyYK8eAHCVzL2jQ8kK+97nhMvI DHDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=4723XndPNDxFccyrpXLlwTzAHoRKzJ+tUlBQCBzgHk4=; b=bcKSiJ4Qas7Qa4fxt1wwkGs3W6yGSzwi6BJf8kvO/2R5iN5A2dnDSA1pSeFGVxz9ZD ZMsfhoPhraNDehK7FlC8qA8tPFHGFgzOSEBWH26mSNEEwBzBwnsmwNht17nd/Z/zGRga RNU3+JyGxs0vmSYCCeLVszrG2Qyt3ABBnFQ58SM6GCFEWJEv7QKFv7teaMVQls24vyKa wbfdhjEeB9NYmr9gjxDvAb3wd4SFVFXPg9tL7/2dMUimZ9ivNtNwT9fmW2ik35ctheTu uzUOHviH56Zdj8bRZSa2Mc6aDROLLnph7/u7G+5N3yPwxzyEN2BT+cD+3wBMQ48mohYF T1pg== 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 j8si1532521ejx.291.2020.08.27.07.34.39; Thu, 27 Aug 2020 07:35:02 -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 S1727921AbgH0Od0 (ORCPT + 99 others); Thu, 27 Aug 2020 10:33:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57932 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727927AbgH0O3I (ORCPT ); Thu, 27 Aug 2020 10:29:08 -0400 Received: from ZenIV.linux.org.uk (zeniv.linux.org.uk [IPv6:2002:c35c:fd02::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0C144C061264; Thu, 27 Aug 2020 07:28:54 -0700 (PDT) Received: from viro by ZenIV.linux.org.uk with local (Exim 4.92.3 #3 (Red Hat Linux)) id 1kBItU-005aDQ-Dk; Thu, 27 Aug 2020 14:28:48 +0000 Date: Thu, 27 Aug 2020 15:28:48 +0100 From: Al Viro To: Shaokun Zhang Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Yuqi Jin , kernel test robot , Will Deacon , Mark Rutland , Peter Zijlstra , Boqun Feng Subject: [NAK] Re: [PATCH] fs: Optimized fget to improve performance Message-ID: <20200827142848.GZ1236603@ZenIV.linux.org.uk> References: <1598523584-25601-1-git-send-email-zhangshaokun@hisilicon.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1598523584-25601-1-git-send-email-zhangshaokun@hisilicon.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Aug 27, 2020 at 06:19:44PM +0800, Shaokun Zhang wrote: > From: Yuqi Jin > > It is well known that the performance of atomic_add is better than that of > atomic_cmpxchg. > The initial value of @f_count is 1. While @f_count is increased by 1 in > __fget_files, it will go through three phases: > 0, < 0, and = 0. When the > fixed value 0 is used as the condition for terminating the increase of 1, > only atomic_cmpxchg can be used. When we use < 0 as the condition for > stopping plus 1, we can use atomic_add to obtain better performance. Suppose another thread has just removed it from the descriptor table. > +static inline bool get_file_unless_negative(atomic_long_t *v, long a) > +{ > + long c = atomic_long_read(v); > + > + if (c <= 0) > + return 0; Still 1. Now the other thread has gotten to dropping the last reference, decremented counter to zero and committed to freeing the struct file. > + > + return atomic_long_add_return(a, v) - 1; ... and you increment that sucker back to 1. Sure, you return 0, so the caller does nothing to that struct file. Which includes undoing the changes to its refecount. In the meanwhile, the third thread does fget on the same descriptor, and there we end up bumping the refcount to 2 and succeeding. Which leaves the caller with reference to already doomed struct file... IOW, NAK - this is completely broken. The whole point of atomic_long_add_unless() is that the check and conditional increment are atomic. Together. That's what your optimization takes out.