drop_caches.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Implement the manual drop-all-pagecache function
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/mm.h>
  6. #include <linux/fs.h>
  7. #include <linux/writeback.h>
  8. #include <linux/sysctl.h>
  9. #include <linux/gfp.h>
  10. #include "internal.h"
  11. /* A global variable is a bit ugly, but it keeps the code simple */
  12. int sysctl_drop_caches;
  13. static void drop_pagecache_sb(struct super_block *sb, void *unused)
  14. {
  15. struct inode *inode, *toput_inode = NULL;
  16. spin_lock(&inode_sb_list_lock);
  17. list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
  18. spin_lock(&inode->i_lock);
  19. if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
  20. (inode->i_mapping->nrpages == 0)) {
  21. spin_unlock(&inode->i_lock);
  22. continue;
  23. }
  24. __iget(inode);
  25. spin_unlock(&inode->i_lock);
  26. spin_unlock(&inode_sb_list_lock);
  27. invalidate_mapping_pages(inode->i_mapping, 0, -1);
  28. iput(toput_inode);
  29. toput_inode = inode;
  30. spin_lock(&inode_sb_list_lock);
  31. }
  32. spin_unlock(&inode_sb_list_lock);
  33. iput(toput_inode);
  34. }
  35. int drop_caches_sysctl_handler(struct ctl_table *table, int write,
  36. void __user *buffer, size_t *length, loff_t *ppos)
  37. {
  38. int ret;
  39. ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
  40. if (ret)
  41. return ret;
  42. if (write) {
  43. static int stfu;
  44. if (sysctl_drop_caches & 1) {
  45. iterate_supers(drop_pagecache_sb, NULL);
  46. count_vm_event(DROP_PAGECACHE);
  47. }
  48. if (sysctl_drop_caches & 2) {
  49. drop_slab();
  50. count_vm_event(DROP_SLAB);
  51. }
  52. if (!stfu) {
  53. pr_info("%s (%d): drop_caches: %d\n",
  54. current->comm, task_pid_nr(current),
  55. sysctl_drop_caches);
  56. }
  57. stfu |= sysctl_drop_caches & 4;
  58. }
  59. return 0;
  60. }