Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * include/linux/backing-dev.h
0004  *
0005  * low-level device information and state which is propagated up through
0006  * to high-level code.
0007  */
0008 
0009 #ifndef _LINUX_BACKING_DEV_H
0010 #define _LINUX_BACKING_DEV_H
0011 
0012 #include <linux/kernel.h>
0013 #include <linux/fs.h>
0014 #include <linux/sched.h>
0015 #include <linux/device.h>
0016 #include <linux/writeback.h>
0017 #include <linux/backing-dev-defs.h>
0018 #include <linux/slab.h>
0019 
0020 static inline struct backing_dev_info *bdi_get(struct backing_dev_info *bdi)
0021 {
0022     kref_get(&bdi->refcnt);
0023     return bdi;
0024 }
0025 
0026 struct backing_dev_info *bdi_get_by_id(u64 id);
0027 void bdi_put(struct backing_dev_info *bdi);
0028 
0029 __printf(2, 3)
0030 int bdi_register(struct backing_dev_info *bdi, const char *fmt, ...);
0031 __printf(2, 0)
0032 int bdi_register_va(struct backing_dev_info *bdi, const char *fmt,
0033             va_list args);
0034 void bdi_set_owner(struct backing_dev_info *bdi, struct device *owner);
0035 void bdi_unregister(struct backing_dev_info *bdi);
0036 
0037 struct backing_dev_info *bdi_alloc(int node_id);
0038 
0039 void wb_start_background_writeback(struct bdi_writeback *wb);
0040 void wb_workfn(struct work_struct *work);
0041 void wb_wakeup_delayed(struct bdi_writeback *wb);
0042 
0043 void wb_wait_for_completion(struct wb_completion *done);
0044 
0045 extern spinlock_t bdi_lock;
0046 extern struct list_head bdi_list;
0047 
0048 extern struct workqueue_struct *bdi_wq;
0049 extern struct workqueue_struct *bdi_async_bio_wq;
0050 
0051 static inline bool wb_has_dirty_io(struct bdi_writeback *wb)
0052 {
0053     return test_bit(WB_has_dirty_io, &wb->state);
0054 }
0055 
0056 static inline bool bdi_has_dirty_io(struct backing_dev_info *bdi)
0057 {
0058     /*
0059      * @bdi->tot_write_bandwidth is guaranteed to be > 0 if there are
0060      * any dirty wbs.  See wb_update_write_bandwidth().
0061      */
0062     return atomic_long_read(&bdi->tot_write_bandwidth);
0063 }
0064 
0065 static inline void wb_stat_mod(struct bdi_writeback *wb,
0066                  enum wb_stat_item item, s64 amount)
0067 {
0068     percpu_counter_add_batch(&wb->stat[item], amount, WB_STAT_BATCH);
0069 }
0070 
0071 static inline void inc_wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
0072 {
0073     wb_stat_mod(wb, item, 1);
0074 }
0075 
0076 static inline void dec_wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
0077 {
0078     wb_stat_mod(wb, item, -1);
0079 }
0080 
0081 static inline s64 wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
0082 {
0083     return percpu_counter_read_positive(&wb->stat[item]);
0084 }
0085 
0086 static inline s64 wb_stat_sum(struct bdi_writeback *wb, enum wb_stat_item item)
0087 {
0088     return percpu_counter_sum_positive(&wb->stat[item]);
0089 }
0090 
0091 extern void wb_writeout_inc(struct bdi_writeback *wb);
0092 
0093 /*
0094  * maximal error of a stat counter.
0095  */
0096 static inline unsigned long wb_stat_error(void)
0097 {
0098 #ifdef CONFIG_SMP
0099     return nr_cpu_ids * WB_STAT_BATCH;
0100 #else
0101     return 1;
0102 #endif
0103 }
0104 
0105 int bdi_set_min_ratio(struct backing_dev_info *bdi, unsigned int min_ratio);
0106 int bdi_set_max_ratio(struct backing_dev_info *bdi, unsigned int max_ratio);
0107 
0108 /*
0109  * Flags in backing_dev_info::capability
0110  *
0111  * BDI_CAP_WRITEBACK:       Supports dirty page writeback, and dirty pages
0112  *              should contribute to accounting
0113  * BDI_CAP_WRITEBACK_ACCT:  Automatically account writeback pages
0114  * BDI_CAP_STRICTLIMIT:     Keep number of dirty pages below bdi threshold
0115  */
0116 #define BDI_CAP_WRITEBACK       (1 << 0)
0117 #define BDI_CAP_WRITEBACK_ACCT      (1 << 1)
0118 #define BDI_CAP_STRICTLIMIT     (1 << 2)
0119 
0120 extern struct backing_dev_info noop_backing_dev_info;
0121 
0122 int bdi_init(struct backing_dev_info *bdi);
0123 
0124 /**
0125  * writeback_in_progress - determine whether there is writeback in progress
0126  * @wb: bdi_writeback of interest
0127  *
0128  * Determine whether there is writeback waiting to be handled against a
0129  * bdi_writeback.
0130  */
0131 static inline bool writeback_in_progress(struct bdi_writeback *wb)
0132 {
0133     return test_bit(WB_writeback_running, &wb->state);
0134 }
0135 
0136 struct backing_dev_info *inode_to_bdi(struct inode *inode);
0137 
0138 static inline bool mapping_can_writeback(struct address_space *mapping)
0139 {
0140     return inode_to_bdi(mapping->host)->capabilities & BDI_CAP_WRITEBACK;
0141 }
0142 
0143 #ifdef CONFIG_CGROUP_WRITEBACK
0144 
0145 struct bdi_writeback *wb_get_lookup(struct backing_dev_info *bdi,
0146                     struct cgroup_subsys_state *memcg_css);
0147 struct bdi_writeback *wb_get_create(struct backing_dev_info *bdi,
0148                     struct cgroup_subsys_state *memcg_css,
0149                     gfp_t gfp);
0150 void wb_memcg_offline(struct mem_cgroup *memcg);
0151 void wb_blkcg_offline(struct cgroup_subsys_state *css);
0152 
0153 /**
0154  * inode_cgwb_enabled - test whether cgroup writeback is enabled on an inode
0155  * @inode: inode of interest
0156  *
0157  * Cgroup writeback requires support from the filesystem.  Also, both memcg and
0158  * iocg have to be on the default hierarchy.  Test whether all conditions are
0159  * met.
0160  *
0161  * Note that the test result may change dynamically on the same inode
0162  * depending on how memcg and iocg are configured.
0163  */
0164 static inline bool inode_cgwb_enabled(struct inode *inode)
0165 {
0166     struct backing_dev_info *bdi = inode_to_bdi(inode);
0167 
0168     return cgroup_subsys_on_dfl(memory_cgrp_subsys) &&
0169         cgroup_subsys_on_dfl(io_cgrp_subsys) &&
0170         (bdi->capabilities & BDI_CAP_WRITEBACK) &&
0171         (inode->i_sb->s_iflags & SB_I_CGROUPWB);
0172 }
0173 
0174 /**
0175  * wb_find_current - find wb for %current on a bdi
0176  * @bdi: bdi of interest
0177  *
0178  * Find the wb of @bdi which matches both the memcg and blkcg of %current.
0179  * Must be called under rcu_read_lock() which protects the returend wb.
0180  * NULL if not found.
0181  */
0182 static inline struct bdi_writeback *wb_find_current(struct backing_dev_info *bdi)
0183 {
0184     struct cgroup_subsys_state *memcg_css;
0185     struct bdi_writeback *wb;
0186 
0187     memcg_css = task_css(current, memory_cgrp_id);
0188     if (!memcg_css->parent)
0189         return &bdi->wb;
0190 
0191     wb = radix_tree_lookup(&bdi->cgwb_tree, memcg_css->id);
0192 
0193     /*
0194      * %current's blkcg equals the effective blkcg of its memcg.  No
0195      * need to use the relatively expensive cgroup_get_e_css().
0196      */
0197     if (likely(wb && wb->blkcg_css == task_css(current, io_cgrp_id)))
0198         return wb;
0199     return NULL;
0200 }
0201 
0202 /**
0203  * wb_get_create_current - get or create wb for %current on a bdi
0204  * @bdi: bdi of interest
0205  * @gfp: allocation mask
0206  *
0207  * Equivalent to wb_get_create() on %current's memcg.  This function is
0208  * called from a relatively hot path and optimizes the common cases using
0209  * wb_find_current().
0210  */
0211 static inline struct bdi_writeback *
0212 wb_get_create_current(struct backing_dev_info *bdi, gfp_t gfp)
0213 {
0214     struct bdi_writeback *wb;
0215 
0216     rcu_read_lock();
0217     wb = wb_find_current(bdi);
0218     if (wb && unlikely(!wb_tryget(wb)))
0219         wb = NULL;
0220     rcu_read_unlock();
0221 
0222     if (unlikely(!wb)) {
0223         struct cgroup_subsys_state *memcg_css;
0224 
0225         memcg_css = task_get_css(current, memory_cgrp_id);
0226         wb = wb_get_create(bdi, memcg_css, gfp);
0227         css_put(memcg_css);
0228     }
0229     return wb;
0230 }
0231 
0232 /**
0233  * inode_to_wb - determine the wb of an inode
0234  * @inode: inode of interest
0235  *
0236  * Returns the wb @inode is currently associated with.  The caller must be
0237  * holding either @inode->i_lock, the i_pages lock, or the
0238  * associated wb's list_lock.
0239  */
0240 static inline struct bdi_writeback *inode_to_wb(const struct inode *inode)
0241 {
0242 #ifdef CONFIG_LOCKDEP
0243     WARN_ON_ONCE(debug_locks &&
0244              (!lockdep_is_held(&inode->i_lock) &&
0245               !lockdep_is_held(&inode->i_mapping->i_pages.xa_lock) &&
0246               !lockdep_is_held(&inode->i_wb->list_lock)));
0247 #endif
0248     return inode->i_wb;
0249 }
0250 
0251 static inline struct bdi_writeback *inode_to_wb_wbc(
0252                 struct inode *inode,
0253                 struct writeback_control *wbc)
0254 {
0255     /*
0256      * If wbc does not have inode attached, it means cgroup writeback was
0257      * disabled when wbc started. Just use the default wb in that case.
0258      */
0259     return wbc->wb ? wbc->wb : &inode_to_bdi(inode)->wb;
0260 }
0261 
0262 /**
0263  * unlocked_inode_to_wb_begin - begin unlocked inode wb access transaction
0264  * @inode: target inode
0265  * @cookie: output param, to be passed to the end function
0266  *
0267  * The caller wants to access the wb associated with @inode but isn't
0268  * holding inode->i_lock, the i_pages lock or wb->list_lock.  This
0269  * function determines the wb associated with @inode and ensures that the
0270  * association doesn't change until the transaction is finished with
0271  * unlocked_inode_to_wb_end().
0272  *
0273  * The caller must call unlocked_inode_to_wb_end() with *@cookie afterwards and
0274  * can't sleep during the transaction.  IRQs may or may not be disabled on
0275  * return.
0276  */
0277 static inline struct bdi_writeback *
0278 unlocked_inode_to_wb_begin(struct inode *inode, struct wb_lock_cookie *cookie)
0279 {
0280     rcu_read_lock();
0281 
0282     /*
0283      * Paired with store_release in inode_switch_wbs_work_fn() and
0284      * ensures that we see the new wb if we see cleared I_WB_SWITCH.
0285      */
0286     cookie->locked = smp_load_acquire(&inode->i_state) & I_WB_SWITCH;
0287 
0288     if (unlikely(cookie->locked))
0289         xa_lock_irqsave(&inode->i_mapping->i_pages, cookie->flags);
0290 
0291     /*
0292      * Protected by either !I_WB_SWITCH + rcu_read_lock() or the i_pages
0293      * lock.  inode_to_wb() will bark.  Deref directly.
0294      */
0295     return inode->i_wb;
0296 }
0297 
0298 /**
0299  * unlocked_inode_to_wb_end - end inode wb access transaction
0300  * @inode: target inode
0301  * @cookie: @cookie from unlocked_inode_to_wb_begin()
0302  */
0303 static inline void unlocked_inode_to_wb_end(struct inode *inode,
0304                         struct wb_lock_cookie *cookie)
0305 {
0306     if (unlikely(cookie->locked))
0307         xa_unlock_irqrestore(&inode->i_mapping->i_pages, cookie->flags);
0308 
0309     rcu_read_unlock();
0310 }
0311 
0312 #else   /* CONFIG_CGROUP_WRITEBACK */
0313 
0314 static inline bool inode_cgwb_enabled(struct inode *inode)
0315 {
0316     return false;
0317 }
0318 
0319 static inline struct bdi_writeback *wb_find_current(struct backing_dev_info *bdi)
0320 {
0321     return &bdi->wb;
0322 }
0323 
0324 static inline struct bdi_writeback *
0325 wb_get_create_current(struct backing_dev_info *bdi, gfp_t gfp)
0326 {
0327     return &bdi->wb;
0328 }
0329 
0330 static inline struct bdi_writeback *inode_to_wb(struct inode *inode)
0331 {
0332     return &inode_to_bdi(inode)->wb;
0333 }
0334 
0335 static inline struct bdi_writeback *inode_to_wb_wbc(
0336                 struct inode *inode,
0337                 struct writeback_control *wbc)
0338 {
0339     return inode_to_wb(inode);
0340 }
0341 
0342 
0343 static inline struct bdi_writeback *
0344 unlocked_inode_to_wb_begin(struct inode *inode, struct wb_lock_cookie *cookie)
0345 {
0346     return inode_to_wb(inode);
0347 }
0348 
0349 static inline void unlocked_inode_to_wb_end(struct inode *inode,
0350                         struct wb_lock_cookie *cookie)
0351 {
0352 }
0353 
0354 static inline void wb_memcg_offline(struct mem_cgroup *memcg)
0355 {
0356 }
0357 
0358 static inline void wb_blkcg_offline(struct cgroup_subsys_state *css)
0359 {
0360 }
0361 
0362 #endif  /* CONFIG_CGROUP_WRITEBACK */
0363 
0364 const char *bdi_dev_name(struct backing_dev_info *bdi);
0365 
0366 #endif  /* _LINUX_BACKING_DEV_H */