Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __LINUX_BACKING_DEV_DEFS_H
0003 #define __LINUX_BACKING_DEV_DEFS_H
0004 
0005 #include <linux/list.h>
0006 #include <linux/radix-tree.h>
0007 #include <linux/rbtree.h>
0008 #include <linux/spinlock.h>
0009 #include <linux/percpu_counter.h>
0010 #include <linux/percpu-refcount.h>
0011 #include <linux/flex_proportions.h>
0012 #include <linux/timer.h>
0013 #include <linux/workqueue.h>
0014 #include <linux/kref.h>
0015 #include <linux/refcount.h>
0016 
0017 struct page;
0018 struct device;
0019 struct dentry;
0020 
0021 /*
0022  * Bits in bdi_writeback.state
0023  */
0024 enum wb_state {
0025     WB_registered,      /* bdi_register() was done */
0026     WB_writeback_running,   /* Writeback is in progress */
0027     WB_has_dirty_io,    /* Dirty inodes on ->b_{dirty|io|more_io} */
0028     WB_start_all,       /* nr_pages == 0 (all) work pending */
0029 };
0030 
0031 enum wb_stat_item {
0032     WB_RECLAIMABLE,
0033     WB_WRITEBACK,
0034     WB_DIRTIED,
0035     WB_WRITTEN,
0036     NR_WB_STAT_ITEMS
0037 };
0038 
0039 #define WB_STAT_BATCH (8*(1+ilog2(nr_cpu_ids)))
0040 
0041 /*
0042  * why some writeback work was initiated
0043  */
0044 enum wb_reason {
0045     WB_REASON_BACKGROUND,
0046     WB_REASON_VMSCAN,
0047     WB_REASON_SYNC,
0048     WB_REASON_PERIODIC,
0049     WB_REASON_LAPTOP_TIMER,
0050     WB_REASON_FS_FREE_SPACE,
0051     /*
0052      * There is no bdi forker thread any more and works are done
0053      * by emergency worker, however, this is TPs userland visible
0054      * and we'll be exposing exactly the same information,
0055      * so it has a mismatch name.
0056      */
0057     WB_REASON_FORKER_THREAD,
0058     WB_REASON_FOREIGN_FLUSH,
0059 
0060     WB_REASON_MAX,
0061 };
0062 
0063 struct wb_completion {
0064     atomic_t        cnt;
0065     wait_queue_head_t   *waitq;
0066 };
0067 
0068 #define __WB_COMPLETION_INIT(_waitq)    \
0069     (struct wb_completion){ .cnt = ATOMIC_INIT(1), .waitq = (_waitq) }
0070 
0071 /*
0072  * If one wants to wait for one or more wb_writeback_works, each work's
0073  * ->done should be set to a wb_completion defined using the following
0074  * macro.  Once all work items are issued with wb_queue_work(), the caller
0075  * can wait for the completion of all using wb_wait_for_completion().  Work
0076  * items which are waited upon aren't freed automatically on completion.
0077  */
0078 #define WB_COMPLETION_INIT(bdi)     __WB_COMPLETION_INIT(&(bdi)->wb_waitq)
0079 
0080 #define DEFINE_WB_COMPLETION(cmpl, bdi) \
0081     struct wb_completion cmpl = WB_COMPLETION_INIT(bdi)
0082 
0083 /*
0084  * Each wb (bdi_writeback) can perform writeback operations, is measured
0085  * and throttled, independently.  Without cgroup writeback, each bdi
0086  * (bdi_writeback) is served by its embedded bdi->wb.
0087  *
0088  * On the default hierarchy, blkcg implicitly enables memcg.  This allows
0089  * using memcg's page ownership for attributing writeback IOs, and every
0090  * memcg - blkcg combination can be served by its own wb by assigning a
0091  * dedicated wb to each memcg, which enables isolation across different
0092  * cgroups and propagation of IO back pressure down from the IO layer upto
0093  * the tasks which are generating the dirty pages to be written back.
0094  *
0095  * A cgroup wb is indexed on its bdi by the ID of the associated memcg,
0096  * refcounted with the number of inodes attached to it, and pins the memcg
0097  * and the corresponding blkcg.  As the corresponding blkcg for a memcg may
0098  * change as blkcg is disabled and enabled higher up in the hierarchy, a wb
0099  * is tested for blkcg after lookup and removed from index on mismatch so
0100  * that a new wb for the combination can be created.
0101  *
0102  * Each bdi_writeback that is not embedded into the backing_dev_info must hold
0103  * a reference to the parent backing_dev_info.  See cgwb_create() for details.
0104  */
0105 struct bdi_writeback {
0106     struct backing_dev_info *bdi;   /* our parent bdi */
0107 
0108     unsigned long state;        /* Always use atomic bitops on this */
0109     unsigned long last_old_flush;   /* last old data flush */
0110 
0111     struct list_head b_dirty;   /* dirty inodes */
0112     struct list_head b_io;      /* parked for writeback */
0113     struct list_head b_more_io; /* parked for more writeback */
0114     struct list_head b_dirty_time;  /* time stamps are dirty */
0115     spinlock_t list_lock;       /* protects the b_* lists */
0116 
0117     atomic_t writeback_inodes;  /* number of inodes under writeback */
0118     struct percpu_counter stat[NR_WB_STAT_ITEMS];
0119 
0120     unsigned long bw_time_stamp;    /* last time write bw is updated */
0121     unsigned long dirtied_stamp;
0122     unsigned long written_stamp;    /* pages written at bw_time_stamp */
0123     unsigned long write_bandwidth;  /* the estimated write bandwidth */
0124     unsigned long avg_write_bandwidth; /* further smoothed write bw, > 0 */
0125 
0126     /*
0127      * The base dirty throttle rate, re-calculated on every 200ms.
0128      * All the bdi tasks' dirty rate will be curbed under it.
0129      * @dirty_ratelimit tracks the estimated @balanced_dirty_ratelimit
0130      * in small steps and is much more smooth/stable than the latter.
0131      */
0132     unsigned long dirty_ratelimit;
0133     unsigned long balanced_dirty_ratelimit;
0134 
0135     struct fprop_local_percpu completions;
0136     int dirty_exceeded;
0137     enum wb_reason start_all_reason;
0138 
0139     spinlock_t work_lock;       /* protects work_list & dwork scheduling */
0140     struct list_head work_list;
0141     struct delayed_work dwork;  /* work item used for writeback */
0142     struct delayed_work bw_dwork;   /* work item used for bandwidth estimate */
0143 
0144     unsigned long dirty_sleep;  /* last wait */
0145 
0146     struct list_head bdi_node;  /* anchored at bdi->wb_list */
0147 
0148 #ifdef CONFIG_CGROUP_WRITEBACK
0149     struct percpu_ref refcnt;   /* used only for !root wb's */
0150     struct fprop_local_percpu memcg_completions;
0151     struct cgroup_subsys_state *memcg_css; /* the associated memcg */
0152     struct cgroup_subsys_state *blkcg_css; /* and blkcg */
0153     struct list_head memcg_node;    /* anchored at memcg->cgwb_list */
0154     struct list_head blkcg_node;    /* anchored at blkcg->cgwb_list */
0155     struct list_head b_attached;    /* attached inodes, protected by list_lock */
0156     struct list_head offline_node;  /* anchored at offline_cgwbs */
0157 
0158     union {
0159         struct work_struct release_work;
0160         struct rcu_head rcu;
0161     };
0162 #endif
0163 };
0164 
0165 struct backing_dev_info {
0166     u64 id;
0167     struct rb_node rb_node; /* keyed by ->id */
0168     struct list_head bdi_list;
0169     unsigned long ra_pages; /* max readahead in PAGE_SIZE units */
0170     unsigned long io_pages; /* max allowed IO size */
0171 
0172     struct kref refcnt; /* Reference counter for the structure */
0173     unsigned int capabilities; /* Device capabilities */
0174     unsigned int min_ratio;
0175     unsigned int max_ratio, max_prop_frac;
0176 
0177     /*
0178      * Sum of avg_write_bw of wbs with dirty inodes.  > 0 if there are
0179      * any dirty wbs, which is depended upon by bdi_has_dirty().
0180      */
0181     atomic_long_t tot_write_bandwidth;
0182 
0183     struct bdi_writeback wb;  /* the root writeback info for this bdi */
0184     struct list_head wb_list; /* list of all wbs */
0185 #ifdef CONFIG_CGROUP_WRITEBACK
0186     struct radix_tree_root cgwb_tree; /* radix tree of active cgroup wbs */
0187     struct mutex cgwb_release_mutex;  /* protect shutdown of wb structs */
0188     struct rw_semaphore wb_switch_rwsem; /* no cgwb switch while syncing */
0189 #endif
0190     wait_queue_head_t wb_waitq;
0191 
0192     struct device *dev;
0193     char dev_name[64];
0194     struct device *owner;
0195 
0196     struct timer_list laptop_mode_wb_timer;
0197 
0198 #ifdef CONFIG_DEBUG_FS
0199     struct dentry *debug_dir;
0200 #endif
0201 };
0202 
0203 struct wb_lock_cookie {
0204     bool locked;
0205     unsigned long flags;
0206 };
0207 
0208 #ifdef CONFIG_CGROUP_WRITEBACK
0209 
0210 /**
0211  * wb_tryget - try to increment a wb's refcount
0212  * @wb: bdi_writeback to get
0213  */
0214 static inline bool wb_tryget(struct bdi_writeback *wb)
0215 {
0216     if (wb != &wb->bdi->wb)
0217         return percpu_ref_tryget(&wb->refcnt);
0218     return true;
0219 }
0220 
0221 /**
0222  * wb_get - increment a wb's refcount
0223  * @wb: bdi_writeback to get
0224  */
0225 static inline void wb_get(struct bdi_writeback *wb)
0226 {
0227     if (wb != &wb->bdi->wb)
0228         percpu_ref_get(&wb->refcnt);
0229 }
0230 
0231 /**
0232  * wb_put - decrement a wb's refcount
0233  * @wb: bdi_writeback to put
0234  * @nr: number of references to put
0235  */
0236 static inline void wb_put_many(struct bdi_writeback *wb, unsigned long nr)
0237 {
0238     if (WARN_ON_ONCE(!wb->bdi)) {
0239         /*
0240          * A driver bug might cause a file to be removed before bdi was
0241          * initialized.
0242          */
0243         return;
0244     }
0245 
0246     if (wb != &wb->bdi->wb)
0247         percpu_ref_put_many(&wb->refcnt, nr);
0248 }
0249 
0250 /**
0251  * wb_put - decrement a wb's refcount
0252  * @wb: bdi_writeback to put
0253  */
0254 static inline void wb_put(struct bdi_writeback *wb)
0255 {
0256     wb_put_many(wb, 1);
0257 }
0258 
0259 /**
0260  * wb_dying - is a wb dying?
0261  * @wb: bdi_writeback of interest
0262  *
0263  * Returns whether @wb is unlinked and being drained.
0264  */
0265 static inline bool wb_dying(struct bdi_writeback *wb)
0266 {
0267     return percpu_ref_is_dying(&wb->refcnt);
0268 }
0269 
0270 #else   /* CONFIG_CGROUP_WRITEBACK */
0271 
0272 static inline bool wb_tryget(struct bdi_writeback *wb)
0273 {
0274     return true;
0275 }
0276 
0277 static inline void wb_get(struct bdi_writeback *wb)
0278 {
0279 }
0280 
0281 static inline void wb_put(struct bdi_writeback *wb)
0282 {
0283 }
0284 
0285 static inline void wb_put_many(struct bdi_writeback *wb, unsigned long nr)
0286 {
0287 }
0288 
0289 static inline bool wb_dying(struct bdi_writeback *wb)
0290 {
0291     return false;
0292 }
0293 
0294 #endif  /* CONFIG_CGROUP_WRITEBACK */
0295 
0296 #endif  /* __LINUX_BACKING_DEV_DEFS_H */