purge.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/NonnullRefPtrVector.h>
  7. #include <Kernel/Arch/x86/InterruptDisabler.h>
  8. #include <Kernel/Process.h>
  9. #include <Kernel/VM/AnonymousVMObject.h>
  10. #include <Kernel/VM/InodeVMObject.h>
  11. #include <Kernel/VM/MemoryManager.h>
  12. namespace Kernel {
  13. KResultOr<FlatPtr> Process::sys$purge(int mode)
  14. {
  15. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  16. REQUIRE_NO_PROMISES;
  17. if (!is_superuser())
  18. return EPERM;
  19. size_t purged_page_count = 0;
  20. if (mode & PURGE_ALL_VOLATILE) {
  21. NonnullRefPtrVector<AnonymousVMObject> vmobjects;
  22. {
  23. KResult result(KSuccess);
  24. MM.for_each_vmobject([&](auto& vmobject) {
  25. if (vmobject.is_anonymous()) {
  26. // In the event that the append fails, only attempt to continue
  27. // the purge if we have already appended something successfully.
  28. if (!vmobjects.try_append(vmobject) && vmobjects.is_empty()) {
  29. result = ENOMEM;
  30. return IterationDecision::Break;
  31. }
  32. }
  33. return IterationDecision::Continue;
  34. });
  35. if (result.is_error())
  36. return result.error();
  37. }
  38. for (auto& vmobject : vmobjects) {
  39. purged_page_count += vmobject.purge();
  40. }
  41. }
  42. if (mode & PURGE_ALL_CLEAN_INODE) {
  43. NonnullRefPtrVector<InodeVMObject> vmobjects;
  44. {
  45. KResult result(KSuccess);
  46. MM.for_each_vmobject([&](auto& vmobject) {
  47. if (vmobject.is_inode()) {
  48. // In the event that the append fails, only attempt to continue
  49. // the purge if we have already appended something successfully.
  50. if (!vmobjects.try_append(static_cast<InodeVMObject&>(vmobject)) && vmobjects.is_empty()) {
  51. result = ENOMEM;
  52. return IterationDecision::Break;
  53. }
  54. }
  55. return IterationDecision::Continue;
  56. });
  57. if (result.is_error())
  58. return result.error();
  59. }
  60. for (auto& vmobject : vmobjects) {
  61. purged_page_count += vmobject.release_all_clean_pages();
  62. }
  63. }
  64. return purged_page_count;
  65. }
  66. }