Locking 20 KB

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