1 /* 2 * Copyright (C) 2021 Red Hat, Inc. All rights reserved. 3 * 4 * This file is released under the GPL. 5 */ 6 7 #ifndef DM_IO_TRACKER_H 8 #define DM_IO_TRACKER_H 9 10 #include <linux/jiffies.h> 11 12 struct dm_io_tracker { 13 spinlock_t lock; 14 15 /* 16 * Sectors of in-flight IO. 17 */ 18 sector_t in_flight; 19 20 /* 21 * The time, in jiffies, when this device became idle 22 * (if it is indeed idle). 23 */ 24 unsigned long idle_time; 25 unsigned long last_update_time; 26 }; 27 28 static inline void dm_iot_init(struct dm_io_tracker *iot) 29 { 30 spin_lock_init(&iot->lock); 31 iot->in_flight = 0ul; 32 iot->idle_time = 0ul; 33 iot->last_update_time = jiffies; 34 } 35 36 static inline bool dm_iot_idle_for(struct dm_io_tracker *iot, unsigned long j) 37 { 38 bool r = false; 39 40 spin_lock_irq(&iot->lock); 41 if (!iot->in_flight) 42 r = time_after(jiffies, iot->idle_time + j); 43 spin_unlock_irq(&iot->lock); 44 45 return r; 46 } 47 48 static inline void dm_iot_io_begin(struct dm_io_tracker *iot, sector_t len) 49 { 50 spin_lock_irq(&iot->lock); 51 iot->in_flight += len; 52 spin_unlock_irq(&iot->lock); 53 } 54 55 static inline void dm_iot_io_end(struct dm_io_tracker *iot, sector_t len) 56 { 57 unsigned long flags; 58 59 if (!len) 60 return; 61 62 spin_lock_irqsave(&iot->lock, flags); 63 iot->in_flight -= len; 64 if (!iot->in_flight) 65 iot->idle_time = jiffies; 66 spin_unlock_irqrestore(&iot->lock, flags); 67 } 68 69 #endif 70