Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp2820631rdh; Wed, 27 Sep 2023 13:53:29 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEMkC7tiQ3PjLw2NHQom17OO2q8JTFjH+BBocoCjmNq6aw+kEO1nCIYgaLEDcKw/Q4qrHfu X-Received: by 2002:a05:6a20:9707:b0:153:81a6:b171 with SMTP id hr7-20020a056a20970700b0015381a6b171mr2869435pzc.38.1695848009019; Wed, 27 Sep 2023 13:53:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695848009; cv=none; d=google.com; s=arc-20160816; b=R6rwDv17kXpVLNBWWW0L20K4R35Pq9/YkKa0PNdo+DH+MwfwHkivIorYhhvVtyoF3F jknhUz5g0ciJzVN3LdTtlXqp9kaSzXvcReGFSQGil5iFgowvwnUwmPxu9ymhDpSuVTiX dBfckAFO3GIK1U3BBYsYhjxTB33kMY0UzXWDxk1DdmjYkRhwa/ldUMenLUs5cD8Qv/ke bI3lLLT3VP8P5X7u0ot1XEOPoEFu+FnFLn++4VlSmseFbzksWkxLKue7LNA+6nIMeXgG gN2Q9FXhaD1gqvOhVWpWr1vIqauQm6S5A7QIzYjYQPooq0VV+enJQtRxpHWzVgMQFV0B vwTw== 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=ekDcWyVkbd7zDXNRjBeJ3lySaiZP1rXVA38mYoAQqBc=; fh=3fuGkk6t8rL4T2AilM3pBZQt6nXcfYvg3/GrJ/UWcNc=; b=MAVPdh0P83+mE+42xjs2RMPH9lJ5QrUVUP9Bc6LG8YBfmTYpEHmuaU3rqwUOnq4mqy 2/gZXW4Eo19to2HwS7gLfR5f6lfjCamKL6t6C+cBTVZK3wmwx8OqIqSRkJHY8GtIDT63 hbec06a7wxGnpwFznI0vSQRtZ2aAFd5HdSHZW5HYx58zIg4JoEB+ribmU+ERvXIbru0U k4IpYIsoXV6sQdPbNMx1MrM8IVih2NCCjpGT6u6vFBIlXTebi+orDJfZeO/XIdG4NAo9 L+WlCpgsDR4iWy93UOud6m8dTVUMkZwWXG/7RFhgf2GxE97jlCYMFLjyDsVsLSa5ySfF qsDg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=gQwLPtDJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id mh5-20020a17090b4ac500b00276df8c5b83si11902658pjb.143.2023.09.27.13.53.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Sep 2023 13:53:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=gQwLPtDJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 2D96582159EF; Wed, 27 Sep 2023 11:32:40 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229682AbjI0Sch (ORCPT + 99 others); Wed, 27 Sep 2023 14:32:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46730 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229437AbjI0Scg (ORCPT ); Wed, 27 Sep 2023 14:32:36 -0400 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4EB018F; Wed, 27 Sep 2023 11:32:34 -0700 (PDT) Received: by mail-wr1-x42f.google.com with SMTP id ffacd0b85a97d-3231dff4343so5289819f8f.0; Wed, 27 Sep 2023 11:32:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695839553; x=1696444353; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=ekDcWyVkbd7zDXNRjBeJ3lySaiZP1rXVA38mYoAQqBc=; b=gQwLPtDJimUZfMiMBDUOyuVnIDqf2sh9z/tByCtY9djP7BfKHLgUdE7bXz8HTiVQUi GtJrvtTU4DRyisMwUv/gW0q33SCXXxzdoDZ81jIKiodNQFxmKV/XoXn4Ewrm871y15JN LexsZKF1J4n9GeUppX2G3V1ieAyE0sIi98LOlZnsI5L9n88znFpJQyq/kb1eUPI1Jq2a sydb7vMTi1r1xxIlaa+J8TPh/+A0KOt1EWfM+nBDCgLElzRXmHjZnx9ffDWIzZE3LFMS yuJh0pRBzdgS/YNakc7iounI23D99i2vQ9eVfTSoaqegL/B680k6CPweMk/p970mdJK7 +6Fw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695839553; x=1696444353; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=ekDcWyVkbd7zDXNRjBeJ3lySaiZP1rXVA38mYoAQqBc=; b=ZS6Cylo3TQGS+Py8gMq8N8jjIISKufIrVeQLTHvf1fZrHRY9tZPr8VyEkrA6yZOx4A LbU+QzIj9bkbt0qNqpO9zp7M2lI7iAkT15g5V8qWnqIN6aF4ibsHbyMnzbinWN/XdaWp i7o2tqeeKJ04C1cmNXHfcEb927gCb+DQovQzepVze2cBCL8TaVPzK4t/emptRr3RUQYu X9mtl740mlbVTOxNaBd/HBFXOFp3mXZfOTFdZiDNq/fXoO63bX5N9SjuS8huvmI3LOnJ nZoHHj3fodzCVlrUV22rcaVCkrJU8xxIJQ21oNTl5kIMfwiogl9ooQmLzFV3/9hhDZsS 9fpw== X-Gm-Message-State: AOJu0YxvKw44ewyTQC6ARd8xRuurJZF3jkWxS/TmccgJfgGiJ5UGVbRx /c2zgz7l8S5Vd8vWdloJij0= X-Received: by 2002:a05:6000:4cf:b0:31f:d50e:a14f with SMTP id h15-20020a05600004cf00b0031fd50ea14fmr2498610wri.10.1695839552461; Wed, 27 Sep 2023 11:32:32 -0700 (PDT) Received: from f (cst-prg-67-191.cust.vodafone.cz. [46.135.67.191]) by smtp.gmail.com with ESMTPSA id b12-20020a5d634c000000b0031ad2f9269dsm17639613wrw.40.2023.09.27.11.32.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Sep 2023 11:32:31 -0700 (PDT) Date: Wed, 27 Sep 2023 20:32:09 +0200 From: Mateusz Guzik To: Linus Torvalds Cc: Christian Brauner , viro@zeniv.linux.org.uk, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: Re: [PATCH v2] vfs: shave work on failed file open Message-ID: References: <20230926162228.68666-1-mjguzik@gmail.com> <20230927-kosmetik-babypuppen-75bee530b9f0@brauner> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 27 Sep 2023 11:32:40 -0700 (PDT) On Wed, Sep 27, 2023 at 11:05:37AM -0700, Linus Torvalds wrote: > On Wed, 27 Sept 2023 at 10:56, Mateusz Guzik wrote: > > > > Comments in the patch explicitly mention dodgin RCU for the file object. > > Not the commit message,. and the comment is also actually pretty > obscure and only talks about the freeing part. > How about this: ================== cut here ================== vfs: shave work on failed file open Failed opens (mostly ENOENT) legitimately happen a lot, for example here are stats from stracing kernel build for few seconds (strace -fc make): % time seconds usecs/call calls errors syscall ------ ----------- ----------- --------- --------- ------------------ 0.76 0.076233 5 15040 3688 openat (this is tons of header files tried in different paths) In the common case of there being nothing to close (only the file object to free) there is a lot of overhead which can be avoided. This boils down to 2 items: 1. avoiding delegation of fput to task_work, see 021a160abf62 ("fs: use __fput_sync in close(2)" for more details on overhead) 2. avoiding freeing the file with RCU Benchmarked with will-it-scale with a custom testcase based on tests/open1.c, stuffed into tests/openneg.c: [snip] while (1) { int fd = open("/tmp/nonexistent", O_RDONLY); assert(fd == -1); (*iterations)++; } [/snip] Sapphire Rapids, openneg_processes -t 1 (ops/s): before: 1950013 after: 2914973 (+49%) file refcount is checked with an atomic cmpxchg as a safety belt against buggy consumers. Technically it is not necessary, but it happens to not be measurable due to several other atomics which immediately follow. Optmizing them away to make this atomic into a problem is left as an exercise for the reader. ================== cut here ================== Comment in v2 is: /* * Clean up after failing to open (e.g., open(2) returns with -ENOENT). * * This represents opportunities to shave on work in the common case of * FMODE_OPENED not being set: * 1. there is nothing to close, just the file object to free and consequently * no need to delegate to task_work * 2. as nobody else had seen the file then there is no need to delegate * freeing to RCU */ I don't see anything wrong with it as far as information goes. > > Well put_cred is called synchronously, but should this happen to be > > the last ref on them, they will get call_rcu(&cred->rcu, > > put_cred_rcu)'ed. > > Yes. But the way it's done in __fput() you end up potentially > RCU-delaying it twice. Odd. > > The reason we rcu-delay the 'struct file *' is because of the > __fget_files_rcu() games. > > But I don't see why the cred thing is there. > > Historical mistake? But it all looks a bit odd, and because of that it > worries me. > 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) -- you ensured put_cred was always called without inspecting any other places. If there is something magic going on here I don't see it, it definitely was not intended at least.