From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-dy1-f193.google.com (mail-dy1-f193.google.com [74.125.82.193]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E907C235358 for ; Wed, 3 Jun 2026 00:15:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.193 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780445754; cv=none; b=g36IpCaOfXNdo+xCUq2A42lyiLmTjIP5Z4bA1opNqhbm3Ip9RN+y6hPNh4IsSBM/y8+h8HRQM+CWPnRYdvFfSfPqNtTcfpdl8LgGbqrt1yFmpc0oR95OSCWshXTjJC9wszPFRTqLSQEKB4p/jYdZ2ve0fsnxgcHqYJuJQqVTzQg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780445754; c=relaxed/simple; bh=OzxvXiS7gZrNxYNXj9poA0um2pTBU84WZQnPOJzbptA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DYN9cOL5pmwhkYsBlx0AW3iK1MRIlZmyC17MdcohiBK3Z6C7+5zXocWPW9LAGhSCkY2hr1VsWlqM7lbxIY+k0OTxNywF/V1v88OgOhcng3Cajyzcc0oNFbfpaWpjOjPYf1zPEvqOmUj5So63aSluurbSywzJ2c8xWkc8cB4+Y9g= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=oKX9hQx6; arc=none smtp.client-ip=74.125.82.193 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="oKX9hQx6" Received: by mail-dy1-f193.google.com with SMTP id 5a478bee46e88-304f0039c02so9063223eec.1 for ; Tue, 02 Jun 2026 17:15:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1780445751; x=1781050551; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=30Zmw8a16oaljA7WTVadlHqTZXIGr1FxDCtkAmoDkcE=; b=oKX9hQx65Ydl6FpV9E280E7lmwrQRxGADePEQGTDz5NwPwkXkM3W0EjoRJUKukzM/A 9ssEkyWI60zzC/G+kYkv4h7ORU9qX4CHu/2M3ahNohJwaHHngtPMJ7J1n+vSEYZMNX2z +uZjJdWIJ0n4fUc05m9CpMhIkz839ftKfC6MoKbJC5a6EFSJs9y/ka6xsJCD3u+/L5bE h5AfGzvhd53L5IRxeAoJDvGJXvJ6knUy/6HLph62BtQouUNQhuN/GodsEJT9brW1CZbe xwWEChNGqkWkg/Lzs3v9ZMsxTVnw5pHaIGBX5fgYv2e9BvYqHaYMvZAj7hQ7aqWOlWvL RI4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780445751; x=1781050551; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=30Zmw8a16oaljA7WTVadlHqTZXIGr1FxDCtkAmoDkcE=; b=nOyg3ruWSNQOwMYBsGaslJcdocNlWKgJdnQKrWUB3qhO+3dqdxz2xEklAdmZBh+ctq BpXYizR391yIrB8VcMZTABolkaNoJqmT4uQAnd7c3ifu+75ktBUyRmKBBsNekkcDqg4R XqR1XswjMSfXKbUEerF1UvxqbSO//uRcstd/3TEcVd07TZiGje/yXkYiBydU7oaoTBNM biNWjqKe8XS4jFh9V7Ofx4/6GjevCO4n1iEvhn3O+aAVHBi4+DrKeIoMZJeYtEA/vXSa v7oHSK8wERm770RgHnhlAF48NMUracrFInOSPBQh/DhPB4ogfTGSyBJCGerKU3qX2wIh VqfA== X-Forwarded-Encrypted: i=1; AFNElJ/jEeIGgUgkQfCbHC/eQA+9FkqqVpUqQEvTQkrwt3S798e9aPoYQ9dZ+Pq+cj6XJTeJxM+cN7d+Fi7yzLY=@vger.kernel.org X-Gm-Message-State: AOJu0YzKvIA7EKpnaBvRUsEz6moFcpZxQPS+O29q2dc46xkGzcO0vQ3j RrYq6pjFHSvNxXuuEFulwOg6PYS280u1s0Mc9d5fLtnMdIQ8nB+knU6t X-Gm-Gg: Acq92OGRpV/P9kbLZ2cNwtLAbBfqjKzAJuNDy/t4jyJR4Bbup84RaRJZBNnmJuXHhX/ /LLkFuZxXokYmNgxo8znyPzHf0iWu+N++zlJd3UJYxLLSCA3w3fDgMUlbGVXp1TxzP0SnOyTSvo FvN7wmgOSZtSJiv0QARBZlWJRZFn3a4TjD6+5CrlEr2/KAn8PyJyDbRDHnff89sr3t+ZwtXcHsX idxseCktN9gBzo7EhCCVnYhoNnzX0P3oay7vibvVRgRt2vhUloGtrWOP2HM02CH2AlJEqe25B7E T26xWWJWhKVg5zEWDnLR0VkDeWzrmijfXggfSrhpf1AiA4+CgbtbNSm72LyOtC5+MmofGBBbtPn uj5ayWUt40V04kQlzoDI8tz2JU2V5HUutWq4c6pAU9GC6toggdBmrKadCRIBc/weFxaF5qokoaz yDDrjU2D4LJRhvMUcTumnqYkW3PIkOvw== X-Received: by 2002:a05:7301:1685:b0:305:56a:c38f with SMTP id 5a478bee46e88-3074fbd9cadmr545153eec.26.1780445751023; Tue, 02 Jun 2026 17:15:51 -0700 (PDT) Received: from anonyme ([2605:52c0:2:2f27:be24:11ff:fe89:6f0f]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-3074dcb98aasm829069eec.12.2026.06.02.17.15.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jun 2026 17:15:50 -0700 (PDT) From: AnonymeMeow To: jack@suse.cz Cc: amir73il@gmail.com, brauner@kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, AnonymeMeow Subject: [PATCH v4 1/2] fanotify: report thread pidfds for FAN_REPORT_TID Date: Wed, 3 Jun 2026 08:15:15 +0800 Message-ID: <20260603001516.26234-2-anonymemeow@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260603001516.26234-1-anonymemeow@gmail.com> References: <20260603001516.26234-1-anonymemeow@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The FAN_REPORT_PIDFD and FAN_REPORT_TID flags used to be mutually exclusive because by the time the pidfd support was introduced to fanotify, pidfds could only be created for thread group leaders. Now that the pidfd API supports thread-specific pidfds via PIDFD_THREAD, this restriction can be lifted. Signed-off-by: AnonymeMeow --- fs/notify/fanotify/fanotify_user.c | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index ae904451dfc0..ebdd48942029 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -19,6 +19,7 @@ #include #include #include +#include #include @@ -903,25 +904,21 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group, metadata.fd = fd >= 0 ? fd : FAN_NOFD; if (pidfd_mode) { - /* - * Complain if the FAN_REPORT_PIDFD and FAN_REPORT_TID mutual - * exclusion is ever lifted. At the time of incoporating pidfd - * support within fanotify, the pidfd API only supported the - * creation of pidfds for thread-group leaders. - */ - WARN_ON_ONCE(FAN_GROUP_FLAG(group, FAN_REPORT_TID)); + unsigned int tid_mode = FAN_GROUP_FLAG(group, FAN_REPORT_TID); + enum pid_type pidtype = tid_mode ? PIDTYPE_PID : PIDTYPE_TGID; + unsigned int pidfd_flags = tid_mode ? PIDFD_THREAD : 0; /* - * The PIDTYPE_TGID check for an event->pid is performed + * The pid_has_task() check for an event->pid is performed * preemptively in an attempt to catch out cases where the event - * listener reads events after the event generating process has + * listener reads events after the event generating task has * already terminated. Depending on flag FAN_REPORT_FD_ERROR, * report either -ESRCH or FAN_NOPIDFD to the event listener in * those cases with all other pidfd creation errors reported as * the error code itself or as FAN_EPIDFD. */ - if (metadata.pid && pid_has_task(event->pid, PIDTYPE_TGID)) - pidfd = pidfd_prepare(event->pid, 0, &pidfd_file); + if (metadata.pid && pid_has_task(event->pid, pidtype)) + pidfd = pidfd_prepare(event->pid, pidfd_flags, &pidfd_file); if (!FAN_GROUP_FLAG(group, FAN_REPORT_FD_ERROR) && pidfd < 0) pidfd = pidfd == -ESRCH ? FAN_NOPIDFD : FAN_EPIDFD; @@ -1628,14 +1625,6 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags) #endif return -EINVAL; - /* - * A pidfd can only be returned for a thread-group leader; thus - * FAN_REPORT_PIDFD and FAN_REPORT_TID need to remain mutually - * exclusive. - */ - if ((flags & FAN_REPORT_PIDFD) && (flags & FAN_REPORT_TID)) - return -EINVAL; - /* Don't allow mixing mnt events with inode events for now */ if (flags & FAN_REPORT_MNT) { if (class != FAN_CLASS_NOTIF) -- 2.54.0