Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp8381394rwr; Wed, 10 May 2023 23:54:46 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ51FvMzt75cePOs2kAfNWyOWUgmX0Kd2FbL3etfbE4N0BZkuLzxmQdfwRXnuTLy/0zd501D X-Received: by 2002:a05:6a00:13a8:b0:63d:6744:8cae with SMTP id t40-20020a056a0013a800b0063d67448caemr26497304pfg.2.1683788086122; Wed, 10 May 2023 23:54:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683788086; cv=none; d=google.com; s=arc-20160816; b=IS9bn1v1esrtm9OC+QEMgodtQk+WKGyE3n2pu/eR1oMnc8AdIqxZA+geAtBJVmmnAT TpSraWhr+upjfHsoaKeYUHSEvjlfgnAn9Vumxvtw1D1Gr0qanGTnVSaw84eXziUErwGG gmNRaGEjsk/fDOf+3fFPu8puikoOPoEk1Yugff1GMOhcfgx+a22ICte5Wbg5gRW83VTu QcJSqU2Zpm2MorGTnbzb9hEx0aw3WjlLZsRzSWvlb6W8kXYlH04ls1N48LyKVbA/+mQi x7BurCa0FTGY4JW4z7yHIIXc6mb2QqcdEcvgwMONFI8coFfYIzzgSruoyPIPhYXTdhE0 /kqA== 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-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=RtWxN7e4QjgKeMqyRm8+weDfqFq3jfnkuzd3W1qjtiM=; b=IgLF0RzaaH5kGNDsKizecHF53l7Fd0IkCEIHu9Fs9qx34x3cTeDcf7xmEMRF4d3u6C cskVjcO9scG2rNmhaqKd5Ui3HmMXUknMqp/2jeBoyihnZM715Ciyz/XDZMrM6mPNhHon ocRgangWmLUb5NLcMRkEbz5Gd4WFqbHZE7gWrcRqWQud2oyigdh8g6ArI+qiguWHSnEx diSJNu5qe9uvVgRIGd5dhmJ8p1aZDZI66GY2vsrsYvOb49qVXuNEAHguemno2f7Pvp7R mcgQ3JuaHMgDMDtx//KovcHWKca2HXKclG8/z1XvRUWlBZ8w/6I1+B3XIXTtNCMortvU 3YwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.org.uk header.s=zeniv-20220401 header.b="jw/mMH9q"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zeniv.linux.org.uk Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b19-20020aa79513000000b006262bc88219si6977034pfp.160.2023.05.10.23.54.32; Wed, 10 May 2023 23:54:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.org.uk header.s=zeniv-20220401 header.b="jw/mMH9q"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zeniv.linux.org.uk Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236865AbjEKGlJ (ORCPT + 99 others); Thu, 11 May 2023 02:41:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49996 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229609AbjEKGlI (ORCPT ); Thu, 11 May 2023 02:41:08 -0400 Received: from zeniv.linux.org.uk (zeniv.linux.org.uk [IPv6:2a03:a000:7:0:5054:ff:fe1c:15ff]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6CC39103; Wed, 10 May 2023 23:41:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=linux.org.uk; s=zeniv-20220401; h=Sender:In-Reply-To: Content-Transfer-Encoding:Content-Type:MIME-Version:References:Message-ID: Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description; bh=RtWxN7e4QjgKeMqyRm8+weDfqFq3jfnkuzd3W1qjtiM=; b=jw/mMH9qT+/rpGk7mfI4YGlPGe uWTb2lZmqfZIuwVdgWuQnLtmQkWFasYr7kqa7CUIFVCLKRFtUvi1n8Qd+6fMBYIIAbvrl/GdFxv2J lqAmLygNJHA+XCTSh+mldXiQG+o7dI8T8FS5MrntPj1B3P2ekHWFpwYFdC4tjvXb2QUArkaE0igA9 oZtyK6diEXw40KxwmYst51pwkoEifoIuQGmWWNdXLmKJ3cCOtTTeK1EclD5jXQT9bgdhOYp/vs0Fz UBoWWTUbFlQsarbW1h9IC4EJZW1ubTxAV15CCwSdzwFvYrZWcxDjqD9Lezep+zRrsTgNjjVxyMM2m zJpZGunw==; Received: from viro by zeniv.linux.org.uk with local (Exim 4.96 #2 (Red Hat Linux)) id 1pwzyw-001dzq-0x; Thu, 11 May 2023 06:40:54 +0000 Date: Thu, 11 May 2023 07:40:54 +0100 From: Al Viro To: Christian =?iso-8859-1?Q?K=F6nig?= Cc: ye.xingchen@zte.com.cn, sumit.semwal@linaro.org, gustavo@padovan.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] dma-buf/sync_file: Use fdget() Message-ID: <20230511064054.GM3390869@ZenIV> References: <202305051103396748797@zte.com.cn> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: Sender: Al Viro X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, May 05, 2023 at 10:22:09AM +0200, Christian K?nig wrote: > Am 05.05.23 um 05:03 schrieb ye.xingchen@zte.com.cn: > > From: Ye Xingchen > > > > convert the fget() use to fdget(). > > Well the rational is missing. Why should we do that? We very definitely should not. The series appears to be pure cargo-culting and it's completely wrong. There is such thing as unwarranted use of fget(). Under some conditions converting to fdget() is legitimate *and* is an improvement. HOWEVER, those conditions are not met in this case. Background: references in descriptor table do contribute to struct file refcount. fget() finds the reference by descriptor and returns it, having bumped the refcount. In case when descriptor table is shared, we must do that - otherwise e.g. close() or dup2() from another thread could very well have destroyed the struct file we'd just found. However, if descriptor table is *NOT* shared, there's no need to mess with refcount at all. Provided that * we are not grabbing the reference to keep it (stash into some data structure, etc.); as soon as we return from syscall, the reference in descriptor table is fair game for e.g. close(2). Or exit(2), for that matter. * we remember whether it was shared or not - we can't just recheck that when we are done with the file; after all, descriptor table might have been shared when we looked the file up, but another thread might've died since then and left it not shared anymore. * we do not rip the same reference out of our descriptor table ourselves - not without seriously convoluted precautions. Very few places in the kernel can lead to closing descriptors, so in practice it only becomes a problem when a particularly ugly ioctl decides that it would be neat to close some descriptor(s). Example of such convolutions: binder_deferred_fd_close(). fdget() returns a pair that consists of struct file reference *AND* indication whether we have grabbed a reference. fdput() takes such pair. Both are inlined, and compiler is smart enough to split the pair into two separate local variables. The underlying primitive actually stashes the "have grabbed the refcount" into the LSB of returned word; see __to_fd() in include/linux/file.h for details. It really generates a decent code and a plenty of places where we want a file by descriptor are just fine with it. This patch is flat-out broken, since it loses the "have we bumped the refcount" information - the callers do not get it. It might be possible to massage the calling conventions to enable the conversion to fdget(), but it's not obvious that result would be cleaner and in any case, the patch in question doesn't even try that.