Locking 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. locking rules:
  18. none have BKL
  19. dcache_lock rename_lock ->d_lock may block
  20. d_revalidate: no no no yes
  21. d_hash no no no yes
  22. d_compare: no yes no no
  23. d_delete: yes no yes no
  24. d_release: no no no yes
  25. d_iput: no no no yes
  26. --------------------------- inode_operations ---------------------------
  27. prototypes:
  28. int (*create) (struct inode *,struct dentry *,int, struct nameidata *);
  29. struct dentry * (*lookup) (struct inode *,struct dentry *, struct nameid
  30. ata *);
  31. int (*link) (struct dentry *,struct inode *,struct dentry *);
  32. int (*unlink) (struct inode *,struct dentry *);
  33. int (*symlink) (struct inode *,struct dentry *,const char *);
  34. int (*mkdir) (struct inode *,struct dentry *,int);
  35. int (*rmdir) (struct inode *,struct dentry *);
  36. int (*mknod) (struct inode *,struct dentry *,int,dev_t);
  37. int (*rename) (struct inode *, struct dentry *,
  38. struct inode *, struct dentry *);
  39. int (*readlink) (struct dentry *, char __user *,int);
  40. int (*follow_link) (struct dentry *, struct nameidata *);
  41. void (*truncate) (struct inode *);
  42. int (*permission) (struct inode *, int, struct nameidata *);
  43. int (*setattr) (struct dentry *, struct iattr *);
  44. int (*getattr) (struct vfsmount *, struct dentry *, struct kstat *);
  45. int (*setxattr) (struct dentry *, const char *,const void *,size_t,int);
  46. ssize_t (*getxattr) (struct dentry *, const char *, void *, size_t);
  47. ssize_t (*listxattr) (struct dentry *, char *, size_t);
  48. int (*removexattr) (struct dentry *, const char *);
  49. locking rules:
  50. all may block, none have BKL
  51. i_sem(inode)
  52. lookup: yes
  53. create: yes
  54. link: yes (both)
  55. mknod: yes
  56. symlink: yes
  57. mkdir: yes
  58. unlink: yes (both)
  59. rmdir: yes (both) (see below)
  60. rename: yes (all) (see below)
  61. readlink: no
  62. follow_link: no
  63. truncate: yes (see below)
  64. setattr: yes
  65. permission: no
  66. getattr: no
  67. setxattr: yes
  68. getxattr: no
  69. listxattr: no
  70. removexattr: yes
  71. Additionally, ->rmdir(), ->unlink() and ->rename() have ->i_sem on
  72. victim.
  73. cross-directory ->rename() has (per-superblock) ->s_vfs_rename_sem.
  74. ->truncate() is never called directly - it's a callback, not a
  75. method. It's called by vmtruncate() - library function normally used by
  76. ->setattr(). Locking information above applies to that call (i.e. is
  77. inherited from ->setattr() - vmtruncate() is used when ATTR_SIZE had been
  78. passed).
  79. See Documentation/filesystems/directory-locking for more detailed discussion
  80. of the locking scheme for directory operations.
  81. --------------------------- super_operations ---------------------------
  82. prototypes:
  83. struct inode *(*alloc_inode)(struct super_block *sb);
  84. void (*destroy_inode)(struct inode *);
  85. void (*read_inode) (struct inode *);
  86. void (*dirty_inode) (struct inode *);
  87. int (*write_inode) (struct inode *, int);
  88. void (*put_inode) (struct inode *);
  89. void (*drop_inode) (struct inode *);
  90. void (*delete_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. void (*write_super_lockfs) (struct super_block *);
  95. void (*unlockfs) (struct super_block *);
  96. int (*statfs) (struct dentry *, struct kstatfs *);
  97. int (*remount_fs) (struct super_block *, int *, char *);
  98. void (*clear_inode) (struct inode *);
  99. void (*umount_begin) (struct super_block *);
  100. int (*show_options)(struct seq_file *, struct vfsmount *);
  101. ssize_t (*quota_read)(struct super_block *, int, char *, size_t, loff_t);
  102. ssize_t (*quota_write)(struct super_block *, int, const char *, size_t, loff_t);
  103. locking rules:
  104. All may block.
  105. BKL s_lock s_umount
  106. alloc_inode: no no no
  107. destroy_inode: no
  108. read_inode: no (see below)
  109. dirty_inode: no (must not sleep)
  110. write_inode: no
  111. put_inode: no
  112. drop_inode: no !!!inode_lock!!!
  113. delete_inode: no
  114. put_super: yes yes no
  115. write_super: no yes read
  116. sync_fs: no no read
  117. write_super_lockfs: ?
  118. unlockfs: ?
  119. statfs: no no no
  120. remount_fs: no yes maybe (see below)
  121. clear_inode: no
  122. umount_begin: yes no no
  123. show_options: no (vfsmount->sem)
  124. quota_read: no no no (see below)
  125. quota_write: no no no (see below)
  126. ->read_inode() is not a method - it's a callback used in iget().
  127. ->remount_fs() will have the s_umount lock if it's already mounted.
  128. When called from get_sb_single, it does NOT have the s_umount lock.
  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 yes
  142. kill_sb yes yes
  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 (*prepare_write)(struct file *, struct page *, unsigned, unsigned);
  157. int (*commit_write)(struct file *, struct page *, unsigned, unsigned);
  158. sector_t (*bmap)(struct address_space *, sector_t);
  159. int (*invalidatepage) (struct page *, unsigned long);
  160. int (*releasepage) (struct page *, int);
  161. int (*direct_IO)(int, struct kiocb *, const struct iovec *iov,
  162. loff_t offset, unsigned long nr_segs);
  163. locking rules:
  164. All except set_page_dirty may block
  165. BKL PageLocked(page)
  166. writepage: no yes, unlocks (see below)
  167. readpage: no yes, unlocks
  168. sync_page: no maybe
  169. writepages: no
  170. set_page_dirty no no
  171. readpages: no
  172. prepare_write: no yes
  173. commit_write: no yes
  174. bmap: yes
  175. invalidatepage: no yes
  176. releasepage: no yes
  177. direct_IO: no
  178. ->prepare_write(), ->commit_write(), ->sync_page() and ->readpage()
  179. may be called from the request handler (/dev/loop).
  180. ->readpage() unlocks the page, either synchronously or via I/O
  181. completion.
  182. ->readpages() populates the pagecache with the passed pages and starts
  183. I/O against them. They come unlocked upon I/O completion.
  184. ->writepage() is used for two purposes: for "memory cleansing" and for
  185. "sync". These are quite different operations and the behaviour may differ
  186. depending upon the mode.
  187. If writepage is called for sync (wbc->sync_mode != WBC_SYNC_NONE) then
  188. it *must* start I/O against the page, even if that would involve
  189. blocking on in-progress I/O.
  190. If writepage is called for memory cleansing (sync_mode ==
  191. WBC_SYNC_NONE) then its role is to get as much writeout underway as
  192. possible. So writepage should try to avoid blocking against
  193. currently-in-progress I/O.
  194. If the filesystem is not called for "sync" and it determines that it
  195. would need to block against in-progress I/O to be able to start new I/O
  196. against the page the filesystem should redirty the page with
  197. redirty_page_for_writepage(), then unlock the page and return zero.
  198. This may also be done to avoid internal deadlocks, but rarely.
  199. If the filesytem is called for sync then it must wait on any
  200. in-progress I/O and then start new I/O.
  201. The filesystem should unlock the page synchronously, before returning to the
  202. caller, unless ->writepage() returns special WRITEPAGE_ACTIVATE
  203. value. WRITEPAGE_ACTIVATE means that page cannot really be written out
  204. currently, and VM should stop calling ->writepage() on this page for some
  205. time. VM does this by moving page to the head of the active list, hence the
  206. name.
  207. Unless the filesystem is going to redirty_page_for_writepage(), unlock the page
  208. and return zero, writepage *must* run set_page_writeback() against the page,
  209. followed by unlocking it. Once set_page_writeback() has been run against the
  210. page, write I/O can be submitted and the write I/O completion handler must run
  211. end_page_writeback() once the I/O is complete. If no I/O is submitted, the
  212. filesystem must run end_page_writeback() against the page before returning from
  213. writepage.
  214. That is: after 2.5.12, pages which are under writeout are *not* locked. Note,
  215. if the filesystem needs the page to be locked during writeout, that is ok, too,
  216. the page is allowed to be unlocked at any point in time between the calls to
  217. set_page_writeback() and end_page_writeback().
  218. Note, failure to run either redirty_page_for_writepage() or the combination of
  219. set_page_writeback()/end_page_writeback() on a page submitted to writepage
  220. will leave the page itself marked clean but it will be tagged as dirty in the
  221. radix tree. This incoherency can lead to all sorts of hard-to-debug problems
  222. in the filesystem like having dirty inodes at umount and losing written data.
  223. ->sync_page() locking rules are not well-defined - usually it is called
  224. with lock on page, but that is not guaranteed. Considering the currently
  225. existing instances of this method ->sync_page() itself doesn't look
  226. well-defined...
  227. ->writepages() is used for periodic writeback and for syscall-initiated
  228. sync operations. The address_space should start I/O against at least
  229. *nr_to_write pages. *nr_to_write must be decremented for each page which is
  230. written. The address_space implementation may write more (or less) pages
  231. than *nr_to_write asks for, but it should try to be reasonably close. If
  232. nr_to_write is NULL, all dirty pages must be written.
  233. writepages should _only_ write pages which are present on
  234. mapping->io_pages.
  235. ->set_page_dirty() is called from various places in the kernel
  236. when the target page is marked as needing writeback. It may be called
  237. under spinlock (it cannot block) and is sometimes called with the page
  238. not locked.
  239. ->bmap() is currently used by legacy ioctl() (FIBMAP) provided by some
  240. filesystems and by the swapper. The latter will eventually go away. All
  241. instances do not actually need the BKL. Please, keep it that way and don't
  242. breed new callers.
  243. ->invalidatepage() is called when the filesystem must attempt to drop
  244. some or all of the buffers from the page when it is being truncated. It
  245. returns zero on success. If ->invalidatepage is zero, the kernel uses
  246. block_invalidatepage() instead.
  247. ->releasepage() is called when the kernel is about to try to drop the
  248. buffers from the page in preparation for freeing it. It returns zero to
  249. indicate that the buffers are (or may be) freeable. If ->releasepage is zero,
  250. the kernel assumes that the fs has no private interest in the buffers.
  251. Note: currently almost all instances of address_space methods are
  252. using BKL for internal serialization and that's one of the worst sources
  253. of contention. Normally they are calling library functions (in fs/buffer.c)
  254. and pass foo_get_block() as a callback (on local block-based filesystems,
  255. indeed). BKL is not needed for library stuff and is usually taken by
  256. foo_get_block(). It's an overkill, since block bitmaps can be protected by
  257. internal fs locking and real critical areas are much smaller than the areas
  258. filesystems protect now.
  259. ----------------------- file_lock_operations ------------------------------
  260. prototypes:
  261. void (*fl_insert)(struct file_lock *); /* lock insertion callback */
  262. void (*fl_remove)(struct file_lock *); /* lock removal callback */
  263. void (*fl_copy_lock)(struct file_lock *, struct file_lock *);
  264. void (*fl_release_private)(struct file_lock *);
  265. locking rules:
  266. BKL may block
  267. fl_insert: yes no
  268. fl_remove: yes no
  269. fl_copy_lock: yes no
  270. fl_release_private: yes yes
  271. ----------------------- lock_manager_operations ---------------------------
  272. prototypes:
  273. int (*fl_compare_owner)(struct file_lock *, struct file_lock *);
  274. void (*fl_notify)(struct file_lock *); /* unblock callback */
  275. void (*fl_copy_lock)(struct file_lock *, struct file_lock *);
  276. void (*fl_release_private)(struct file_lock *);
  277. void (*fl_break)(struct file_lock *); /* break_lease callback */
  278. locking rules:
  279. BKL may block
  280. fl_compare_owner: yes no
  281. fl_notify: yes no
  282. fl_copy_lock: yes no
  283. fl_release_private: yes yes
  284. fl_break: yes no
  285. Currently only NFSD and NLM provide instances of this class. None of the
  286. them block. If you have out-of-tree instances - please, show up. Locking
  287. in that area will change.
  288. --------------------------- buffer_head -----------------------------------
  289. prototypes:
  290. void (*b_end_io)(struct buffer_head *bh, int uptodate);
  291. locking rules:
  292. called from interrupts. In other words, extreme care is needed here.
  293. bh is locked, but that's all warranties we have here. Currently only RAID1,
  294. highmem, fs/buffer.c, and fs/ntfs/aops.c are providing these. Block devices
  295. call this method upon the IO completion.
  296. --------------------------- block_device_operations -----------------------
  297. prototypes:
  298. int (*open) (struct inode *, struct file *);
  299. int (*release) (struct inode *, struct file *);
  300. int (*ioctl) (struct inode *, struct file *, unsigned, unsigned long);
  301. int (*media_changed) (struct gendisk *);
  302. int (*revalidate_disk) (struct gendisk *);
  303. locking rules:
  304. BKL bd_sem
  305. open: yes yes
  306. release: yes yes
  307. ioctl: yes no
  308. media_changed: no no
  309. revalidate_disk: no no
  310. The last two are called only from check_disk_change().
  311. --------------------------- file_operations -------------------------------
  312. prototypes:
  313. loff_t (*llseek) (struct file *, loff_t, int);
  314. ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
  315. ssize_t (*aio_read) (struct kiocb *, char __user *, size_t, loff_t);
  316. ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
  317. ssize_t (*aio_write) (struct kiocb *, const char __user *, size_t,
  318. loff_t);
  319. int (*readdir) (struct file *, void *, filldir_t);
  320. unsigned int (*poll) (struct file *, struct poll_table_struct *);
  321. int (*ioctl) (struct inode *, struct file *, unsigned int,
  322. unsigned long);
  323. long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
  324. long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
  325. int (*mmap) (struct file *, struct vm_area_struct *);
  326. int (*open) (struct inode *, struct file *);
  327. int (*flush) (struct file *);
  328. int (*release) (struct inode *, struct file *);
  329. int (*fsync) (struct file *, struct dentry *, int datasync);
  330. int (*aio_fsync) (struct kiocb *, int datasync);
  331. int (*fasync) (int, struct file *, int);
  332. int (*lock) (struct file *, int, struct file_lock *);
  333. ssize_t (*readv) (struct file *, const struct iovec *, unsigned long,
  334. loff_t *);
  335. ssize_t (*writev) (struct file *, const struct iovec *, unsigned long,
  336. loff_t *);
  337. ssize_t (*sendfile) (struct file *, loff_t *, size_t, read_actor_t,
  338. void __user *);
  339. ssize_t (*sendpage) (struct file *, struct page *, int, size_t,
  340. loff_t *, int);
  341. unsigned long (*get_unmapped_area)(struct file *, unsigned long,
  342. unsigned long, unsigned long, unsigned long);
  343. int (*check_flags)(int);
  344. int (*dir_notify)(struct file *, unsigned long);
  345. };
  346. locking rules:
  347. All except ->poll() may block.
  348. BKL
  349. llseek: no (see below)
  350. read: no
  351. aio_read: no
  352. write: no
  353. aio_write: no
  354. readdir: no
  355. poll: no
  356. ioctl: yes (see below)
  357. unlocked_ioctl: no (see below)
  358. compat_ioctl: no
  359. mmap: no
  360. open: maybe (see below)
  361. flush: no
  362. release: no
  363. fsync: no (see below)
  364. aio_fsync: no
  365. fasync: yes (see below)
  366. lock: yes
  367. readv: no
  368. writev: no
  369. sendfile: no
  370. sendpage: no
  371. get_unmapped_area: no
  372. check_flags: no
  373. dir_notify: no
  374. ->llseek() locking has moved from llseek to the individual llseek
  375. implementations. If your fs is not using generic_file_llseek, you
  376. need to acquire and release the appropriate locks in your ->llseek().
  377. For many filesystems, it is probably safe to acquire the inode
  378. semaphore. Note some filesystems (i.e. remote ones) provide no
  379. protection for i_size so you will need to use the BKL.
  380. ->open() locking is in-transit: big lock partially moved into the methods.
  381. The only exception is ->open() in the instances of file_operations that never
  382. end up in ->i_fop/->proc_fops, i.e. ones that belong to character devices
  383. (chrdev_open() takes lock before replacing ->f_op and calling the secondary
  384. method. As soon as we fix the handling of module reference counters all
  385. instances of ->open() will be called without the BKL.
  386. Note: ext2_release() was *the* source of contention on fs-intensive
  387. loads and dropping BKL on ->release() helps to get rid of that (we still
  388. grab BKL for cases when we close a file that had been opened r/w, but that
  389. can and should be done using the internal locking with smaller critical areas).
  390. Current worst offender is ext2_get_block()...
  391. ->fasync() is a mess. This area needs a big cleanup and that will probably
  392. affect locking.
  393. ->readdir() and ->ioctl() on directories must be changed. Ideally we would
  394. move ->readdir() to inode_operations and use a separate method for directory
  395. ->ioctl() or kill the latter completely. One of the problems is that for
  396. anything that resembles union-mount we won't have a struct file for all
  397. components. And there are other reasons why the current interface is a mess...
  398. ->ioctl() on regular files is superceded by the ->unlocked_ioctl() that
  399. doesn't take the BKL.
  400. ->read on directories probably must go away - we should just enforce -EISDIR
  401. in sys_read() and friends.
  402. ->fsync() has i_sem on inode.
  403. --------------------------- dquot_operations -------------------------------
  404. prototypes:
  405. int (*initialize) (struct inode *, int);
  406. int (*drop) (struct inode *);
  407. int (*alloc_space) (struct inode *, qsize_t, int);
  408. int (*alloc_inode) (const struct inode *, unsigned long);
  409. int (*free_space) (struct inode *, qsize_t);
  410. int (*free_inode) (const struct inode *, unsigned long);
  411. int (*transfer) (struct inode *, struct iattr *);
  412. int (*write_dquot) (struct dquot *);
  413. int (*acquire_dquot) (struct dquot *);
  414. int (*release_dquot) (struct dquot *);
  415. int (*mark_dirty) (struct dquot *);
  416. int (*write_info) (struct super_block *, int);
  417. These operations are intended to be more or less wrapping functions that ensure
  418. a proper locking wrt the filesystem and call the generic quota operations.
  419. What filesystem should expect from the generic quota functions:
  420. FS recursion Held locks when called
  421. initialize: yes maybe dqonoff_sem
  422. drop: yes -
  423. alloc_space: ->mark_dirty() -
  424. alloc_inode: ->mark_dirty() -
  425. free_space: ->mark_dirty() -
  426. free_inode: ->mark_dirty() -
  427. transfer: yes -
  428. write_dquot: yes dqonoff_sem or dqptr_sem
  429. acquire_dquot: yes dqonoff_sem or dqptr_sem
  430. release_dquot: yes dqonoff_sem or dqptr_sem
  431. mark_dirty: no -
  432. write_info: yes dqonoff_sem
  433. FS recursion means calling ->quota_read() and ->quota_write() from superblock
  434. operations.
  435. ->alloc_space(), ->alloc_inode(), ->free_space(), ->free_inode() are called
  436. only directly by the filesystem and do not call any fs functions only
  437. the ->mark_dirty() operation.
  438. More details about quota locking can be found in fs/dquot.c.
  439. --------------------------- vm_operations_struct -----------------------------
  440. prototypes:
  441. void (*open)(struct vm_area_struct*);
  442. void (*close)(struct vm_area_struct*);
  443. struct page *(*nopage)(struct vm_area_struct*, unsigned long, int *);
  444. locking rules:
  445. BKL mmap_sem
  446. open: no yes
  447. close: no yes
  448. nopage: no yes
  449. ================================================================================
  450. Dubious stuff
  451. (if you break something or notice that it is broken and do not fix it yourself
  452. - at least put it here)
  453. ipc/shm.c::shm_delete() - may need BKL.
  454. ->read() and ->write() in many drivers are (probably) missing BKL.
  455. drivers/sgi/char/graphics.c::sgi_graphics_nopage() - may need BKL.