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