Locking 21 KB

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