drop_caches.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. do {
  39. int nid;
  40. nr_objects = 0;
  41. for_each_online_node(nid)
  42. nr_objects += shrink_node_slabs(GFP_KERNEL, nid,
  43. 1000, 1000);
  44. } while (nr_objects > 10);
  45. }
  46. int drop_caches_sysctl_handler(struct 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. static int stfu;
  55. if (sysctl_drop_caches & 1) {
  56. iterate_supers(drop_pagecache_sb, NULL);
  57. count_vm_event(DROP_PAGECACHE);
  58. }
  59. if (sysctl_drop_caches & 2) {
  60. drop_slab();
  61. count_vm_event(DROP_SLAB);
  62. }
  63. if (!stfu) {
  64. pr_info("%s (%d): drop_caches: %d\n",
  65. current->comm, task_pid_nr(current),
  66. sysctl_drop_caches);
  67. }
  68. stfu |= sysctl_drop_caches & 4;
  69. }
  70. return 0;
  71. }