drop_caches.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Implement the manual drop-all-pagecache function
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/mm.h>
  7. #include <linux/fs.h>
  8. #include <linux/writeback.h>
  9. #include <linux/sysctl.h>
  10. #include <linux/gfp.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(&sb->s_inode_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(&sb->s_inode_list_lock);
  28. invalidate_mapping_pages(inode->i_mapping, 0, -1);
  29. iput(toput_inode);
  30. toput_inode = inode;
  31. spin_lock(&sb->s_inode_list_lock);
  32. }
  33. spin_unlock(&sb->s_inode_list_lock);
  34. iput(toput_inode);
  35. }
  36. int drop_caches_sysctl_handler(struct ctl_table *table, int write,
  37. void __user *buffer, size_t *length, loff_t *ppos)
  38. {
  39. int ret;
  40. ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
  41. if (ret)
  42. return ret;
  43. if (write) {
  44. static int stfu;
  45. if (sysctl_drop_caches & 1) {
  46. iterate_supers(drop_pagecache_sb, NULL);
  47. count_vm_event(DROP_PAGECACHE);
  48. }
  49. if (sysctl_drop_caches & 2) {
  50. drop_slab();
  51. count_vm_event(DROP_SLAB);
  52. }
  53. if (!stfu) {
  54. pr_info("%s (%d): drop_caches: %d\n",
  55. current->comm, task_pid_nr(current),
  56. sysctl_drop_caches);
  57. }
  58. stfu |= sysctl_drop_caches & 4;
  59. }
  60. return 0;
  61. }