files.rst 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ===================================
  3. File management in the Linux kernel
  4. ===================================
  5. This document describes how locking for files (struct file)
  6. and file descriptor table (struct files) works.
  7. Up until 2.6.12, the file descriptor table has been protected
  8. with a lock (files->file_lock) and reference count (files->count).
  9. ->file_lock protected accesses to all the file related fields
  10. of the table. ->count was used for sharing the file descriptor
  11. table between tasks cloned with CLONE_FILES flag. Typically
  12. this would be the case for posix threads. As with the common
  13. refcounting model in the kernel, the last task doing
  14. a put_files_struct() frees the file descriptor (fd) table.
  15. The files (struct file) themselves are protected using
  16. reference count (->f_count).
  17. In the new lock-free model of file descriptor management,
  18. the reference counting is similar, but the locking is
  19. based on RCU. The file descriptor table contains multiple
  20. elements - the fd sets (open_fds and close_on_exec, the
  21. array of file pointers, the sizes of the sets and the array
  22. etc.). In order for the updates to appear atomic to
  23. a lock-free reader, all the elements of the file descriptor
  24. table are in a separate structure - struct fdtable.
  25. files_struct contains a pointer to struct fdtable through
  26. which the actual fd table is accessed. Initially the
  27. fdtable is embedded in files_struct itself. On a subsequent
  28. expansion of fdtable, a new fdtable structure is allocated
  29. and files->fdtab points to the new structure. The fdtable
  30. structure is freed with RCU and lock-free readers either
  31. see the old fdtable or the new fdtable making the update
  32. appear atomic. Here are the locking rules for
  33. the fdtable structure -
  34. 1. All references to the fdtable must be done through
  35. the files_fdtable() macro::
  36. struct fdtable *fdt;
  37. rcu_read_lock();
  38. fdt = files_fdtable(files);
  39. ....
  40. if (n <= fdt->max_fds)
  41. ....
  42. ...
  43. rcu_read_unlock();
  44. files_fdtable() uses rcu_dereference() macro which takes care of
  45. the memory barrier requirements for lock-free dereference.
  46. The fdtable pointer must be read within the read-side
  47. critical section.
  48. 2. Reading of the fdtable as described above must be protected
  49. by rcu_read_lock()/rcu_read_unlock().
  50. 3. For any update to the fd table, files->file_lock must
  51. be held.
  52. 4. To look up the file structure given an fd, a reader
  53. must use either lookup_fdget_rcu() or files_lookup_fdget_rcu() APIs. These
  54. take care of barrier requirements due to lock-free lookup.
  55. An example::
  56. struct file *file;
  57. rcu_read_lock();
  58. file = lookup_fdget_rcu(fd);
  59. rcu_read_unlock();
  60. if (file) {
  61. ...
  62. fput(file);
  63. }
  64. ....
  65. 5. Since both fdtable and file structures can be looked up
  66. lock-free, they must be installed using rcu_assign_pointer()
  67. API. If they are looked up lock-free, rcu_dereference()
  68. must be used. However it is advisable to use files_fdtable()
  69. and lookup_fdget_rcu()/files_lookup_fdget_rcu() which take care of these
  70. issues.
  71. 6. While updating, the fdtable pointer must be looked up while
  72. holding files->file_lock. If ->file_lock is dropped, then
  73. another thread expand the files thereby creating a new
  74. fdtable and making the earlier fdtable pointer stale.
  75. For example::
  76. spin_lock(&files->file_lock);
  77. fd = locate_fd(files, file, start);
  78. if (fd >= 0) {
  79. /* locate_fd() may have expanded fdtable, load the ptr */
  80. fdt = files_fdtable(files);
  81. __set_open_fd(fd, fdt);
  82. __clear_close_on_exec(fd, fdt);
  83. spin_unlock(&files->file_lock);
  84. .....
  85. Since locate_fd() can drop ->file_lock (and reacquire ->file_lock),
  86. the fdtable pointer (fdt) must be loaded after locate_fd().
  87. On newer kernels rcu based file lookup has been switched to rely on
  88. SLAB_TYPESAFE_BY_RCU instead of call_rcu(). It isn't sufficient anymore
  89. to just acquire a reference to the file in question under rcu using
  90. atomic_long_inc_not_zero() since the file might have already been
  91. recycled and someone else might have bumped the reference. In other
  92. words, callers might see reference count bumps from newer users. For
  93. this is reason it is necessary to verify that the pointer is the same
  94. before and after the reference count increment. This pattern can be seen
  95. in get_file_rcu() and __files_get_rcu().
  96. In addition, it isn't possible to access or check fields in struct file
  97. without first acquiring a reference on it under rcu lookup. Not doing
  98. that was always very dodgy and it was only usable for non-pointer data
  99. in struct file. With SLAB_TYPESAFE_BY_RCU it is necessary that callers
  100. either first acquire a reference or they must hold the files_lock of the
  101. fdtable.