Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp3032845rdh; Wed, 27 Sep 2023 23:47:42 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEk64cu0wQ8HautweFbyI99AzF7y+wPxVLL7aDg6fhO1Ra/SALMZN6fMi/6SLf6x05HCPQW X-Received: by 2002:a17:90b:812:b0:271:c207:b285 with SMTP id bk18-20020a17090b081200b00271c207b285mr376143pjb.9.1695883662290; Wed, 27 Sep 2023 23:47:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695883662; cv=none; d=google.com; s=arc-20160816; b=jPp3qr2jhzWimIfq6lxCrdWwY3t/tqpuUqOIUw9xSDvAQSTKkI+Av6TsNt/4MP6iCl JMiLGAGwODbXzT02Lu+yDftTOI9Q9pF4Iem8YRcNLarbAsvyeNsJfmeg7NyG3HXgre/s MhhKH1cY+XIUVqq+XtVDJ4jFadDPzC/42G/k1UXwnwqO8JVrOdEMCn7cRFm0DA04OMxK +mrJJnKRzvQ42hKSG9iT0v38/dg7QkEtM9SpA413/iWEoqSmsG8SIAm3JqQZcV7pWCYC lDJL9Us4pazhL8dTMSDw6dSHS/E9FPM/jSQqr233d7Pu7K9gKQyGCz6IRfuR6PK2aS9+ O63g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=n5tY2gwDez0Qf3DHOTXDYMh4745U3+Wh/mJcDvesXVQ=; fh=ziJSrE0sDvMIhmkBFU1jJK15yvgZkLMOUWuncUnCmvo=; b=eFrSokaWFb43gg1JjPMXetti3Sy3YRkK/lL/lzTm1C9GkUXE7Y/eiLrz40amUDXzQj 7vf1qQ/9h0QLld1He4Xlyd7Spm6lO//3ftL0VH6vsTM9iszPGUgOYY9ULww6+9fNNRm8 uQvjOPh1WI/yP3mVLy348dKkrOtw7iX5vATBxFk5oxvO/py9c9N705HQeRcDRejzANHa Q0EvNUc/sVy64lvTU8Lkwf6hZhbKiAqWtwdvZ42IiGTGDKDgd1nkWucipcavfV8Z0xdJ tgd3oL8DsYerJqPSubSIhqdmiNvplHirDuyRdX0KRAaMC7jfVYnOW0adqpWeqUB8Bs+E CXlw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=QbgzPV21; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id c21-20020a17090aa61500b002776c8df9b7si8172398pjq.144.2023.09.27.23.47.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Sep 2023 23:47:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=QbgzPV21; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 3791880A32FA; Wed, 27 Sep 2023 13:28:27 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229771AbjI0U2P (ORCPT + 99 others); Wed, 27 Sep 2023 16:28:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51276 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229547AbjI0U2O (ORCPT ); Wed, 27 Sep 2023 16:28:14 -0400 Received: from mail-ej1-x632.google.com (mail-ej1-x632.google.com [IPv6:2a00:1450:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3D61D10A for ; Wed, 27 Sep 2023 13:28:13 -0700 (PDT) Received: by mail-ej1-x632.google.com with SMTP id a640c23a62f3a-9b275afb6abso1734573766b.1 for ; Wed, 27 Sep 2023 13:28:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=google; t=1695846491; x=1696451291; darn=vger.kernel.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=n5tY2gwDez0Qf3DHOTXDYMh4745U3+Wh/mJcDvesXVQ=; b=QbgzPV21eemJ0abhYY6R5cvLm5+4Z0x7TuZ1NiXd27JtBgI9OQY8qzUTHObhvYrAPH vb+2JxvqhnVPdDI/qGAeBAHlTyOdPdD8kh5ZLodmpEeTGAfFd4CUnWnpaOPCT+LOfEvZ 6Dn0dzrgsNH3XLBM0I+4zxd6jBuCxQTLVrRrc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695846491; x=1696451291; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=n5tY2gwDez0Qf3DHOTXDYMh4745U3+Wh/mJcDvesXVQ=; b=GRxiq7gcgd0RPxC1TnN3d2A66ZetDVlWOXAGHgD8q2knIkBdyL4HYlRKSB8qZeyH7J 4/UaaIa0RHjhkMB5zSoGLulfDcZhc5Dqp+4WMZo0NIC9x8HbZ2WyxSLYRy4VvbkvkEJF 5bwMd0izRvnFUT2d16JI/ksv8jAUGOYuHxoUrwsYaDx0+JzQ4ZvTB57SXbuVvQf6e0jI Hy0PqBS6QTOASZDHL02YVj9Ker+crkZUdy5Y0r5f5vxHEoXZCZDPdNGgm7Msnmqm0ZNU xF9mx98pp885wSjxsFioueo49IAOtpEBF9ik4QsdduY1imTiIbwR+L1DvI3TTCqDn7T+ eRnQ== X-Gm-Message-State: AOJu0YwVXkafqw1fx768wc4GYWWDRrh2FaPSN1jOe7EayPFK+ixa3R+u usSmoanNKkHaKIm6E2JAEZua5M92sPATUofmji5eTg== X-Received: by 2002:a17:907:7b95:b0:9ae:614f:b159 with SMTP id ne21-20020a1709077b9500b009ae614fb159mr10019900ejc.36.1695846491568; Wed, 27 Sep 2023 13:28:11 -0700 (PDT) Received: from mail-ed1-f42.google.com (mail-ed1-f42.google.com. [209.85.208.42]) by smtp.gmail.com with ESMTPSA id e5-20020a170906080500b0099b76c3041csm9691955ejd.7.2023.09.27.13.28.10 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 27 Sep 2023 13:28:10 -0700 (PDT) Received: by mail-ed1-f42.google.com with SMTP id 4fb4d7f45d1cf-530fa34ab80so28687207a12.0 for ; Wed, 27 Sep 2023 13:28:10 -0700 (PDT) X-Received: by 2002:aa7:c549:0:b0:533:dd4d:2941 with SMTP id s9-20020aa7c549000000b00533dd4d2941mr4099814edr.16.1695846490123; Wed, 27 Sep 2023 13:28:10 -0700 (PDT) MIME-Version: 1.0 References: <20230926162228.68666-1-mjguzik@gmail.com> <20230927-kosmetik-babypuppen-75bee530b9f0@brauner> In-Reply-To: From: Linus Torvalds Date: Wed, 27 Sep 2023 13:27:51 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v2] vfs: shave work on failed file open To: Mateusz Guzik Cc: Christian Brauner , viro@zeniv.linux.org.uk, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Content-Type: multipart/mixed; boundary="000000000000a3607f06065d07b1" X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Wed, 27 Sep 2023 13:28:27 -0700 (PDT) --000000000000a3607f06065d07b1 Content-Type: text/plain; charset="UTF-8" On Wed, 27 Sept 2023 at 11:32, Mateusz Guzik wrote: > > put_cred showed up in file_free_rcu in d76b0d9b2d87 ("CRED: Use creds in > file structs"). Commit message does not claim any dependency on this > being in an rcu callback already and it looks like it was done this way > because this was the ony spot with kmem_cache_free(filp_cachep, f) Yes, that looks about right. So the rcu-freeing is almost an accident. Btw, I think we could get rid of the RCU freeing of 'struct file *' entirely. The way to fix it is (a) make sure all f_count accesses are atomic ops (the one special case is the "0 -> X" initialization, which is ok) (b) make filp_cachep be SLAB_TYPESAFE_BY_RCU because then get_file_rcu() can do the atomic_long_inc_not_zero() knowing it's still a 'struct file *' while holding the RCU read lock even if it was just free'd. And __fget_files_rcu() will then re-check that it's the *right* 'struct file *' and do a fput() on it and re-try if it isn't. End result: no need for any RCU freeing. But the difference is that a *new* 'struct file *' might see a temporary atomic increment / decrement of the file pointer because another CPU is going through that __fget_files_rcu() dance. Which is why "0 -> X" is ok to do as a "atomic_long_set()", but everything else would need to be done as "atomic_long_inc()" etc. Which all seems to be the case already, so with the put_cred() not needing the RCU delay, I thing we really could do this patch (note: independent of other issues, but makes your patch require that "atomic_long_cmpxchg()" and the WARN_ON() should probably go away, because it can actually happen). That should help the normal file open/close case a bit, in that it doesn't cause that extra RCU work. Of course, on some loads it might be advantageous to do a delayed de-allocation in some other RCU context, so .. What do you think? Linus PS. And as always: ENTIRELY UNTESTED. --000000000000a3607f06065d07b1 Content-Type: text/x-patch; charset="US-ASCII"; name="patch.diff" Content-Disposition: attachment; filename="patch.diff" Content-Transfer-Encoding: base64 Content-ID: X-Attachment-Id: f_ln277b020 IGZzL2ZpbGVfdGFibGUuYyB8IDE5ICsrK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