drop_caches.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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(&sb->s_inode_list_lock);
  17. list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
  18. spin_lock(&inode->i_lock);
  19. /*
  20. * We must skip inodes in unusual state. We may also skip
  21. * inodes without pages but we deliberately won't in case
  22. * we need to reschedule to avoid softlockups.
  23. */
  24. if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
  25. (inode->i_mapping->nrpages == 0 && !need_resched())) {
  26. spin_unlock(&inode->i_lock);
  27. continue;
  28. }
  29. __iget(inode);
  30. spin_unlock(&inode->i_lock);
  31. spin_unlock(&sb->s_inode_list_lock);
  32. cond_resched();
  33. invalidate_mapping_pages(inode->i_mapping, 0, -1);
  34. iput(toput_inode);
  35. toput_inode = inode;
  36. spin_lock(&sb->s_inode_list_lock);
  37. }
  38. spin_unlock(&sb->s_inode_list_lock);
  39. iput(toput_inode);
  40. }
  41. int drop_caches_sysctl_handler(struct ctl_table *table, int write,
  42. void __user *buffer, size_t *length, loff_t *ppos)
  43. {
  44. int ret;
  45. ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
  46. if (ret)
  47. return ret;
  48. if (write) {
  49. static int stfu;
  50. if (sysctl_drop_caches & 1) {
  51. iterate_supers(drop_pagecache_sb, NULL);
  52. count_vm_event(DROP_PAGECACHE);
  53. }
  54. if (sysctl_drop_caches & 2) {
  55. drop_slab();
  56. count_vm_event(DROP_SLAB);
  57. }
  58. if (!stfu) {
  59. pr_info("%s (%d): drop_caches: %d\n",
  60. current->comm, task_pid_nr(current),
  61. sysctl_drop_caches);
  62. }
  63. stfu |= sysctl_drop_caches & 4;
  64. }
  65. return 0;
  66. }