xref: /linux/include/linux/watch_queue.h (revision 0ea5c948cb64bab5bc7a5516774eb8536f05aa0d)
1 c73be61cSDavid Howells // SPDX-License-Identifier: GPL-2.0
2 c73be61cSDavid Howells /* User-mappable watch queue
3 c73be61cSDavid Howells  *
4 c73be61cSDavid Howells  * Copyright (C) 2020 Red Hat, Inc. All Rights Reserved.
5 c73be61cSDavid Howells  * Written by David Howells (dhowells@redhat.com)
6 c73be61cSDavid Howells  *
7 c02b872aSMauro Carvalho Chehab  * See Documentation/core-api/watch_queue.rst
8 c73be61cSDavid Howells  */
9 c73be61cSDavid Howells 
10 c73be61cSDavid Howells #ifndef _LINUX_WATCH_QUEUE_H
11 c73be61cSDavid Howells #define _LINUX_WATCH_QUEUE_H
12 c73be61cSDavid Howells 
13 c73be61cSDavid Howells #include <uapi/linux/watch_queue.h>
14 c73be61cSDavid Howells #include <linux/kref.h>
15 c73be61cSDavid Howells #include <linux/rcupdate.h>
16 c73be61cSDavid Howells 
17 c73be61cSDavid Howells #ifdef CONFIG_WATCH_QUEUE
18 c73be61cSDavid Howells 
19 c73be61cSDavid Howells struct cred;
20 c73be61cSDavid Howells 
21 c73be61cSDavid Howells struct watch_type_filter {
22 c73be61cSDavid Howells 	enum watch_notification_type type;
23 c73be61cSDavid Howells 	__u32		subtype_filter[1];	/* Bitmask of subtypes to filter on */
24 c73be61cSDavid Howells 	__u32		info_filter;		/* Filter on watch_notification::info */
25 c73be61cSDavid Howells 	__u32		info_mask;		/* Mask of relevant bits in info_filter */
26 c73be61cSDavid Howells };
27 c73be61cSDavid Howells 
28 c73be61cSDavid Howells struct watch_filter {
29 c73be61cSDavid Howells 	union {
30 c73be61cSDavid Howells 		struct rcu_head	rcu;
31 c993ee0fSDavid Howells 		/* Bitmask of accepted types */
32 c993ee0fSDavid Howells 		DECLARE_BITMAP(type_filter, WATCH_TYPE__NR);
33 c73be61cSDavid Howells 	};
34 c73be61cSDavid Howells 	u32			nr_filters;	/* Number of filters */
35 *85fadf89SKees Cook 	struct watch_type_filter filters[] __counted_by(nr_filters);
36 c73be61cSDavid Howells };
37 c73be61cSDavid Howells 
38 c73be61cSDavid Howells struct watch_queue {
39 c73be61cSDavid Howells 	struct rcu_head		rcu;
40 c73be61cSDavid Howells 	struct watch_filter __rcu *filter;
41 943211c8SSiddh Raman Pant 	struct pipe_inode_info	*pipe;		/* Pipe we use as a buffer, NULL if queue closed */
42 c73be61cSDavid Howells 	struct hlist_head	watches;	/* Contributory watches */
43 c73be61cSDavid Howells 	struct page		**notes;	/* Preallocated notifications */
44 c73be61cSDavid Howells 	unsigned long		*notes_bitmap;	/* Allocation bitmap for notes */
45 c73be61cSDavid Howells 	struct kref		usage;		/* Object usage count */
46 c73be61cSDavid Howells 	spinlock_t		lock;
47 c73be61cSDavid Howells 	unsigned int		nr_notes;	/* Number of notes */
48 c73be61cSDavid Howells 	unsigned int		nr_pages;	/* Number of pages in notes[] */
49 c73be61cSDavid Howells };
50 c73be61cSDavid Howells 
51 c73be61cSDavid Howells /*
52 c73be61cSDavid Howells  * Representation of a watch on an object.
53 c73be61cSDavid Howells  */
54 c73be61cSDavid Howells struct watch {
55 c73be61cSDavid Howells 	union {
56 c73be61cSDavid Howells 		struct rcu_head	rcu;
57 c73be61cSDavid Howells 		u32		info_id;	/* ID to be OR'd in to info field */
58 c73be61cSDavid Howells 	};
59 c73be61cSDavid Howells 	struct watch_queue __rcu *queue;	/* Queue to post events to */
60 c73be61cSDavid Howells 	struct hlist_node	queue_node;	/* Link in queue->watches */
61 c73be61cSDavid Howells 	struct watch_list __rcu	*watch_list;
62 c73be61cSDavid Howells 	struct hlist_node	list_node;	/* Link in watch_list->watchers */
63 c73be61cSDavid Howells 	const struct cred	*cred;		/* Creds of the owner of the watch */
64 c73be61cSDavid Howells 	void			*private;	/* Private data for the watched object */
65 c73be61cSDavid Howells 	u64			id;		/* Internal identifier */
66 c73be61cSDavid Howells 	struct kref		usage;		/* Object usage count */
67 c73be61cSDavid Howells };
68 c73be61cSDavid Howells 
69 c73be61cSDavid Howells /*
70 c73be61cSDavid Howells  * List of watches on an object.
71 c73be61cSDavid Howells  */
72 c73be61cSDavid Howells struct watch_list {
73 c73be61cSDavid Howells 	struct rcu_head		rcu;
74 c73be61cSDavid Howells 	struct hlist_head	watchers;
75 c73be61cSDavid Howells 	void (*release_watch)(struct watch *);
76 c73be61cSDavid Howells 	spinlock_t		lock;
77 c73be61cSDavid Howells };
78 c73be61cSDavid Howells 
79 c73be61cSDavid Howells extern void __post_watch_notification(struct watch_list *,
80 c73be61cSDavid Howells 				      struct watch_notification *,
81 c73be61cSDavid Howells 				      const struct cred *,
82 c73be61cSDavid Howells 				      u64);
83 c73be61cSDavid Howells extern struct watch_queue *get_watch_queue(int);
84 c73be61cSDavid Howells extern void put_watch_queue(struct watch_queue *);
85 c73be61cSDavid Howells extern void init_watch(struct watch *, struct watch_queue *);
86 c73be61cSDavid Howells extern int add_watch_to_object(struct watch *, struct watch_list *);
87 c73be61cSDavid Howells extern int remove_watch_from_object(struct watch_list *, struct watch_queue *, u64, bool);
88 c73be61cSDavid Howells extern long watch_queue_set_size(struct pipe_inode_info *, unsigned int);
89 c73be61cSDavid Howells extern long watch_queue_set_filter(struct pipe_inode_info *,
90 c73be61cSDavid Howells 				   struct watch_notification_filter __user *);
91 c73be61cSDavid Howells extern int watch_queue_init(struct pipe_inode_info *);
92 c73be61cSDavid Howells extern void watch_queue_clear(struct watch_queue *);
93 c73be61cSDavid Howells 
init_watch_list(struct watch_list * wlist,void (* release_watch)(struct watch *))94 c73be61cSDavid Howells static inline void init_watch_list(struct watch_list *wlist,
95 c73be61cSDavid Howells 				   void (*release_watch)(struct watch *))
96 c73be61cSDavid Howells {
97 c73be61cSDavid Howells 	INIT_HLIST_HEAD(&wlist->watchers);
98 c73be61cSDavid Howells 	spin_lock_init(&wlist->lock);
99 c73be61cSDavid Howells 	wlist->release_watch = release_watch;
100 c73be61cSDavid Howells }
101 c73be61cSDavid Howells 
post_watch_notification(struct watch_list * wlist,struct watch_notification * n,const struct cred * cred,u64 id)102 c73be61cSDavid Howells static inline void post_watch_notification(struct watch_list *wlist,
103 c73be61cSDavid Howells 					   struct watch_notification *n,
104 c73be61cSDavid Howells 					   const struct cred *cred,
105 c73be61cSDavid Howells 					   u64 id)
106 c73be61cSDavid Howells {
107 c73be61cSDavid Howells 	if (unlikely(wlist))
108 c73be61cSDavid Howells 		__post_watch_notification(wlist, n, cred, id);
109 c73be61cSDavid Howells }
110 c73be61cSDavid Howells 
remove_watch_list(struct watch_list * wlist,u64 id)111 c73be61cSDavid Howells static inline void remove_watch_list(struct watch_list *wlist, u64 id)
112 c73be61cSDavid Howells {
113 c73be61cSDavid Howells 	if (wlist) {
114 c73be61cSDavid Howells 		remove_watch_from_object(wlist, NULL, id, true);
115 c73be61cSDavid Howells 		kfree_rcu(wlist, rcu);
116 c73be61cSDavid Howells 	}
117 c73be61cSDavid Howells }
118 c73be61cSDavid Howells 
119 c73be61cSDavid Howells /**
120 c73be61cSDavid Howells  * watch_sizeof - Calculate the information part of the size of a watch record,
121 c73be61cSDavid Howells  * given the structure size.
122 c73be61cSDavid Howells  */
123 c73be61cSDavid Howells #define watch_sizeof(STRUCT) (sizeof(STRUCT) << WATCH_INFO_LENGTH__SHIFT)
124 c73be61cSDavid Howells 
125 8a018eb5SQian Cai #else
watch_queue_init(struct pipe_inode_info * pipe)126 8a018eb5SQian Cai static inline int watch_queue_init(struct pipe_inode_info *pipe)
127 8a018eb5SQian Cai {
128 8a018eb5SQian Cai 	return -ENOPKG;
129 8a018eb5SQian Cai }
130 8a018eb5SQian Cai 
131 c73be61cSDavid Howells #endif
132 c73be61cSDavid Howells 
133 c73be61cSDavid Howells #endif /* _LINUX_WATCH_QUEUE_H */
134