drop_caches.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. 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. void drop_slab(void)
  36. {
  37. int nr_objects;
  38. struct shrink_control shrink = {
  39. .gfp_mask = GFP_KERNEL,
  40. };
  41. do {
  42. shrink.priority = DEF_PRIORITY;
  43. nr_objects = shrink_slab(&shrink, 1000, 1000);
  44. } while (nr_objects > 10);
  45. }
  46. int drop_caches_sysctl_handler(ctl_table *table, int write,
  47. void __user *buffer, size_t *length, loff_t *ppos)
  48. {
  49. int ret;
  50. ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
  51. if (ret)
  52. return ret;
  53. if (write) {
  54. if (sysctl_drop_caches & 1)
  55. iterate_supers(drop_pagecache_sb, NULL);
  56. if (sysctl_drop_caches & 2)
  57. drop_slab();
  58. }
  59. return 0;
  60. }