refs.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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(struct io_kiocb *req)
  17. {
  18. if (likely(!(req->flags & REQ_F_REFCOUNT)))
  19. return true;
  20. WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
  21. return atomic_dec_and_test(&req->refs);
  22. }
  23. static inline void req_ref_get(struct io_kiocb *req)
  24. {
  25. WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
  26. WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
  27. atomic_inc(&req->refs);
  28. }
  29. static inline void req_ref_put(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_dec(&req->refs);
  34. }
  35. static inline void __io_req_set_refcount(struct io_kiocb *req, int nr)
  36. {
  37. if (!(req->flags & REQ_F_REFCOUNT)) {
  38. req->flags |= REQ_F_REFCOUNT;
  39. atomic_set(&req->refs, nr);
  40. }
  41. }
  42. static inline void io_req_set_refcount(struct io_kiocb *req)
  43. {
  44. __io_req_set_refcount(req, 1);
  45. }
  46. #endif