Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* User-mappable watch queue
0003  *
0004  * Copyright (C) 2020 Red Hat, Inc. All Rights Reserved.
0005  * Written by David Howells (dhowells@redhat.com)
0006  *
0007  * See Documentation/core-api/watch_queue.rst
0008  */
0009 
0010 #ifndef _LINUX_WATCH_QUEUE_H
0011 #define _LINUX_WATCH_QUEUE_H
0012 
0013 #include <uapi/linux/watch_queue.h>
0014 #include <linux/kref.h>
0015 #include <linux/rcupdate.h>
0016 
0017 #ifdef CONFIG_WATCH_QUEUE
0018 
0019 struct cred;
0020 
0021 struct watch_type_filter {
0022     enum watch_notification_type type;
0023     __u32       subtype_filter[1];  /* Bitmask of subtypes to filter on */
0024     __u32       info_filter;        /* Filter on watch_notification::info */
0025     __u32       info_mask;      /* Mask of relevant bits in info_filter */
0026 };
0027 
0028 struct watch_filter {
0029     union {
0030         struct rcu_head rcu;
0031         /* Bitmask of accepted types */
0032         DECLARE_BITMAP(type_filter, WATCH_TYPE__NR);
0033     };
0034     u32         nr_filters; /* Number of filters */
0035     struct watch_type_filter filters[];
0036 };
0037 
0038 struct watch_queue {
0039     struct rcu_head     rcu;
0040     struct watch_filter __rcu *filter;
0041     struct pipe_inode_info  *pipe;      /* The pipe we're using as a buffer */
0042     struct hlist_head   watches;    /* Contributory watches */
0043     struct page     **notes;    /* Preallocated notifications */
0044     unsigned long       *notes_bitmap;  /* Allocation bitmap for notes */
0045     struct kref     usage;      /* Object usage count */
0046     spinlock_t      lock;
0047     unsigned int        nr_notes;   /* Number of notes */
0048     unsigned int        nr_pages;   /* Number of pages in notes[] */
0049     bool            defunct;    /* T when queues closed */
0050 };
0051 
0052 /*
0053  * Representation of a watch on an object.
0054  */
0055 struct watch {
0056     union {
0057         struct rcu_head rcu;
0058         u32     info_id;    /* ID to be OR'd in to info field */
0059     };
0060     struct watch_queue __rcu *queue;    /* Queue to post events to */
0061     struct hlist_node   queue_node; /* Link in queue->watches */
0062     struct watch_list __rcu *watch_list;
0063     struct hlist_node   list_node;  /* Link in watch_list->watchers */
0064     const struct cred   *cred;      /* Creds of the owner of the watch */
0065     void            *private;   /* Private data for the watched object */
0066     u64         id;     /* Internal identifier */
0067     struct kref     usage;      /* Object usage count */
0068 };
0069 
0070 /*
0071  * List of watches on an object.
0072  */
0073 struct watch_list {
0074     struct rcu_head     rcu;
0075     struct hlist_head   watchers;
0076     void (*release_watch)(struct watch *);
0077     spinlock_t      lock;
0078 };
0079 
0080 extern void __post_watch_notification(struct watch_list *,
0081                       struct watch_notification *,
0082                       const struct cred *,
0083                       u64);
0084 extern struct watch_queue *get_watch_queue(int);
0085 extern void put_watch_queue(struct watch_queue *);
0086 extern void init_watch(struct watch *, struct watch_queue *);
0087 extern int add_watch_to_object(struct watch *, struct watch_list *);
0088 extern int remove_watch_from_object(struct watch_list *, struct watch_queue *, u64, bool);
0089 extern long watch_queue_set_size(struct pipe_inode_info *, unsigned int);
0090 extern long watch_queue_set_filter(struct pipe_inode_info *,
0091                    struct watch_notification_filter __user *);
0092 extern int watch_queue_init(struct pipe_inode_info *);
0093 extern void watch_queue_clear(struct watch_queue *);
0094 
0095 static inline void init_watch_list(struct watch_list *wlist,
0096                    void (*release_watch)(struct watch *))
0097 {
0098     INIT_HLIST_HEAD(&wlist->watchers);
0099     spin_lock_init(&wlist->lock);
0100     wlist->release_watch = release_watch;
0101 }
0102 
0103 static inline void post_watch_notification(struct watch_list *wlist,
0104                        struct watch_notification *n,
0105                        const struct cred *cred,
0106                        u64 id)
0107 {
0108     if (unlikely(wlist))
0109         __post_watch_notification(wlist, n, cred, id);
0110 }
0111 
0112 static inline void remove_watch_list(struct watch_list *wlist, u64 id)
0113 {
0114     if (wlist) {
0115         remove_watch_from_object(wlist, NULL, id, true);
0116         kfree_rcu(wlist, rcu);
0117     }
0118 }
0119 
0120 /**
0121  * watch_sizeof - Calculate the information part of the size of a watch record,
0122  * given the structure size.
0123  */
0124 #define watch_sizeof(STRUCT) (sizeof(STRUCT) << WATCH_INFO_LENGTH__SHIFT)
0125 
0126 #else
0127 static inline int watch_queue_init(struct pipe_inode_info *pipe)
0128 {
0129     return -ENOPKG;
0130 }
0131 
0132 #endif
0133 
0134 #endif /* _LINUX_WATCH_QUEUE_H */