VisionFive2 Linux kernel

StarFive Tech Linux Kernel for VisionFive (JH7110) boards (mirror)

More than 9999 Commits   32 Branches   54 Tags
c82ee6d3beaa4 (Thomas Gleixner  2019-05-19 15:51:48 +0200   1) // SPDX-License-Identifier: GPL-2.0-or-later
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400   2) /*
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400   3)  *  Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com>
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400   4)  */
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400   5) 
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400   6) #include <linux/list.h>
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400   7) #include <linux/mutex.h>
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400   8) #include <linux/slab.h>
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400   9) #include <linux/srcu.h>
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400  10) #include <linux/rculist.h>
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400  11) #include <linux/wait.h>
d46eb14b735b1 (Shakeel Butt     2018-08-17 15:46:39 -0700  12) #include <linux/memcontrol.h>
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400  13) 
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400  14) #include <linux/fsnotify_backend.h>
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400  15) #include "fsnotify.h"
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400  16) 
60063497a95e7 (Arun Sharma      2011-07-26 16:09:06 -0700  17) #include <linux/atomic.h>
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400  18) 
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400  19) /*
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400  20)  * Final freeing of a group
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400  21)  */
cafbaae8afdb1 (Andrew Morton    2014-10-09 15:24:35 -0700  22) static void fsnotify_final_destroy_group(struct fsnotify_group *group)
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400  23) {
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400  24) 	if (group->ops->free_group_priv)
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400  25) 		group->ops->free_group_priv(group);
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400  26) 
d46eb14b735b1 (Shakeel Butt     2018-08-17 15:46:39 -0700  27) 	mem_cgroup_put(group->memcg);
191e1656d18c4 (Fabian Frederick 2020-05-12 20:18:03 +0200  28) 	mutex_destroy(&group->mark_mutex);
d46eb14b735b1 (Shakeel Butt     2018-08-17 15:46:39 -0700  29) 
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400  30) 	kfree(group);
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400  31) }
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400  32) 
12703dbfeb154 (Jan Kara         2016-09-19 14:44:27 -0700  33) /*
12703dbfeb154 (Jan Kara         2016-09-19 14:44:27 -0700  34)  * Stop queueing new events for this group. Once this function returns
12703dbfeb154 (Jan Kara         2016-09-19 14:44:27 -0700  35)  * fsnotify_add_event() will not add any new events to the group's queue.
12703dbfeb154 (Jan Kara         2016-09-19 14:44:27 -0700  36)  */
12703dbfeb154 (Jan Kara         2016-09-19 14:44:27 -0700  37) void fsnotify_group_stop_queueing(struct fsnotify_group *group)
12703dbfeb154 (Jan Kara         2016-09-19 14:44:27 -0700  38) {
c21dbe20f6062 (Jan Kara         2016-10-07 16:56:52 -0700  39) 	spin_lock(&group->notification_lock);
12703dbfeb154 (Jan Kara         2016-09-19 14:44:27 -0700  40) 	group->shutdown = true;
c21dbe20f6062 (Jan Kara         2016-10-07 16:56:52 -0700  41) 	spin_unlock(&group->notification_lock);
12703dbfeb154 (Jan Kara         2016-09-19 14:44:27 -0700  42) }
12703dbfeb154 (Jan Kara         2016-09-19 14:44:27 -0700  43) 
3be25f49b9d6a (Eric Paris       2009-05-21 17:01:26 -0400  44) /*
23e964c284ca0 (Lino Sanfilippo  2011-06-14 17:29:47 +0200  45)  * Trying to get rid of a group. Remove all marks, flush all events and release
23e964c284ca0 (Lino Sanfilippo  2011-06-14 17:29:47 +0200  46)  * the group reference.
23e964c284ca0 (Lino Sanfilippo  2011-06-14 17:29:47 +0200  47)  * Note that another thread calling fsnotify_clear_marks_by_group() may still
23e964c284ca0 (Lino Sanfilippo  2011-06-14 17:29:47 +0200  48)  * hold a ref to the group.
3be25f49b9d6a (Eric Paris       2009-05-21 17:01:26 -0400  49)  */
d8153d4d8b7b6 (Lino Sanfilippo  2011-06-14 17:29:45 +0200  50) void fsnotify_destroy_group(struct fsnotify_group *group)
3be25f49b9d6a (Eric Paris       2009-05-21 17:01:26 -0400  51) {
12703dbfeb154 (Jan Kara         2016-09-19 14:44:27 -0700  52) 	/*
12703dbfeb154 (Jan Kara         2016-09-19 14:44:27 -0700  53) 	 * Stop queueing new events. The code below is careful enough to not
12703dbfeb154 (Jan Kara         2016-09-19 14:44:27 -0700  54) 	 * require this but fanotify needs to stop queuing events even before
12703dbfeb154 (Jan Kara         2016-09-19 14:44:27 -0700  55) 	 * fsnotify_destroy_group() is called and this makes the other callers
12703dbfeb154 (Jan Kara         2016-09-19 14:44:27 -0700  56) 	 * of fsnotify_destroy_group() to see the same behavior.
12703dbfeb154 (Jan Kara         2016-09-19 14:44:27 -0700  57) 	 */
12703dbfeb154 (Jan Kara         2016-09-19 14:44:27 -0700  58) 	fsnotify_group_stop_queueing(group);
12703dbfeb154 (Jan Kara         2016-09-19 14:44:27 -0700  59) 
f09b04a03e023 (Jan Kara         2016-12-21 14:48:18 +0100  60) 	/* Clear all marks for this group and queue them for destruction */
d6f7b98bc8147 (Amir Goldstein   2018-04-20 16:10:49 -0700  61) 	fsnotify_clear_marks_by_group(group, FSNOTIFY_OBJ_ALL_TYPES_MASK);
2e37c6ca8d76c (Jan Kara         2017-01-04 10:51:58 +0100  62) 
2e37c6ca8d76c (Jan Kara         2017-01-04 10:51:58 +0100  63) 	/*
2e37c6ca8d76c (Jan Kara         2017-01-04 10:51:58 +0100  64) 	 * Some marks can still be pinned when waiting for response from
2e37c6ca8d76c (Jan Kara         2017-01-04 10:51:58 +0100  65) 	 * userspace. Wait for those now. fsnotify_prepare_user_wait() will
2e37c6ca8d76c (Jan Kara         2017-01-04 10:51:58 +0100  66) 	 * not succeed now so this wait is race-free.
2e37c6ca8d76c (Jan Kara         2017-01-04 10:51:58 +0100  67) 	 */
2e37c6ca8d76c (Jan Kara         2017-01-04 10:51:58 +0100  68) 	wait_event(group->notification_waitq, !atomic_read(&group->user_waits));
3be25f49b9d6a (Eric Paris       2009-05-21 17:01:26 -0400  69) 
35e481761cdc6 (Jan Kara         2016-05-19 17:08:59 -0700  70) 	/*
f09b04a03e023 (Jan Kara         2016-12-21 14:48:18 +0100  71) 	 * Wait until all marks get really destroyed. We could actually destroy
f09b04a03e023 (Jan Kara         2016-12-21 14:48:18 +0100  72) 	 * them ourselves instead of waiting for worker to do it, however that
f09b04a03e023 (Jan Kara         2016-12-21 14:48:18 +0100  73) 	 * would be racy as worker can already be processing some marks before
f09b04a03e023 (Jan Kara         2016-12-21 14:48:18 +0100  74) 	 * we even entered fsnotify_destroy_group().
35e481761cdc6 (Jan Kara         2016-05-19 17:08:59 -0700  75) 	 */
f09b04a03e023 (Jan Kara         2016-12-21 14:48:18 +0100  76) 	fsnotify_wait_marks_destroyed();
75c1be487a690 (Eric Paris       2010-07-28 10:18:38 -0400  77) 
35e481761cdc6 (Jan Kara         2016-05-19 17:08:59 -0700  78) 	/*
35e481761cdc6 (Jan Kara         2016-05-19 17:08:59 -0700  79) 	 * Since we have waited for fsnotify_mark_srcu in
35e481761cdc6 (Jan Kara         2016-05-19 17:08:59 -0700  80) 	 * fsnotify_mark_destroy_list() there can be no outstanding event
35e481761cdc6 (Jan Kara         2016-05-19 17:08:59 -0700  81) 	 * notification against this group. So clearing the notification queue
35e481761cdc6 (Jan Kara         2016-05-19 17:08:59 -0700  82) 	 * of all events is reliable now.
35e481761cdc6 (Jan Kara         2016-05-19 17:08:59 -0700  83) 	 */
23e964c284ca0 (Lino Sanfilippo  2011-06-14 17:29:47 +0200  84) 	fsnotify_flush_notify(group);
23e964c284ca0 (Lino Sanfilippo  2011-06-14 17:29:47 +0200  85) 
ff57cd5863cf3 (Jan Kara         2014-02-21 19:14:11 +0100  86) 	/*
ff57cd5863cf3 (Jan Kara         2014-02-21 19:14:11 +0100  87) 	 * Destroy overflow event (we cannot use fsnotify_destroy_event() as
ff57cd5863cf3 (Jan Kara         2014-02-21 19:14:11 +0100  88) 	 * that deliberately ignores overflow events.
ff57cd5863cf3 (Jan Kara         2014-02-21 19:14:11 +0100  89) 	 */
ff57cd5863cf3 (Jan Kara         2014-02-21 19:14:11 +0100  90) 	if (group->overflow_event)
ff57cd5863cf3 (Jan Kara         2014-02-21 19:14:11 +0100  91) 		group->ops->free_event(group->overflow_event);
ff57cd5863cf3 (Jan Kara         2014-02-21 19:14:11 +0100  92) 
23e964c284ca0 (Lino Sanfilippo  2011-06-14 17:29:47 +0200  93) 	fsnotify_put_group(group);
3be25f49b9d6a (Eric Paris       2009-05-21 17:01:26 -0400  94) }
3be25f49b9d6a (Eric Paris       2009-05-21 17:01:26 -0400  95) 
986129520479d (Lino Sanfilippo  2011-06-14 17:29:46 +0200  96) /*
986129520479d (Lino Sanfilippo  2011-06-14 17:29:46 +0200  97)  * Get reference to a group.
986129520479d (Lino Sanfilippo  2011-06-14 17:29:46 +0200  98)  */
986129520479d (Lino Sanfilippo  2011-06-14 17:29:46 +0200  99) void fsnotify_get_group(struct fsnotify_group *group)
986129520479d (Lino Sanfilippo  2011-06-14 17:29:46 +0200 100) {
7761daa6a1599 (Elena Reshetova  2017-10-20 13:26:01 +0300 101) 	refcount_inc(&group->refcnt);
986129520479d (Lino Sanfilippo  2011-06-14 17:29:46 +0200 102) }
986129520479d (Lino Sanfilippo  2011-06-14 17:29:46 +0200 103) 
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400 104) /*
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400 105)  * Drop a reference to a group.  Free it if it's through.
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400 106)  */
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400 107) void fsnotify_put_group(struct fsnotify_group *group)
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400 108) {
7761daa6a1599 (Elena Reshetova  2017-10-20 13:26:01 +0300 109) 	if (refcount_dec_and_test(&group->refcnt))
23e964c284ca0 (Lino Sanfilippo  2011-06-14 17:29:47 +0200 110) 		fsnotify_final_destroy_group(group);
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400 111) }
b72679ee89a0a (Trond Myklebust  2019-08-18 14:18:46 -0400 112) EXPORT_SYMBOL_GPL(fsnotify_put_group);
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400 113) 
ac7b79fd190b0 (Shakeel Butt     2020-12-19 20:46:08 -0800 114) static struct fsnotify_group *__fsnotify_alloc_group(
ac7b79fd190b0 (Shakeel Butt     2020-12-19 20:46:08 -0800 115) 				const struct fsnotify_ops *ops, gfp_t gfp)
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400 116) {
74be0cc82835a (Eric Paris       2009-12-17 21:24:22 -0500 117) 	struct fsnotify_group *group;
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400 118) 
ac7b79fd190b0 (Shakeel Butt     2020-12-19 20:46:08 -0800 119) 	group = kzalloc(sizeof(struct fsnotify_group), gfp);
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400 120) 	if (!group)
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400 121) 		return ERR_PTR(-ENOMEM);
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400 122) 
36fddebaa8a91 (Eric Paris       2009-12-17 21:24:23 -0500 123) 	/* set to 0 when there a no external references to this group */
7761daa6a1599 (Elena Reshetova  2017-10-20 13:26:01 +0300 124) 	refcount_set(&group->refcnt, 1);
abc77577a669f (Jan Kara         2016-11-10 16:02:11 +0100 125) 	atomic_set(&group->user_waits, 0);
36fddebaa8a91 (Eric Paris       2009-12-17 21:24:23 -0500 126) 
c21dbe20f6062 (Jan Kara         2016-10-07 16:56:52 -0700 127) 	spin_lock_init(&group->notification_lock);
a2d8bc6cb4a30 (Eric Paris       2009-05-21 17:01:37 -0400 128) 	INIT_LIST_HEAD(&group->notification_list);
a2d8bc6cb4a30 (Eric Paris       2009-05-21 17:01:37 -0400 129) 	init_waitqueue_head(&group->notification_waitq);
a2d8bc6cb4a30 (Eric Paris       2009-05-21 17:01:37 -0400 130) 	group->max_events = UINT_MAX;
a2d8bc6cb4a30 (Eric Paris       2009-05-21 17:01:37 -0400 131) 
986ab09807ca9 (Lino Sanfilippo  2011-06-14 17:29:50 +0200 132) 	mutex_init(&group->mark_mutex);
e61ce86737b4d (Eric Paris       2009-12-17 21:24:24 -0500 133) 	INIT_LIST_HEAD(&group->marks_list);
3be25f49b9d6a (Eric Paris       2009-05-21 17:01:26 -0400 134) 
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400 135) 	group->ops = ops;
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400 136) 
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400 137) 	return group;
90586523eb4b3 (Eric Paris       2009-05-21 17:01:20 -0400 138) }
ac7b79fd190b0 (Shakeel Butt     2020-12-19 20:46:08 -0800 139) 
ac7b79fd190b0 (Shakeel Butt     2020-12-19 20:46:08 -0800 140) /*
ac7b79fd190b0 (Shakeel Butt     2020-12-19 20:46:08 -0800 141)  * Create a new fsnotify_group and hold a reference for the group returned.
ac7b79fd190b0 (Shakeel Butt     2020-12-19 20:46:08 -0800 142)  */
ac7b79fd190b0 (Shakeel Butt     2020-12-19 20:46:08 -0800 143) struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops)
ac7b79fd190b0 (Shakeel Butt     2020-12-19 20:46:08 -0800 144) {
ac7b79fd190b0 (Shakeel Butt     2020-12-19 20:46:08 -0800 145) 	return __fsnotify_alloc_group(ops, GFP_KERNEL);
ac7b79fd190b0 (Shakeel Butt     2020-12-19 20:46:08 -0800 146) }
b72679ee89a0a (Trond Myklebust  2019-08-18 14:18:46 -0400 147) EXPORT_SYMBOL_GPL(fsnotify_alloc_group);
0a6b6bd5919a6 (Eric Paris       2011-10-14 17:43:39 -0400 148) 
ac7b79fd190b0 (Shakeel Butt     2020-12-19 20:46:08 -0800 149) /*
ac7b79fd190b0 (Shakeel Butt     2020-12-19 20:46:08 -0800 150)  * Create a new fsnotify_group and hold a reference for the group returned.
ac7b79fd190b0 (Shakeel Butt     2020-12-19 20:46:08 -0800 151)  */
ac7b79fd190b0 (Shakeel Butt     2020-12-19 20:46:08 -0800 152) struct fsnotify_group *fsnotify_alloc_user_group(const struct fsnotify_ops *ops)
ac7b79fd190b0 (Shakeel Butt     2020-12-19 20:46:08 -0800 153) {
ac7b79fd190b0 (Shakeel Butt     2020-12-19 20:46:08 -0800 154) 	return __fsnotify_alloc_group(ops, GFP_KERNEL_ACCOUNT);
ac7b79fd190b0 (Shakeel Butt     2020-12-19 20:46:08 -0800 155) }
ac7b79fd190b0 (Shakeel Butt     2020-12-19 20:46:08 -0800 156) EXPORT_SYMBOL_GPL(fsnotify_alloc_user_group);
ac7b79fd190b0 (Shakeel Butt     2020-12-19 20:46:08 -0800 157) 
0a6b6bd5919a6 (Eric Paris       2011-10-14 17:43:39 -0400 158) int fsnotify_fasync(int fd, struct file *file, int on)
0a6b6bd5919a6 (Eric Paris       2011-10-14 17:43:39 -0400 159) {
0a6b6bd5919a6 (Eric Paris       2011-10-14 17:43:39 -0400 160) 	struct fsnotify_group *group = file->private_data;
0a6b6bd5919a6 (Eric Paris       2011-10-14 17:43:39 -0400 161) 
0a6b6bd5919a6 (Eric Paris       2011-10-14 17:43:39 -0400 162) 	return fasync_helper(fd, file, on, &group->fsn_fa) >= 0 ? 0 : -EIO;
0a6b6bd5919a6 (Eric Paris       2011-10-14 17:43:39 -0400 163) }