Locking 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. The text below describes the locking rules for VFS-related methods.
  2. It is (believed to be) up-to-date. *Please*, if you change anything in
  3. prototypes or locking protocols - update this file. And update the relevant
  4. instances in the tree, don't leave that to maintainers of filesystems/devices/
  5. etc. At the very least, put the list of dubious cases in the end of this file.
  6. Don't turn it into log - maintainers of out-of-the-tree code are supposed to
  7. be able to use diff(1).
  8. Thing currently missing here: socket operations. Alexey?
  9. --------------------------- dentry_operations --------------------------
  10. prototypes:
  11. int (*d_revalidate)(struct dentry *, int);
  12. int (*d_hash) (struct dentry *, struct qstr *);
  13. int (*d_compare) (struct dentry *, struct qstr *, struct qstr *);
  14. int (*d_delete)(struct dentry *);
  15. void (*d_release)(struct dentry *);
  16. void (*d_iput)(struct dentry *, struct inode *);
  17. char *(*d_dname)((struct dentry *dentry, char *buffer, int buflen);
  18. locking rules:
  19. none have BKL
  20. dcache_lock rename_lock ->d_lock may block
  21. d_revalidate: no no no yes
  22. d_hash no no no yes
  23. d_compare: no yes no no
  24. d_delete: yes no yes no
  25. d_release: no no no yes
  26. d_iput: no no no yes
  27. d_dname: no no no no
  28. --------------------------- inode_operations ---------------------------
  29. prototypes:
  30. int (*create) (struct inode *,struct dentry *,int, struct nameidata *);
  31. struct dentry * (*lookup) (struct inode *,struct dentry *, struct nameid
  32. ata *);
  33. int (*link) (struct dentry *,struct inode *,struct dentry *);
  34. int (*unlink) (struct inode *,struct dentry *);
  35. int (*symlink) (struct inode *,struct dentry *,const char *);
  36. int (*mkdir) (struct inode *,struct dentry *,int);
  37. int (*rmdir) (struct inode *,struct dentry *);
  38. int (*mknod) (struct inode *,struct dentry *,int,dev_t);
  39. int (*rename) (struct inode *, struct dentry *,
  40. struct inode *, struct dentry *);
  41. int (*readlink) (struct dentry *, char __user *,int);
  42. int (*follow_link) (struct dentry *, struct nameidata *);
  43. void (*truncate) (struct inode *);
  44. int (*permission) (struct inode *, int, struct nameidata *);
  45. int (*setattr) (struct dentry *, struct iattr *);
  46. int (*getattr) (struct vfsmount *, struct dentry *, struct kstat *);
  47. int (*setxattr) (struct dentry *, const char *,const void *,size_t,int);
  48. ssize_t (*getxattr) (struct dentry *, const char *, void *, size_t);
  49. ssize_t (*listxattr) (struct dentry *, char *, size_t);
  50. int (*removexattr) (struct dentry *, const char *);
  51. locking rules:
  52. all may block, none have BKL
  53. i_mutex(inode)
  54. lookup: yes
  55. create: yes
  56. link: yes (both)
  57. mknod: yes
  58. symlink: yes
  59. mkdir: yes
  60. unlink: yes (both)
  61. rmdir: yes (both) (see below)
  62. rename: yes (all) (see below)
  63. readlink: no
  64. follow_link: no
  65. truncate: yes (see below)
  66. setattr: yes
  67. permission: no
  68. getattr: no
  69. setxattr: yes
  70. getxattr: no
  71. listxattr: no
  72. removexattr: yes
  73. Additionally, ->rmdir(), ->unlink() and ->rename() have ->i_mutex on
  74. victim.
  75. cross-directory ->rename() has (per-superblock) ->s_vfs_rename_sem.
  76. ->truncate() is never called directly - it's a callback, not a
  77. method. It's called by vmtruncate() - library function normally used by
  78. ->setattr(). Locking information above applies to that call (i.e. is
  79. inherited from ->setattr() - vmtruncate() is used when ATTR_SIZE had been
  80. passed).
  81. See Documentation/filesystems/directory-locking for more detailed discussion
  82. of the locking scheme for directory operations.
  83. --------------------------- super_operations ---------------------------
  84. prototypes:
  85. struct inode *(*alloc_inode)(struct super_block *sb);
  86. void (*destroy_inode)(struct inode *);
  87. void (*dirty_inode) (struct inode *);
  88. int (*write_inode) (struct inode *, int);
  89. int (*drop_inode) (struct inode *);
  90. void (*evict_inode) (struct inode *);
  91. void (*put_super) (struct super_block *);
  92. void (*write_super) (struct super_block *);
  93. int (*sync_fs)(struct super_block *sb, int wait);
  94. int (*freeze_fs) (struct super_block *);
  95. int (*unfreeze_fs) (struct super_block *);
  96. int (*statfs) (struct dentry *, struct kstatfs *);
  97. int (*remount_fs) (struct super_block *, int *, char *);
  98. void (*umount_begin) (struct super_block *);
  99. int (*show_options)(struct seq_file *, struct vfsmount *);
  100. ssize_t (*quota_read)(struct super_block *, int, char *, size_t, loff_t);
  101. ssize_t (*quota_write)(struct super_block *, int, const char *, size_t, loff_t);
  102. locking rules:
  103. All may block [not true, see below]
  104. None have BKL
  105. s_umount
  106. alloc_inode:
  107. destroy_inode:
  108. dirty_inode: (must not sleep)
  109. write_inode:
  110. drop_inode: !!!inode_lock!!!
  111. evict_inode:
  112. put_super: write
  113. write_super: read
  114. sync_fs: read
  115. freeze_fs: read
  116. unfreeze_fs: read
  117. statfs: maybe(read) (see below)
  118. remount_fs: write
  119. umount_begin: no
  120. show_options: no (namespace_sem)
  121. quota_read: no (see below)
  122. quota_write: no (see below)
  123. ->statfs() has s_umount (shared) when called by ustat(2) (native or
  124. compat), but that's an accident of bad API; s_umount is used to pin
  125. the superblock down when we only have dev_t given us by userland to
  126. identify the superblock. Everything else (statfs(), fstatfs(), etc.)
  127. doesn't hold it when calling ->statfs() - superblock is pinned down
  128. by resolving the pathname passed to syscall.
  129. ->quota_read() and ->quota_write() functions are both guaranteed to
  130. be the only ones operating on the quota file by the quota code (via
  131. dqio_sem) (unless an admin really wants to screw up something and
  132. writes to quota files with quotas on). For other details about locking
  133. see also dquot_operations section.
  134. --------------------------- file_system_type ---------------------------
  135. prototypes:
  136. int (*get_sb) (struct file_system_type *, int,
  137. const char *, void *, struct vfsmount *);
  138. void (*kill_sb) (struct super_block *);
  139. locking rules:
  140. may block BKL
  141. get_sb yes no
  142. kill_sb yes no
  143. ->get_sb() returns error or 0 with locked superblock attached to the vfsmount
  144. (exclusive on ->s_umount).
  145. ->kill_sb() takes a write-locked superblock, does all shutdown work on it,
  146. unlocks and drops the reference.
  147. --------------------------- address_space_operations --------------------------
  148. prototypes:
  149. int (*writepage)(struct page *page, struct writeback_control *wbc);
  150. int (*readpage)(struct file *, struct page *);
  151. int (*sync_page)(struct page *);
  152. int (*writepages)(struct address_space *, struct writeback_control *);
  153. int (*set_page_dirty)(struct page *page);
  154. int (*readpages)(struct file *filp, struct address_space *mapping,
  155. struct list_head *pages, unsigned nr_pages);
  156. int (*write_begin)(struct file *, struct address_space *mapping,
  157. loff_t pos, unsigned len, unsigned flags,
  158. struct page **pagep, void **fsdata);
  159. int (*write_end)(struct file *, struct address_space *mapping,
  160. loff_t pos, unsigned len, unsigned copied,
  161. struct page *page, void *fsdata);
  162. sector_t (*bmap)(struct address_space *, sector_t);
  163. int (*invalidatepage) (struct page *, unsigned long);
  164. int (*releasepage) (struct page *, int);
  165. int (*direct_IO)(int, struct kiocb *, const struct iovec *iov,
  166. loff_t offset, unsigned long nr_segs);
  167. int (*launder_page) (struct page *);
  168. locking rules:
  169. All except set_page_dirty may block
  170. BKL PageLocked(page) i_mutex
  171. writepage: no yes, unlocks (see below)
  172. readpage: no yes, unlocks
  173. sync_page: no maybe
  174. writepages: no
  175. set_page_dirty no no
  176. readpages: no
  177. write_begin: no locks the page yes
  178. write_end: no yes, unlocks yes
  179. perform_write: no n/a yes
  180. bmap: no
  181. invalidatepage: no yes
  182. releasepage: no yes
  183. direct_IO: no
  184. launder_page: no yes
  185. ->write_begin(), ->write_end(), ->sync_page() and ->readpage()
  186. may be called from the request handler (/dev/loop).
  187. ->readpage() unlocks the page, either synchronously or via I/O
  188. completion.
  189. ->readpages() populates the pagecache with the passed pages and starts
  190. I/O against them. They come unlocked upon I/O completion.
  191. ->writepage() is used for two purposes: for "memory cleansing" and for
  192. "sync". These are quite different operations and the behaviour may differ
  193. depending upon the mode.
  194. If writepage is called for sync (wbc->sync_mode != WBC_SYNC_NONE) then
  195. it *must* start I/O against the page, even if that would involve
  196. blocking on in-progress I/O.
  197. If writepage is called for memory cleansing (sync_mode ==
  198. WBC_SYNC_NONE) then its role is to get as much writeout underway as
  199. possible. So writepage should try to avoid blocking against
  200. currently-in-progress I/O.
  201. If the filesystem is not called for "sync" and it determines that it
  202. would need to block against in-progress I/O to be able to start new I/O
  203. against the page the filesystem should redirty the page with
  204. redirty_page_for_writepage(), then unlock the page and return zero.
  205. This may also be done to avoid internal deadlocks, but rarely.
  206. If the filesystem is called for sync then it must wait on any
  207. in-progress I/O and then start new I/O.
  208. The filesystem should unlock the page synchronously, before returning to the
  209. caller, unless ->writepage() returns special WRITEPAGE_ACTIVATE
  210. value. WRITEPAGE_ACTIVATE means that page cannot really be written out
  211. currently, and VM should stop calling ->writepage() on this page for some
  212. time. VM does this by moving page to the head of the active list, hence the
  213. name.
  214. Unless the filesystem is going to redirty_page_for_writepage(), unlock the page
  215. and return zero, writepage *must* run set_page_writeback() against the page,
  216. followed by unlocking it. Once set_page_writeback() has been run against the
  217. page, write I/O can be submitted and the write I/O completion handler must run
  218. end_page_writeback() once the I/O is complete. If no I/O is submitted, the
  219. filesystem must run end_page_writeback() against the page before returning from
  220. writepage.
  221. That is: after 2.5.12, pages which are under writeout are *not* locked. Note,
  222. if the filesystem needs the page to be locked during writeout, that is ok, too,
  223. the page is allowed to be unlocked at any point in time between the calls to
  224. set_page_writeback() and end_page_writeback().
  225. Note, failure to run either redirty_page_for_writepage() or the combination of
  226. set_page_writeback()/end_page_writeback() on a page submitted to writepage
  227. will leave the page itself marked clean but it will be tagged as dirty in the
  228. radix tree. This incoherency can lead to all sorts of hard-to-debug problems
  229. in the filesystem like having dirty inodes at umount and losing written data.
  230. ->sync_page() locking rules are not well-defined - usually it is called
  231. with lock on page, but that is not guaranteed. Considering the currently
  232. existing instances of this method ->sync_page() itself doesn't look
  233. well-defined...
  234. ->writepages() is used for periodic writeback and for syscall-initiated
  235. sync operations. The address_space should start I/O against at least
  236. *nr_to_write pages. *nr_to_write must be decremented for each page which is
  237. written. The address_space implementation may write more (or less) pages
  238. than *nr_to_write asks for, but it should try to be reasonably close. If
  239. nr_to_write is NULL, all dirty pages must be written.
  240. writepages should _only_ write pages which are present on
  241. mapping->io_pages.
  242. ->set_page_dirty() is called from various places in the kernel
  243. when the target page is marked as needing writeback. It may be called
  244. under spinlock (it cannot block) and is sometimes called with the page
  245. not locked.
  246. ->bmap() is currently used by legacy ioctl() (FIBMAP) provided by some
  247. filesystems and by the swapper. The latter will eventually go away. All
  248. instances do not actually need the BKL. Please, keep it that way and don't
  249. breed new callers.
  250. ->invalidatepage() is called when the filesystem must attempt to drop
  251. some or all of the buffers from the page when it is being truncated. It
  252. returns zero on success. If ->invalidatepage is zero, the kernel uses
  253. block_invalidatepage() instead.
  254. ->releasepage() is called when the kernel is about to try to drop the
  255. buffers from the page in preparation for freeing it. It returns zero to
  256. indicate that the buffers are (or may be) freeable. If ->releasepage is zero,
  257. the kernel assumes that the fs has no private interest in the buffers.
  258. ->launder_page() may be called prior to releasing a page if
  259. it is still found to be dirty. It returns zero if the page was successfully
  260. cleaned, or an error value if not. Note that in order to prevent the page
  261. getting mapped back in and redirtied, it needs to be kept locked
  262. across the entire operation.
  263. Note: currently almost all instances of address_space methods are
  264. using BKL for internal serialization and that's one of the worst sources
  265. of contention. Normally they are calling library functions (in fs/buffer.c)
  266. and pass foo_get_block() as a callback (on local block-based filesystems,
  267. indeed). BKL is not needed for library stuff and is usually taken by
  268. foo_get_block(). It's an overkill, since block bitmaps can be protected by
  269. internal fs locking and real critical areas are much smaller than the areas
  270. filesystems protect now.
  271. ----------------------- file_lock_operations ------------------------------
  272. prototypes:
  273. void (*fl_insert)(struct file_lock *); /* lock insertion callback */
  274. void (*fl_remove)(struct file_lock *); /* lock removal callback */
  275. void (*fl_copy_lock)(struct file_lock *, struct file_lock *);
  276. void (*fl_release_private)(struct file_lock *);
  277. locking rules:
  278. BKL may block
  279. fl_insert: yes no
  280. fl_remove: yes no
  281. fl_copy_lock: yes no
  282. fl_release_private: yes yes
  283. ----------------------- lock_manager_operations ---------------------------
  284. prototypes:
  285. int (*fl_compare_owner)(struct file_lock *, struct file_lock *);
  286. void (*fl_notify)(struct file_lock *); /* unblock callback */
  287. void (*fl_copy_lock)(struct file_lock *, struct file_lock *);
  288. void (*fl_release_private)(struct file_lock *);
  289. void (*fl_break)(struct file_lock *); /* break_lease callback */
  290. locking rules:
  291. BKL may block
  292. fl_compare_owner: yes no
  293. fl_notify: yes no
  294. fl_copy_lock: yes no
  295. fl_release_private: yes yes
  296. fl_break: yes no
  297. Currently only NFSD and NLM provide instances of this class. None of the
  298. them block. If you have out-of-tree instances - please, show up. Locking
  299. in that area will change.
  300. --------------------------- buffer_head -----------------------------------
  301. prototypes:
  302. void (*b_end_io)(struct buffer_head *bh, int uptodate);
  303. locking rules:
  304. called from interrupts. In other words, extreme care is needed here.
  305. bh is locked, but that's all warranties we have here. Currently only RAID1,
  306. highmem, fs/buffer.c, and fs/ntfs/aops.c are providing these. Block devices
  307. call this method upon the IO completion.
  308. --------------------------- block_device_operations -----------------------
  309. prototypes:
  310. int (*open) (struct inode *, struct file *);
  311. int (*release) (struct inode *, struct file *);
  312. int (*ioctl) (struct inode *, struct file *, unsigned, unsigned long);
  313. int (*media_changed) (struct gendisk *);
  314. int (*revalidate_disk) (struct gendisk *);
  315. locking rules:
  316. BKL bd_sem
  317. open: yes yes
  318. release: yes yes
  319. ioctl: yes no
  320. media_changed: no no
  321. revalidate_disk: no no
  322. The last two are called only from check_disk_change().
  323. --------------------------- file_operations -------------------------------
  324. prototypes:
  325. loff_t (*llseek) (struct file *, loff_t, int);
  326. ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
  327. ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
  328. ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
  329. ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
  330. int (*readdir) (struct file *, void *, filldir_t);
  331. unsigned int (*poll) (struct file *, struct poll_table_struct *);
  332. long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
  333. long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
  334. int (*mmap) (struct file *, struct vm_area_struct *);
  335. int (*open) (struct inode *, struct file *);
  336. int (*flush) (struct file *);
  337. int (*release) (struct inode *, struct file *);
  338. int (*fsync) (struct file *, int datasync);
  339. int (*aio_fsync) (struct kiocb *, int datasync);
  340. int (*fasync) (int, struct file *, int);
  341. int (*lock) (struct file *, int, struct file_lock *);
  342. ssize_t (*readv) (struct file *, const struct iovec *, unsigned long,
  343. loff_t *);
  344. ssize_t (*writev) (struct file *, const struct iovec *, unsigned long,
  345. loff_t *);
  346. ssize_t (*sendfile) (struct file *, loff_t *, size_t, read_actor_t,
  347. void __user *);
  348. ssize_t (*sendpage) (struct file *, struct page *, int, size_t,
  349. loff_t *, int);
  350. unsigned long (*get_unmapped_area)(struct file *, unsigned long,
  351. unsigned long, unsigned long, unsigned long);
  352. int (*check_flags)(int);
  353. };
  354. locking rules:
  355. All may block.
  356. BKL
  357. llseek: no (see below)
  358. read: no
  359. aio_read: no
  360. write: no
  361. aio_write: no
  362. readdir: no
  363. poll: no
  364. unlocked_ioctl: no
  365. compat_ioctl: no
  366. mmap: no
  367. open: no
  368. flush: no
  369. release: no
  370. fsync: no (see below)
  371. aio_fsync: no
  372. fasync: no
  373. lock: yes
  374. readv: no
  375. writev: no
  376. sendfile: no
  377. sendpage: no
  378. get_unmapped_area: no
  379. check_flags: no
  380. ->llseek() locking has moved from llseek to the individual llseek
  381. implementations. If your fs is not using generic_file_llseek, you
  382. need to acquire and release the appropriate locks in your ->llseek().
  383. For many filesystems, it is probably safe to acquire the inode
  384. mutex or just to use i_size_read() instead.
  385. Note: this does not protect the file->f_pos against concurrent modifications
  386. since this is something the userspace has to take care about.
  387. Note: ext2_release() was *the* source of contention on fs-intensive
  388. loads and dropping BKL on ->release() helps to get rid of that (we still
  389. grab BKL for cases when we close a file that had been opened r/w, but that
  390. can and should be done using the internal locking with smaller critical areas).
  391. Current worst offender is ext2_get_block()...
  392. ->fasync() is called without BKL protection, and is responsible for
  393. maintaining the FASYNC bit in filp->f_flags. Most instances call
  394. fasync_helper(), which does that maintenance, so it's not normally
  395. something one needs to worry about. Return values > 0 will be mapped to
  396. zero in the VFS layer.
  397. ->readdir() and ->ioctl() on directories must be changed. Ideally we would
  398. move ->readdir() to inode_operations and use a separate method for directory
  399. ->ioctl() or kill the latter completely. One of the problems is that for
  400. anything that resembles union-mount we won't have a struct file for all
  401. components. And there are other reasons why the current interface is a mess...
  402. ->read on directories probably must go away - we should just enforce -EISDIR
  403. in sys_read() and friends.
  404. ->fsync() has i_mutex on inode.
  405. --------------------------- dquot_operations -------------------------------
  406. prototypes:
  407. int (*write_dquot) (struct dquot *);
  408. int (*acquire_dquot) (struct dquot *);
  409. int (*release_dquot) (struct dquot *);
  410. int (*mark_dirty) (struct dquot *);
  411. int (*write_info) (struct super_block *, int);
  412. These operations are intended to be more or less wrapping functions that ensure
  413. a proper locking wrt the filesystem and call the generic quota operations.
  414. What filesystem should expect from the generic quota functions:
  415. FS recursion Held locks when called
  416. write_dquot: yes dqonoff_sem or dqptr_sem
  417. acquire_dquot: yes dqonoff_sem or dqptr_sem
  418. release_dquot: yes dqonoff_sem or dqptr_sem
  419. mark_dirty: no -
  420. write_info: yes dqonoff_sem
  421. FS recursion means calling ->quota_read() and ->quota_write() from superblock
  422. operations.
  423. More details about quota locking can be found in fs/dquot.c.
  424. --------------------------- vm_operations_struct -----------------------------
  425. prototypes:
  426. void (*open)(struct vm_area_struct*);
  427. void (*close)(struct vm_area_struct*);
  428. int (*fault)(struct vm_area_struct*, struct vm_fault *);
  429. int (*page_mkwrite)(struct vm_area_struct *, struct vm_fault *);
  430. int (*access)(struct vm_area_struct *, unsigned long, void*, int, int);
  431. locking rules:
  432. BKL mmap_sem PageLocked(page)
  433. open: no yes
  434. close: no yes
  435. fault: no yes can return with page locked
  436. page_mkwrite: no yes can return with page locked
  437. access: no yes
  438. ->fault() is called when a previously not present pte is about
  439. to be faulted in. The filesystem must find and return the page associated
  440. with the passed in "pgoff" in the vm_fault structure. If it is possible that
  441. the page may be truncated and/or invalidated, then the filesystem must lock
  442. the page, then ensure it is not already truncated (the page lock will block
  443. subsequent truncate), and then return with VM_FAULT_LOCKED, and the page
  444. locked. The VM will unlock the page.
  445. ->page_mkwrite() is called when a previously read-only pte is
  446. about to become writeable. The filesystem again must ensure that there are
  447. no truncate/invalidate races, and then return with the page locked. If
  448. the page has been truncated, the filesystem should not look up a new page
  449. like the ->fault() handler, but simply return with VM_FAULT_NOPAGE, which
  450. will cause the VM to retry the fault.
  451. ->access() is called when get_user_pages() fails in
  452. acces_process_vm(), typically used to debug a process through
  453. /proc/pid/mem or ptrace. This function is needed only for
  454. VM_IO | VM_PFNMAP VMAs.
  455. ================================================================================
  456. Dubious stuff
  457. (if you break something or notice that it is broken and do not fix it yourself
  458. - at least put it here)
  459. ipc/shm.c::shm_delete() - may need BKL.
  460. ->read() and ->write() in many drivers are (probably) missing BKL.