drop_caches.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 <linux/export.h>
  11. #include "internal.h"
  12. /* A global variable is a bit ugly, but it keeps the code simple */
  13. int sysctl_drop_caches;
  14. static void drop_pagecache_sb(struct super_block *sb, void *unused)
  15. {
  16. struct inode *inode, *toput_inode = NULL;
  17. spin_lock(&inode_sb_list_lock);
  18. list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
  19. spin_lock(&inode->i_lock);
  20. if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
  21. (inode->i_mapping->nrpages == 0)) {
  22. spin_unlock(&inode->i_lock);
  23. continue;
  24. }
  25. __iget(inode);
  26. spin_unlock(&inode->i_lock);
  27. spin_unlock(&inode_sb_list_lock);
  28. invalidate_mapping_pages(inode->i_mapping, 0, -1);
  29. iput(toput_inode);
  30. toput_inode = inode;
  31. spin_lock(&inode_sb_list_lock);
  32. }
  33. spin_unlock(&inode_sb_list_lock);
  34. iput(toput_inode);
  35. }
  36. static void drop_slab(void)
  37. {
  38. int nr_objects;
  39. struct shrink_control shrink = {
  40. .gfp_mask = GFP_KERNEL,
  41. };
  42. nodes_setall(shrink.nodes_to_scan);
  43. do {
  44. nr_objects = shrink_slab(&shrink, 1000, 1000);
  45. } while (nr_objects > 10);
  46. }
  47. /* For TuxOnIce */
  48. void drop_pagecache(void)
  49. {
  50. iterate_supers(drop_pagecache_sb, NULL);
  51. }
  52. EXPORT_SYMBOL_GPL(drop_pagecache);
  53. int drop_caches_sysctl_handler(struct ctl_table *table, int write,
  54. void __user *buffer, size_t *length, loff_t *ppos)
  55. {
  56. int ret;
  57. ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
  58. if (ret)
  59. return ret;
  60. if (write) {
  61. static int stfu;
  62. if (sysctl_drop_caches & 1) {
  63. iterate_supers(drop_pagecache_sb, NULL);
  64. count_vm_event(DROP_PAGECACHE);
  65. }
  66. if (sysctl_drop_caches & 2) {
  67. drop_slab();
  68. count_vm_event(DROP_SLAB);
  69. }
  70. if (!stfu) {
  71. pr_info("%s (%d): drop_caches: %d\n",
  72. current->comm, task_pid_nr(current),
  73. sysctl_drop_caches);
  74. }
  75. stfu |= sysctl_drop_caches & 4;
  76. }
  77. return 0;
  78. }