From patchwork Fri Sep 21 18:20:30 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 10610839 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 5DD52161F for ; Fri, 21 Sep 2018 18:20:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4DC8A2E6C9 for ; Fri, 21 Sep 2018 18:20:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4C2812E6D1; Fri, 21 Sep 2018 18:20:43 +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 D1DF02E6D6 for ; Fri, 21 Sep 2018 18:20:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390912AbeIVAKp (ORCPT ); Fri, 21 Sep 2018 20:10:45 -0400 Received: from mail-wr1-f65.google.com ([209.85.221.65]:44508 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390076AbeIVAKp (ORCPT ); Fri, 21 Sep 2018 20:10:45 -0400 Received: by mail-wr1-f65.google.com with SMTP id v16-v6so13718924wro.11 for ; Fri, 21 Sep 2018 11:20:40 -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=EeY7yAhV2Epsxk/1V453uA88dHryurcKB9UOvuj5AAs=; b=FGtDWCw3FWUajeNi4WizJ1AVr7Nj0+nvejLis1/Gg22EZEgpqhMzf9mnMVJNldbYGq IjnUZbdklD1S9mkffz+PPkEd889GielyetuOXsyxlnBhNedT+w7oHFZugbdKG+Pp4GJb BK1qBZsmmV2sgQMyMIw3CujiJTYkEgXjDexHJQENwI0mVHeb8yCq5E3r9k987U0WR0UW AwKEksXseNJP95OPsNvaFhXam/5sUG/q/g6lqfPNtXRkFBCz5fsSIZZRqwH5+BlkigxV aViaQI4VPhxwpTC1cCtUOCor3Rsuv5E26//jP6/BFO/AdsOV1VaooZLirrULZSFg3NRI Wg1g== 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=EeY7yAhV2Epsxk/1V453uA88dHryurcKB9UOvuj5AAs=; b=P8KQCB/0637ugyiZz2kAhwXCh2bb46X/p8RENXvApRD7B91X2ofjLFolDFT2cnrlvk dF8zK0dAFy4V3ADZjxtCGNStpdEYVPGKdUfEsNjxu6xehFPymZNIahmH2TkqbSjeCT8c YFaoZzYBdoY8N7Cii7/9pJ+QjyZPuV41XXUemoTvEvg3MfuR2j4Tc3xqTX8/B5fZWG/i 5zbIUP8IxZhUjnU1HYiNHoYNcTqDeaaZ4UXSN8My5k1l2929cOZpUKFYMzruFj9JrA5H wQZFvtPGLwe/dRBoy2j9fo+6dgSG4p1a5RhcfHViishyUDxBkbq/yl1AwRWWBT7yKtF9 SLSQ== X-Gm-Message-State: ABuFfog4kBFIZ84k2yfG19iwGCw+RgSlC/tSsJYBkM+kGwha60P+6txo 5FB2Mwcoew0bDPvi2ax6Nlaeclyg X-Google-Smtp-Source: ACcGV60hT8/JuFJi6p8rpGYzy+khcC3J33cBuqxJzUJqnv7T3OK/bbtUz0u3aFJEo3/oiYweISgJlQ== X-Received: by 2002:adf:ac4a:: with SMTP id v68-v6mr2735244wrc.165.1537554040093; Fri, 21 Sep 2018 11:20:40 -0700 (PDT) Received: from localhost.localdomain ([141.226.14.107]) by smtp.gmail.com with ESMTPSA id v133-v6sm7037787wma.36.2018.09.21.11.20.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 21 Sep 2018 11:20:39 -0700 (PDT) From: Amir Goldstein To: Jan Kara Cc: nixiaoming@huawei.com, linux-fsdevel@vger.kernel.org Subject: [PATCH v2 1/2] fanotify: store fanotify_init() flags in group's fanotify_data Date: Fri, 21 Sep 2018 21:20:30 +0300 Message-Id: <20180921182031.3184-2-amir73il@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180921182031.3184-1-amir73il@gmail.com> References: <20180921182031.3184-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 This averts the need to re-generate flags in fanotify_show_fdinfo() and sets the scene for addition of more upcoming flags without growing new members to the fanotify_data struct. Signed-off-by: Amir Goldstein --- fs/notify/fanotify/fanotify_user.c | 8 ++++---- fs/notify/fdinfo.c | 24 +----------------------- include/linux/fanotify.h | 4 ++++ include/linux/fsnotify_backend.h | 4 ++-- 4 files changed, 11 insertions(+), 29 deletions(-) diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index 1347c588f778..15719d4aa4b5 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -191,7 +191,7 @@ static int process_access_response(struct fsnotify_group *group, if (fd < 0) return -EINVAL; - if ((response & FAN_AUDIT) && !group->fanotify_data.audit) + if ((response & FAN_AUDIT) && !FAN_GROUP_FLAG(group, FAN_ENABLE_AUDIT)) return -EINVAL; event = dequeue_event(group, fd); @@ -701,8 +701,8 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags) struct user_struct *user; struct fanotify_event_info *oevent; - pr_debug("%s: flags=%d event_f_flags=%d\n", - __func__, flags, event_f_flags); + pr_debug("%s: flags=%x event_f_flags=%x\n", + __func__, flags, event_f_flags); if (!capable(CAP_SYS_ADMIN)) return -EPERM; @@ -746,6 +746,7 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags) } group->fanotify_data.user = user; + group->fanotify_data.flags = flags; atomic_inc(&user->fanotify_listeners); group->memcg = get_mem_cgroup_from_mm(current->mm); @@ -798,7 +799,6 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags) fd = -EPERM; if (!capable(CAP_AUDIT_WRITE)) goto out_destroy_group; - group->fanotify_data.audit = true; } fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags); diff --git a/fs/notify/fdinfo.c b/fs/notify/fdinfo.c index 25385e336ac7..348a184bcdda 100644 --- a/fs/notify/fdinfo.c +++ b/fs/notify/fdinfo.c @@ -142,31 +142,9 @@ static void fanotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark) void fanotify_show_fdinfo(struct seq_file *m, struct file *f) { struct fsnotify_group *group = f->private_data; - unsigned int flags = 0; - - switch (group->priority) { - case FS_PRIO_0: - flags |= FAN_CLASS_NOTIF; - break; - case FS_PRIO_1: - flags |= FAN_CLASS_CONTENT; - break; - case FS_PRIO_2: - flags |= FAN_CLASS_PRE_CONTENT; - break; - } - - if (group->max_events == UINT_MAX) - flags |= FAN_UNLIMITED_QUEUE; - - if (group->fanotify_data.max_marks == UINT_MAX) - flags |= FAN_UNLIMITED_MARKS; - - if (group->fanotify_data.audit) - flags |= FAN_ENABLE_AUDIT; seq_printf(m, "fanotify flags:%x event-flags:%x\n", - flags, group->fanotify_data.f_flags); + group->fanotify_data.flags, group->fanotify_data.f_flags); show_fdinfo(m, f, fanotify_fdinfo); } diff --git a/include/linux/fanotify.h b/include/linux/fanotify.h index 096c96f4f16a..9c5ea3bdfaa0 100644 --- a/include/linux/fanotify.h +++ b/include/linux/fanotify.h @@ -6,4 +6,8 @@ /* not valid from userspace, only kernel internal */ #define FAN_MARK_ONDIR 0x00000100 + +#define FAN_GROUP_FLAG(group, flag) \ + ((group)->fanotify_data.flags & (flag)) + #endif /* _LINUX_FANOTIFY_H */ diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h index 81b88fc9df31..8e91341cbd8a 100644 --- a/include/linux/fsnotify_backend.h +++ b/include/linux/fsnotify_backend.h @@ -189,10 +189,10 @@ struct fsnotify_group { /* allows a group to block waiting for a userspace response */ struct list_head access_list; wait_queue_head_t access_waitq; - int f_flags; + int flags; /* flags from fanotify_init() */ + int f_flags; /* event_f_flags from fanotify_init() */ unsigned int max_marks; struct user_struct *user; - bool audit; } fanotify_data; #endif /* CONFIG_FANOTIFY */ }; From patchwork Fri Sep 21 18:20:31 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 10610841 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 3022215A6 for ; Fri, 21 Sep 2018 18:20:47 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1C0D02E6C3 for ; Fri, 21 Sep 2018 18:20:47 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 188942E6B5; Fri, 21 Sep 2018 18:20:47 +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 924002E6BB for ; Fri, 21 Sep 2018 18:20:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390999AbeIVAKt (ORCPT ); Fri, 21 Sep 2018 20:10:49 -0400 Received: from mail-wr1-f65.google.com ([209.85.221.65]:45670 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390076AbeIVAKt (ORCPT ); Fri, 21 Sep 2018 20:10:49 -0400 Received: by mail-wr1-f65.google.com with SMTP id 20-v6so13713117wrb.12; Fri, 21 Sep 2018 11:20:44 -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=ofFmOs5nnL2deLE3faa5s8EHSrkPhwPFocB03Qw+hTI=; b=hsj9MniNrPfxMQ3u4eEuvCfGXfglyynI1vRg45XGYULBsvgA9gZlnyYT/+zfZscM35 jp+fxnOCYtdVu6P3em3A/HB5AfQGJCRRpMD1xCwKDu08Wmz6l5a0kqbfSK/8QYLLHjHp 2+/VC9SiE3Jj/D9/PhMbJaI5T7pPptxhsxP0zQkOGYxisHD6uizfpjiKE2CMo9ArLpcC V9egNPpX3+a0LJfWRlb/fnnlvsUwQJ/drc3WKIQq8Q+apkVFJtMDYJoOf1BTtCsjDg7/ 0dalYJF1v/Pgqy96cHHFFDd0UYaP8AVDNvDrGdYTtfZoN2psXMRy1Op5uEoIoGjnyC73 2yeg== 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=ofFmOs5nnL2deLE3faa5s8EHSrkPhwPFocB03Qw+hTI=; b=O++aT8xdAya0RFjhbPQezFpXoSD4vHDETJrTxN/JWwxw6RVGiJ2AUhf0FrmK/rHt0l ZrfZ8yMYYeJ9Io87NFfCnhbXqjuiSGLxhwJx/mfGQL3Nt8xY01uVAqjXGHv9v//8Wv/8 Uoh9dIOn6ozmGyiJPvrjgxHfIbJQbdQmpcse3adS6xLHcbxArSEjZ+Y8GBm4BgDfnBTj 0LPsXzezKKaAkHRV1jYhVZ0xr3/qZsXtQQcxY6ThPvJVCXv7285F5KYL0IRN7BZ2HnBs 1Cony20gboIMA8iLQ3pHMM3b/znMQobv1KBBBMb+xCOZf1OFXxDvalNPPKq9yAvoVciL F3kA== X-Gm-Message-State: APzg51CMjt9HH8/NLMqME7wsDDvYq0Kvgyv3CxEBmgRoZHDaHpkt3a4N KRYFffpIOsDEkn3JWdysR5w= X-Google-Smtp-Source: ANB0VdZwDtBFl/OGoDbVA4Qqj3G7V7kEQNBrbsk84qzilEhAGCnhEa5Cvna7CjfQjFZ7JDKnGWuO3A== X-Received: by 2002:adf:b112:: with SMTP id l18-v6mr41556898wra.101.1537554041411; Fri, 21 Sep 2018 11:20:41 -0700 (PDT) Received: from localhost.localdomain ([141.226.14.107]) by smtp.gmail.com with ESMTPSA id v133-v6sm7037787wma.36.2018.09.21.11.20.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 21 Sep 2018 11:20:40 -0700 (PDT) From: Amir Goldstein To: Jan Kara Cc: nixiaoming@huawei.com, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org Subject: [PATCH v2 2/2] fanotify: support reporting thread id instead of process id Date: Fri, 21 Sep 2018 21:20:31 +0300 Message-Id: <20180921182031.3184-3-amir73il@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180921182031.3184-1-amir73il@gmail.com> References: <20180921182031.3184-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 From: nixiaoming 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 | 2 +- include/uapi/linux/fanotify.h | 9 +++++++-- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c index 94b52157bf8d..e380027e545c 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); @@ -268,7 +271,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 8609ba06f474..d8d85104e7ff 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 15719d4aa4b5..039d31ea4380 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 & FAN_ALL_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 { diff --git a/include/uapi/linux/fanotify.h b/include/uapi/linux/fanotify.h index ad81234d1919..6d98d14a6314 100644 --- a/include/uapi/linux/fanotify.h +++ b/include/uapi/linux/fanotify.h @@ -38,9 +38,14 @@ #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 */ + +#define FAN_EVENT_INFO_FLAGS (FAN_EVENT_INFO_TID) + #define FAN_ALL_INIT_FLAGS (FAN_CLOEXEC | FAN_NONBLOCK | \ - FAN_ALL_CLASS_BITS | FAN_UNLIMITED_QUEUE |\ - FAN_UNLIMITED_MARKS) + FAN_ALL_CLASS_BITS | FAN_EVENT_INFO_FLAGS | \ + FAN_UNLIMITED_QUEUE | FAN_UNLIMITED_MARKS) /* flags used for fanotify_modify_mark() */ #define FAN_MARK_ADD 0x00000001