refs.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #ifndef IOU_REQ_REF_H
  2. #define IOU_REQ_REF_H
  3. #include <linux/atomic.h>
  4. #include <linux/io_uring_types.h>
  5. /*
  6. * Shamelessly stolen from the mm implementation of page reference checking,
  7. * see commit f958d7b528b1 for details.
  8. */
  9. #define req_ref_zero_or_close_to_overflow(req) \
  10. ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u)
  11. static inline bool req_ref_inc_not_zero(struct io_kiocb *req)
  12. {
  13. WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
  14. return atomic_inc_not_zero(&req->refs);
  15. }
  16. static inline bool req_ref_put_and_test_atomic(struct io_kiocb *req)
  17. {
  18. WARN_ON_ONCE(!(data_race(req->flags) & REQ_F_REFCOUNT));
  19. WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
  20. return atomic_dec_and_test(&req->refs);
  21. }
  22. static inline bool req_ref_put_and_test(struct io_kiocb *req)
  23. {
  24. if (likely(!(req->flags & REQ_F_REFCOUNT)))
  25. return true;
  26. WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
  27. return atomic_dec_and_test(&req->refs);
  28. }
  29. static inline void req_ref_get(struct io_kiocb *req)
  30. {
  31. WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
  32. WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
  33. atomic_inc(&req->refs);
  34. }
  35. static inline void req_ref_put(struct io_kiocb *req)
  36. {
  37. WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
  38. WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
  39. atomic_dec(&req->refs);
  40. }
  41. static inline void __io_req_set_refcount(struct io_kiocb *req, int nr)
  42. {
  43. if (!(req->flags & REQ_F_REFCOUNT)) {
  44. req->flags |= REQ_F_REFCOUNT;
  45. atomic_set(&req->refs, nr);
  46. }
  47. }
  48. static inline void io_req_set_refcount(struct io_kiocb *req)
  49. {
  50. __io_req_set_refcount(req, 1);
  51. }
  52. #endif