From patchwork Wed Oct 3 21:25:38 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 10625333 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1710C1731 for ; Wed, 3 Oct 2018 21:26:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0819129080 for ; Wed, 3 Oct 2018 21:26:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id F09D32908A; Wed, 3 Oct 2018 21:26:00 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7CFB029080 for ; Wed, 3 Oct 2018 21:26:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726770AbeJDEQG (ORCPT ); Thu, 4 Oct 2018 00:16:06 -0400 Received: from mail-wm1-f66.google.com ([209.85.128.66]:40623 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725871AbeJDEQF (ORCPT ); Thu, 4 Oct 2018 00:16:05 -0400 Received: by mail-wm1-f66.google.com with SMTP id z204-v6so6158072wmc.5 for ; Wed, 03 Oct 2018 14:25:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Of+1832wB5nEvkGcr3xBQ2mHK0QBsVUpt4R7Z1J/Nes=; b=f5mX66jjLEfD6/Xs1Oep2XnA6OVMxGkaO4kzMqSnCHK2J1VeRUcNvkMc3ky1YjAlME agaDAyWzHTt782SjR6kz0dGx3Scy/3H3+yDoUQuIvC7CNj3VZg51/SJGf1ZH4Vz50OYB E7mCGG1YPMqmwGBizPH5LLj2QmBz5lAsXBYuC9y8UNtpSTNDjOe1xMDBEMcJdzk9GWKH uJqjYoPWf7R4tZbTkaqo20LKmv0O5PR4hElk3J9P/rBOYWT2FJwPFTk2cMVfBmPdm+/3 s/X5ZWoJ3R/6b04bSbcGDWiph2OPODk0mqnR+zZqUQdMswPypqHSn5EyXf7EFyiArqjy BKKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Of+1832wB5nEvkGcr3xBQ2mHK0QBsVUpt4R7Z1J/Nes=; b=FoTTZVRtDWeIBCWLvybxzwAWHGssEuc/jOydLksbEwUUh/LD/cDSzo7rU7X88/4v0H 7+MU8mykC/qvgJ24TnUYkgaszBJABfjKeuuq0NvfESEgB4t2zmW0q1eUUh8UWBdukhGD O+KT56syQFz7FSXSDB85+N6qWpdBOcAs0FE+4hUNzooo4GGdpem/WAvFL1JQKCVsCHRI wCqu+RL3PBe+3uSnaTPOIJoGo4Ra4lRpuZvrdGAugJKaEoHFNeYvVcHUnW4uP4l0lo8a og0wlLQiP2/qaMY7dWo4dZXyu42yjMQqxHuGWOIjdhhEFZregUzMGOGtBqJSE/2EzKsw Bh/Q== X-Gm-Message-State: ABuFfogCLnuYYQYyVXe5+29lMbkUYb/zeI6wN7O7C06p29x1kswHEMVv h4FrfjMLk6J8PCP4/mEYAuzS9Vty X-Google-Smtp-Source: ACcGV60RKnY1m78lUYx4L6REw3WdyNeP7eOQ3XpuRs3TmiC8ljAMboQVolujaqwsEmQxjMSoDljRPw== X-Received: by 2002:a7b:c150:: with SMTP id z16-v6mr2725232wmi.25.1538601956473; Wed, 03 Oct 2018 14:25:56 -0700 (PDT) Received: from localhost.localdomain ([141.226.14.107]) by smtp.gmail.com with ESMTPSA id 193-v6sm4073866wmj.21.2018.10.03.14.25.55 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 03 Oct 2018 14:25:55 -0700 (PDT) From: Amir Goldstein To: Jan Kara Cc: linux-fsdevel@vger.kernel.org, nixiaoming Subject: [PATCH v3 7/8] fanotify: support reporting thread id instead of process id Date: Thu, 4 Oct 2018 00:25:38 +0300 Message-Id: <20181003212539.2384-8-amir73il@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181003212539.2384-1-amir73il@gmail.com> References: <20181003212539.2384-1-amir73il@gmail.com> Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP In order to identify which thread triggered the event in a multi-threaded program, add the FAN_EVENT_INFO_TID flag in fanotify_init to opt-in for reporting the event creator's thread id information. Signed-off-by: nixiaoming Signed-off-by: Amir Goldstein --- fs/notify/fanotify/fanotify.c | 9 ++++++--- fs/notify/fanotify/fanotify.h | 2 +- fs/notify/fanotify/fanotify_user.c | 4 ++-- include/linux/fanotify.h | 3 +++ include/uapi/linux/fanotify.h | 3 +++ 5 files changed, 15 insertions(+), 6 deletions(-) diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c index 361e3a0a445c..2c57186caa2e 100644 --- a/fs/notify/fanotify/fanotify.c +++ b/fs/notify/fanotify/fanotify.c @@ -25,7 +25,7 @@ static bool should_merge(struct fsnotify_event *old_fsn, old = FANOTIFY_E(old_fsn); new = FANOTIFY_E(new_fsn); - if (old_fsn->inode == new_fsn->inode && old->tgid == new->tgid && + if (old_fsn->inode == new_fsn->inode && old->pid == new->pid && old->path.mnt == new->path.mnt && old->path.dentry == new->path.dentry) return true; @@ -171,7 +171,10 @@ struct fanotify_event_info *fanotify_alloc_event(struct fsnotify_group *group, goto out; init: __maybe_unused fsnotify_init_event(&event->fse, inode, mask); - event->tgid = get_pid(task_tgid(current)); + if (FAN_GROUP_FLAG(group, FAN_EVENT_INFO_TID)) + event->pid = get_pid(task_pid(current)); + else + event->pid = get_pid(task_tgid(current)); if (path) { event->path = *path; path_get(&event->path); @@ -270,7 +273,7 @@ static void fanotify_free_event(struct fsnotify_event *fsn_event) event = FANOTIFY_E(fsn_event); path_put(&event->path); - put_pid(event->tgid); + put_pid(event->pid); if (fanotify_is_perm_event(fsn_event->mask)) { kmem_cache_free(fanotify_perm_event_cachep, FANOTIFY_PE(fsn_event)); diff --git a/fs/notify/fanotify/fanotify.h b/fs/notify/fanotify/fanotify.h index 88a8290a61cb..ea05b8a401e7 100644 --- a/fs/notify/fanotify/fanotify.h +++ b/fs/notify/fanotify/fanotify.h @@ -19,7 +19,7 @@ struct fanotify_event_info { * during this object's lifetime */ struct path path; - struct pid *tgid; + struct pid *pid; }; /* diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index 14594e491d2b..e03be5071362 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -132,7 +132,7 @@ static int fill_event_metadata(struct fsnotify_group *group, metadata->vers = FANOTIFY_METADATA_VERSION; metadata->reserved = 0; metadata->mask = fsn_event->mask & FANOTIFY_OUTGOING_EVENTS; - metadata->pid = pid_vnr(event->tgid); + metadata->pid = pid_vnr(event->pid); if (unlikely(fsn_event->mask & FAN_Q_OVERFLOW)) metadata->fd = FAN_NOFD; else { @@ -944,7 +944,7 @@ COMPAT_SYSCALL_DEFINE6(fanotify_mark, */ static int __init fanotify_user_setup(void) { - BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 6); + BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 7); BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9); fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, diff --git a/include/linux/fanotify.h b/include/linux/fanotify.h index afddd7e0d5a1..05b696b4856b 100644 --- a/include/linux/fanotify.h +++ b/include/linux/fanotify.h @@ -18,7 +18,10 @@ #define FANOTIFY_CLASS_BITS (FAN_CLASS_NOTIF | FAN_CLASS_CONTENT | \ FAN_CLASS_PRE_CONTENT) +#define FANOTIFY_EVENT_INFO_FLAGS (FAN_EVENT_INFO_TID) + #define FANOTIFY_INIT_FLAGS (FANOTIFY_CLASS_BITS | \ + FANOTIFY_EVENT_INFO_FLAGS | \ FAN_CLOEXEC | FAN_NONBLOCK | \ FAN_UNLIMITED_QUEUE | FAN_UNLIMITED_MARKS) diff --git a/include/uapi/linux/fanotify.h b/include/uapi/linux/fanotify.h index d0c05de670ef..00b2304ed124 100644 --- a/include/uapi/linux/fanotify.h +++ b/include/uapi/linux/fanotify.h @@ -40,6 +40,9 @@ #define FAN_UNLIMITED_MARKS 0x00000020 #define FAN_ENABLE_AUDIT 0x00000040 +/* Flags to determine fanotify event format */ +#define FAN_EVENT_INFO_TID 0x00000100 /* event->pid is thread id */ + /* Deprecated - do not use this in programs and do not add new flags here! */ #define FAN_ALL_INIT_FLAGS (FAN_CLOEXEC | FAN_NONBLOCK | \ FAN_ALL_CLASS_BITS | FAN_UNLIMITED_QUEUE |\