drop_caches.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. /* A global variable is a bit ugly, but it keeps the code simple */
  11. int sysctl_drop_caches;
  12. static void drop_pagecache_sb(struct super_block *sb, void *unused)
  13. {
  14. struct inode *inode, *toput_inode = NULL;
  15. spin_lock(&inode_lock);
  16. list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
  17. spin_lock(&inode->i_lock);
  18. if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
  19. (inode->i_mapping->nrpages == 0)) {
  20. spin_unlock(&inode->i_lock);
  21. continue;
  22. }
  23. __iget(inode);
  24. spin_unlock(&inode->i_lock);
  25. spin_unlock(&inode_lock);
  26. invalidate_mapping_pages(inode->i_mapping, 0, -1);
  27. iput(toput_inode);
  28. toput_inode = inode;
  29. spin_lock(&inode_lock);
  30. }
  31. spin_unlock(&inode_lock);
  32. iput(toput_inode);
  33. }
  34. static void drop_slab(void)
  35. {
  36. int nr_objects;
  37. do {
  38. nr_objects = shrink_slab(1000, GFP_KERNEL, 1000);
  39. } while (nr_objects > 10);
  40. }
  41. int drop_caches_sysctl_handler(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. if (sysctl_drop_caches & 1)
  50. iterate_supers(drop_pagecache_sb, NULL);
  51. if (sysctl_drop_caches & 2)
  52. drop_slab();
  53. }
  54. return 0;
  55. }