Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp4011878rwl; Tue, 28 Mar 2023 01:01:26 -0700 (PDT) X-Google-Smtp-Source: AKy350bu0pWewRGhIh175v9cYDxvz5VGtS2m0AolGXRQUPOHwXViyJpfITHs7gGc3pc5u56aWXWQ X-Received: by 2002:a17:906:6946:b0:92f:8324:e3b7 with SMTP id c6-20020a170906694600b0092f8324e3b7mr14201551ejs.37.1679990486002; Tue, 28 Mar 2023 01:01:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679990485; cv=none; d=google.com; s=arc-20160816; b=yTlAP4e6eRr6A3StB00/Wtuw47KP/TsnOlyRk01LZBITz/ZCH9TVD3slydK/c060vz oCWS8Pi7JdIS1L3OXz8KlrGDo9+KZjEzmZ8kjamIzoWV4HBVQubdM+EKcJ6FEWGxzy7E OPlVoUc0ZzeIHUpjrOJjGNDSSDdnUn4bwflzNK5z3eMyziRWAviGNSCkALV6XYGlnHgV eRmeobVqAn5Qb+52UQf+L7+qrggNgxEtDHxHDpMRqoXNCIye3NxRLhJQG2pbUuNVFkOv 4ifk6TtBjWraYzpdT2CX0d/Mehs5bnYfn4phjjXypWHN9c1pbiNrpuN7Ywb8fIRCYtWV lK/A== 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 :dkim-signature; bh=6iPgbcWLfS4ADF8Ltx8cl7gqwqXWbZdK3uZ1Oe3HsXk=; b=ZKIny5wxNWK44sU3Wrq3HDuUVtqu/DGXqVebfdUqf3S5z8ucEYM7jQPqcrZBEISOM7 6a6IpwsaNZsL99ONJtukeraR/9xBLX4lk4wWQ+2Jeti1sP/EnqwtbYlUILVZ5pTTrMh7 ZJym1f1vF20amRSmxdg9klHq1AelaJMVeZ6OBJba0Q5w6cMQQl+gVi/n2hgCAoPGnaEm qx9XtlCbZnum0kOI86AfjucdWBXzSn7u6Bc/d651zrx5lfW6iPfxlCy2aLqeZwHtd3vI 7G4TdkBlNMvU/FplMRQzmpCrjtK9THrHX2XYK1f8DS8MbQTIxQtT67Ze9w2+6Z/NQBF0 sxHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=qji3cJQx; dkim=neutral (no key) header.i=@suse.cz; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a18-20020a1709066d5200b0092722a78f71si25640550ejt.300.2023.03.28.01.01.00; Tue, 28 Mar 2023 01:01:25 -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=@suse.cz header.s=susede2_rsa header.b=qji3cJQx; dkim=neutral (no key) header.i=@suse.cz; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232506AbjC1Hys (ORCPT + 99 others); Tue, 28 Mar 2023 03:54:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45560 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230325AbjC1Hyr (ORCPT ); Tue, 28 Mar 2023 03:54:47 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DE1EFDB; Tue, 28 Mar 2023 00:54:42 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 99D6A1FD81; Tue, 28 Mar 2023 07:54:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1679990081; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=6iPgbcWLfS4ADF8Ltx8cl7gqwqXWbZdK3uZ1Oe3HsXk=; b=qji3cJQxQ2PPraU0cvs1P6hZHNuL3LCIfGNHtd3MiY4M4yeGdsac+9UanEil7qkJYD1Ybi 8Vj9XQBt2tMyQmCqkKBYfazRUPh7zjtMbubyG0R4QyvzWyZ93daDQjrBLhIa2Hq9sEgXDz FsrRLmWOs9jT7G2h7PlsKgjd7J7l4qI= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1679990081; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=6iPgbcWLfS4ADF8Ltx8cl7gqwqXWbZdK3uZ1Oe3HsXk=; b=fXIP9q6BsSFUheXM71xvrTRt8yBigoZGVDl/LEvfKnCcq28RnC9zjucGn6mKr3SnUsLLhW ttB7UW7NMw3YbVAQ== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 881631390D; Tue, 28 Mar 2023 07:54:41 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id ULIvIUGdImSzEAAAMHmgww (envelope-from ); Tue, 28 Mar 2023 07:54:41 +0000 Received: by quack3.suse.cz (Postfix, from userid 1000) id 06816A071C; Tue, 28 Mar 2023 09:54:41 +0200 (CEST) Date: Tue, 28 Mar 2023 09:54:40 +0200 From: Jan Kara To: Christian Brauner Cc: linux-fsdevel@vger.kernel.org, Jan Kara , Amir Goldstein , Matthew Bobrowski , linux-kernel@vger.kernel.org Subject: Re: [PATCH 3/3] fanotify: use pidfd_prepare() Message-ID: <20230328075440.ho55dt2xhvbz7yog@quack3> References: <20230327-pidfd-file-api-v1-0-5c0e9a3158e4@kernel.org> <20230327-pidfd-file-api-v1-3-5c0e9a3158e4@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230327-pidfd-file-api-v1-3-5c0e9a3158e4@kernel.org> X-Spam-Status: No, score=-1.5 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_SOFTFAIL autolearn=unavailable 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 Mon 27-03-23 20:22:53, Christian Brauner wrote: > We generally try to avoid installing a file descriptor into the caller's > file descriptor table just to close it again via close_fd() in case an > error occurs. Instead we reserve a file descriptor but don't install it > into the caller's file descriptor table yet. If we fail for other, > unrelated reasons we can just close the reserved file descriptor and if > we make it past all meaningful error paths we just install it. Fanotify > gets this right already for one fd type but not for pidfds. > > Use the new pidfd_prepare() helper to reserve a pidfd and a pidfd file > and switch to the more common fd allocation and installation pattern. > > Signed-off-by: Christian Brauner Thanks for the improvement! It looks good to me. Feel free to add: Acked-by: Jan Kara Honza > --- > fs/notify/fanotify/fanotify_user.c | 13 +++++++++---- > 1 file changed, 9 insertions(+), 4 deletions(-) > > diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c > index 8f430bfad487..22fb1cf7e1fc 100644 > --- a/fs/notify/fanotify/fanotify_user.c > +++ b/fs/notify/fanotify/fanotify_user.c > @@ -663,7 +663,7 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group, > struct fanotify_info *info = fanotify_event_info(event); > unsigned int info_mode = FAN_GROUP_FLAG(group, FANOTIFY_INFO_MODES); > unsigned int pidfd_mode = info_mode & FAN_REPORT_PIDFD; > - struct file *f = NULL; > + struct file *f = NULL, *pidfd_file = NULL; > int ret, pidfd = FAN_NOPIDFD, fd = FAN_NOFD; > > pr_debug("%s: group=%p event=%p\n", __func__, group, event); > @@ -718,7 +718,7 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group, > !pid_has_task(event->pid, PIDTYPE_TGID)) { > pidfd = FAN_NOPIDFD; > } else { > - pidfd = pidfd_create(event->pid, 0); > + pidfd = pidfd_prepare(event->pid, 0, &pidfd_file); > if (pidfd < 0) > pidfd = FAN_EPIDFD; > } > @@ -751,6 +751,9 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group, > if (f) > fd_install(fd, f); > > + if (pidfd_file) > + fd_install(pidfd, pidfd_file); > + > return metadata.event_len; > > out_close_fd: > @@ -759,8 +762,10 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group, > fput(f); > } > > - if (pidfd >= 0) > - close_fd(pidfd); > + if (pidfd >= 0) { > + put_unused_fd(pidfd); > + fput(pidfd_file); > + } > > return ret; > } > > -- > 2.34.1 > -- Jan Kara SUSE Labs, CR