]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/kern/kern_memorystatus.c
xnu-3248.50.21.tar.gz
[apple/xnu.git] / bsd / kern / kern_memorystatus.c
index 852037af4d7699baa55f80dab3e3a419b84581c3..22f7edbd3572dd87a491255d28a695f9e107dbf0 100644 (file)
 #include <kern/kalloc.h>
 #include <kern/assert.h>
 #include <kern/debug.h>
 #include <kern/kalloc.h>
 #include <kern/assert.h>
 #include <kern/debug.h>
-#include <kern/lock.h>
+#include <kern/locks.h>
 #include <kern/task.h>
 #include <kern/thread.h>
 #include <kern/host.h>
 #include <libkern/libkern.h>
 #include <kern/task.h>
 #include <kern/thread.h>
 #include <kern/host.h>
 #include <libkern/libkern.h>
+#include <mach/coalition.h>
 #include <mach/mach_time.h>
 #include <mach/task.h>
 #include <mach/host_priv.h>
 #include <mach/mach_host.h>
 #include <pexpert/pexpert.h>
 #include <mach/mach_time.h>
 #include <mach/task.h>
 #include <mach/host_priv.h>
 #include <mach/mach_host.h>
 #include <pexpert/pexpert.h>
+#include <sys/coalition.h>
 #include <sys/kern_event.h>
 #include <sys/proc.h>
 #include <sys/proc_info.h>
 #include <sys/kern_event.h>
 #include <sys/proc.h>
 #include <sys/proc_info.h>
 
 #include <sys/kern_memorystatus.h> 
 
 
 #include <sys/kern_memorystatus.h> 
 
+#if CONFIG_JETSAM
+/* For logging clarity */
+static const char *jetsam_kill_cause_name[] = {
+       ""                      ,
+       "jettisoned"            ,       /* kMemorystatusKilled                  */
+       "highwater"             ,       /* kMemorystatusKilledHiwat             */
+       "vnode-limit"           ,       /* kMemorystatusKilledVnodes            */
+       "vm-pageshortage"       ,       /* kMemorystatusKilledVMPageShortage    */
+       "vm-thrashing"          ,       /* kMemorystatusKilledVMThrashing       */
+       "fc-thrashing"          ,       /* kMemorystatusKilledFCThrashing       */
+       "per-process-limit"     ,       /* kMemorystatusKilledPerProcessLimit   */
+       "diagnostic"            ,       /* kMemorystatusKilledDiagnostic        */
+       "idle-exit"             ,       /* kMemorystatusKilledIdleExit          */
+};
+
+/* Does cause indicate vm or fc thrashing? */
+static boolean_t
+is_thrashing(unsigned cause)
+{
+       switch (cause) {
+       case kMemorystatusKilledVMThrashing:
+       case kMemorystatusKilledFCThrashing:
+               return TRUE;
+       default:
+               return FALSE;
+       }
+}
+
+/* Callback into vm_compressor.c to signal that thrashing has been mitigated. */
+extern void vm_thrashing_jetsam_done(void);
+#endif
+
 /* These are very verbose printfs(), enable with
  * MEMORYSTATUS_DEBUG_LOG
  */
 /* These are very verbose printfs(), enable with
  * MEMORYSTATUS_DEBUG_LOG
  */
@@ -72,6 +106,70 @@ do {                                              \
 #define MEMORYSTATUS_DEBUG(cond, format, ...)
 #endif
 
 #define MEMORYSTATUS_DEBUG(cond, format, ...)
 #endif
 
+/*
+ * Active / Inactive limit support
+ * proc list must be locked
+ *
+ * The SET_*** macros are used to initialize a limit
+ * for the first time.
+ *
+ * The CACHE_*** macros are use to cache the limit that will
+ * soon be in effect down in the ledgers.
+ */
+
+#define SET_ACTIVE_LIMITS_LOCKED(p, limit, is_fatal)                   \
+MACRO_BEGIN                                                            \
+(p)->p_memstat_memlimit_active = (limit);                              \
+   (p)->p_memstat_state &= ~P_MEMSTAT_MEMLIMIT_ACTIVE_EXC_TRIGGERED;   \
+   if (is_fatal) {                                                     \
+          (p)->p_memstat_state |= P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL;     \
+   } else {                                                            \
+          (p)->p_memstat_state &= ~P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL;    \
+   }                                                                   \
+MACRO_END
+
+#define SET_INACTIVE_LIMITS_LOCKED(p, limit, is_fatal)                 \
+MACRO_BEGIN                                                            \
+(p)->p_memstat_memlimit_inactive = (limit);                            \
+   (p)->p_memstat_state &= ~P_MEMSTAT_MEMLIMIT_INACTIVE_EXC_TRIGGERED; \
+   if (is_fatal) {                                                     \
+          (p)->p_memstat_state |= P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL;   \
+   } else {                                                            \
+          (p)->p_memstat_state &= ~P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL;  \
+   }                                                                   \
+MACRO_END
+
+#define CACHE_ACTIVE_LIMITS_LOCKED(p, trigger_exception)               \
+MACRO_BEGIN                                                            \
+(p)->p_memstat_memlimit = (p)->p_memstat_memlimit_active;              \
+   if ((p)->p_memstat_state & P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL) {       \
+          (p)->p_memstat_state |= P_MEMSTAT_FATAL_MEMLIMIT;            \
+   } else {                                                            \
+          (p)->p_memstat_state &= ~P_MEMSTAT_FATAL_MEMLIMIT;           \
+   }                                                                   \
+   if ((p)->p_memstat_state & P_MEMSTAT_MEMLIMIT_ACTIVE_EXC_TRIGGERED) { \
+          trigger_exception = FALSE;                                   \
+   } else {                                                            \
+          trigger_exception = TRUE;                                    \
+   }                                                                   \
+MACRO_END
+
+#define CACHE_INACTIVE_LIMITS_LOCKED(p, trigger_exception)             \
+MACRO_BEGIN                                                            \
+(p)->p_memstat_memlimit = (p)->p_memstat_memlimit_inactive;            \
+   if ((p)->p_memstat_state & P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL) {     \
+          (p)->p_memstat_state |= P_MEMSTAT_FATAL_MEMLIMIT;            \
+   } else {                                                            \
+          (p)->p_memstat_state &= ~P_MEMSTAT_FATAL_MEMLIMIT;           \
+   }                                                                   \
+   if ((p)->p_memstat_state & P_MEMSTAT_MEMLIMIT_INACTIVE_EXC_TRIGGERED) { \
+          trigger_exception = FALSE;                                   \
+   } else {                                                            \
+          trigger_exception = TRUE;                                    \
+   }                                                                   \
+MACRO_END
+
+
 /* General tunables */
 
 unsigned long delta_percentage = 5;
 /* General tunables */
 
 unsigned long delta_percentage = 5;
@@ -105,8 +203,9 @@ struct filterops memorystatus_filtops = {
 };
 
 enum {
 };
 
 enum {
-       kMemorystatusNoPressure = 1,
-       kMemorystatusPressure = 2
+       kMemorystatusNoPressure = 0x1,
+       kMemorystatusPressure = 0x2,
+       kMemorystatusLowSwap = 0x4
 };
 
 /* Idle guard handling */
 };
 
 /* Idle guard handling */
@@ -120,11 +219,15 @@ static void memorystatus_schedule_idle_demotion_locked(proc_t p, boolean_t set_s
 static void memorystatus_invalidate_idle_demotion_locked(proc_t p, boolean_t clean_state);
 static void memorystatus_reschedule_idle_demotion_locked(void);
 
 static void memorystatus_invalidate_idle_demotion_locked(proc_t p, boolean_t clean_state);
 static void memorystatus_reschedule_idle_demotion_locked(void);
 
-static void memorystatus_update_priority_locked(proc_t p, int priority);
+static void memorystatus_update_priority_locked(proc_t p, int priority, boolean_t head_insert);
+
+boolean_t is_knote_registered_modify_task_pressure_bits(struct knote*, int, task_t, vm_pressure_level_t, vm_pressure_level_t);
+void memorystatus_send_low_swap_note(void);
 
 int memorystatus_wakeup = 0;
 
 unsigned int memorystatus_level = 0;
 
 int memorystatus_wakeup = 0;
 
 unsigned int memorystatus_level = 0;
+unsigned int memorystatus_early_boot_level = 0;
 
 static int memorystatus_list_count = 0;
 
 
 static int memorystatus_list_count = 0;
 
@@ -141,9 +244,9 @@ uint64_t memstat_idle_demotion_deadline = 0;
 
 static unsigned int memorystatus_dirty_count = 0;
 
 
 static unsigned int memorystatus_dirty_count = 0;
 
-#if !CONFIG_JETSAM
-static boolean_t kill_idle_exit = FALSE;
-#endif
+#if CONFIG_JETSAM
+SYSCTL_INT(_kern, OID_AUTO, max_task_pmem, CTLFLAG_RD|CTLFLAG_LOCKED|CTLFLAG_MASKED, &max_task_footprint_mb, 0, "");
+#endif // CONFIG_JETSAM
 
 
 int
 
 
 int
@@ -169,53 +272,101 @@ static void memorystatus_thread(void *param __unused, wait_result_t wr __unused)
 
 #if CONFIG_JETSAM
 
 
 #if CONFIG_JETSAM
 
+static int memorystatus_cmd_set_jetsam_memory_limit(pid_t pid, int32_t high_water_mark, __unused int32_t *retval, boolean_t is_fatal_limit);
+
+static int memorystatus_cmd_set_memlimit_properties(pid_t pid, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval);
+
+static int memorystatus_set_memlimit_properties(pid_t pid, memorystatus_memlimit_properties_t *entry);
+
+static int memorystatus_cmd_get_memlimit_properties(pid_t pid, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval);
+
+static boolean_t proc_jetsam_state_is_active_locked(proc_t);
+
+int proc_get_memstat_priority(proc_t, boolean_t);
+
 /* Kill processes exceeding their limit either under memory pressure (1), or as soon as possible (0) */
 #define LEGACY_HIWATER 1
 
 /* Kill processes exceeding their limit either under memory pressure (1), or as soon as possible (0) */
 #define LEGACY_HIWATER 1
 
-static int memorystatus_highwater_enabled = 1;
+static boolean_t memorystatus_idle_snapshot = 0;
 
 
-extern unsigned int    vm_page_free_count;
-extern unsigned int    vm_page_active_count;
-extern unsigned int    vm_page_inactive_count;
-extern unsigned int    vm_page_throttled_count;
-extern unsigned int    vm_page_purgeable_count;
-extern unsigned int    vm_page_wire_count;
+static int memorystatus_highwater_enabled = 1;  /* Update the cached memlimit data. This should be removed. */
 
 unsigned int memorystatus_delta = 0;
 
 
 unsigned int memorystatus_delta = 0;
 
-static unsigned int memorystatus_available_pages = (unsigned int)-1;
-static unsigned int memorystatus_available_pages_pressure = 0;
-static unsigned int memorystatus_available_pages_critical = 0;
 static unsigned int memorystatus_available_pages_critical_base = 0;
 static unsigned int memorystatus_available_pages_critical_base = 0;
-static unsigned int memorystatus_last_foreground_pressure_pages = (unsigned int)-1;
-#if !LATENCY_JETSAM
+//static unsigned int memorystatus_last_foreground_pressure_pages = (unsigned int)-1;
 static unsigned int memorystatus_available_pages_critical_idle_offset = 0;
 static unsigned int memorystatus_available_pages_critical_idle_offset = 0;
-#endif
+
+/* Jetsam Loop Detection */
+static boolean_t memorystatus_jld_enabled = TRUE;              /* Enables jetsam loop detection on all devices */
+static uint32_t memorystatus_jld_eval_period_msecs = 0;                /* Init pass sets this based on device memory size */
+static int      memorystatus_jld_eval_aggressive_count = 3;    /* Raise the priority max after 'n' aggressive loops */
+static int      memorystatus_jld_eval_aggressive_priority_band_max = 15;  /* Kill aggressively up through this band */
+
+/*
+ * A FG app can request that the aggressive jetsam mechanism display some leniency in the FG band. This 'lenient' mode is described as:
+ * --- if aggressive jetsam kills an app in the FG band and gets back >=AGGRESSIVE_JETSAM_LENIENT_MODE_THRESHOLD memory, it will stop the aggressive march further into and up the jetsam bands.
+ *
+ * RESTRICTIONS:
+ * - Such a request is respected/acknowledged only once while that 'requesting' app is in the FG band i.e. if aggressive jetsam was
+ * needed and the 'lenient' mode was deployed then that's it for this special mode while the app is in the FG band. 
+ *
+ * - If the app is still in the FG band and aggressive jetsam is needed again, there will be no stop-and-check the next time around.
+ *
+ * - Also, the transition of the 'requesting' app away from the FG band will void this special behavior.
+ */
+
+#define AGGRESSIVE_JETSAM_LENIENT_MODE_THRESHOLD       25
+boolean_t      memorystatus_aggressive_jetsam_lenient_allowed = FALSE;
+boolean_t      memorystatus_aggressive_jetsam_lenient = FALSE;
+
+#if DEVELOPMENT || DEBUG
+/* 
+ * Jetsam Loop Detection tunables.
+ */
+
+SYSCTL_UINT(_kern, OID_AUTO, memorystatus_jld_eval_period_msecs, CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_jld_eval_period_msecs, 0, "");
+SYSCTL_UINT(_kern, OID_AUTO, memorystatus_jld_eval_aggressive_count, CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_jld_eval_aggressive_count, 0, "");
+SYSCTL_UINT(_kern, OID_AUTO, memorystatus_jld_eval_aggressive_priority_band_max, CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_jld_eval_aggressive_priority_band_max, 0, "");
+#endif /* DEVELOPMENT || DEBUG */
 
 #if DEVELOPMENT || DEBUG
 static unsigned int memorystatus_jetsam_panic_debug = 0;
 
 static unsigned int memorystatus_jetsam_policy = kPolicyDefault;
 static unsigned int memorystatus_jetsam_policy_offset_pages_diagnostic = 0;
 
 #if DEVELOPMENT || DEBUG
 static unsigned int memorystatus_jetsam_panic_debug = 0;
 
 static unsigned int memorystatus_jetsam_policy = kPolicyDefault;
 static unsigned int memorystatus_jetsam_policy_offset_pages_diagnostic = 0;
+static unsigned int memorystatus_debug_dump_this_bucket = 0;
 #endif
 
 #endif
 
-static boolean_t kill_under_pressure = FALSE;
+static unsigned int memorystatus_thread_wasted_wakeup = 0;
 
 
+static uint32_t kill_under_pressure_cause = 0;
+
+/*
+ * default jetsam snapshot support
+ */
 static memorystatus_jetsam_snapshot_t *memorystatus_jetsam_snapshot;
 #define memorystatus_jetsam_snapshot_list memorystatus_jetsam_snapshot->entries
 static memorystatus_jetsam_snapshot_t *memorystatus_jetsam_snapshot;
 #define memorystatus_jetsam_snapshot_list memorystatus_jetsam_snapshot->entries
-
 static unsigned int memorystatus_jetsam_snapshot_count = 0;
 static unsigned int memorystatus_jetsam_snapshot_max = 0;
 static unsigned int memorystatus_jetsam_snapshot_count = 0;
 static unsigned int memorystatus_jetsam_snapshot_max = 0;
+static uint64_t memorystatus_jetsam_snapshot_last_timestamp = 0;
+static uint64_t memorystatus_jetsam_snapshot_timeout = 0;
+#define JETSAM_SNAPSHOT_TIMEOUT_SECS 30
+
+/*
+ * snapshot support for memstats collected at boot.
+ */
+static memorystatus_jetsam_snapshot_t memorystatus_at_boot_snapshot;
 
 static void memorystatus_clear_errors(void);
 
 static void memorystatus_clear_errors(void);
-static void memorystatus_get_task_page_counts(task_t task, uint32_t *footprint, uint32_t *max_footprint);
-static int memorystatus_send_note(int event_code, void *data, size_t data_length);
+static void memorystatus_get_task_page_counts(task_t task, uint32_t *footprint, uint32_t *max_footprint, uint32_t *max_footprint_lifetime, uint32_t *purgeable_pages);
 static uint32_t memorystatus_build_state(proc_t p);
 static void memorystatus_update_levels_locked(boolean_t critical_only);
 static uint32_t memorystatus_build_state(proc_t p);
 static void memorystatus_update_levels_locked(boolean_t critical_only);
-static boolean_t memorystatus_issue_pressure_kevent(boolean_t pressured);
+//static boolean_t memorystatus_issue_pressure_kevent(boolean_t pressured);
 
 static boolean_t memorystatus_kill_specific_process(pid_t victim_pid, uint32_t cause);
 
 static boolean_t memorystatus_kill_specific_process(pid_t victim_pid, uint32_t cause);
-static boolean_t memorystatus_kill_top_process(boolean_t any, uint32_t cause, int32_t *priority, uint32_t *errors);
+static boolean_t memorystatus_kill_top_process(boolean_t any, boolean_t sort_flag, uint32_t cause, int32_t *priority, uint32_t *errors);
+static boolean_t memorystatus_kill_top_process_aggressive(boolean_t any, uint32_t cause, int aggr_count, int32_t priority_max, uint32_t *errors);
 #if LEGACY_HIWATER
 static boolean_t memorystatus_kill_hiwat_proc(uint32_t *errors);
 #endif
 #if LEGACY_HIWATER
 static boolean_t memorystatus_kill_hiwat_proc(uint32_t *errors);
 #endif
@@ -223,18 +374,60 @@ static boolean_t memorystatus_kill_hiwat_proc(uint32_t *errors);
 static boolean_t memorystatus_kill_process_async(pid_t victim_pid, uint32_t cause);
 static boolean_t memorystatus_kill_process_sync(pid_t victim_pid, uint32_t cause);
 
 static boolean_t memorystatus_kill_process_async(pid_t victim_pid, uint32_t cause);
 static boolean_t memorystatus_kill_process_sync(pid_t victim_pid, uint32_t cause);
 
+/* Priority Band Sorting Routines */
+static int  memorystatus_sort_bucket(unsigned int bucket_index, int sort_order);
+static int  memorystatus_sort_by_largest_coalition_locked(unsigned int bucket_index, int coal_sort_order);
+static void memorystatus_sort_by_largest_process_locked(unsigned int bucket_index);
+static int  memorystatus_move_list_locked(unsigned int bucket_index, pid_t *pid_list, int list_sz);
+
+/* qsort routines */
+typedef int (*cmpfunc_t)(const void *a, const void *b);
+extern void qsort(void *a, size_t n, size_t es, cmpfunc_t cmp);
+static int memstat_asc_cmp(const void *a, const void *b);
+
 #endif /* CONFIG_JETSAM */
 
 /* VM pressure */
 
 #endif /* CONFIG_JETSAM */
 
 /* VM pressure */
 
+extern unsigned int    vm_page_free_count;
+extern unsigned int    vm_page_active_count;
+extern unsigned int    vm_page_inactive_count;
+extern unsigned int    vm_page_throttled_count;
+extern unsigned int    vm_page_purgeable_count;
+extern unsigned int    vm_page_wire_count;
+
 #if VM_PRESSURE_EVENTS
 
 #include "vm_pressure.h"
 
 #if VM_PRESSURE_EVENTS
 
 #include "vm_pressure.h"
 
-extern boolean_t memorystatus_warn_process(pid_t pid);
+extern boolean_t memorystatus_warn_process(pid_t pid, boolean_t critical);
 
 vm_pressure_level_t memorystatus_vm_pressure_level = kVMPressureNormal;
 
 
 vm_pressure_level_t memorystatus_vm_pressure_level = kVMPressureNormal;
 
+#if CONFIG_MEMORYSTATUS
+unsigned int memorystatus_available_pages = (unsigned int)-1;
+unsigned int memorystatus_available_pages_pressure = 0;
+unsigned int memorystatus_available_pages_critical = 0;
+unsigned int memorystatus_frozen_count = 0;
+unsigned int memorystatus_suspended_count = 0;
+
+/*
+ * We use this flag to signal if we have any HWM offenders
+ * on the system. This way we can reduce the number of wakeups
+ * of the memorystatus_thread when the system is between the
+ * "pressure" and "critical" threshold.
+ *
+ * The (re-)setting of this variable is done without any locks
+ * or synchronization simply because it is not possible (currently)
+ * to keep track of HWM offenders that drop down below their memory
+ * limit and/or exit. So, we choose to burn a couple of wasted wakeups
+ * by allowing the unguarded modification of this variable.
+ */
+boolean_t memorystatus_hwm_candidates = 0;
+
+static int memorystatus_send_note(int event_code, void *data, size_t data_length);
+#endif /* CONFIG_MEMORYSTATUS */
+
 #endif /* VM_PRESSURE_EVENTS */
 
 /* Freeze */
 #endif /* VM_PRESSURE_EVENTS */
 
 /* Freeze */
@@ -244,6 +437,10 @@ vm_pressure_level_t memorystatus_vm_pressure_level = kVMPressureNormal;
 boolean_t memorystatus_freeze_enabled = FALSE;
 int memorystatus_freeze_wakeup = 0;
 
 boolean_t memorystatus_freeze_enabled = FALSE;
 int memorystatus_freeze_wakeup = 0;
 
+lck_grp_attr_t *freezer_lck_grp_attr;
+lck_grp_t *freezer_lck_grp;
+static lck_mtx_t freezer_mutex;
+
 static inline boolean_t memorystatus_can_freeze_processes(void);
 static boolean_t memorystatus_can_freeze(boolean_t *memorystatus_freeze_swap_low);
 
 static inline boolean_t memorystatus_can_freeze_processes(void);
 static boolean_t memorystatus_can_freeze(boolean_t *memorystatus_freeze_swap_low);
 
@@ -252,13 +449,13 @@ static void memorystatus_freeze_thread(void *param __unused, wait_result_t wr __
 /* Thresholds */
 static unsigned int memorystatus_freeze_threshold = 0;
 
 /* Thresholds */
 static unsigned int memorystatus_freeze_threshold = 0;
 
-static unsigned int memorystatus_freeze_pages_min = FREEZE_PAGES_MIN;
-static unsigned int memorystatus_freeze_pages_max = FREEZE_PAGES_MAX;
-
-static unsigned int memorystatus_frozen_count = 0;
+static unsigned int memorystatus_freeze_pages_min = 0;
+static unsigned int memorystatus_freeze_pages_max = 0;
 
 static unsigned int memorystatus_freeze_suspended_threshold = FREEZE_SUSPENDED_THRESHOLD_DEFAULT;
 
 
 static unsigned int memorystatus_freeze_suspended_threshold = FREEZE_SUSPENDED_THRESHOLD_DEFAULT;
 
+static unsigned int memorystatus_freeze_daily_mb_max = FREEZE_DAILY_MB_MAX_DEFAULT;
+
 /* Stats */
 static uint64_t memorystatus_freeze_count = 0;
 static uint64_t memorystatus_freeze_pageouts = 0;
 /* Stats */
 static uint64_t memorystatus_freeze_count = 0;
 static uint64_t memorystatus_freeze_pageouts = 0;
@@ -271,17 +468,105 @@ static throttle_interval_t throttle_intervals[] = {
 
 static uint64_t memorystatus_freeze_throttle_count = 0;
 
 
 static uint64_t memorystatus_freeze_throttle_count = 0;
 
-static unsigned int memorystatus_suspended_count = 0;
 static unsigned int memorystatus_suspended_footprint_total = 0;
 
 static unsigned int memorystatus_suspended_footprint_total = 0;
 
+extern uint64_t vm_swap_get_free_space(void);
+
+static boolean_t memorystatus_freeze_update_throttle();
+
 #endif /* CONFIG_FREEZE */
 
 /* Debug */
 
 #endif /* CONFIG_FREEZE */
 
 /* Debug */
 
+extern struct knote *vm_find_knote_from_pid(pid_t, struct klist *);
+
 #if DEVELOPMENT || DEBUG
 
 #if CONFIG_JETSAM
 
 #if DEVELOPMENT || DEBUG
 
 #if CONFIG_JETSAM
 
+static void
+memorystatus_debug_dump_bucket_locked (unsigned int bucket_index)
+{
+       proc_t p = NULL;
+       uint32_t pages = 0;
+       uint32_t pages_in_mb = 0;
+       unsigned int b = bucket_index;
+       boolean_t traverse_all_buckets = FALSE;
+
+        if (bucket_index >= MEMSTAT_BUCKET_COUNT) {
+               traverse_all_buckets = TRUE;
+               b = 0;
+        } else {
+               traverse_all_buckets = FALSE;
+               b = bucket_index;
+       }
+
+       /*
+        * Missing from this dump is the value actually
+        * stored in the ledger... also, format could be better.
+        */
+        printf("memorystatus_debug_dump ***START***\n");
+       printf("bucket [pid] [pages/pages-mb] state [EP / RP] dirty deadline [C-limit / A-limit / IA-limit] name\n");
+       p = memorystatus_get_first_proc_locked(&b, traverse_all_buckets);
+       while (p) {
+               memorystatus_get_task_page_counts(p->task, &pages, NULL, NULL, NULL);
+               pages_in_mb = (pages * 4096) /1024 / 1024;
+                printf("%d     [%d]     [%d/%dMB] 0x%x [%d / %d] 0x%x %lld    [%d%s / %d%s / %d%s] %s\n", 
+                      b, p->p_pid, pages, pages_in_mb,
+                      p->p_memstat_state, p->p_memstat_effectivepriority, p->p_memstat_requestedpriority, p->p_memstat_dirty, p->p_memstat_idledeadline,
+                      p->p_memstat_memlimit,
+                      (p->p_memstat_state & P_MEMSTAT_FATAL_MEMLIMIT ? "F " : "NF"),
+                      p->p_memstat_memlimit_active, 
+                      (p->p_memstat_state & P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL ? "F " : "NF"),
+                      p->p_memstat_memlimit_inactive, 
+                      (p->p_memstat_state & P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL ? "F " : "NF"),
+                      (p->p_comm ? p->p_comm : "unknown"));
+               p = memorystatus_get_next_proc_locked(&b, p, traverse_all_buckets);
+        }
+        printf("memorystatus_debug_dump ***END***\n");
+}
+
+static int
+sysctl_memorystatus_debug_dump_bucket SYSCTL_HANDLER_ARGS
+{
+#pragma unused(oidp, arg2)
+        int bucket_index = 0;
+        int error;
+       error = SYSCTL_OUT(req, arg1, sizeof(int));
+       if (error || !req->newptr) {
+               return (error);
+       }
+        error = SYSCTL_IN(req, &bucket_index, sizeof(int));
+        if (error || !req->newptr) {
+                return (error);
+        }
+       if (bucket_index >= MEMSTAT_BUCKET_COUNT) {
+               /*
+                * All jetsam buckets will be dumped.
+                */
+        } else {
+               /*
+                * Only a single bucket will be dumped.
+                */
+       }
+
+       proc_list_lock();
+       memorystatus_debug_dump_bucket_locked(bucket_index);
+       proc_list_unlock();
+       memorystatus_debug_dump_this_bucket = bucket_index;
+       return (error);
+}
+
+/*
+ * Debug aid to look at jetsam buckets and proc jetsam fields.
+ *     Use this sysctl to act on a particular jetsam bucket.
+ *     Writing the sysctl triggers the dump.
+ *     Usage: sysctl kern.memorystatus_debug_dump_this_bucket=<bucket_index>
+ */
+
+SYSCTL_PROC(_kern, OID_AUTO, memorystatus_debug_dump_this_bucket, CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_debug_dump_this_bucket, 0, sysctl_memorystatus_debug_dump_bucket, "I", "");
+
+
 /* Debug aid to aid determination of limit */
 
 static int
 /* Debug aid to aid determination of limit */
 
 static int
@@ -291,7 +576,6 @@ sysctl_memorystatus_highwater_enable SYSCTL_HANDLER_ARGS
        proc_t p;
        unsigned int b = 0;
        int error, enable = 0;
        proc_t p;
        unsigned int b = 0;
        int error, enable = 0;
-       int32_t memlimit;
 
        error = SYSCTL_OUT(req, arg1, sizeof(int));
        if (error || !req->newptr) {
 
        error = SYSCTL_OUT(req, arg1, sizeof(int));
        if (error || !req->newptr) {
@@ -311,17 +595,35 @@ sysctl_memorystatus_highwater_enable SYSCTL_HANDLER_ARGS
 
        p = memorystatus_get_first_proc_locked(&b, TRUE);
        while (p) {
 
        p = memorystatus_get_first_proc_locked(&b, TRUE);
        while (p) {
+               boolean_t trigger_exception;
+
                if (enable) {
                if (enable) {
-                       if ((p->p_memstat_state & P_MEMSTAT_MEMLIMIT_BACKGROUND) && (p->p_memstat_effectivepriority >= JETSAM_PRIORITY_FOREGROUND)) {          
-                               memlimit = -1;
+                       /*
+                        * No need to consider P_MEMSTAT_MEMLIMIT_BACKGROUND anymore.
+                        * Background limits are described via the inactive limit slots.
+                        */
+
+                       if (proc_jetsam_state_is_active_locked(p) == TRUE) {
+                               CACHE_ACTIVE_LIMITS_LOCKED(p, trigger_exception);
                        } else {
                        } else {
-                               memlimit = p->p_memstat_memlimit;                               
+                               CACHE_INACTIVE_LIMITS_LOCKED(p, trigger_exception);
                        }
                        }
+
                } else {
                } else {
-                       memlimit = -1;
+                       /*
+                        * Disabling limits does not touch the stored variants.
+                        * Set the cached limit fields to system_wide defaults.
+                        */
+                       p->p_memstat_memlimit = -1;
+                       p->p_memstat_state |= P_MEMSTAT_FATAL_MEMLIMIT;
+                       trigger_exception = TRUE;
                }
                }
-               task_set_phys_footprint_limit_internal(p->task, (memlimit  > 0) ? memlimit : -1, NULL, TRUE);
-               
+
+               /*
+                * Enforce the cached limit by writing to the ledger.
+                */
+               task_set_phys_footprint_limit_internal(p->task, (p->p_memstat_memlimit > 0) ? p->p_memstat_memlimit: -1, NULL, trigger_exception);
+
                p = memorystatus_get_next_proc_locked(&b, p, TRUE);
        }
        
                p = memorystatus_get_next_proc_locked(&b, p, TRUE);
        }
        
@@ -330,16 +632,17 @@ sysctl_memorystatus_highwater_enable SYSCTL_HANDLER_ARGS
        proc_list_unlock();
 
        return 0;
        proc_list_unlock();
 
        return 0;
+
 }
 
 }
 
+SYSCTL_INT(_kern, OID_AUTO, memorystatus_idle_snapshot, CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_idle_snapshot, 0, "");
+
 SYSCTL_PROC(_kern, OID_AUTO, memorystatus_highwater_enabled, CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_highwater_enabled, 0, sysctl_memorystatus_highwater_enable, "I", "");
 
 SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages, CTLFLAG_RD|CTLFLAG_LOCKED, &memorystatus_available_pages, 0, "");
 SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages_critical, CTLFLAG_RD|CTLFLAG_LOCKED, &memorystatus_available_pages_critical, 0, "");
 SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages_critical_base, CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_available_pages_critical_base, 0, "");
 SYSCTL_PROC(_kern, OID_AUTO, memorystatus_highwater_enabled, CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_highwater_enabled, 0, sysctl_memorystatus_highwater_enable, "I", "");
 
 SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages, CTLFLAG_RD|CTLFLAG_LOCKED, &memorystatus_available_pages, 0, "");
 SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages_critical, CTLFLAG_RD|CTLFLAG_LOCKED, &memorystatus_available_pages_critical, 0, "");
 SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages_critical_base, CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_available_pages_critical_base, 0, "");
-#if !LATENCY_JETSAM
 SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages_critical_idle_offset, CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_available_pages_critical_idle_offset, 0, "");
 SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages_critical_idle_offset, CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_available_pages_critical_idle_offset, 0, "");
-#endif
 
 /* Diagnostic code */
 
 
 /* Diagnostic code */
 
@@ -418,34 +721,64 @@ SYSCTL_UINT(_kern, OID_AUTO, memorystatus_jetsam_policy_offset_pages_diagnostic,
 
 SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages_pressure, CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_available_pages_pressure, 0, "");
 
 
 SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages_pressure, CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_available_pages_pressure, 0, "");
 
-static int
-sysctl_memorystatus_vm_pressure_level SYSCTL_HANDLER_ARGS
-{
-#pragma unused(arg1, arg2, oidp)
-       int error = 0;
-
-       error = priv_check_cred(kauth_cred_get(), PRIV_VM_PRESSURE, 0);
-       if (error)
-               return (error);
-
-       return SYSCTL_OUT(req, &memorystatus_vm_pressure_level, sizeof(memorystatus_vm_pressure_level));
-}
 
 
-SYSCTL_PROC(_kern, OID_AUTO, memorystatus_vm_pressure_level, CTLTYPE_INT|CTLFLAG_RD|CTLFLAG_LOCKED|CTLFLAG_MASKED,
-    0, 0, &sysctl_memorystatus_vm_pressure_level, "I", "");
+/*
+ * This routine is used for targeted notifications
+ * regardless of system memory pressure.
+ * "memnote" is the current user.
+ */
 
 static int
 sysctl_memorystatus_vm_pressure_send SYSCTL_HANDLER_ARGS
 {
 #pragma unused(arg1, arg2)
 
 
 static int
 sysctl_memorystatus_vm_pressure_send SYSCTL_HANDLER_ARGS
 {
 #pragma unused(arg1, arg2)
 
-       int error, pid = 0;
+       int error = 0, pid = 0;
+       int ret = 0;
+       struct knote *kn = NULL;
+       boolean_t found_knote = FALSE;
 
        error = sysctl_handle_int(oidp, &pid, 0, req);
        if (error || !req->newptr)
                return (error);
 
 
        error = sysctl_handle_int(oidp, &pid, 0, req);
        if (error || !req->newptr)
                return (error);
 
-       return vm_dispatch_pressure_note_to_pid(pid, FALSE);
+       /*
+        * We inspect 3 lists here for targeted notifications:
+        * - memorystatus_klist
+        * - vm_pressure_klist
+        * - vm_pressure_dormant_klist
+        *
+        * The vm_pressure_* lists are tied to the old VM_PRESSURE
+        * notification mechanism. We intend to stop using that
+        * mechanism and, in turn, get rid of the 2 lists and
+        * vm_dispatch_pressure_note_to_pid() too.
+        */
+
+       memorystatus_klist_lock();
+
+       SLIST_FOREACH(kn, &memorystatus_klist, kn_selnext) {
+               proc_t knote_proc = kn->kn_kq->kq_p;
+               pid_t knote_pid = knote_proc->p_pid;
+
+               if (knote_pid == pid) {
+                       /*
+                        * Forcibly send this pid a "warning" memory pressure notification.
+                        */
+                       kn->kn_fflags = NOTE_MEMORYSTATUS_PRESSURE_WARN;
+                       found_knote = TRUE;
+               }
+       }
+
+       if (found_knote) {
+               KNOTE(&memorystatus_klist, 0);
+               ret = 0;
+       } else {
+               ret = vm_dispatch_pressure_note_to_pid(pid, FALSE);
+       }
+
+       memorystatus_klist_unlock();
+
+       return ret;
 }
 
 SYSCTL_PROC(_kern, OID_AUTO, memorystatus_vm_pressure_send, CTLTYPE_INT|CTLFLAG_WR|CTLFLAG_LOCKED|CTLFLAG_MASKED,
 }
 
 SYSCTL_PROC(_kern, OID_AUTO, memorystatus_vm_pressure_send, CTLTYPE_INT|CTLFLAG_WR|CTLFLAG_LOCKED|CTLFLAG_MASKED,
@@ -455,10 +788,10 @@ SYSCTL_PROC(_kern, OID_AUTO, memorystatus_vm_pressure_send, CTLTYPE_INT|CTLFLAG_
 
 #endif /* CONFIG_JETSAM */
 
 
 #endif /* CONFIG_JETSAM */
 
-#endif /* DEVELOPMENT || DEBUG */
-
 #if CONFIG_FREEZE
 
 #if CONFIG_FREEZE
 
+SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_daily_mb_max, CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_freeze_daily_mb_max, 0, "");
+
 SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_threshold, CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_freeze_threshold, 0, "");
 
 SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_pages_min, CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_freeze_pages_min, 0, "");
 SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_threshold, CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_freeze_threshold, 0, "");
 
 SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_pages_min, CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_freeze_pages_min, 0, "");
@@ -473,41 +806,74 @@ boolean_t memorystatus_freeze_throttle_enabled = TRUE;
 SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_throttle_enabled, CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_freeze_throttle_enabled, 0, "");
 
 /* 
 SYSCTL_UINT(_kern, OID_AUTO, memorystatus_freeze_throttle_enabled, CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_freeze_throttle_enabled, 0, "");
 
 /* 
- * Enabled via: <rdar://problem/13248767> Enable the sysctl_memorystatus_freeze/thaw sysctls on Release KC
- * 
- * TODO: Manual trigger of freeze and thaw for dev / debug kernels only.
- * <rdar://problem/13248795> Disable/restrict the sysctl_memorystatus_freeze/thaw sysctls on Release KC
+ * Manual trigger of freeze and thaw for dev / debug kernels only.
  */
 static int
 sysctl_memorystatus_freeze SYSCTL_HANDLER_ARGS
 {
 #pragma unused(arg1, arg2)
  */
 static int
 sysctl_memorystatus_freeze SYSCTL_HANDLER_ARGS
 {
 #pragma unused(arg1, arg2)
-
        int error, pid = 0;
        proc_t p;
 
        int error, pid = 0;
        proc_t p;
 
+       if (memorystatus_freeze_enabled == FALSE) {
+               return ENOTSUP;
+       }
+
        error = sysctl_handle_int(oidp, &pid, 0, req);
        if (error || !req->newptr)
                return (error);
 
        error = sysctl_handle_int(oidp, &pid, 0, req);
        if (error || !req->newptr)
                return (error);
 
+       if (pid == 2) {
+               vm_pageout_anonymous_pages();
+
+               return 0;
+       }
+
+       lck_mtx_lock(&freezer_mutex);
+
        p = proc_find(pid);
        if (p != NULL) {
                uint32_t purgeable, wired, clean, dirty;
                boolean_t shared;
                uint32_t max_pages = 0;
 
        p = proc_find(pid);
        if (p != NULL) {
                uint32_t purgeable, wired, clean, dirty;
                boolean_t shared;
                uint32_t max_pages = 0;
 
-               if (DEFAULT_FREEZER_IS_ACTIVE || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE) {
-                       max_pages = MIN(default_pager_swap_pages_free(), memorystatus_freeze_pages_max);
+               if (DEFAULT_FREEZER_IS_ACTIVE || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPBACKED) {
+
+                       unsigned int avail_swap_space = 0; /* in pages. */
+
+                       if (DEFAULT_FREEZER_IS_ACTIVE) {
+                               /*
+                                * Freezer backed by default pager and swap file(s).
+                                */
+                               avail_swap_space = default_pager_swap_pages_free();
+                       } else {
+                               /*
+                                * Freezer backed by the compressor and swap file(s)
+                                * while will hold compressed data.
+                                */
+                               avail_swap_space = vm_swap_get_free_space() / PAGE_SIZE_64;
+                       }
+
+                       max_pages = MIN(avail_swap_space, memorystatus_freeze_pages_max);
+
                } else {
                } else {
+                       /*
+                        * We only have the compressor without any swap.
+                        */
                        max_pages = UINT32_MAX - 1;
                }
                        max_pages = UINT32_MAX - 1;
                }
+
                error = task_freeze(p->task, &purgeable, &wired, &clean, &dirty, max_pages, &shared, FALSE);
                proc_rele(p);
 
                if (error)
                        error = EIO;
                error = task_freeze(p->task, &purgeable, &wired, &clean, &dirty, max_pages, &shared, FALSE);
                proc_rele(p);
 
                if (error)
                        error = EIO;
+
+               lck_mtx_unlock(&freezer_mutex);
                return error;
        }
                return error;
        }
+
+       lck_mtx_unlock(&freezer_mutex);
        return EINVAL;
 }
 
        return EINVAL;
 }
 
@@ -522,6 +888,10 @@ sysctl_memorystatus_available_pages_thaw SYSCTL_HANDLER_ARGS
        int error, pid = 0;
        proc_t p;
 
        int error, pid = 0;
        proc_t p;
 
+       if (memorystatus_freeze_enabled == FALSE) {
+               return ENOTSUP;
+       }
+
        error = sysctl_handle_int(oidp, &pid, 0, req);
        if (error || !req->newptr)
                return (error);
        error = sysctl_handle_int(oidp, &pid, 0, req);
        if (error || !req->newptr)
                return (error);
@@ -544,11 +914,126 @@ SYSCTL_PROC(_kern, OID_AUTO, memorystatus_thaw, CTLTYPE_INT|CTLFLAG_WR|CTLFLAG_L
 
 #endif /* CONFIG_FREEZE */
 
 
 #endif /* CONFIG_FREEZE */
 
+#endif /* DEVELOPMENT || DEBUG */
+
 extern kern_return_t kernel_thread_start_priority(thread_continue_t continuation,
                                                   void *parameter,
                                                   integer_t priority,
                                                   thread_t *new_thread);
 
 extern kern_return_t kernel_thread_start_priority(thread_continue_t continuation,
                                                   void *parameter,
                                                   integer_t priority,
                                                   thread_t *new_thread);
 
+#if CONFIG_JETSAM
+/*
+ * Picks the sorting routine for a given jetsam priority band.
+ *
+ * Input:
+ *     bucket_index - jetsam priority band to be sorted.
+ *     sort_order - JETSAM_SORT_xxx from kern_memorystatus.h
+ *             Currently sort_order is only meaningful when handling
+ *             coalitions.
+ *
+ * Return: 
+ *     0     on success
+ *     non-0 on failure
+ */
+static int memorystatus_sort_bucket(unsigned int bucket_index, int sort_order)
+{
+       int coal_sort_order;
+
+       /*
+        * Verify the jetsam priority
+        */
+        if (bucket_index >= MEMSTAT_BUCKET_COUNT) {
+               return(EINVAL);
+        }
+
+#if DEVELOPMENT || DEBUG
+        if (sort_order == JETSAM_SORT_DEFAULT) {
+               coal_sort_order = COALITION_SORT_DEFAULT;
+       } else {
+               coal_sort_order = sort_order;           /* only used for testing scenarios */
+       }
+#else
+       /* Verify default */
+        if (sort_order == JETSAM_SORT_DEFAULT) {
+               coal_sort_order = COALITION_SORT_DEFAULT;
+       } else {
+               return(EINVAL);
+       }
+#endif
+
+       proc_list_lock();
+       switch (bucket_index) {
+       case JETSAM_PRIORITY_FOREGROUND:
+               if (memorystatus_sort_by_largest_coalition_locked(bucket_index, coal_sort_order) == 0) {
+                       /*
+                        * Fall back to per process sorting when zero coalitions are found.
+                        */
+                       memorystatus_sort_by_largest_process_locked(bucket_index);
+               }
+               break;
+       default:
+               memorystatus_sort_by_largest_process_locked(bucket_index);
+               break;
+       }
+       proc_list_unlock();
+       
+        return(0);
+}
+
+/*
+ * Sort processes by size for a single jetsam bucket.
+ */
+
+static void memorystatus_sort_by_largest_process_locked(unsigned int bucket_index)
+{
+       proc_t p = NULL, insert_after_proc = NULL, max_proc = NULL;
+       proc_t next_p = NULL, prev_max_proc = NULL;
+       uint32_t pages = 0, max_pages = 0;
+       memstat_bucket_t *current_bucket;
+               
+       if (bucket_index >= MEMSTAT_BUCKET_COUNT) {
+               return;
+       }
+               
+       current_bucket = &memstat_bucket[bucket_index];
+
+       p = TAILQ_FIRST(&current_bucket->list);
+
+       while (p) {
+               memorystatus_get_task_page_counts(p->task, &pages, NULL, NULL, NULL);
+               max_pages = pages;
+               max_proc = p;
+               prev_max_proc = p;
+               
+               while ((next_p = TAILQ_NEXT(p, p_memstat_list)) != NULL) {
+                       /* traversing list until we find next largest process */
+                       p=next_p;
+                       memorystatus_get_task_page_counts(p->task, &pages, NULL, NULL, NULL);
+                       if (pages > max_pages) {
+                               max_pages = pages;
+                               max_proc = p;
+                       }
+               }
+
+               if (prev_max_proc != max_proc) {
+                       /* found a larger process, place it in the list */
+                       TAILQ_REMOVE(&current_bucket->list, max_proc, p_memstat_list);
+                       if (insert_after_proc == NULL) {
+                               TAILQ_INSERT_HEAD(&current_bucket->list, max_proc, p_memstat_list);
+                       } else {
+                               TAILQ_INSERT_AFTER(&current_bucket->list, insert_after_proc, max_proc, p_memstat_list);
+                       }
+                       prev_max_proc = max_proc;
+               }
+
+               insert_after_proc = max_proc;
+
+               p = TAILQ_NEXT(max_proc, p_memstat_list);
+       }
+}
+
+#endif /* CONFIG_JETSAM */
+
 static proc_t memorystatus_get_first_proc_locked(unsigned int *bucket_index, boolean_t search) {
        memstat_bucket_t *current_bucket;
        proc_t next_p;
 static proc_t memorystatus_get_first_proc_locked(unsigned int *bucket_index, boolean_t search) {
        memstat_bucket_t *current_bucket;
        proc_t next_p;
@@ -593,6 +1078,11 @@ memorystatus_init(void)
        kern_return_t result;
        int i;
 
        kern_return_t result;
        int i;
 
+#if CONFIG_FREEZE
+       memorystatus_freeze_pages_min = FREEZE_PAGES_MIN;
+       memorystatus_freeze_pages_max = FREEZE_PAGES_MAX;
+#endif
+
        nanoseconds_to_absolutetime((uint64_t)DEFERRED_IDLE_EXIT_TIME_SECS * NSEC_PER_SEC, &memorystatus_idle_delay_time);
        
        /* Init buckets */
        nanoseconds_to_absolutetime((uint64_t)DEFERRED_IDLE_EXIT_TIME_SECS * NSEC_PER_SEC, &memorystatus_idle_delay_time);
        
        /* Init buckets */
@@ -616,11 +1106,11 @@ memorystatus_init(void)
        assert(freeze_threshold_percentage < 100);
        
 #if CONFIG_JETSAM
        assert(freeze_threshold_percentage < 100);
        
 #if CONFIG_JETSAM
+       /* device tree can request to take snapshots for idle-exit kills by default */
+       PE_get_default("kern.jetsam_idle_snapshot", &memorystatus_idle_snapshot, sizeof(memorystatus_idle_snapshot));
+
        memorystatus_delta = delta_percentage * atop_64(max_mem) / 100;
        memorystatus_delta = delta_percentage * atop_64(max_mem) / 100;
-#if !LATENCY_JETSAM
        memorystatus_available_pages_critical_idle_offset = idle_offset_percentage * atop_64(max_mem) / 100;
        memorystatus_available_pages_critical_idle_offset = idle_offset_percentage * atop_64(max_mem) / 100;
-#endif
-
        memorystatus_available_pages_critical_base = (critical_threshold_percentage / delta_percentage) * memorystatus_delta;
        
        memorystatus_jetsam_snapshot_max = maxproc;
        memorystatus_available_pages_critical_base = (critical_threshold_percentage / delta_percentage) * memorystatus_delta;
        
        memorystatus_jetsam_snapshot_max = maxproc;
@@ -631,8 +1121,21 @@ memorystatus_init(void)
                panic("Could not allocate memorystatus_jetsam_snapshot");
        }
 
                panic("Could not allocate memorystatus_jetsam_snapshot");
        }
 
+       nanoseconds_to_absolutetime((uint64_t)JETSAM_SNAPSHOT_TIMEOUT_SECS * NSEC_PER_SEC, &memorystatus_jetsam_snapshot_timeout);
+
+       memset(&memorystatus_at_boot_snapshot, 0, sizeof(memorystatus_jetsam_snapshot_t));
+
        /* No contention at this point */
        memorystatus_update_levels_locked(FALSE);
        /* No contention at this point */
        memorystatus_update_levels_locked(FALSE);
+
+       /* Jetsam Loop Detection */
+       if (max_mem <= (512 * 1024 * 1024)) {
+               /* 512 MB devices */
+               memorystatus_jld_eval_period_msecs = 8000;      /* 8000 msecs == 8 second window */
+       } else {
+               /* 1GB and larger devices */
+               memorystatus_jld_eval_period_msecs = 6000;      /* 6000 msecs == 6 second window */
+       }
 #endif
        
 #if CONFIG_FREEZE
 #endif
        
 #if CONFIG_FREEZE
@@ -651,10 +1154,29 @@ memorystatus_init(void)
 extern void
 vm_wake_compactor_swapper(void);
 
 extern void
 vm_wake_compactor_swapper(void);
 
+/*
+ * The jetsam no frills kill call
+ *     Return: 0 on success
+ *             error code on failure (EINVAL...)
+ */
+static int
+jetsam_do_kill(proc_t p, int jetsam_flags) {
+       int error = 0;
+       error = exit1_internal(p, W_EXITCODE(0, SIGKILL), (int *)NULL, FALSE, FALSE, jetsam_flags);
+       return(error);
+}
+
+/*
+ * Wrapper for processes exiting with memorystatus details
+ */
 static boolean_t
 memorystatus_do_kill(proc_t p, uint32_t cause) {
 
 static boolean_t
 memorystatus_do_kill(proc_t p, uint32_t cause) {
 
-       int retval = 0;
+       int error = 0;
+       __unused pid_t victim_pid = p->p_pid;
+
+       KERNEL_DEBUG_CONSTANT( (BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_DO_KILL)) | DBG_FUNC_START,
+                              victim_pid, cause, vm_page_free_count, 0, 0);
 
 #if CONFIG_JETSAM && (DEVELOPMENT || DEBUG)
        if (memorystatus_jetsam_panic_debug & (1 << cause)) {
 
 #if CONFIG_JETSAM && (DEVELOPMENT || DEBUG)
        if (memorystatus_jetsam_panic_debug & (1 << cause)) {
@@ -669,16 +1191,18 @@ memorystatus_do_kill(proc_t p, uint32_t cause) {
                case kMemorystatusKilledVnodes:                 jetsam_flags |= P_JETSAM_VNODE; break;
                case kMemorystatusKilledVMPageShortage:         jetsam_flags |= P_JETSAM_VMPAGESHORTAGE; break;
                case kMemorystatusKilledVMThrashing:            jetsam_flags |= P_JETSAM_VMTHRASHING; break;
                case kMemorystatusKilledVnodes:                 jetsam_flags |= P_JETSAM_VNODE; break;
                case kMemorystatusKilledVMPageShortage:         jetsam_flags |= P_JETSAM_VMPAGESHORTAGE; break;
                case kMemorystatusKilledVMThrashing:            jetsam_flags |= P_JETSAM_VMTHRASHING; break;
+               case kMemorystatusKilledFCThrashing:            jetsam_flags |= P_JETSAM_FCTHRASHING; break;
                case kMemorystatusKilledPerProcessLimit:        jetsam_flags |= P_JETSAM_PID; break;
                case kMemorystatusKilledIdleExit:               jetsam_flags |= P_JETSAM_IDLEEXIT; break;
        }
                case kMemorystatusKilledPerProcessLimit:        jetsam_flags |= P_JETSAM_PID; break;
                case kMemorystatusKilledIdleExit:               jetsam_flags |= P_JETSAM_IDLEEXIT; break;
        }
-       retval = exit1_internal(p, W_EXITCODE(0, SIGKILL), (int *)NULL, FALSE, FALSE, jetsam_flags);
+       error = jetsam_do_kill(p, jetsam_flags);
 
 
-       if (COMPRESSED_PAGER_IS_ACTIVE || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE) {
-               vm_wake_compactor_swapper();
-       }
-       
-       return (retval == 0);
+       KERNEL_DEBUG_CONSTANT( (BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_DO_KILL)) | DBG_FUNC_END, 
+                              victim_pid, cause, vm_page_free_count, error, 0);
+
+       vm_wake_compactor_swapper();
+
+       return (error == 0);
 }
 
 /*
 }
 
 /*
@@ -721,11 +1245,12 @@ memorystatus_perform_idle_demotion(__unused void *spare1, __unused void *spare2)
                if (current_time >= p->p_memstat_idledeadline) {
 #if DEBUG || DEVELOPMENT
                        if (!(p->p_memstat_dirty & P_DIRTY_MARKED)) {
                if (current_time >= p->p_memstat_idledeadline) {
 #if DEBUG || DEVELOPMENT
                        if (!(p->p_memstat_dirty & P_DIRTY_MARKED)) {
-                               printf("memorystatus_perform_idle_demotion: moving process %d to idle band, but never dirtied (0x%x)!\n", p->p_pid, p->p_memstat_dirty);
+                               printf("memorystatus_perform_idle_demotion: moving process %d [%s] to idle band, but never dirtied (0x%x)!\n",
+                                       p->p_pid, (p->p_comm ? p->p_comm : "(unknown)"), p->p_memstat_dirty);
                        }
 #endif
                        memorystatus_invalidate_idle_demotion_locked(p, TRUE);
                        }
 #endif
                        memorystatus_invalidate_idle_demotion_locked(p, TRUE);
-                       memorystatus_update_priority_locked(p, JETSAM_PRIORITY_IDLE);
+                       memorystatus_update_priority_locked(p, JETSAM_PRIORITY_IDLE, false);
                        
                        // The prior process has moved out of the demotion bucket, so grab the new head and continue
                        p = TAILQ_FIRST(&demotion_bucket->list);
                        
                        // The prior process has moved out of the demotion bucket, so grab the new head and continue
                        p = TAILQ_FIRST(&demotion_bucket->list);
@@ -746,35 +1271,53 @@ memorystatus_perform_idle_demotion(__unused void *spare1, __unused void *spare2)
 static void
 memorystatus_schedule_idle_demotion_locked(proc_t p, boolean_t set_state) 
 {      
 static void
 memorystatus_schedule_idle_demotion_locked(proc_t p, boolean_t set_state) 
 {      
-       MEMORYSTATUS_DEBUG(1, "memorystatus_schedule_idle_demotion_locked: scheduling demotion to idle band for process %d (dirty:0x%x, set_state %d, demotions %d).\n", 
+       boolean_t present_in_deferred_bucket = FALSE;
+       
+       if (p->p_memstat_effectivepriority == JETSAM_PRIORITY_IDLE_DEFERRED) {
+               present_in_deferred_bucket = TRUE;
+       }
+
+       MEMORYSTATUS_DEBUG(1, "memorystatus_schedule_idle_demotion_locked: scheduling demotion to idle band for pid %d (dirty:0x%x, set_state %d, demotions %d).\n", 
            p->p_pid, p->p_memstat_dirty, set_state, memorystatus_scheduled_idle_demotions);
 
            p->p_pid, p->p_memstat_dirty, set_state, memorystatus_scheduled_idle_demotions);
 
-       assert((p->p_memstat_dirty & (P_DIRTY_IDLE_EXIT_ENABLED|P_DIRTY_DEFER_IN_PROGRESS)) == (P_DIRTY_IDLE_EXIT_ENABLED|P_DIRTY_DEFER_IN_PROGRESS));
+       assert((p->p_memstat_dirty & P_DIRTY_IDLE_EXIT_ENABLED) == P_DIRTY_IDLE_EXIT_ENABLED);
 
        if (set_state) {
                assert(p->p_memstat_idledeadline == 0);
 
        if (set_state) {
                assert(p->p_memstat_idledeadline == 0);
+               p->p_memstat_dirty |= P_DIRTY_DEFER_IN_PROGRESS;
                p->p_memstat_idledeadline = mach_absolute_time() + memorystatus_idle_delay_time;
        }
        
                p->p_memstat_idledeadline = mach_absolute_time() + memorystatus_idle_delay_time;
        }
        
-       assert(p->p_memstat_idledeadline);
+       assert(p->p_memstat_idledeadline);
        
        
-       memorystatus_scheduled_idle_demotions++;
+       if (present_in_deferred_bucket == FALSE) {
+               memorystatus_scheduled_idle_demotions++;
+       }
 }
 
 static void
 memorystatus_invalidate_idle_demotion_locked(proc_t p, boolean_t clear_state) 
 {
 }
 
 static void
 memorystatus_invalidate_idle_demotion_locked(proc_t p, boolean_t clear_state) 
 {
-       MEMORYSTATUS_DEBUG(1, "memorystatus_invalidate_idle_demotion(): invalidating demotion to idle band for process %d (clear_state %d, demotions %d).\n", 
+       boolean_t present_in_deferred_bucket = FALSE;
+       
+       if (p->p_memstat_effectivepriority == JETSAM_PRIORITY_IDLE_DEFERRED) {
+               present_in_deferred_bucket = TRUE;
+               assert(p->p_memstat_idledeadline);
+       }
+
+       MEMORYSTATUS_DEBUG(1, "memorystatus_invalidate_idle_demotion(): invalidating demotion to idle band for pid %d (clear_state %d, demotions %d).\n", 
            p->p_pid, clear_state, memorystatus_scheduled_idle_demotions);
     
            p->p_pid, clear_state, memorystatus_scheduled_idle_demotions);
     
-       assert(p->p_memstat_idledeadline);
  
        if (clear_state) {
                p->p_memstat_idledeadline = 0;
                p->p_memstat_dirty &= ~P_DIRTY_DEFER_IN_PROGRESS;
        }
        
  
        if (clear_state) {
                p->p_memstat_idledeadline = 0;
                p->p_memstat_dirty &= ~P_DIRTY_DEFER_IN_PROGRESS;
        }
        
-       memorystatus_scheduled_idle_demotions--;
+       if (present_in_deferred_bucket == TRUE) {
+               memorystatus_scheduled_idle_demotions--;
+       }
+
        assert(memorystatus_scheduled_idle_demotions >= 0);
 }
 
        assert(memorystatus_scheduled_idle_demotions >= 0);
 }
 
@@ -791,11 +1334,12 @@ memorystatus_reschedule_idle_demotion_locked(void) {
                proc_t p;
                demotion_bucket = &memstat_bucket[JETSAM_PRIORITY_IDLE_DEFERRED];
                p = TAILQ_FIRST(&demotion_bucket->list);
                proc_t p;
                demotion_bucket = &memstat_bucket[JETSAM_PRIORITY_IDLE_DEFERRED];
                p = TAILQ_FIRST(&demotion_bucket->list);
-               assert(p && p->p_memstat_idledeadline);
                
                
-               if (memstat_idle_demotion_deadline != p->p_memstat_idledeadline){
-                       thread_call_enter_delayed(memorystatus_idle_demotion_call, p->p_memstat_idledeadline);
-                       memstat_idle_demotion_deadline = p->p_memstat_idledeadline;
+               assert(p && p->p_memstat_idledeadline);
+               
+               if (memstat_idle_demotion_deadline != p->p_memstat_idledeadline){
+                       thread_call_enter_delayed(memorystatus_idle_demotion_call, p->p_memstat_idledeadline);
+                       memstat_idle_demotion_deadline = p->p_memstat_idledeadline;
                }
        }
 }
                }
        }
 }
@@ -809,7 +1353,7 @@ memorystatus_add(proc_t p, boolean_t locked)
 {
        memstat_bucket_t *bucket;
        
 {
        memstat_bucket_t *bucket;
        
-       MEMORYSTATUS_DEBUG(1, "memorystatus_list_add(): adding process %d with priority %d.\n", p->pid, priority);
+       MEMORYSTATUS_DEBUG(1, "memorystatus_list_add(): adding pid %d with priority %d.\n", p->p_pid, p->p_memstat_effectivepriority);
    
        if (!locked) {
                proc_list_lock();
    
        if (!locked) {
                proc_list_lock();
@@ -822,6 +1366,10 @@ memorystatus_add(proc_t p, boolean_t locked)
        
        bucket = &memstat_bucket[p->p_memstat_effectivepriority];
        
        
        bucket = &memstat_bucket[p->p_memstat_effectivepriority];
        
+       if (p->p_memstat_effectivepriority == JETSAM_PRIORITY_IDLE_DEFERRED) {
+               assert(bucket->count == memorystatus_scheduled_idle_demotions);
+       }
+
        TAILQ_INSERT_TAIL(&bucket->list, p, p_memstat_list);
        bucket->count++;
 
        TAILQ_INSERT_TAIL(&bucket->list, p, p_memstat_list);
        bucket->count++;
 
@@ -837,8 +1385,16 @@ exit:
        return 0;
 }
 
        return 0;
 }
 
+/*
+ * Description:
+ *     Moves a process from one jetsam bucket to another.
+ *     which changes the LRU position of the process.
+ *
+ *     Monitors transition between buckets and if necessary
+ *     will update cached memory limits accordingly.
+ */
 static void
 static void
-memorystatus_update_priority_locked(proc_t p, int priority)
+memorystatus_update_priority_locked(proc_t p, int priority, boolean_t head_insert)
 {
        memstat_bucket_t *old_bucket, *new_bucket;
        
 {
        memstat_bucket_t *old_bucket, *new_bucket;
        
@@ -849,42 +1405,151 @@ memorystatus_update_priority_locked(proc_t p, int priority)
                return;
        }
        
                return;
        }
        
-       MEMORYSTATUS_DEBUG(1, "memorystatus_update_priority_locked(): setting process %d to priority %d\n", p->p_pid, priority);        
+       MEMORYSTATUS_DEBUG(1, "memorystatus_update_priority_locked(): setting pid %d to priority %d, inserting at %s\n",
+                          p->p_pid, priority, head_insert ? "head" : "tail");
 
        old_bucket = &memstat_bucket[p->p_memstat_effectivepriority];
 
        old_bucket = &memstat_bucket[p->p_memstat_effectivepriority];
+       if (p->p_memstat_effectivepriority == JETSAM_PRIORITY_IDLE_DEFERRED) {
+               assert(old_bucket->count == (memorystatus_scheduled_idle_demotions + 1));
+       }
+
        TAILQ_REMOVE(&old_bucket->list, p, p_memstat_list);
        old_bucket->count--;
        
        new_bucket = &memstat_bucket[priority]; 
        TAILQ_REMOVE(&old_bucket->list, p, p_memstat_list);
        old_bucket->count--;
        
        new_bucket = &memstat_bucket[priority]; 
-       TAILQ_INSERT_TAIL(&new_bucket->list, p, p_memstat_list);
+       if (head_insert)
+               TAILQ_INSERT_HEAD(&new_bucket->list, p, p_memstat_list);
+       else
+               TAILQ_INSERT_TAIL(&new_bucket->list, p, p_memstat_list);
        new_bucket->count++;
        new_bucket->count++;
-       
+
 #if CONFIG_JETSAM
 #if CONFIG_JETSAM
-       if (memorystatus_highwater_enabled && (p->p_memstat_state & P_MEMSTAT_MEMLIMIT_BACKGROUND)) {        
-               if (((priority >= JETSAM_PRIORITY_FOREGROUND) && (p->p_memstat_effectivepriority < JETSAM_PRIORITY_FOREGROUND)) ||
-                       ((priority < JETSAM_PRIORITY_FOREGROUND) && (p->p_memstat_effectivepriority >= JETSAM_PRIORITY_FOREGROUND))) {            
-                       int32_t memlimit = (priority >= JETSAM_PRIORITY_FOREGROUND) ? -1 : p->p_memstat_memlimit;
-                       task_set_phys_footprint_limit_internal(p->task, (memlimit  > 0) ? memlimit : -1, NULL, TRUE);
+       if (memorystatus_highwater_enabled) {
+               boolean_t trigger_exception;
+
+               /* 
+                * If cached limit data is updated, then the limits
+                * will be enforced by writing to the ledgers.
+                */
+               boolean_t ledger_update_needed = TRUE;
+
+               /*
+                * No need to consider P_MEMSTAT_MEMLIMIT_BACKGROUND anymore.
+                * Background limits are described via the inactive limit slots.
+                *
+                * Here, we must update the cached memory limit if the task 
+                * is transitioning between:
+                *      active <--> inactive
+                *      FG     <-->       BG
+                * but:
+                *      dirty  <-->    clean   is ignored
+                *
+                * We bypass processes that have opted into dirty tracking because
+                * a move between buckets does not imply a transition between the
+                * dirty <--> clean state.
+                * Setting limits on processes opted into dirty tracking is handled
+                * in memorystatus_dirty_set() where the transition is very clear.
+                */
+
+               if (p->p_memstat_dirty & P_DIRTY_TRACK) {
+
+                       ledger_update_needed = FALSE;
+
+               } else if ((priority >= JETSAM_PRIORITY_FOREGROUND) && (p->p_memstat_effectivepriority < JETSAM_PRIORITY_FOREGROUND)) {
+                       /*
+                        *      inactive --> active
+                        *      BG       -->     FG
+                        *      assign active state
+                        */
+                       CACHE_ACTIVE_LIMITS_LOCKED(p, trigger_exception);
+
+               } else if ((priority < JETSAM_PRIORITY_FOREGROUND) && (p->p_memstat_effectivepriority >= JETSAM_PRIORITY_FOREGROUND)) {
+                       /*
+                        *      active --> inactive
+                        *      FG     -->       BG
+                        *      assign inactive state
+                        */
+                       CACHE_INACTIVE_LIMITS_LOCKED(p, trigger_exception);
+               } else {
+                       /*
+                        * The transition between jetsam priority buckets apparently did
+                        * not affect active/inactive state.
+                        * This is not unusual... especially during startup when
+                        * processes are getting established in their respective bands.
+                        */
+                       ledger_update_needed = FALSE;
+               }
+
+               /*
+                * Enforce the new limits by writing to the ledger
+                */
+               if (ledger_update_needed) {
+                       task_set_phys_footprint_limit_internal(p->task, (p->p_memstat_memlimit > 0) ? p->p_memstat_memlimit : -1, NULL, trigger_exception);
+
+                       MEMORYSTATUS_DEBUG(3, "memorystatus_update_priority_locked: new limit on pid %d (%dMB %s) priority old --> new (%d --> %d) dirty?=0x%x %s\n",
+                                          p->p_pid, (p->p_memstat_memlimit > 0 ? p->p_memstat_memlimit : -1),
+                                          (p->p_memstat_state & P_MEMSTAT_FATAL_MEMLIMIT ? "F " : "NF"), p->p_memstat_effectivepriority, priority, p->p_memstat_dirty,
+                                          (p->p_memstat_dirty ? ((p->p_memstat_dirty & P_DIRTY) ? "isdirty" : "isclean") : ""));
                }
        }
                }
        }
-#endif
+
+#endif  /* CONFIG_JETSAM */
        
        p->p_memstat_effectivepriority = priority;
        
        memorystatus_check_levels_locked();
 }
 
        
        p->p_memstat_effectivepriority = priority;
        
        memorystatus_check_levels_locked();
 }
 
+/*
+ *
+ * Description: Update the jetsam priority and memory limit attributes for a given process.
+ *
+ * Parameters:
+ *     p       init this process's jetsam information.
+ *     priority          The jetsam priority band
+ *     user_data         user specific data, unused by the kernel
+ *     effective         guards against race if process's update already occurred
+ *     update_memlimit   When true we know this is the init step via the posix_spawn path.
+ *
+ *     memlimit_active   Value in megabytes; The monitored footprint level while the
+ *                       process is active.  Exceeding it may result in termination
+ *                       based on it's associated fatal flag.
+ *
+ *     memlimit_active_is_fatal  When a process is active and exceeds its memory footprint,
+ *                               this describes whether or not it should be immediately fatal.
+ *
+ *     memlimit_inactive Value in megabytes; The monitored footprint level while the
+ *                       process is inactive.  Exceeding it may result in termination
+ *                       based on it's associated fatal flag.
+ *
+ *     memlimit_inactive_is_fatal  When a process is inactive and exceeds its memory footprint,
+ *                                 this describes whether or not it should be immediatly fatal.
+ *
+ *     memlimit_background     This process has a high-water-mark while in the background.
+ *                             No longer meaningful.  Background limits are described via
+ *                             the inactive slots.  Flag is ignored.
+ *
+ *
+ * Returns:     0      Success
+ *             non-0   Failure
+ */
+
 int
 int
-memorystatus_update(proc_t p, int priority, uint64_t user_data, boolean_t effective, boolean_t update_memlimit, int32_t memlimit, boolean_t memlimit_background)
+memorystatus_update(proc_t p, int priority, uint64_t user_data, boolean_t effective, boolean_t update_memlimit,
+                   int32_t memlimit_active,   boolean_t memlimit_active_is_fatal,
+                    int32_t memlimit_inactive, boolean_t memlimit_inactive_is_fatal,
+                    __unused boolean_t memlimit_background)
 {
        int ret;
 {
        int ret;
+       boolean_t head_insert = false;
        
 #if !CONFIG_JETSAM
        
 #if !CONFIG_JETSAM
-#pragma unused(update_memlimit, memlimit, memlimit_background)
-#endif
+#pragma unused(update_memlimit, memlimit_active, memlimit_inactive)
+#pragma unused(memlimit_active_is_fatal, memlimit_inactive_is_fatal)
+#endif /* !CONFIG_JETSAM */
+
+       MEMORYSTATUS_DEBUG(1, "memorystatus_update: changing pid %d: priority %d, user_data 0x%llx\n", p->p_pid, priority, user_data);
 
 
-       MEMORYSTATUS_DEBUG(1, "memorystatus_update: changing process %d: priority %d, user_data 0x%llx\n", p->p_pid, priority, user_data);
-    
        KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_UPDATE) | DBG_FUNC_START, p->p_pid, priority, user_data, effective, 0);
        
        if (priority == -1) {
        KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_UPDATE) | DBG_FUNC_START, p->p_pid, priority, user_data, effective, 0);
        
        if (priority == -1) {
@@ -893,12 +1558,16 @@ memorystatus_update(proc_t p, int priority, uint64_t user_data, boolean_t effect
        } else if (priority == JETSAM_PRIORITY_IDLE_DEFERRED) {
                /* JETSAM_PRIORITY_IDLE_DEFERRED is reserved for internal use; if requested, adjust to JETSAM_PRIORITY_IDLE. */
                priority = JETSAM_PRIORITY_IDLE;                
        } else if (priority == JETSAM_PRIORITY_IDLE_DEFERRED) {
                /* JETSAM_PRIORITY_IDLE_DEFERRED is reserved for internal use; if requested, adjust to JETSAM_PRIORITY_IDLE. */
                priority = JETSAM_PRIORITY_IDLE;                
+       } else if (priority == JETSAM_PRIORITY_IDLE_HEAD) {
+               /* JETSAM_PRIORITY_IDLE_HEAD inserts at the head of the idle queue */
+               priority = JETSAM_PRIORITY_IDLE;
+               head_insert = TRUE;
        } else if ((priority < 0) || (priority >= MEMSTAT_BUCKET_COUNT)) {
                /* Sanity check */
                ret = EINVAL;
                goto out;
        }
        } else if ((priority < 0) || (priority >= MEMSTAT_BUCKET_COUNT)) {
                /* Sanity check */
                ret = EINVAL;
                goto out;
        }
-       
+
        proc_list_lock();
        
        assert(!(p->p_memstat_state & P_MEMSTAT_INTERNAL));
        proc_list_lock();
        
        assert(!(p->p_memstat_state & P_MEMSTAT_INTERNAL));
@@ -906,7 +1575,16 @@ memorystatus_update(proc_t p, int priority, uint64_t user_data, boolean_t effect
        if (effective && (p->p_memstat_state & P_MEMSTAT_PRIORITYUPDATED)) {
                ret = EALREADY;
                proc_list_unlock();
        if (effective && (p->p_memstat_state & P_MEMSTAT_PRIORITYUPDATED)) {
                ret = EALREADY;
                proc_list_unlock();
-               MEMORYSTATUS_DEBUG(1, "memorystatus_update: effective change specified for pid %d, but change already occurred.\n", pid);
+               MEMORYSTATUS_DEBUG(1, "memorystatus_update: effective change specified for pid %d, but change already occurred.\n", p->p_pid);
+               goto out;             
+       }
+
+       if ((p->p_memstat_state & P_MEMSTAT_TERMINATED) || ((p->p_listflag & P_LIST_EXITED) != 0)) {
+               /*
+                * This could happen when a process calling posix_spawn() is exiting on the jetsam thread.
+                */
+               ret = EBUSY;
+               proc_list_unlock();
                goto out;             
        }
 
                goto out;             
        }
 
@@ -916,20 +1594,118 @@ memorystatus_update(proc_t p, int priority, uint64_t user_data, boolean_t effect
        
 #if CONFIG_JETSAM
        if (update_memlimit) {
        
 #if CONFIG_JETSAM
        if (update_memlimit) {
-               p->p_memstat_memlimit = memlimit;
+               boolean_t trigger_exception;
+
+               /*
+                * Posix_spawn'd processes come through this path to instantiate ledger limits.
+                * Forked processes do not come through this path, so no ledger limits exist.
+                * (That's why forked processes can consume unlimited memory.)
+                */
+
+               MEMORYSTATUS_DEBUG(3, "memorystatus_update(enter): pid %d, priority %d, dirty=0x%x, Active(%dMB %s), Inactive(%dMB, %s)\n",
+                                  p->p_pid, priority, p->p_memstat_dirty,
+                                  memlimit_active,   (memlimit_active_is_fatal ? "F " : "NF"),
+                                  memlimit_inactive, (memlimit_inactive_is_fatal ? "F " : "NF"));
+
                if (memlimit_background) {
                if (memlimit_background) {
-                       /* Will be set as priority is updated */
-                       p->p_memstat_state |= P_MEMSTAT_MEMLIMIT_BACKGROUND;
-               } else {
-                       /* Otherwise, apply now */
-                       if (memorystatus_highwater_enabled) {
-                               task_set_phys_footprint_limit_internal(p->task, (memlimit  > 0) ? memlimit : -1, NULL, TRUE);
+
+                       /*
+                        * With 2-level HWM support, we no longer honor P_MEMSTAT_MEMLIMIT_BACKGROUND.
+                        * Background limits are described via the inactive limit slots.
+                        */
+
+                       // p->p_memstat_state |= P_MEMSTAT_MEMLIMIT_BACKGROUND;
+
+#if DEVELOPMENT || DEBUG
+                       printf("memorystatus_update: WARNING %s[%d] set unused flag P_MEMSTAT_MEMLIMIT_BACKGROUND [A==%dMB %s] [IA==%dMB %s]\n",
+                              (p->p_comm ? p->p_comm : "unknown"), p->p_pid,
+                              memlimit_active, (memlimit_active_is_fatal ? "F " : "NF"),
+                              memlimit_inactive, (memlimit_inactive_is_fatal ? "F " : "NF"));
+#endif /* DEVELOPMENT || DEBUG */
+               }
+
+               if (memlimit_active <= 0) {
+                       /*
+                        * This process will have a system_wide task limit when active.
+                        * System_wide task limit is always fatal.
+                        * It's quite common to see non-fatal flag passed in here.
+                        * It's not an error, we just ignore it.
+                        */
+
+                       /*
+                        * For backward compatibility with some unexplained launchd behavior,
+                        * we allow a zero sized limit.  But we still enforce system_wide limit
+                        * when written to the ledgers.  
+                        */
+
+                       if (memlimit_active < 0) {
+                               memlimit_active = -1;  /* enforces system_wide task limit */
                        }
                        }
+                       memlimit_active_is_fatal = TRUE;
+               }
+
+               if (memlimit_inactive <= 0) {
+                       /*
+                        * This process will have a system_wide task limit when inactive.
+                        * System_wide task limit is always fatal.
+                        */
+
+                       memlimit_inactive = -1;
+                       memlimit_inactive_is_fatal = TRUE;
+               }
+
+               /*
+                * Initialize the active limit variants for this process.
+                */
+               SET_ACTIVE_LIMITS_LOCKED(p, memlimit_active, memlimit_active_is_fatal);
+
+               /*
+                * Initialize the inactive limit variants for this process.
+                */
+               SET_INACTIVE_LIMITS_LOCKED(p, memlimit_inactive, memlimit_inactive_is_fatal);
+
+               /*
+                * Initialize the cached limits for target process.
+                * When the target process is dirty tracked, it's typically
+                * in a clean state.  Non dirty tracked processes are
+                * typically active (Foreground or above).
+                * But just in case, we don't make assumptions...
+                */
+
+               if (proc_jetsam_state_is_active_locked(p) == TRUE) {
+                       CACHE_ACTIVE_LIMITS_LOCKED(p, trigger_exception);
+               } else {
+                       CACHE_INACTIVE_LIMITS_LOCKED(p, trigger_exception);
+               }
+
+               /*
+                * Enforce the cached limit by writing to the ledger.
+                */
+               if (memorystatus_highwater_enabled) {
+                       /* apply now */
+                       assert(trigger_exception == TRUE);
+                       task_set_phys_footprint_limit_internal(p->task, ((p->p_memstat_memlimit > 0) ? p->p_memstat_memlimit : -1), NULL, trigger_exception);
+
+                       MEMORYSTATUS_DEBUG(3, "memorystatus_update: init: limit on pid %d (%dMB %s) targeting priority(%d) dirty?=0x%x %s\n",
+                                          p->p_pid, (p->p_memstat_memlimit > 0 ? p->p_memstat_memlimit : -1),
+                                          (p->p_memstat_state & P_MEMSTAT_FATAL_MEMLIMIT ? "F " : "NF"), priority, p->p_memstat_dirty,
+                                          (p->p_memstat_dirty ? ((p->p_memstat_dirty & P_DIRTY) ? "isdirty" : "isclean") : ""));
                }
        }
                }
        }
-#endif
+#endif /* CONFIG_JETSAM */
 
 
-       memorystatus_update_priority_locked(p, priority);
+       /*
+        * We can't add to the JETSAM_PRIORITY_IDLE_DEFERRED bucket here.
+        * But, we could be removing it from the bucket.
+        * Check and take appropriate steps if so.
+        */
+       
+       if (p->p_memstat_effectivepriority == JETSAM_PRIORITY_IDLE_DEFERRED) {
+               
+               memorystatus_invalidate_idle_demotion_locked(p, TRUE);
+       }
+       
+       memorystatus_update_priority_locked(p, priority, head_insert);
        
        proc_list_unlock();
        ret = 0;
        
        proc_list_unlock();
        ret = 0;
@@ -946,15 +1722,19 @@ memorystatus_remove(proc_t p, boolean_t locked)
        int ret;
        memstat_bucket_t *bucket;
 
        int ret;
        memstat_bucket_t *bucket;
 
-       MEMORYSTATUS_DEBUG(1, "memorystatus_list_remove: removing process %d\n", pid);
+       MEMORYSTATUS_DEBUG(1, "memorystatus_list_remove: removing pid %d\n", p->p_pid);
 
        if (!locked) {
                proc_list_lock();
        }
 
        assert(!(p->p_memstat_state & P_MEMSTAT_INTERNAL));
 
        if (!locked) {
                proc_list_lock();
        }
 
        assert(!(p->p_memstat_state & P_MEMSTAT_INTERNAL));
-
+       
        bucket = &memstat_bucket[p->p_memstat_effectivepriority];
        bucket = &memstat_bucket[p->p_memstat_effectivepriority];
+       if (p->p_memstat_effectivepriority == JETSAM_PRIORITY_IDLE_DEFERRED) {
+               assert(bucket->count == memorystatus_scheduled_idle_demotions);
+       }
+
        TAILQ_REMOVE(&bucket->list, p, p_memstat_list);
        bucket->count--;
 
        TAILQ_REMOVE(&bucket->list, p, p_memstat_list);
        bucket->count--;
 
@@ -992,32 +1772,46 @@ memorystatus_remove(proc_t p, boolean_t locked)
        return ret;
 }
 
        return ret;
 }
 
-static boolean_t
+/*
+ * Validate dirty tracking flags with process state.
+ *
+ * Return:
+ *     0     on success
+ *     non-0 on failure
+ */
+
+static int
 memorystatus_validate_track_flags(struct proc *target_p, uint32_t pcontrol) {
        /* See that the process isn't marked for termination */
        if (target_p->p_memstat_dirty & P_DIRTY_TERMINATED) {
 memorystatus_validate_track_flags(struct proc *target_p, uint32_t pcontrol) {
        /* See that the process isn't marked for termination */
        if (target_p->p_memstat_dirty & P_DIRTY_TERMINATED) {
-               return FALSE;
+               return EBUSY;
        }
        
        /* Idle exit requires that process be tracked */
        if ((pcontrol & PROC_DIRTY_ALLOW_IDLE_EXIT) &&
           !(pcontrol & PROC_DIRTY_TRACK)) {
        }
        
        /* Idle exit requires that process be tracked */
        if ((pcontrol & PROC_DIRTY_ALLOW_IDLE_EXIT) &&
           !(pcontrol & PROC_DIRTY_TRACK)) {
-               return FALSE;           
+               return EINVAL;
+       }
+
+       /* 'Launch in progress' tracking requires that process have enabled dirty tracking too. */
+       if ((pcontrol & PROC_DIRTY_LAUNCH_IN_PROGRESS) &&
+          !(pcontrol & PROC_DIRTY_TRACK)) {
+               return EINVAL;
        }
 
        /* Deferral is only relevant if idle exit is specified */
        if ((pcontrol & PROC_DIRTY_DEFER) && 
           !(pcontrol & PROC_DIRTY_ALLOWS_IDLE_EXIT)) {
        }
 
        /* Deferral is only relevant if idle exit is specified */
        if ((pcontrol & PROC_DIRTY_DEFER) && 
           !(pcontrol & PROC_DIRTY_ALLOWS_IDLE_EXIT)) {
-               return FALSE;           
+               return EINVAL;
        }
        
        }
        
-       return TRUE;
+       return(0);
 }
 
 static void
 memorystatus_update_idle_priority_locked(proc_t p) {
        int32_t priority;
 }
 
 static void
 memorystatus_update_idle_priority_locked(proc_t p) {
        int32_t priority;
-       
+
        MEMORYSTATUS_DEBUG(1, "memorystatus_update_idle_priority_locked(): pid %d dirty 0x%X\n", p->p_pid, p->p_memstat_dirty);
        
        if ((p->p_memstat_dirty & (P_DIRTY_IDLE_EXIT_ENABLED|P_DIRTY_IS_DIRTY)) == P_DIRTY_IDLE_EXIT_ENABLED) {
        MEMORYSTATUS_DEBUG(1, "memorystatus_update_idle_priority_locked(): pid %d dirty 0x%X\n", p->p_pid, p->p_memstat_dirty);
        
        if ((p->p_memstat_dirty & (P_DIRTY_IDLE_EXIT_ENABLED|P_DIRTY_IS_DIRTY)) == P_DIRTY_IDLE_EXIT_ENABLED) {
@@ -1026,7 +1820,9 @@ memorystatus_update_idle_priority_locked(proc_t p) {
                priority = p->p_memstat_requestedpriority;
        }
        
                priority = p->p_memstat_requestedpriority;
        }
        
-       memorystatus_update_priority_locked(p, priority);
+       if (priority != p->p_memstat_effectivepriority) {
+               memorystatus_update_priority_locked(p, priority, false);
+       }
 } 
 
 /*
 } 
 
 /*
@@ -1050,19 +1846,32 @@ int
 memorystatus_dirty_track(proc_t p, uint32_t pcontrol) {
        unsigned int old_dirty;
        boolean_t reschedule = FALSE;
 memorystatus_dirty_track(proc_t p, uint32_t pcontrol) {
        unsigned int old_dirty;
        boolean_t reschedule = FALSE;
-       int ret;
+       boolean_t already_deferred = FALSE;
+       boolean_t defer_now = FALSE;
+       int ret = 0;
     
     
+       KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_DIRTY_TRACK),
+               p->p_pid, p->p_memstat_dirty, pcontrol, 0, 0);
+       
        proc_list_lock();
        
        proc_list_lock();
        
+       if ((p->p_listflag & P_LIST_EXITED) != 0) {
+               /*
+                * Process is on its way out.
+                */
+               ret = EBUSY;
+               goto exit;
+       }
+
        if (p->p_memstat_state & P_MEMSTAT_INTERNAL) {
                ret = EPERM;
                goto exit;
        }
        
        if (p->p_memstat_state & P_MEMSTAT_INTERNAL) {
                ret = EPERM;
                goto exit;
        }
        
-       if (!memorystatus_validate_track_flags(p, pcontrol)) {
-               ret = EINVAL;
+       if ((ret = memorystatus_validate_track_flags(p, pcontrol)) != 0) {
+               /* error  */
                goto exit;
                goto exit;
-        }
+       }
 
         old_dirty = p->p_memstat_dirty;
 
 
         old_dirty = p->p_memstat_dirty;
 
@@ -1075,28 +1884,82 @@ memorystatus_dirty_track(proc_t p, uint32_t pcontrol) {
                p->p_memstat_dirty |= P_DIRTY_ALLOW_IDLE_EXIT;                                  
        }
 
                p->p_memstat_dirty |= P_DIRTY_ALLOW_IDLE_EXIT;                                  
        }
 
+       if (pcontrol & PROC_DIRTY_LAUNCH_IN_PROGRESS) {
+               p->p_memstat_dirty |= P_DIRTY_LAUNCH_IN_PROGRESS;
+       }
+
+       if (old_dirty & P_DIRTY_DEFER_IN_PROGRESS) {
+               already_deferred = TRUE;
+       }
+
        /* This can be set and cleared exactly once. */
        /* This can be set and cleared exactly once. */
-       if ((pcontrol & PROC_DIRTY_DEFER) && !(old_dirty & P_DIRTY_DEFER)) {
-               p->p_memstat_dirty |= (P_DIRTY_DEFER|P_DIRTY_DEFER_IN_PROGRESS);                                
-       } else {
-               p->p_memstat_dirty &= ~P_DIRTY_DEFER_IN_PROGRESS;                                       
+       if (pcontrol & PROC_DIRTY_DEFER) {
+
+               if ( !(old_dirty & P_DIRTY_DEFER)) {
+                       p->p_memstat_dirty |= P_DIRTY_DEFER;
+               }
+
+               defer_now = TRUE;
        }
 
        }
 
-       MEMORYSTATUS_DEBUG(1, "memorystatus_on_track_dirty(): set idle-exit %s / deferred %s / dirty %s for process %d\n",
+       MEMORYSTATUS_DEBUG(1, "memorystatus_on_track_dirty(): set idle-exit %s / defer %s / dirty %s for pid %d\n",
                ((p->p_memstat_dirty & P_DIRTY_IDLE_EXIT_ENABLED) == P_DIRTY_IDLE_EXIT_ENABLED) ? "Y" : "N",
                ((p->p_memstat_dirty & P_DIRTY_IDLE_EXIT_ENABLED) == P_DIRTY_IDLE_EXIT_ENABLED) ? "Y" : "N",
-               p->p_memstat_dirty & P_DIRTY_DEFER_IN_PROGRESS ? "Y" : "N",
+               defer_now ? "Y" : "N",
                p->p_memstat_dirty & P_DIRTY ? "Y" : "N",
                p->p_pid);
 
        /* Kick off or invalidate the idle exit deferment if there's a state transition. */
        if (!(p->p_memstat_dirty & P_DIRTY_IS_DIRTY)) {
                if (((p->p_memstat_dirty & P_DIRTY_IDLE_EXIT_ENABLED) == P_DIRTY_IDLE_EXIT_ENABLED) && 
                p->p_memstat_dirty & P_DIRTY ? "Y" : "N",
                p->p_pid);
 
        /* Kick off or invalidate the idle exit deferment if there's a state transition. */
        if (!(p->p_memstat_dirty & P_DIRTY_IS_DIRTY)) {
                if (((p->p_memstat_dirty & P_DIRTY_IDLE_EXIT_ENABLED) == P_DIRTY_IDLE_EXIT_ENABLED) && 
-                       (p->p_memstat_dirty & P_DIRTY_DEFER_IN_PROGRESS) && !(old_dirty & P_DIRTY_DEFER_IN_PROGRESS)) {
+                       defer_now && !already_deferred) {
+                       
+                       /*
+                        * Request to defer a clean process that's idle-exit enabled 
+                        * and not already in the jetsam deferred band.
+                        */
                        memorystatus_schedule_idle_demotion_locked(p, TRUE);
                        reschedule = TRUE;
                        memorystatus_schedule_idle_demotion_locked(p, TRUE);
                        reschedule = TRUE;
-               } else if (!(p->p_memstat_dirty & P_DIRTY_DEFER_IN_PROGRESS) && (old_dirty & P_DIRTY_DEFER_IN_PROGRESS)) {
+
+               } else if (!defer_now && already_deferred) {
+
+                       /*
+                        * Either the process is no longer idle-exit enabled OR
+                        * there's a request to cancel a currently active deferral.
+                        */
+                       memorystatus_invalidate_idle_demotion_locked(p, TRUE);
+                       reschedule = TRUE;
+               }
+       } else {
+
+               /*
+                * We are trying to operate on a dirty process. Dirty processes have to
+                * be removed from the deferred band. The question is do we reset the 
+                * deferred state or not?
+                *
+                * This could be a legal request like:
+                * - this process had opted into the JETSAM_DEFERRED band
+                * - but it's now dirty and requests to opt out.
+                * In this case, we remove the process from the band and reset its
+                * state too. It'll opt back in properly when needed.
+                *
+                * OR, this request could be a user-space bug. E.g.:
+                * - this process had opted into the JETSAM_DEFERRED band when clean
+                * - and, then issues another request to again put it into the band except
+                *   this time the process is dirty.
+                * The process going dirty, as a transition in memorystatus_dirty_set(), will pull the process out of
+                * the deferred band with its state intact. So our request below is no-op.
+                * But we do it here anyways for coverage.
+                *
+                * memorystatus_update_idle_priority_locked()
+                * single-mindedly treats a dirty process as "cannot be in the deferred band".
+                */
+
+               if (!defer_now && already_deferred) {
                        memorystatus_invalidate_idle_demotion_locked(p, TRUE);
                        reschedule = TRUE;
                        memorystatus_invalidate_idle_demotion_locked(p, TRUE);
                        reschedule = TRUE;
+               } else {
+                       memorystatus_invalidate_idle_demotion_locked(p, FALSE);
+                       reschedule = TRUE;
                }
        }
 
                }
        }
 
@@ -1123,9 +1986,19 @@ memorystatus_dirty_set(proc_t p, boolean_t self, uint32_t pcontrol) {
        boolean_t now_dirty = FALSE;
 
        MEMORYSTATUS_DEBUG(1, "memorystatus_dirty_set(): %d %d 0x%x 0x%x\n", self, p->p_pid, pcontrol, p->p_memstat_dirty);
        boolean_t now_dirty = FALSE;
 
        MEMORYSTATUS_DEBUG(1, "memorystatus_dirty_set(): %d %d 0x%x 0x%x\n", self, p->p_pid, pcontrol, p->p_memstat_dirty);
+       
+       KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_DIRTY_SET), p->p_pid, self, pcontrol, 0, 0);
 
        proc_list_lock();
 
 
        proc_list_lock();
 
+       if ((p->p_listflag & P_LIST_EXITED) != 0) {
+               /*
+                * Process is on its way out.
+                */
+               ret = EBUSY;
+               goto exit;
+       }
+
        if (p->p_memstat_state & P_MEMSTAT_INTERNAL) {
                ret = EPERM;
                goto exit;
        if (p->p_memstat_state & P_MEMSTAT_INTERNAL) {
                ret = EPERM;
                goto exit;
@@ -1151,7 +2024,7 @@ memorystatus_dirty_set(proc_t p, boolean_t self, uint32_t pcontrol) {
                        memorystatus_dirty_count++;
                        ret = 0;
                } else if ((pcontrol == 0) && (p->p_memstat_dirty & flag)) {
                        memorystatus_dirty_count++;
                        ret = 0;
                } else if ((pcontrol == 0) && (p->p_memstat_dirty & flag)) {
-                       if ((flag == P_DIRTY_SHUTDOWN) && (!p->p_memstat_dirty & P_DIRTY)) {
+                       if ((flag == P_DIRTY_SHUTDOWN) && (!(p->p_memstat_dirty & P_DIRTY))) {
                                /* Clearing the dirty shutdown flag, and the process is otherwise clean - kill */
                                p->p_memstat_dirty |= P_DIRTY_TERMINATED;
                                kill = true;
                                /* Clearing the dirty shutdown flag, and the process is otherwise clean - kill */
                                p->p_memstat_dirty |= P_DIRTY_TERMINATED;
                                kill = true;
@@ -1171,7 +2044,7 @@ memorystatus_dirty_set(proc_t p, boolean_t self, uint32_t pcontrol) {
        if (ret != 0) {
                goto exit;
        }
        if (ret != 0) {
                goto exit;
        }
-           
+
        if (p->p_memstat_dirty & P_DIRTY_IS_DIRTY)
                now_dirty = TRUE;
 
        if (p->p_memstat_dirty & P_DIRTY_IS_DIRTY)
                now_dirty = TRUE;
 
@@ -1181,31 +2054,130 @@ memorystatus_dirty_set(proc_t p, boolean_t self, uint32_t pcontrol) {
                /* Manage idle exit deferral, if applied */
                if ((p->p_memstat_dirty & (P_DIRTY_IDLE_EXIT_ENABLED|P_DIRTY_DEFER_IN_PROGRESS)) ==
                    (P_DIRTY_IDLE_EXIT_ENABLED|P_DIRTY_DEFER_IN_PROGRESS)) {
                /* Manage idle exit deferral, if applied */
                if ((p->p_memstat_dirty & (P_DIRTY_IDLE_EXIT_ENABLED|P_DIRTY_DEFER_IN_PROGRESS)) ==
                    (P_DIRTY_IDLE_EXIT_ENABLED|P_DIRTY_DEFER_IN_PROGRESS)) {
+
+                       /*
+                        * P_DIRTY_DEFER_IN_PROGRESS means the process is in the deferred band OR it might be heading back
+                        * there once it's clean again and has some protection window left.
+                        */
+
                        if (p->p_memstat_dirty & P_DIRTY_IS_DIRTY) {
                        if (p->p_memstat_dirty & P_DIRTY_IS_DIRTY) {
+                               /*
+                                * New dirty process i.e. "was_dirty == FALSE && now_dirty == TRUE"
+                                *
+                                * The process will move from the deferred band to its higher requested
+                                * jetsam band. But we don't clear its state i.e. we want to remember that
+                                * this process was part of the "deferred" band and will return to it.
+                                *
+                                * This way, we don't let it age beyond the protection
+                                * window when it returns to "clean". All the while giving
+                                * it a chance to perform its work while "dirty".
+                                *
+                                */
                                memorystatus_invalidate_idle_demotion_locked(p, FALSE);
                                reschedule = TRUE;
                        } else {
                                memorystatus_invalidate_idle_demotion_locked(p, FALSE);
                                reschedule = TRUE;
                        } else {
-                               /* We evaluate lazily, so reset the idle-deadline if it's expired by the time the process becomes clean. */
+
+                               /*
+                                * Process is back from "dirty" to "clean".
+                                * 
+                                * Is its timer up OR does it still have some protection
+                                * window left?
+                                */
+
                                if (mach_absolute_time() >= p->p_memstat_idledeadline) {
                                if (mach_absolute_time() >= p->p_memstat_idledeadline) {
-                                       p->p_memstat_idledeadline = 0;
-                                       p->p_memstat_dirty &= ~P_DIRTY_DEFER_IN_PROGRESS;
+                                       /*
+                                        * The process' deadline has expired. It currently
+                                        * does not reside in the DEFERRED bucket.
+                                        * 
+                                        * It's on its way to the JETSAM_PRIORITY_IDLE 
+                                        * bucket via memorystatus_update_idle_priority_locked()
+                                        * below.
+                                        
+                                        * So all we need to do is reset all the state on the
+                                        * process that's related to the DEFERRED bucket i.e.
+                                        * the DIRTY_DEFER_IN_PROGRESS flag and the timer deadline.
+                                        *
+                                        */
+
+                                       memorystatus_invalidate_idle_demotion_locked(p, TRUE);
+                                       reschedule = TRUE;
                                } else {
                                } else {
+                                       /*
+                                        * It still has some protection window left and so
+                                        * we just re-arm the timer without modifying any
+                                        * state on the process.
+                                        */
                                        memorystatus_schedule_idle_demotion_locked(p, FALSE);
                                        reschedule = TRUE;
                                }
                        }
                }
                                        memorystatus_schedule_idle_demotion_locked(p, FALSE);
                                        reschedule = TRUE;
                                }
                        }
                }
-    
+
                memorystatus_update_idle_priority_locked(p);
                memorystatus_update_idle_priority_locked(p);
+
+#if CONFIG_JETSAM
+               if (memorystatus_highwater_enabled) {
+                       boolean_t trigger_exception;
+                       /* 
+                        * We are in this path because this process transitioned between 
+                        * dirty <--> clean state.  Update the cached memory limits.
+                        */
+
+                       if (proc_jetsam_state_is_active_locked(p) == TRUE) {
+                               /*
+                                * process is dirty
+                                */
+                               CACHE_ACTIVE_LIMITS_LOCKED(p, trigger_exception);
+                       } else {
+                               /*
+                                * process is clean
+                                */
+                               CACHE_INACTIVE_LIMITS_LOCKED(p, trigger_exception);
+                       }
+
+                       /*
+                        * Enforce the new limits by writing to the ledger.
+                        *
+                        * This is a hot path and holding the proc_list_lock while writing to the ledgers,
+                        * (where the task lock is taken) is bad.  So, we temporarily drop the proc_list_lock.
+                        * We aren't traversing the jetsam bucket list here, so we should be safe.
+                        * See rdar://21394491.
+                        */
+
+                       if (proc_ref_locked(p) == p) {
+                               int ledger_limit;
+                               if (p->p_memstat_memlimit > 0) {
+                                       ledger_limit = p->p_memstat_memlimit;
+                               } else {
+                                       ledger_limit = -1;
+                               }
+                               proc_list_unlock();
+                               task_set_phys_footprint_limit_internal(p->task, ledger_limit, NULL, trigger_exception);
+                               proc_list_lock();
+                               proc_rele_locked(p);
+
+                               MEMORYSTATUS_DEBUG(3, "memorystatus_dirty_set: new limit on pid %d (%dMB %s) priority(%d) dirty?=0x%x %s\n",
+                                          p->p_pid, (p->p_memstat_memlimit > 0 ? p->p_memstat_memlimit : -1),
+                                          (p->p_memstat_state & P_MEMSTAT_FATAL_MEMLIMIT ? "F " : "NF"), p->p_memstat_effectivepriority, p->p_memstat_dirty,
+                                          (p->p_memstat_dirty ? ((p->p_memstat_dirty & P_DIRTY) ? "isdirty" : "isclean") : ""));
+                       }
+
+               }
+#endif /* CONFIG_JETSAM */
        
                /* If the deferral state changed, reschedule the demotion timer */
                if (reschedule) {
                        memorystatus_reschedule_idle_demotion_locked();
                }
        }
        
                /* If the deferral state changed, reschedule the demotion timer */
                if (reschedule) {
                        memorystatus_reschedule_idle_demotion_locked();
                }
        }
-               
+
        if (kill) {
        if (kill) {
-               psignal(p, SIGKILL);
+               if (proc_ref_locked(p) == p) {
+                       proc_list_unlock();
+                       psignal(p, SIGKILL);
+                       proc_list_lock();
+                       proc_rele_locked(p);
+               }
        }
        
 exit:
        }
        
 exit:
@@ -1214,6 +2186,65 @@ exit:
        return ret;
 }
 
        return ret;
 }
 
+int
+memorystatus_dirty_clear(proc_t p, uint32_t pcontrol) {
+
+       int ret = 0;
+
+       MEMORYSTATUS_DEBUG(1, "memorystatus_dirty_clear(): %d 0x%x 0x%x\n", p->p_pid, pcontrol, p->p_memstat_dirty);
+       
+       KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_DIRTY_CLEAR), p->p_pid, pcontrol, 0, 0, 0);
+
+       proc_list_lock();
+
+       if ((p->p_listflag & P_LIST_EXITED) != 0) {
+               /*
+                * Process is on its way out.
+                */
+               ret = EBUSY;
+               goto exit;
+       }
+
+       if (p->p_memstat_state & P_MEMSTAT_INTERNAL) {
+               ret = EPERM;
+               goto exit;
+       }
+
+       if (!(p->p_memstat_dirty & P_DIRTY_TRACK)) {
+               /* Dirty tracking not enabled */
+               ret = EINVAL;                   
+               goto exit;
+       } 
+
+       if (!pcontrol || (pcontrol & (PROC_DIRTY_LAUNCH_IN_PROGRESS | PROC_DIRTY_DEFER)) == 0) {
+               ret = EINVAL;
+               goto exit;
+       }
+
+       if (pcontrol & PROC_DIRTY_LAUNCH_IN_PROGRESS) {
+               p->p_memstat_dirty &= ~P_DIRTY_LAUNCH_IN_PROGRESS;
+       }
+
+       /* This can be set and cleared exactly once. */
+       if (pcontrol & PROC_DIRTY_DEFER) {
+
+               if (p->p_memstat_dirty & P_DIRTY_DEFER) {
+
+                       p->p_memstat_dirty &= ~P_DIRTY_DEFER;
+
+                       memorystatus_invalidate_idle_demotion_locked(p, TRUE);
+                       memorystatus_update_idle_priority_locked(p);
+                       memorystatus_reschedule_idle_demotion_locked();
+               }
+       }
+
+       ret = 0;
+exit:
+       proc_list_unlock();
+
+       return ret;
+}
+
 int
 memorystatus_dirty_get(proc_t p) {
        int ret = 0;
 int
 memorystatus_dirty_get(proc_t p) {
        int ret = 0;
@@ -1228,6 +2259,9 @@ memorystatus_dirty_get(proc_t p) {
                if (p->p_memstat_dirty & P_DIRTY) {
                        ret |= PROC_DIRTY_IS_DIRTY;
                }
                if (p->p_memstat_dirty & P_DIRTY) {
                        ret |= PROC_DIRTY_IS_DIRTY;
                }
+               if (p->p_memstat_dirty & P_DIRTY_LAUNCH_IN_PROGRESS) {
+                       ret |= PROC_DIRTY_LAUNCH_IS_IN_PROGRESS;
+               }
        }
        
        proc_list_unlock();
        }
        
        proc_list_unlock();
@@ -1261,7 +2295,7 @@ memorystatus_on_suspend(proc_t p)
 {
 #if CONFIG_FREEZE
        uint32_t pages;
 {
 #if CONFIG_FREEZE
        uint32_t pages;
-       memorystatus_get_task_page_counts(p->task, &pages, NULL);
+       memorystatus_get_task_page_counts(p->task, &pages, NULL, NULL, NULL);
 #endif
        proc_list_lock();
 #if CONFIG_FREEZE
 #endif
        proc_list_lock();
 #if CONFIG_FREEZE
@@ -1392,10 +2426,14 @@ kill_idle_exit_proc(void)
 }
 #endif
 
 }
 #endif
 
+#if CONFIG_JETSAM
 static void
 memorystatus_thread_wake(void) {
        thread_wakeup((event_t)&memorystatus_wakeup);
 }
 static void
 memorystatus_thread_wake(void) {
        thread_wakeup((event_t)&memorystatus_wakeup);
 }
+#endif /* CONFIG_JETSAM */
+
+extern void vm_pressure_response(void);
 
 static int
 memorystatus_thread_block(uint32_t interval_ms, thread_continue_t continuation)
 
 static int
 memorystatus_thread_block(uint32_t interval_ms, thread_continue_t continuation)
@@ -1409,16 +2447,29 @@ memorystatus_thread_block(uint32_t interval_ms, thread_continue_t continuation)
        return thread_block(continuation);   
 }
 
        return thread_block(continuation);   
 }
 
-extern boolean_t vm_compressor_thrashing_detected;
-extern uint64_t vm_compressor_total_compressions(void);
-
 static void
 memorystatus_thread(void *param __unused, wait_result_t wr __unused)
 {
        static boolean_t is_vm_privileged = FALSE;
 static void
 memorystatus_thread(void *param __unused, wait_result_t wr __unused)
 {
        static boolean_t is_vm_privileged = FALSE;
+
 #if CONFIG_JETSAM
        boolean_t post_snapshot = FALSE;
        uint32_t errors = 0;
 #if CONFIG_JETSAM
        boolean_t post_snapshot = FALSE;
        uint32_t errors = 0;
+       uint32_t hwm_kill = 0;
+       boolean_t sort_flag = TRUE;
+
+        /* Jetsam Loop Detection - locals */
+       memstat_bucket_t *bucket;
+       int             jld_bucket_count = 0;
+       struct timeval  jld_now_tstamp = {0,0};
+       uint64_t        jld_now_msecs = 0;
+
+       /* Jetsam Loop Detection - statics */
+       static uint64_t  jld_timestamp_msecs = 0;
+       static int       jld_idle_kill_candidates = 0;  /* Number of available processes in band 0,1 at start */
+       static int       jld_idle_kills = 0;            /* Number of procs killed during eval period  */
+       static int       jld_eval_aggressive_count = 0;         /* Bumps the max priority in aggressive loop */
+       static int32_t   jld_priority_band_max = JETSAM_PRIORITY_UI_SUPPORT;
 #endif
 
        if (is_vm_privileged == FALSE) {
 #endif
 
        if (is_vm_privileged == FALSE) {
@@ -1429,120 +2480,247 @@ memorystatus_thread(void *param __unused, wait_result_t wr __unused)
                thread_wire(host_priv_self(), current_thread(), TRUE);
                is_vm_privileged = TRUE;
                
                thread_wire(host_priv_self(), current_thread(), TRUE);
                is_vm_privileged = TRUE;
                
+               if (vm_restricted_to_single_processor == TRUE)
+                       thread_vm_bind_group_add();
+
                memorystatus_thread_block(0, memorystatus_thread);
        }
        
 #if CONFIG_JETSAM
        
        KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_SCAN) | DBG_FUNC_START,
                memorystatus_thread_block(0, memorystatus_thread);
        }
        
 #if CONFIG_JETSAM
        
        KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_SCAN) | DBG_FUNC_START,
-               memorystatus_available_pages, 0, 0, 0, 0);
+                             memorystatus_available_pages, memorystatus_jld_enabled, memorystatus_jld_eval_period_msecs, memorystatus_jld_eval_aggressive_count,0);
 
 
-       uint32_t cause = vm_compressor_thrashing_detected ? kMemorystatusKilledVMThrashing : kMemorystatusKilledVMPageShortage;
-
-       /* Jetsam aware version.
+       /*
+        * Jetsam aware version.
         *
         *
-        * If woken under pressure, go down the path of killing:
+        * The VM pressure notification thread is working it's way through clients in parallel.
         *
         *
-        * - processes exceeding their highwater mark if no clean victims available
-        * - the least recently used process if no highwater mark victims available
+        * So, while the pressure notification thread is targeting processes in order of 
+        * increasing jetsam priority, we can hopefully reduce / stop it's work by killing 
+        * any processes that have exceeded their highwater mark.
+        *
+        * If we run out of HWM processes and our available pages drops below the critical threshold, then,
+        * we target the least recently used process in order of increasing jetsam priority (exception: the FG band).
         */
         */
-#if !LATENCY_JETSAM       
-       while (vm_compressor_thrashing_detected || memorystatus_available_pages <= memorystatus_available_pages_critical) {
-#else
-       while (kill_under_pressure) {
-               const uint32_t SNAPSHOT_WAIT_TIMEOUT_MS = 100;
-               wait_result_t wait_result;
-#endif
+       while (is_thrashing(kill_under_pressure_cause) ||
+              memorystatus_available_pages <= memorystatus_available_pages_pressure) {
                boolean_t killed;
                int32_t priority;
                boolean_t killed;
                int32_t priority;
+               uint32_t cause;
+
+               if (kill_under_pressure_cause) {
+                       cause = kill_under_pressure_cause;
+               } else {
+                       cause = kMemorystatusKilledVMPageShortage;
+               }
 
 #if LEGACY_HIWATER
                /* Highwater */
                killed = memorystatus_kill_hiwat_proc(&errors);
                if (killed) {
 
 #if LEGACY_HIWATER
                /* Highwater */
                killed = memorystatus_kill_hiwat_proc(&errors);
                if (killed) {
+                       hwm_kill++;
                        post_snapshot = TRUE;
                        goto done;
                        post_snapshot = TRUE;
                        goto done;
+               } else {
+                       memorystatus_hwm_candidates = FALSE;
+               }
+
+               /* No highwater processes to kill. Continue or stop for now? */
+               if (!is_thrashing(kill_under_pressure_cause) &&
+                   (memorystatus_available_pages > memorystatus_available_pages_critical)) {
+                       /*
+                        * We are _not_ out of pressure but we are above the critical threshold and there's:
+                        * - no compressor thrashing
+                        * - no more HWM processes left.
+                        * For now, don't kill any other processes.
+                        */
+               
+                       if (hwm_kill == 0) {
+                               memorystatus_thread_wasted_wakeup++;
+                       }
+
+                       break;
                }
 #endif
                }
 #endif
+               if (memorystatus_jld_enabled == TRUE) {
+
+                       /*
+                        * Jetsam Loop Detection: attempt to detect
+                        * rapid daemon relaunches in the lower bands.
+                        */
+                       
+                       microuptime(&jld_now_tstamp);
+
+                       /*
+                        * Ignore usecs in this calculation.
+                        * msecs granularity is close enough.
+                        */
+                       jld_now_msecs = (jld_now_tstamp.tv_sec * 1000);
+
+                       proc_list_lock();
+                       bucket = &memstat_bucket[JETSAM_PRIORITY_IDLE];
+                       jld_bucket_count = bucket->count;
+                       bucket = &memstat_bucket[JETSAM_PRIORITY_IDLE_DEFERRED];
+                       jld_bucket_count += bucket->count;
+                       proc_list_unlock();
+
+                       /*
+                        * memorystatus_jld_eval_period_msecs is a tunable
+                        * memorystatus_jld_eval_aggressive_count is a tunable
+                        * memorystatus_jld_eval_aggressive_priority_band_max is a tunable
+                        */
+                       if ( (jld_bucket_count == 0) || 
+                            (jld_now_msecs > (jld_timestamp_msecs + memorystatus_jld_eval_period_msecs))) {
+
+                               /* 
+                                * Refresh evaluation parameters 
+                                */
+                               jld_timestamp_msecs      = jld_now_msecs;
+                               jld_idle_kill_candidates = jld_bucket_count;
+                               jld_idle_kills           = 0;
+                               jld_eval_aggressive_count = 0;
+                               jld_priority_band_max   = JETSAM_PRIORITY_UI_SUPPORT;
+                       }
+
+                       if (jld_idle_kills > jld_idle_kill_candidates) {
+                               jld_eval_aggressive_count++;
+                               if (jld_eval_aggressive_count > memorystatus_jld_eval_aggressive_count) {
+                                       /* 
+                                        * Bump up the jetsam priority limit (eg: the bucket index)
+                                        * Enforce bucket index sanity.
+                                        */
+                                       if ((memorystatus_jld_eval_aggressive_priority_band_max < 0) || 
+                                           (memorystatus_jld_eval_aggressive_priority_band_max >= MEMSTAT_BUCKET_COUNT)) {
+                                               /*
+                                                * Do nothing.  Stick with the default level.
+                                                */
+                                       } else {
+                                               jld_priority_band_max = memorystatus_jld_eval_aggressive_priority_band_max;
+                                       }
+                               }
+
+                               killed = memorystatus_kill_top_process_aggressive(
+                                       TRUE, 
+                                       kMemorystatusKilledVMThrashing,
+                                       jld_eval_aggressive_count, 
+                                       jld_priority_band_max, 
+                                       &errors);
+
+                                       
+                               if (killed) {
+                                       /* Always generate logs after aggressive kill */
+                                       post_snapshot = TRUE;
+                                       goto done;
+                               } 
+                       } 
+               }
                
                /* LRU */
                
                /* LRU */
-               killed = memorystatus_kill_top_process(TRUE, cause, &priority, &errors);
+               killed = memorystatus_kill_top_process(TRUE, sort_flag, cause, &priority, &errors);
+               sort_flag = FALSE;
+
                if (killed) {
                if (killed) {
-                       if (!kill_under_pressure && (priority != JETSAM_PRIORITY_IDLE)) {
-                               /* Don't generate logs for steady-state idle-exit kills */
+                       /*
+                        * Don't generate logs for steady-state idle-exit kills,
+                        * unless it is overridden for debug or by the device
+                        * tree.
+                        */
+                       if ((priority != JETSAM_PRIORITY_IDLE) || memorystatus_idle_snapshot) {
                                post_snapshot = TRUE;
                        }
                                post_snapshot = TRUE;
                        }
+
+                       /* Jetsam Loop Detection */
+                       if (memorystatus_jld_enabled == TRUE) {
+                               if ((priority == JETSAM_PRIORITY_IDLE) || (priority == JETSAM_PRIORITY_IDLE_DEFERRED)) {
+                                       jld_idle_kills++;
+                               } else {
+                                       /*
+                                        * We've reached into bands beyond idle deferred.
+                                        * We make no attempt to monitor them
+                                        */
+                               }
+                       }
                        goto done;
                }
                        goto done;
                }
-                       
-               /* Under pressure and unable to kill a process - panic */
-               panic("memorystatus_jetsam_thread: no victim! available pages:%d\n", memorystatus_available_pages);
+               
+               if (memorystatus_available_pages <= memorystatus_available_pages_critical) {
+                       /* Under pressure and unable to kill a process - panic */
+                       panic("memorystatus_jetsam_thread: no victim! available pages:%d\n", memorystatus_available_pages);
+               }
                        
 done:          
                        
 done:          
-               kill_under_pressure = FALSE;
-               vm_compressor_thrashing_detected = FALSE;
-                               
-#if LATENCY_JETSAM
-               KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_LATENCY_COALESCE) | DBG_FUNC_START,
-                       memorystatus_available_pages, 0, 0, 0, 0);
-               thread_wakeup((event_t)&latency_jetsam_wakeup);
-               /* 
-                * Coalesce snapshot reports in the face of repeated jetsams by blocking here with a timeout. 
-                * If the wait expires, issue the note.
+
+               /*
+                * We do not want to over-kill when thrashing has been detected.
+                * To avoid that, we reset the flag here and notify the
+                * compressor.
                 */
                 */
-               wait_result = memorystatus_thread_block(SNAPSHOT_WAIT_TIMEOUT_MS, THREAD_CONTINUE_NULL);        
-               KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_LATENCY_COALESCE) | DBG_FUNC_END,
-                       memorystatus_available_pages, 0, 0, 0, 0);
-               if (wait_result != THREAD_AWAKENED) {
-                       /* Catch-all */
-                       break;
+               if (is_thrashing(kill_under_pressure_cause)) {
+                       kill_under_pressure_cause = 0;
+                       vm_thrashing_jetsam_done();
                }
                }
-#endif
        }
        }
-               
+
+       kill_under_pressure_cause = 0;
+       
        if (errors) {
                memorystatus_clear_errors();
        }
 
 #if VM_PRESSURE_EVENTS
        if (errors) {
                memorystatus_clear_errors();
        }
 
 #if VM_PRESSURE_EVENTS
-       memorystatus_update_vm_pressure(TRUE);
+       /*
+        * LD: We used to target the foreground process first and foremost here.
+        * Now, we target all processes, starting from the non-suspended, background
+        * processes first. We will target foreground too.
+        *
+        * memorystatus_update_vm_pressure(TRUE);
+        */
+       //vm_pressure_response();
 #endif
 
        if (post_snapshot) {
                size_t snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) +
                        sizeof(memorystatus_jetsam_snapshot_entry_t) * (memorystatus_jetsam_snapshot_count);
 #endif
 
        if (post_snapshot) {
                size_t snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) +
                        sizeof(memorystatus_jetsam_snapshot_entry_t) * (memorystatus_jetsam_snapshot_count);
-               memorystatus_jetsam_snapshot->notification_time = mach_absolute_time();
-               memorystatus_send_note(kMemorystatusSnapshotNote, &snapshot_size, sizeof(snapshot_size));
+               uint64_t timestamp_now = mach_absolute_time();
+               memorystatus_jetsam_snapshot->notification_time = timestamp_now;
+               if (memorystatus_jetsam_snapshot_last_timestamp == 0 ||
+                               timestamp_now > memorystatus_jetsam_snapshot_last_timestamp + memorystatus_jetsam_snapshot_timeout) {
+                       int ret = memorystatus_send_note(kMemorystatusSnapshotNote, &snapshot_size, sizeof(snapshot_size));
+                       if (!ret) {
+                               proc_list_lock();
+                               memorystatus_jetsam_snapshot_last_timestamp = timestamp_now;
+                               proc_list_unlock();
+                       }
+               }
        }
        }
-       
+
        KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_SCAN) | DBG_FUNC_END,
                memorystatus_available_pages, 0, 0, 0, 0);
 
 #else /* CONFIG_JETSAM */
 
        KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_SCAN) | DBG_FUNC_END,
                memorystatus_available_pages, 0, 0, 0, 0);
 
 #else /* CONFIG_JETSAM */
 
-       /* Simple version.
-        *
-        * Jetsam not enabled, so just kill the first suitable clean process
-        * and sleep.
+       /*
+        * Jetsam not enabled
         */
 
         */
 
-       if (kill_idle_exit) {
-               kill_idle_exit_proc();
-               kill_idle_exit = FALSE;
-       }
-       
 #endif /* CONFIG_JETSAM */
 
        memorystatus_thread_block(0, memorystatus_thread);
 }
 
 #if !CONFIG_JETSAM
 #endif /* CONFIG_JETSAM */
 
        memorystatus_thread_block(0, memorystatus_thread);
 }
 
 #if !CONFIG_JETSAM
+/*
+ * Returns TRUE:
+ *     when an idle-exitable proc was killed
+ * Returns FALSE:
+ *     when there are no more idle-exitable procs found
+ *     when the attempt to kill an idle-exitable proc failed
+ */
 boolean_t memorystatus_idle_exit_from_VM(void) {
 boolean_t memorystatus_idle_exit_from_VM(void) {
-       kill_idle_exit = TRUE;  
-       memorystatus_thread_wake();
-       return TRUE;
+       return(kill_idle_exit_proc());
 }
 }
-#endif
+#endif /* !CONFIG_JETSAM */
 
 #if CONFIG_JETSAM
 
 
 #if CONFIG_JETSAM
 
@@ -1551,41 +2729,130 @@ boolean_t memorystatus_idle_exit_from_VM(void) {
  * (dirty pages + IOKit mappings)
  *
  * This is invoked for both advisory, non-fatal per-task high watermarks,
  * (dirty pages + IOKit mappings)
  *
  * This is invoked for both advisory, non-fatal per-task high watermarks,
- * as well as the fatal system-wide task memory limit.
+ * as well as the fatal task memory limits.
  */
 void
 memorystatus_on_ledger_footprint_exceeded(boolean_t warning, const int max_footprint_mb)
 {
  */
 void
 memorystatus_on_ledger_footprint_exceeded(boolean_t warning, const int max_footprint_mb)
 {
+       boolean_t is_active;
+       boolean_t is_fatal;
+
        proc_t p = current_proc();
        proc_t p = current_proc();
-    
-       printf("process %d (%s) %s physical memory footprint limit of %d MB\n",
-               p->p_pid, p->p_comm,
-               warning ? "approaching" : "exceeded",
-               max_footprint_mb);
+
+       proc_list_lock();
+
+       is_active = proc_jetsam_state_is_active_locked(p);
+       is_fatal = (p->p_memstat_state & P_MEMSTAT_FATAL_MEMLIMIT);
+
+       if (warning == FALSE) {
+               /*
+                * We only want the EXC_RESOURCE to trigger once per lifetime
+                * of the active/inactive limit state. So, here, we detect the
+                * active/inactive state of the process and mark the
+                * state as exception has been triggered.
+                */
+               if (is_active == TRUE) {
+                       /*
+                        * turn off exceptions for active state
+                        */
+                       p->p_memstat_state |= P_MEMSTAT_MEMLIMIT_ACTIVE_EXC_TRIGGERED;
+               } else {
+                       /*
+                        * turn off exceptions for inactive state
+                        */
+                       p->p_memstat_state |= P_MEMSTAT_MEMLIMIT_INACTIVE_EXC_TRIGGERED;
+               }
+
+               /*
+                * Soft memory limit is a non-fatal high-water-mark
+                * Hard memory limit is a fatal custom-task-limit or system-wide per-task memory limit.
+                */
+               printf("process %d (%s) exceeded physical memory footprint, the %s%sMemoryLimit of %d MB\n",
+                      p->p_pid, p->p_comm, (is_active ? "Active" : "Inactive"),
+                      (is_fatal  ? "Hard" : "Soft"), max_footprint_mb);
+
+       }
+
+       proc_list_unlock();
 
 #if VM_PRESSURE_EVENTS
        if (warning == TRUE) {
 
 #if VM_PRESSURE_EVENTS
        if (warning == TRUE) {
-               if (memorystatus_warn_process(p->p_pid) != TRUE) {
+               if (memorystatus_warn_process(p->p_pid, TRUE /* critical? */) != TRUE) {
                        /* Print warning, since it's possible that task has not registered for pressure notifications */
                        /* Print warning, since it's possible that task has not registered for pressure notifications */
-                       printf("task_exceeded_footprint: failed to warn the current task (exiting?).\n");                       
+                       printf("task_exceeded_footprint: failed to warn the current task (exiting, or no handler registered?).\n");
                }
                return;
        }
 #endif /* VM_PRESSURE_EVENTS */
 
                }
                return;
        }
 #endif /* VM_PRESSURE_EVENTS */
 
-       if (p->p_memstat_memlimit <= 0) {
+       if (is_fatal) {
                /*
                /*
-                * If this process has no high watermark, then we have been invoked because the task
-                * has violated the system-wide per-task memory limit.
+                * If this process has no high watermark or has a fatal task limit, then we have been invoked because the task
+                * has violated either the system-wide per-task memory limit OR its own task limit.
                 */
                if (memorystatus_kill_process_sync(p->p_pid, kMemorystatusKilledPerProcessLimit) != TRUE) {
                        printf("task_exceeded_footprint: failed to kill the current task (exiting?).\n");
                }
                 */
                if (memorystatus_kill_process_sync(p->p_pid, kMemorystatusKilledPerProcessLimit) != TRUE) {
                        printf("task_exceeded_footprint: failed to kill the current task (exiting?).\n");
                }
+       } else {
+               /*
+                * HWM offender exists. Done without locks or synchronization.
+                * See comment near its declaration for more details.
+                */
+               memorystatus_hwm_candidates = TRUE;
+       }
+}
+
+/*
+ * Toggle the P_MEMSTAT_TERMINATED state.
+ * Takes the proc_list_lock.
+ */
+void
+proc_memstat_terminated(proc_t p, boolean_t set)
+{
+#if DEVELOPMENT || DEBUG
+       if (p) {
+               proc_list_lock();
+               if (set == TRUE) {
+                       p->p_memstat_state |= P_MEMSTAT_TERMINATED;
+               } else {
+                       p->p_memstat_state &= ~P_MEMSTAT_TERMINATED;
+               }
+               proc_list_unlock();
+       }
+#else
+#pragma unused(p, set)
+       /*
+        * do nothing
+        */
+#endif /* DEVELOPMENT || DEBUG */
+       return;
+}
+
+/*
+ * This is invoked when cpulimits have been exceeded while in fatal mode.
+ * The jetsam_flags do not apply as those are for memory related kills.
+ * We call this routine so that the offending process is killed with 
+ * a non-zero exit status.
+ */
+void
+jetsam_on_ledger_cpulimit_exceeded(void)
+{
+       int retval = 0;
+       int jetsam_flags = 0;  /* make it obvious */
+       proc_t p = current_proc();
+
+       printf("task_exceeded_cpulimit: killing pid %d [%s]\n",
+              p->p_pid, (p->p_comm ? p->p_comm : "(unknown)"));
+
+       retval = jetsam_do_kill(p, jetsam_flags);
+       
+       if (retval) {
+               printf("task_exceeded_cpulimit: failed to kill current task (exiting?).\n");
        }
 }
 
 static void
        }
 }
 
 static void
-memorystatus_get_task_page_counts(task_t task, uint32_t *footprint, uint32_t *max_footprint)
+memorystatus_get_task_page_counts(task_t task, uint32_t *footprint, uint32_t *max_footprint, uint32_t *max_footprint_lifetime, uint32_t *purgeable_pages)
 {
        assert(task);
        assert(footprint);
 {
        assert(task);
        assert(footprint);
@@ -1594,33 +2861,16 @@ memorystatus_get_task_page_counts(task_t task, uint32_t *footprint, uint32_t *ma
        if (max_footprint) {
                *max_footprint = (uint32_t)(get_task_phys_footprint_max(task) / PAGE_SIZE_64);
        }
        if (max_footprint) {
                *max_footprint = (uint32_t)(get_task_phys_footprint_max(task) / PAGE_SIZE_64);
        }
-}
-
-static int
-memorystatus_send_note(int event_code, void *data, size_t data_length) {
-       int ret;
-       struct kev_msg ev_msg;
-       
-       ev_msg.vendor_code    = KEV_VENDOR_APPLE;
-       ev_msg.kev_class      = KEV_SYSTEM_CLASS;
-       ev_msg.kev_subclass   = KEV_MEMORYSTATUS_SUBCLASS;
-
-       ev_msg.event_code     = event_code;
-
-       ev_msg.dv[0].data_length = data_length;
-       ev_msg.dv[0].data_ptr = data;
-       ev_msg.dv[1].data_length = 0;
-
-       ret = kev_post_msg(&ev_msg);
-       if (ret) {
-               printf("%s: kev_post_msg() failed, err %d\n", __func__, ret);
+       if (max_footprint_lifetime) {
+               *max_footprint_lifetime = (uint32_t)(get_task_resident_max(task) / PAGE_SIZE_64);
+       }
+       if (purgeable_pages) {
+               *purgeable_pages = (uint32_t)(get_task_purgeable_size(task) / PAGE_SIZE_64);
        }
        }
-       
-       return ret;
 }
 
 static void
 }
 
 static void
-memorystatus_update_snapshot_locked(proc_t p, uint32_t kill_cause)
+memorystatus_update_jetsam_snapshot_entry_locked(proc_t p, uint32_t kill_cause)
 {
        unsigned int i;
 
 {
        unsigned int i;
 
@@ -1642,6 +2892,25 @@ memorystatus_update_snapshot_locked(proc_t p, uint32_t kill_cause)
 
 void memorystatus_pages_update(unsigned int pages_avail)
 {
 
 void memorystatus_pages_update(unsigned int pages_avail)
 {
+       memorystatus_available_pages = pages_avail;
+
+#if VM_PRESSURE_EVENTS
+       /*
+        * Since memorystatus_available_pages changes, we should
+        * re-evaluate the pressure levels on the system and 
+        * check if we need to wake the pressure thread.
+        * We also update memorystatus_level in that routine.
+        */ 
+       vm_pressure_response();
+
+       if (memorystatus_available_pages <= memorystatus_available_pages_pressure) {
+
+               if (memorystatus_hwm_candidates || (memorystatus_available_pages <= memorystatus_available_pages_critical)) {
+                       memorystatus_thread_wake();
+               }
+       }
+#else /* VM_PRESSURE_EVENTS */
+
        boolean_t critical, delta;
         
        if (!memorystatus_delta) {
        boolean_t critical, delta;
         
        if (!memorystatus_delta) {
@@ -1653,87 +2922,131 @@ void memorystatus_pages_update(unsigned int pages_avail)
                 || (memorystatus_available_pages >= (pages_avail + memorystatus_delta))) ? TRUE : FALSE;
         
        if (critical || delta) {
                 || (memorystatus_available_pages >= (pages_avail + memorystatus_delta))) ? TRUE : FALSE;
         
        if (critical || delta) {
-               memorystatus_available_pages = pages_avail;
                memorystatus_level = memorystatus_available_pages * 100 / atop_64(max_mem);
                memorystatus_level = memorystatus_available_pages * 100 / atop_64(max_mem);
-
-#if LATENCY_JETSAM
-               /* Bail early to avoid excessive wake-ups */
-               if (critical) {
-                       return;
-               }
-#endif
-
                memorystatus_thread_wake();
        }
                memorystatus_thread_wake();
        }
+#endif /* VM_PRESSURE_EVENTS */
 }
 
 static boolean_t
 }
 
 static boolean_t
-memorystatus_get_snapshot_properties_for_proc_locked(proc_t p, memorystatus_jetsam_snapshot_entry_t *entry)
+memorystatus_init_jetsam_snapshot_entry_locked(proc_t p, memorystatus_jetsam_snapshot_entry_t *entry)
 {      
 {      
+       clock_sec_t                     tv_sec;
+       clock_usec_t                    tv_usec;
+
        memset(entry, 0, sizeof(memorystatus_jetsam_snapshot_entry_t));
        
        entry->pid = p->p_pid;
        strlcpy(&entry->name[0], p->p_comm, MAXCOMLEN+1);
        entry->priority = p->p_memstat_effectivepriority;
        memset(entry, 0, sizeof(memorystatus_jetsam_snapshot_entry_t));
        
        entry->pid = p->p_pid;
        strlcpy(&entry->name[0], p->p_comm, MAXCOMLEN+1);
        entry->priority = p->p_memstat_effectivepriority;
-       memorystatus_get_task_page_counts(p->task, &entry->pages, &entry->max_pages);
+       memorystatus_get_task_page_counts(p->task, &entry->pages, &entry->max_pages, &entry->max_pages_lifetime, &entry->purgeable_pages);
        entry->state = memorystatus_build_state(p);
        entry->user_data = p->p_memstat_userdata;
        memcpy(&entry->uuid[0], &p->p_uuid[0], sizeof(p->p_uuid));
        entry->state = memorystatus_build_state(p);
        entry->user_data = p->p_memstat_userdata;
        memcpy(&entry->uuid[0], &p->p_uuid[0], sizeof(p->p_uuid));
+       entry->fds = p->p_fd->fd_nfiles;
+
+       absolutetime_to_microtime(get_task_cpu_time(p->task), &tv_sec, &tv_usec);
+       entry->cpu_time.tv_sec = tv_sec;
+       entry->cpu_time.tv_usec = tv_usec;
 
        return TRUE;    
 }
 
 static void
 
        return TRUE;    
 }
 
 static void
-memorystatus_jetsam_snapshot_procs_locked(void)
+memorystatus_init_snapshot_vmstats(memorystatus_jetsam_snapshot_t *snapshot)
 {
 {
-       proc_t p, next_p;
-       unsigned int b = 0, i = 0;
        kern_return_t kr = KERN_SUCCESS;
        kern_return_t kr = KERN_SUCCESS;
-
        mach_msg_type_number_t  count = HOST_VM_INFO64_COUNT;
        vm_statistics64_data_t  vm_stat;
 
        if ((kr = host_statistics64(host_self(), HOST_VM_INFO64, (host_info64_t)&vm_stat, &count) != KERN_SUCCESS)) {
        mach_msg_type_number_t  count = HOST_VM_INFO64_COUNT;
        vm_statistics64_data_t  vm_stat;
 
        if ((kr = host_statistics64(host_self(), HOST_VM_INFO64, (host_info64_t)&vm_stat, &count) != KERN_SUCCESS)) {
-               printf("memorystatus_jetsam_snapshot_procs_locked: host_statistics64 failed with %d\n", kr);
-               memset(&memorystatus_jetsam_snapshot->stats, 0, sizeof(memorystatus_jetsam_snapshot->stats));
+               printf("memorystatus_init_jetsam_snapshot_stats: host_statistics64 failed with %d\n", kr);
+               memset(&snapshot->stats, 0, sizeof(snapshot->stats));
        } else {
        } else {
-               memorystatus_jetsam_snapshot->stats.free_pages = vm_stat.free_count;
-               memorystatus_jetsam_snapshot->stats.active_pages = vm_stat.active_count;
-               memorystatus_jetsam_snapshot->stats.inactive_pages = vm_stat.inactive_count;
-               memorystatus_jetsam_snapshot->stats.throttled_pages = vm_stat.throttled_count;
-               memorystatus_jetsam_snapshot->stats.purgeable_pages = vm_stat.purgeable_count;
-               memorystatus_jetsam_snapshot->stats.wired_pages = vm_stat.wire_count;
-               
-               memorystatus_jetsam_snapshot->stats.speculative_pages = vm_stat.speculative_count;
-               memorystatus_jetsam_snapshot->stats.filebacked_pages = vm_stat.external_page_count;
-               memorystatus_jetsam_snapshot->stats.anonymous_pages = vm_stat.internal_page_count;
-               memorystatus_jetsam_snapshot->stats.compressions = vm_stat.compressions;
-               memorystatus_jetsam_snapshot->stats.decompressions = vm_stat.decompressions;
-               memorystatus_jetsam_snapshot->stats.compressor_pages = vm_stat.compressor_page_count;
-               memorystatus_jetsam_snapshot->stats.total_uncompressed_pages_in_compressor = vm_stat.total_uncompressed_pages_in_compressor;
+               snapshot->stats.free_pages      = vm_stat.free_count;
+               snapshot->stats.active_pages    = vm_stat.active_count;
+               snapshot->stats.inactive_pages  = vm_stat.inactive_count;
+               snapshot->stats.throttled_pages = vm_stat.throttled_count;
+               snapshot->stats.purgeable_pages = vm_stat.purgeable_count;
+               snapshot->stats.wired_pages     = vm_stat.wire_count;
+
+               snapshot->stats.speculative_pages = vm_stat.speculative_count;
+               snapshot->stats.filebacked_pages  = vm_stat.external_page_count;
+               snapshot->stats.anonymous_pages   = vm_stat.internal_page_count;
+               snapshot->stats.compressions      = vm_stat.compressions;
+               snapshot->stats.decompressions    = vm_stat.decompressions;
+               snapshot->stats.compressor_pages  = vm_stat.compressor_page_count;
+               snapshot->stats.total_uncompressed_pages_in_compressor = vm_stat.total_uncompressed_pages_in_compressor;
+       }
+}
+
+/*
+ * Collect vm statistics at boot.
+ * Called only once (see kern_exec.c)
+ * Data can be consumed at any time.
+ */
+void
+memorystatus_init_at_boot_snapshot() {
+       memorystatus_init_snapshot_vmstats(&memorystatus_at_boot_snapshot);
+       memorystatus_at_boot_snapshot.entry_count = 0;
+       memorystatus_at_boot_snapshot.notification_time = 0;   /* updated when consumed */
+       memorystatus_at_boot_snapshot.snapshot_time = mach_absolute_time();
+}
+
+static void
+memorystatus_init_jetsam_snapshot_locked(memorystatus_jetsam_snapshot_t *od_snapshot, uint32_t ods_list_count )
+{
+       proc_t p, next_p;
+       unsigned int b = 0, i = 0;
+
+       memorystatus_jetsam_snapshot_t *snapshot = NULL;
+       memorystatus_jetsam_snapshot_entry_t *snapshot_list = NULL;
+       unsigned int snapshot_max = 0;
+
+       if (od_snapshot) {
+               /*
+                * This is an on_demand snapshot
+                */
+               snapshot      = od_snapshot;
+               snapshot_list = od_snapshot->entries;
+               snapshot_max  = ods_list_count;
+       } else {
+               /*
+                * This is a jetsam event snapshot
+                */
+               snapshot      = memorystatus_jetsam_snapshot;
+               snapshot_list = memorystatus_jetsam_snapshot->entries;
+               snapshot_max  = memorystatus_jetsam_snapshot_max;
        }
 
        }
 
+       memorystatus_init_snapshot_vmstats(snapshot);
+
        next_p = memorystatus_get_first_proc_locked(&b, TRUE);
        while (next_p) {
                p = next_p;
                next_p = memorystatus_get_next_proc_locked(&b, p, TRUE);
                
        next_p = memorystatus_get_first_proc_locked(&b, TRUE);
        while (next_p) {
                p = next_p;
                next_p = memorystatus_get_next_proc_locked(&b, p, TRUE);
                
-               if (FALSE == memorystatus_get_snapshot_properties_for_proc_locked(p, &memorystatus_jetsam_snapshot_list[i])) {
+               if (FALSE == memorystatus_init_jetsam_snapshot_entry_locked(p, &snapshot_list[i])) {
                        continue;
                }
                
                        continue;
                }
                
-               MEMORYSTATUS_DEBUG(0, "jetsam snapshot pid %d, uuid = %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
+               MEMORYSTATUS_DEBUG(0, "jetsam snapshot pid %d, uuid = %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
                        p->p_pid, 
                        p->p_uuid[0], p->p_uuid[1], p->p_uuid[2], p->p_uuid[3], p->p_uuid[4], p->p_uuid[5], p->p_uuid[6], p->p_uuid[7],
                        p->p_uuid[8], p->p_uuid[9], p->p_uuid[10], p->p_uuid[11], p->p_uuid[12], p->p_uuid[13], p->p_uuid[14], p->p_uuid[15]);
 
                        p->p_pid, 
                        p->p_uuid[0], p->p_uuid[1], p->p_uuid[2], p->p_uuid[3], p->p_uuid[4], p->p_uuid[5], p->p_uuid[6], p->p_uuid[7],
                        p->p_uuid[8], p->p_uuid[9], p->p_uuid[10], p->p_uuid[11], p->p_uuid[12], p->p_uuid[13], p->p_uuid[14], p->p_uuid[15]);
 
-               if (++i == memorystatus_jetsam_snapshot_max) {
+               if (++i == snapshot_max) {
                        break;
                }       
        }
 
                        break;
                }       
        }
 
-       memorystatus_jetsam_snapshot->snapshot_time = mach_absolute_time();
-       memorystatus_jetsam_snapshot->entry_count = memorystatus_jetsam_snapshot_count = i;
+       snapshot->snapshot_time = mach_absolute_time();
+       snapshot->entry_count = i;
+
+       if (!od_snapshot) {
+               /* update the system buffer count */
+               memorystatus_jetsam_snapshot_count = i;
+       }
 }
 
 #if DEVELOPMENT || DEBUG
 }
 
 #if DEVELOPMENT || DEBUG
@@ -1762,6 +3075,30 @@ memorystatus_cmd_set_panic_bits(user_addr_t buffer, uint32_t buffer_size) {
        return ret;
 }
 
        return ret;
 }
 
+/*
+ * Triggers a sort_order on a specified jetsam priority band.
+ * This is for testing only, used to force a path through the sort
+ * function.
+ */
+static int
+memorystatus_cmd_test_jetsam_sort(int priority, int sort_order) {
+
+       int error = 0;
+
+       unsigned int bucket_index = 0;
+
+       if (priority == -1) {
+               /* Use as shorthand for default priority */
+               bucket_index = JETSAM_PRIORITY_DEFAULT;
+       } else {
+               bucket_index = (unsigned int)priority;
+       }
+
+       error = memorystatus_sort_bucket(bucket_index, sort_order);
+
+       return (error);
+}
+
 #endif
 
 /*
 #endif
 
 /*
@@ -1773,23 +3110,24 @@ memorystatus_kill_specific_process(pid_t victim_pid, uint32_t cause) {
        proc_t p;
 
        /* TODO - add a victim queue and push this into the main jetsam thread */
        proc_t p;
 
        /* TODO - add a victim queue and push this into the main jetsam thread */
-
        p = proc_find(victim_pid);
        if (!p) {
                return FALSE;
        }
 
        p = proc_find(victim_pid);
        if (!p) {
                return FALSE;
        }
 
-       printf("memorystatus: specifically killing pid %d [%s] - memorystatus_available_pages: %d\n", 
-               victim_pid, (p->p_comm ? p->p_comm : "(unknown)"), memorystatus_available_pages);
-
        proc_list_lock();
 
        if (memorystatus_jetsam_snapshot_count == 0) {
        proc_list_lock();
 
        if (memorystatus_jetsam_snapshot_count == 0) {
-               memorystatus_jetsam_snapshot_procs_locked();
+               memorystatus_init_jetsam_snapshot_locked(NULL,0);
        }
 
        }
 
-       memorystatus_update_snapshot_locked(p, cause);
+       memorystatus_update_jetsam_snapshot_entry_locked(p, cause);
        proc_list_unlock();
        proc_list_unlock();
+
+       printf("memorystatus: specifically killing pid %d [%s] (%s %d) - memorystatus_available_pages: %d\n",
+               victim_pid, (p->p_comm ? p->p_comm : "(unknown)"),
+              jetsam_kill_cause_name[cause], p->p_memstat_effectivepriority, memorystatus_available_pages);
+
        
        killed = memorystatus_do_kill(p, cause);
        proc_rele(p);
        
        killed = memorystatus_do_kill(p, cause);
        proc_rele(p);
@@ -1801,12 +3139,14 @@ memorystatus_kill_specific_process(pid_t victim_pid, uint32_t cause) {
  * Jetsam the first process in the queue.
  */
 static boolean_t
  * Jetsam the first process in the queue.
  */
 static boolean_t
-memorystatus_kill_top_process(boolean_t any, uint32_t cause, int32_t *priority, uint32_t *errors)
+memorystatus_kill_top_process(boolean_t any, boolean_t sort_flag, uint32_t cause, int32_t *priority, uint32_t *errors)
 {
        pid_t aPid;
        proc_t p = PROC_NULL, next_p = PROC_NULL;
        boolean_t new_snapshot = FALSE, killed = FALSE;
 {
        pid_t aPid;
        proc_t p = PROC_NULL, next_p = PROC_NULL;
        boolean_t new_snapshot = FALSE, killed = FALSE;
+       int kill_count = 0;
        unsigned int i = 0;
        unsigned int i = 0;
+       uint32_t aPid_ep;
 
 #ifndef CONFIG_FREEZE
 #pragma unused(any)
 
 #ifndef CONFIG_FREEZE
 #pragma unused(any)
@@ -1815,6 +3155,11 @@ memorystatus_kill_top_process(boolean_t any, uint32_t cause, int32_t *priority,
        KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM) | DBG_FUNC_START,
                memorystatus_available_pages, 0, 0, 0, 0);
 
        KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM) | DBG_FUNC_START,
                memorystatus_available_pages, 0, 0, 0, 0);
 
+
+       if (sort_flag == TRUE) {
+               (void)memorystatus_sort_bucket(JETSAM_PRIORITY_FOREGROUND, JETSAM_SORT_DEFAULT);
+       }
+
        proc_list_lock();
 
        next_p = memorystatus_get_first_proc_locked(&i, TRUE);
        proc_list_lock();
 
        next_p = memorystatus_get_first_proc_locked(&i, TRUE);
@@ -1833,6 +3178,7 @@ memorystatus_kill_top_process(boolean_t any, uint32_t cause, int32_t *priority,
 #endif /* DEVELOPMENT || DEBUG */
                
                aPid = p->p_pid;
 #endif /* DEVELOPMENT || DEBUG */
                
                aPid = p->p_pid;
+               aPid_ep = p->p_memstat_effectivepriority;
 
                if (p->p_memstat_state & (P_MEMSTAT_ERROR | P_MEMSTAT_TERMINATED)) {
                        continue;
 
                if (p->p_memstat_state & (P_MEMSTAT_ERROR | P_MEMSTAT_TERMINATED)) {
                        continue;
@@ -1845,6 +3191,22 @@ memorystatus_kill_top_process(boolean_t any, uint32_t cause, int32_t *priority,
                }
 #endif /* DEVELOPMENT || DEBUG */
 
                }
 #endif /* DEVELOPMENT || DEBUG */
 
+               if (cause == kMemorystatusKilledVnodes)
+               {
+                       /*
+                        * If the system runs out of vnodes, we systematically jetsam
+                        * processes in hopes of stumbling onto a vnode gain that helps
+                        * the system recover.  The process that happens to trigger
+                        * this path has no known relationship to the vnode consumption.
+                        * We attempt to safeguard that process e.g: do not jetsam it.
+                        */
+
+                       if (p == current_proc()) {
+                               /* do not jetsam the current process */
+                               continue;
+                       }
+               }
+
 #if CONFIG_FREEZE
                boolean_t skip;
                boolean_t reclaim_proc = !(p->p_memstat_state & (P_MEMSTAT_LOCKED | P_MEMSTAT_NORECLAIM));
 #if CONFIG_FREEZE
                boolean_t skip;
                boolean_t reclaim_proc = !(p->p_memstat_state & (P_MEMSTAT_LOCKED | P_MEMSTAT_NORECLAIM));
@@ -1859,18 +3221,14 @@ memorystatus_kill_top_process(boolean_t any, uint32_t cause, int32_t *priority,
                } else
 #endif
                {
                } else
 #endif
                {
-                       if (priority) {
-                               *priority = p->p_memstat_effectivepriority;
-                       }
-                       
                        /*
                         * Capture a snapshot if none exists and:
                         * - priority was not requested (this is something other than an ambient kill)
                         * - the priority was requested *and* the targeted process is not at idle priority
                         */
                        if ((memorystatus_jetsam_snapshot_count == 0) && 
                        /*
                         * Capture a snapshot if none exists and:
                         * - priority was not requested (this is something other than an ambient kill)
                         * - the priority was requested *and* the targeted process is not at idle priority
                         */
                        if ((memorystatus_jetsam_snapshot_count == 0) && 
-                           ((!priority) || (priority && (*priority != JETSAM_PRIORITY_IDLE)))) {
-                               memorystatus_jetsam_snapshot_procs_locked();
+                               (memorystatus_idle_snapshot || ((!priority) || (priority && (*priority != JETSAM_PRIORITY_IDLE))))) {
+                               memorystatus_init_jetsam_snapshot_locked(NULL,0);
                                new_snapshot = TRUE;
                        }
                        
                                new_snapshot = TRUE;
                        }
                        
@@ -1886,7 +3244,7 @@ memorystatus_kill_top_process(boolean_t any, uint32_t cause, int32_t *priority,
                        if ((memorystatus_jetsam_policy & kPolicyDiagnoseActive) && activeProcess) {
                                MEMORYSTATUS_DEBUG(1, "jetsam: suspending pid %d [%s] (active) for diagnosis - memory_status_level: %d\n",
                                        aPid, (p->p_comm ? p->p_comm: "(unknown)"), memorystatus_level);
                        if ((memorystatus_jetsam_policy & kPolicyDiagnoseActive) && activeProcess) {
                                MEMORYSTATUS_DEBUG(1, "jetsam: suspending pid %d [%s] (active) for diagnosis - memory_status_level: %d\n",
                                        aPid, (p->p_comm ? p->p_comm: "(unknown)"), memorystatus_level);
-                               memorystatus_update_snapshot_locked(p, kMemorystatusKilledDiagnostic);
+                               memorystatus_update_jetsam_snapshot_entry_locked(p, kMemorystatusKilledDiagnostic);
                                p->p_memstat_state |= P_MEMSTAT_DIAG_SUSPENDED;
                                if (memorystatus_jetsam_policy & kPolicyDiagnoseFirst) {
                                        jetsam_diagnostic_suspended_one_active_proc = 1;
                                p->p_memstat_state |= P_MEMSTAT_DIAG_SUSPENDED;
                                if (memorystatus_jetsam_policy & kPolicyDiagnoseFirst) {
                                        jetsam_diagnostic_suspended_one_active_proc = 1;
@@ -1897,6 +3255,9 @@ memorystatus_kill_top_process(boolean_t any, uint32_t cause, int32_t *priority,
                                proc_list_unlock();
                                if (p) {
                                        task_suspend(p->task);
                                proc_list_unlock();
                                if (p) {
                                        task_suspend(p->task);
+                                       if (priority) {
+                                               *priority = aPid_ep;
+                                       }
                                        proc_rele(p);
                                        killed = TRUE;
                                }
                                        proc_rele(p);
                                        killed = TRUE;
                                }
@@ -1906,28 +3267,51 @@ memorystatus_kill_top_process(boolean_t any, uint32_t cause, int32_t *priority,
 #endif /* DEVELOPMENT || DEBUG */
                        {
                                /* Shift queue, update stats */
 #endif /* DEVELOPMENT || DEBUG */
                        {
                                /* Shift queue, update stats */
-                               memorystatus_update_snapshot_locked(p, cause);
-                               
-                               p = proc_ref_locked(p);
-                               proc_list_unlock();
-                               if (p) {
-                                       printf("memorystatus: jetsam killing pid %d [%s] - memorystatus_available_pages: %d\n", 
-                                               aPid, (p->p_comm ? p->p_comm : "(unknown)"), memorystatus_available_pages);
+                               memorystatus_update_jetsam_snapshot_entry_locked(p, cause);
+
+                               if (proc_ref_locked(p) == p) {
+                                       proc_list_unlock();
+                                       printf("memorystatus: %s %d [%s] (%s %d) - memorystatus_available_pages: %d\n",
+                                           ((aPid_ep == JETSAM_PRIORITY_IDLE) ?
+                                           "idle exiting pid" : "jetsam killing pid"),
+                                           aPid, (p->p_comm ? p->p_comm : "(unknown)"),
+                                              jetsam_kill_cause_name[cause], aPid_ep, memorystatus_available_pages);
+
                                        killed = memorystatus_do_kill(p, cause);
                                        killed = memorystatus_do_kill(p, cause);
-                               }
+
+                                       /* Success? */
+                                       if (killed) {
+                                               if (priority) {
+                                                       *priority = aPid_ep;
+                                               }
+                                               proc_rele(p);
+                                               kill_count++;
+                                               goto exit;
+                                       }
                                
                                
-                               /* Success? */
-                               if (killed) {
-                                       proc_rele(p);
-                                       goto exit;
+                                       /*
+                                        * Failure - first unwind the state,
+                                        * then fall through to restart the search.
+                                        */
+                                       proc_list_lock();
+                                       proc_rele_locked(p);
+                                       p->p_memstat_state &= ~P_MEMSTAT_TERMINATED;
+                                       p->p_memstat_state |= P_MEMSTAT_ERROR;
+                                       *errors += 1;
                                }
                                
                                }
                                
-                               /* Failure - unwind and restart. */
-                               proc_list_lock();
-                               proc_rele_locked(p);
-                               p->p_memstat_state &= ~P_MEMSTAT_TERMINATED;
-                               p->p_memstat_state |= P_MEMSTAT_ERROR;
-                               *errors += 1;
+                               /*
+                                * Failure - restart the search.
+                                *
+                                * We might have raced with "p" exiting on another core, resulting in no
+                                * ref on "p".  Or, we may have failed to kill "p".
+                                *
+                                * Either way, we fall thru to here, leaving the proc in the
+                                * P_MEMSTAT_TERMINATED state.
+                                *
+                                * And, we hold the the proc_list_lock at this point.
+                                */
+
                                i = 0;
                                next_p = memorystatus_get_first_proc_locked(&i, TRUE);
                        }
                                i = 0;
                                next_p = memorystatus_get_first_proc_locked(&i, TRUE);
                        }
@@ -1943,11 +3327,227 @@ exit:
        }
        
        KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM) | DBG_FUNC_END,
        }
        
        KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM) | DBG_FUNC_END,
-           memorystatus_available_pages, killed ? aPid : 0, 0, 0, 0);
+                             memorystatus_available_pages, killed ? aPid : 0, kill_count, 0, 0);
 
        return killed;
 }
 
 
        return killed;
 }
 
+/*
+ * Jetsam aggressively 
+ */
+static boolean_t
+memorystatus_kill_top_process_aggressive(boolean_t any, uint32_t cause, int aggr_count, int32_t priority_max, 
+                                        uint32_t *errors)
+{
+       pid_t aPid;
+       proc_t p = PROC_NULL, next_p = PROC_NULL;
+       boolean_t new_snapshot = FALSE, killed = FALSE;
+       int kill_count = 0;
+       unsigned int i = 0;
+       int32_t aPid_ep = 0;
+       unsigned int memorystatus_level_snapshot = 0;
+
+#pragma unused(any)
+
+       KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM) | DBG_FUNC_START,
+               memorystatus_available_pages, priority_max, 0, 0, 0);
+
+       memorystatus_sort_bucket(JETSAM_PRIORITY_FOREGROUND, JETSAM_SORT_DEFAULT);
+
+       proc_list_lock();
+
+       next_p = memorystatus_get_first_proc_locked(&i, TRUE);
+       while (next_p) {
+#if DEVELOPMENT || DEBUG
+               int activeProcess;
+               int procSuspendedForDiagnosis;
+#endif /* DEVELOPMENT || DEBUG */
+
+               if ((unsigned int)(next_p->p_memstat_effectivepriority) != i) {
+
+                       /*
+                        * We have raced with next_p running on another core, as it has
+                        * moved to a different jetsam priority band.  This means we have
+                        * lost our place in line while traversing the jetsam list.  We
+                        * attempt to recover by rewinding to the beginning of the band
+                        * we were already traversing.  By doing this, we do not guarantee
+                        * that no process escapes this aggressive march, but we can make
+                        * skipping an entire range of processes less likely. (PR-21069019)
+                        */
+
+                       MEMORYSTATUS_DEBUG(1, "memorystatus: aggressive%d: rewinding %s moved from band %d --> %d\n",
+                              aggr_count, next_p->p_comm, i, next_p->p_memstat_effectivepriority);
+
+                       next_p = memorystatus_get_first_proc_locked(&i, TRUE);
+                       continue;
+               }
+
+               p = next_p;
+               next_p = memorystatus_get_next_proc_locked(&i, p, TRUE);
+
+               if (p->p_memstat_effectivepriority > priority_max) {
+                       /* 
+                        * Bail out of this killing spree if we have
+                        * reached beyond the priority_max jetsam band.
+                        * That is, we kill up to and through the 
+                        * priority_max jetsam band.
+                        */
+                       proc_list_unlock();
+                       goto exit;
+               }
+               
+#if DEVELOPMENT || DEBUG
+               activeProcess = p->p_memstat_state & P_MEMSTAT_FOREGROUND;
+               procSuspendedForDiagnosis = p->p_memstat_state & P_MEMSTAT_DIAG_SUSPENDED;
+#endif /* DEVELOPMENT || DEBUG */
+               
+               aPid = p->p_pid;
+               aPid_ep = p->p_memstat_effectivepriority;
+
+               if (p->p_memstat_state & (P_MEMSTAT_ERROR | P_MEMSTAT_TERMINATED)) {
+                       continue;
+               }
+                   
+#if DEVELOPMENT || DEBUG
+               if ((memorystatus_jetsam_policy & kPolicyDiagnoseActive) && procSuspendedForDiagnosis) {
+                       printf("jetsam: continuing after ignoring proc suspended already for diagnosis - %d\n", aPid);
+                       continue;
+               }
+#endif /* DEVELOPMENT || DEBUG */
+
+               /*
+                * Capture a snapshot if none exists.
+                */
+               if (memorystatus_jetsam_snapshot_count == 0) {
+                       memorystatus_init_jetsam_snapshot_locked(NULL,0);
+                       new_snapshot = TRUE;
+               }
+                       
+               /* 
+                * Mark as terminated so that if exit1() indicates success, but the process (for example)
+                * is blocked in task_exception_notify(), it'll be skipped if encountered again - see 
+                * <rdar://problem/13553476>. This is cheaper than examining P_LEXIT, which requires the 
+                * acquisition of the proc lock.
+                */
+               p->p_memstat_state |= P_MEMSTAT_TERMINATED;
+                       
+               /* Shift queue, update stats */
+               memorystatus_update_jetsam_snapshot_entry_locked(p, cause);
+
+               /*
+                * In order to kill the target process, we will drop the proc_list_lock.
+                * To guaranteee that p and next_p don't disappear out from under the lock,
+                * we must take a ref on both.
+                * If we cannot get a reference, then it's likely we've raced with
+                * that process exiting on another core.
+                */
+               if (proc_ref_locked(p) == p) {
+                       if (next_p) {
+                               while (next_p && (proc_ref_locked(next_p) != next_p)) {
+                                       proc_t temp_p;
+
+                                        /*
+                                         * We must have raced with next_p exiting on another core.
+                                         * Recover by getting the next eligible process in the band.
+                                         */
+
+                                       MEMORYSTATUS_DEBUG(1, "memorystatus: aggressive%d: skipping %d [%s] (exiting?)\n",
+                                              aggr_count, next_p->p_pid, (next_p->p_comm ? next_p->p_comm : "(unknown)"));
+
+                                       temp_p = next_p;
+                                       next_p = memorystatus_get_next_proc_locked(&i, temp_p, TRUE);
+                                }
+                       }
+                       proc_list_unlock();
+
+                       printf("memorystatus: aggressive%d: %s %d [%s] (%s %d) - memorystatus_available_pages: %d\n",
+                              aggr_count,
+                              ((aPid_ep == JETSAM_PRIORITY_IDLE) ? "idle exiting pid" : "jetsam killing pid"),
+                              aPid, (p->p_comm ? p->p_comm : "(unknown)"),
+                              jetsam_kill_cause_name[cause], aPid_ep, memorystatus_available_pages);
+
+                       memorystatus_level_snapshot = memorystatus_level;
+
+                       killed = memorystatus_do_kill(p, cause);
+                               
+                       /* Success? */
+                       if (killed) {
+                               proc_rele(p);
+                               kill_count++;
+                               p = NULL;
+                               killed = FALSE;
+
+                               /* 
+                                * Continue the killing spree.
+                                */
+                               proc_list_lock();
+                               if (next_p) {
+                                       proc_rele_locked(next_p);
+                               }
+
+                               if (aPid_ep == JETSAM_PRIORITY_FOREGROUND && memorystatus_aggressive_jetsam_lenient == TRUE) {
+                                       if (memorystatus_level > memorystatus_level_snapshot && ((memorystatus_level - memorystatus_level_snapshot) >= AGGRESSIVE_JETSAM_LENIENT_MODE_THRESHOLD)) {
+#if DEVELOPMENT || DEBUG
+                                               printf("Disabling Lenient mode after one-time deployment.\n");
+#endif /* DEVELOPMENT || DEBUG */
+                                               memorystatus_aggressive_jetsam_lenient = FALSE;
+                                               break;
+                                       }
+                               }
+
+                               continue;
+                       }
+                                       
+                       /*
+                        * Failure - first unwind the state,
+                        * then fall through to restart the search.
+                        */
+                       proc_list_lock();
+                       proc_rele_locked(p);
+                       if (next_p) {
+                               proc_rele_locked(next_p);
+                       }
+                       p->p_memstat_state &= ~P_MEMSTAT_TERMINATED;
+                       p->p_memstat_state |= P_MEMSTAT_ERROR;
+                       *errors += 1;
+                       p = NULL;
+               }
+
+               /*
+                * Failure - restart the search at the beginning of
+                * the band we were already traversing.
+                *
+                * We might have raced with "p" exiting on another core, resulting in no
+                * ref on "p".  Or, we may have failed to kill "p".
+                *
+                * Either way, we fall thru to here, leaving the proc in the 
+                * P_MEMSTAT_TERMINATED or P_MEMSTAT_ERROR state.
+                *
+                * And, we hold the the proc_list_lock at this point.
+                */
+
+               next_p = memorystatus_get_first_proc_locked(&i, TRUE);
+       }
+       
+       proc_list_unlock();
+       
+exit:
+       /* Clear snapshot if freshly captured and no target was found */
+       if (new_snapshot && (kill_count == 0)) {
+           memorystatus_jetsam_snapshot->entry_count = memorystatus_jetsam_snapshot_count = 0;
+       }
+       
+       KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM) | DBG_FUNC_END,
+                             memorystatus_available_pages, killed ? aPid : 0, kill_count, 0, 0);
+
+       if (kill_count > 0) {
+               return(TRUE);
+       }
+       else {
+               return(FALSE);
+       }
+}
+
 #if LEGACY_HIWATER
 
 static boolean_t
 #if LEGACY_HIWATER
 
 static boolean_t
@@ -1956,7 +3556,9 @@ memorystatus_kill_hiwat_proc(uint32_t *errors)
        pid_t aPid = 0;
        proc_t p = PROC_NULL, next_p = PROC_NULL;
        boolean_t new_snapshot = FALSE, killed = FALSE;
        pid_t aPid = 0;
        proc_t p = PROC_NULL, next_p = PROC_NULL;
        boolean_t new_snapshot = FALSE, killed = FALSE;
+       int kill_count = 0;
        unsigned int i = 0;
        unsigned int i = 0;
+       uint32_t aPid_ep;
        
        KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM_HIWAT) | DBG_FUNC_START,
                memorystatus_available_pages, 0, 0, 0, 0);
        
        KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM_HIWAT) | DBG_FUNC_START,
                memorystatus_available_pages, 0, 0, 0, 0);
@@ -1972,6 +3574,7 @@ memorystatus_kill_hiwat_proc(uint32_t *errors)
                next_p = memorystatus_get_next_proc_locked(&i, p, TRUE);
                
                aPid = p->p_pid;
                next_p = memorystatus_get_next_proc_locked(&i, p, TRUE);
                
                aPid = p->p_pid;
+               aPid_ep = p->p_memstat_effectivepriority;
                
                if (p->p_memstat_state  & (P_MEMSTAT_ERROR | P_MEMSTAT_TERMINATED)) {
                        continue;
                
                if (p->p_memstat_state  & (P_MEMSTAT_ERROR | P_MEMSTAT_TERMINATED)) {
                        continue;
@@ -1981,14 +3584,24 @@ memorystatus_kill_hiwat_proc(uint32_t *errors)
                if (p->p_memstat_memlimit <= 0) {
                        continue;
                }
                if (p->p_memstat_memlimit <= 0) {
                        continue;
                }
-               
+
+#if 0
+               /*
+                * No need to consider P_MEMSTAT_MEMLIMIT_BACKGROUND anymore.
+                * Background limits are described via the inactive limit slots.
+                * Their fatal/non-fatal setting will drive whether or not to be
+                * considered in this kill path.
+                */
+
                /* skip if a currently inapplicable limit is encountered */
                if ((p->p_memstat_state & P_MEMSTAT_MEMLIMIT_BACKGROUND) && (p->p_memstat_effectivepriority >= JETSAM_PRIORITY_FOREGROUND)) {          
                        continue;
                }
                /* skip if a currently inapplicable limit is encountered */
                if ((p->p_memstat_state & P_MEMSTAT_MEMLIMIT_BACKGROUND) && (p->p_memstat_effectivepriority >= JETSAM_PRIORITY_FOREGROUND)) {          
                        continue;
                }
+#endif
 
                footprint = (uint32_t)(get_task_phys_footprint(p->task) / (1024 * 1024));
                skip = (((int32_t)footprint) <= p->p_memstat_memlimit);
 
                footprint = (uint32_t)(get_task_phys_footprint(p->task) / (1024 * 1024));
                skip = (((int32_t)footprint) <= p->p_memstat_memlimit);
+
 #if DEVELOPMENT || DEBUG
                if (!skip && (memorystatus_jetsam_policy & kPolicyDiagnoseActive)) {
                        if (p->p_memstat_state & P_MEMSTAT_DIAG_SUSPENDED) {
 #if DEVELOPMENT || DEBUG
                if (!skip && (memorystatus_jetsam_policy & kPolicyDiagnoseActive)) {
                        if (p->p_memstat_state & P_MEMSTAT_DIAG_SUSPENDED) {
@@ -2010,11 +3623,11 @@ memorystatus_kill_hiwat_proc(uint32_t *errors)
                if (skip) {
                        continue;
                } else {
                if (skip) {
                        continue;
                } else {
-                       MEMORYSTATUS_DEBUG(1, "jetsam: %s pid %d [%s] - %d pages > 1 (%d)\n",
-                               (memorystatus_jetsam_policy & kPolicyDiagnoseActive) ? "suspending": "killing", aPid, p->p_comm, pages, hiwat);
+                       MEMORYSTATUS_DEBUG(1, "jetsam: %s pid %d [%s] - %d Mb > 1 (%d Mb)\n",
+                               (memorystatus_jetsam_policy & kPolicyDiagnoseActive) ? "suspending": "killing", aPid, p->p_comm, footprint, p->p_memstat_memlimit);
                                
                        if (memorystatus_jetsam_snapshot_count == 0) {
                                
                        if (memorystatus_jetsam_snapshot_count == 0) {
-                               memorystatus_jetsam_snapshot_procs_locked();
+                               memorystatus_init_jetsam_snapshot_locked(NULL,0);
                                new_snapshot = TRUE;
                        }
                        
                                new_snapshot = TRUE;
                        }
                        
@@ -2023,7 +3636,7 @@ memorystatus_kill_hiwat_proc(uint32_t *errors)
 #if DEVELOPMENT || DEBUG
                        if (memorystatus_jetsam_policy & kPolicyDiagnoseActive) {
                                MEMORYSTATUS_DEBUG(1, "jetsam: pid %d suspended for diagnosis - memorystatus_available_pages: %d\n", aPid, memorystatus_available_pages);
 #if DEVELOPMENT || DEBUG
                        if (memorystatus_jetsam_policy & kPolicyDiagnoseActive) {
                                MEMORYSTATUS_DEBUG(1, "jetsam: pid %d suspended for diagnosis - memorystatus_available_pages: %d\n", aPid, memorystatus_available_pages);
-                               memorystatus_update_snapshot_locked(p, kMemorystatusKilledDiagnostic);
+                               memorystatus_update_jetsam_snapshot_entry_locked(p, kMemorystatusKilledDiagnostic);
                                p->p_memstat_state |= P_MEMSTAT_DIAG_SUSPENDED;
                                
                                p = proc_ref_locked(p);
                                p->p_memstat_state |= P_MEMSTAT_DIAG_SUSPENDED;
                                
                                p = proc_ref_locked(p);
@@ -2038,28 +3651,46 @@ memorystatus_kill_hiwat_proc(uint32_t *errors)
                        } else
 #endif /* DEVELOPMENT || DEBUG */
                        {
                        } else
 #endif /* DEVELOPMENT || DEBUG */
                        {
-                               memorystatus_update_snapshot_locked(p, kMemorystatusKilledHiwat);
+                               memorystatus_update_jetsam_snapshot_entry_locked(p, kMemorystatusKilledHiwat);
                                
                                
-                               p = proc_ref_locked(p);
-                               proc_list_unlock();
-                               if (p) {
-                                   printf("memorystatus: jetsam killing pid %d [%s] (highwater) - memorystatus_available_pages: %d\n", 
-                                               aPid, (p->p_comm ? p->p_comm : "(unknown)"), memorystatus_available_pages);
-                                   killed = memorystatus_do_kill(p, kMemorystatusKilledHiwat);
-                               }
+                               if (proc_ref_locked(p) == p) {
+                                       proc_list_unlock();
+
+                                       printf("memorystatus: jetsam killing pid %d [%s] (highwater %d) - memorystatus_available_pages: %d\n", 
+                                              aPid, (p->p_comm ? p->p_comm : "(unknown)"), aPid_ep, memorystatus_available_pages);
+
+                                       killed = memorystatus_do_kill(p, kMemorystatusKilledHiwat);
                                
                                
-                               /* Success? */
-                               if (killed) {
-                                       proc_rele(p);
-                                       goto exit;
+                                       /* Success? */
+                                       if (killed) {
+                                               proc_rele(p);
+                                               kill_count++;
+                                               goto exit;
+                                       }
+
+                                       /*
+                                        * Failure - first unwind the state,
+                                        * then fall through to restart the search.
+                                        */
+                                       proc_list_lock();
+                                       proc_rele_locked(p);
+                                       p->p_memstat_state &= ~P_MEMSTAT_TERMINATED;
+                                       p->p_memstat_state |= P_MEMSTAT_ERROR;
+                                       *errors += 1;
                                }
 
                                }
 
-                               /* Failure - unwind and restart. */
-                               proc_list_lock();
-                               proc_rele_locked(p);
-                               p->p_memstat_state &= ~P_MEMSTAT_TERMINATED;
-                               p->p_memstat_state |= P_MEMSTAT_ERROR;
-                               *errors += 1;
+                               /*
+                                * Failure - restart the search.
+                                *
+                                * We might have raced with "p" exiting on another core, resulting in no
+                                * ref on "p".  Or, we may have failed to kill "p".
+                                *
+                                * Either way, we fall thru to here, leaving the proc in the 
+                                * P_MEMSTAT_TERMINATED state.
+                                *
+                                * And, we hold the the proc_list_lock at this point.
+                                */
+
                                i = 0;
                                next_p = memorystatus_get_first_proc_locked(&i, TRUE);
                        }
                                i = 0;
                                next_p = memorystatus_get_first_proc_locked(&i, TRUE);
                        }
@@ -2075,7 +3706,7 @@ exit:
        }
        
        KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM_HIWAT) | DBG_FUNC_END, 
        }
        
        KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_JETSAM_HIWAT) | DBG_FUNC_END, 
-           memorystatus_available_pages, killed ? aPid : 0, 0, 0, 0);
+                             memorystatus_available_pages, killed ? aPid : 0, kill_count, 0, 0);
 
        return killed;
 }
 
        return killed;
 }
@@ -2085,11 +3716,12 @@ exit:
 static boolean_t 
 memorystatus_kill_process_async(pid_t victim_pid, uint32_t cause) {
        /* TODO: allow a general async path */
 static boolean_t 
 memorystatus_kill_process_async(pid_t victim_pid, uint32_t cause) {
        /* TODO: allow a general async path */
-       if ((victim_pid != -1) || (cause != kMemorystatusKilledVMPageShortage || cause != kMemorystatusKilledVMThrashing)) {
+       if ((victim_pid != -1) || (cause != kMemorystatusKilledVMPageShortage && cause != kMemorystatusKilledVMThrashing &&
+                                  cause != kMemorystatusKilledFCThrashing)) {
                return FALSE;
        }
     
                return FALSE;
        }
     
-       kill_under_pressure = TRUE;
+       kill_under_pressure_cause = cause;
        memorystatus_thread_wake();
        return TRUE;
 }
        memorystatus_thread_wake();
        return TRUE;
 }
@@ -2101,7 +3733,7 @@ memorystatus_kill_process_sync(pid_t victim_pid, uint32_t cause) {
     
        if (victim_pid == -1) {
                /* No pid, so kill first process */
     
        if (victim_pid == -1) {
                /* No pid, so kill first process */
-               res = memorystatus_kill_top_process(TRUE, cause, NULL, &errors);
+               res = memorystatus_kill_top_process(TRUE, TRUE, cause, NULL, &errors);
        } else {
                res = memorystatus_kill_specific_process(victim_pid, cause);
        }
        } else {
                res = memorystatus_kill_specific_process(victim_pid, cause);
        }
@@ -2114,8 +3746,17 @@ memorystatus_kill_process_sync(pid_t victim_pid, uint32_t cause) {
                /* Fire off snapshot notification */
                size_t snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) + 
                        sizeof(memorystatus_jetsam_snapshot_entry_t) * memorystatus_jetsam_snapshot_count;
                /* Fire off snapshot notification */
                size_t snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) + 
                        sizeof(memorystatus_jetsam_snapshot_entry_t) * memorystatus_jetsam_snapshot_count;
-               memorystatus_jetsam_snapshot->notification_time = mach_absolute_time();
-               memorystatus_send_note(kMemorystatusSnapshotNote, &snapshot_size, sizeof(snapshot_size));
+               uint64_t timestamp_now = mach_absolute_time();
+               memorystatus_jetsam_snapshot->notification_time = timestamp_now;
+               if (memorystatus_jetsam_snapshot_last_timestamp == 0 ||
+                               timestamp_now > memorystatus_jetsam_snapshot_last_timestamp + memorystatus_jetsam_snapshot_timeout) {
+                       int ret = memorystatus_send_note(kMemorystatusSnapshotNote, &snapshot_size, sizeof(snapshot_size));
+                       if (!ret) {
+                               proc_list_lock();
+                               memorystatus_jetsam_snapshot_last_timestamp = timestamp_now;
+                               proc_list_unlock();
+                       }
+               }
        }
     
        return res;
        }
     
        return res;
@@ -2139,6 +3780,15 @@ memorystatus_kill_on_VM_thrashing(boolean_t async) {
        }
 }
 
        }
 }
 
+boolean_t
+memorystatus_kill_on_FC_thrashing(boolean_t async) {
+       if (async) {
+               return memorystatus_kill_process_async(-1, kMemorystatusKilledFCThrashing);
+       } else {
+               return memorystatus_kill_process_sync(-1, kMemorystatusKilledFCThrashing);
+       }
+}
+
 boolean_t 
 memorystatus_kill_on_vnode_limit(void) {
        return memorystatus_kill_process_sync(-1, kMemorystatusKilledVnodes);
 boolean_t 
 memorystatus_kill_on_vnode_limit(void) {
        return memorystatus_kill_process_sync(-1, kMemorystatusKilledVnodes);
@@ -2153,6 +3803,11 @@ memorystatus_freeze_init(void)
 {
        kern_return_t result;
        thread_t thread;
 {
        kern_return_t result;
        thread_t thread;
+
+       freezer_lck_grp_attr = lck_grp_attr_alloc_init();
+       freezer_lck_grp = lck_grp_alloc_init("freezer", freezer_lck_grp_attr);
+
+       lck_mtx_init(&freezer_mutex, freezer_lck_grp, NULL);
                
        result = kernel_thread_start(memorystatus_freeze_thread, NULL, &thread);
        if (result == KERN_SUCCESS) {
                
        result = kernel_thread_start(memorystatus_freeze_thread, NULL, &thread);
        if (result == KERN_SUCCESS) {
@@ -2162,63 +3817,219 @@ memorystatus_freeze_init(void)
        }
 }
 
        }
 }
 
-static int
-memorystatus_freeze_top_process(boolean_t *memorystatus_freeze_swap_low)
+/*
+ * Synchronously freeze the passed proc. Called with a reference to the proc held.
+ *
+ * Returns EINVAL or the value returned by task_freeze().
+ */
+int
+memorystatus_freeze_process_sync(proc_t p)
 {
 {
+       int ret = EINVAL;
        pid_t aPid = 0;
        pid_t aPid = 0;
-       int ret = -1;
-       proc_t p = PROC_NULL, next_p = PROC_NULL;
-       unsigned int i = 0;
+       boolean_t memorystatus_freeze_swap_low = FALSE;
 
        KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_FREEZE) | DBG_FUNC_START,
                memorystatus_available_pages, 0, 0, 0, 0);
 
 
        KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_FREEZE) | DBG_FUNC_START,
                memorystatus_available_pages, 0, 0, 0, 0);
 
+       lck_mtx_lock(&freezer_mutex);
+
+       if (p == NULL) {
+               goto exit;
+       }
+
+       if (memorystatus_freeze_enabled == FALSE) {
+               goto exit;
+       }
+
+       if (!memorystatus_can_freeze(&memorystatus_freeze_swap_low)) {
+               goto exit;
+       }
+
+       if (memorystatus_freeze_update_throttle()) {
+               printf("memorystatus_freeze_process_sync: in throttle, ignorning freeze\n");
+               memorystatus_freeze_throttle_count++;
+               goto exit;
+       }
+
        proc_list_lock();
        proc_list_lock();
-       
-       next_p = memorystatus_get_first_proc_locked(&i, TRUE);
-       while (next_p) {
-               kern_return_t kr;
-               uint32_t purgeable, wired, clean, dirty;
+
+       if (p != NULL) {
+               uint32_t purgeable, wired, clean, dirty, state;
+               uint32_t max_pages, pages, i;
                boolean_t shared;
                boolean_t shared;
-               uint32_t pages;
-               uint32_t max_pages = 0;
-               uint32_t state;
-               
-               p = next_p;
-               next_p = memorystatus_get_next_proc_locked(&i, p, TRUE);
 
                aPid = p->p_pid;
                state = p->p_memstat_state;
 
                /* Ensure the process is eligible for freezing */
                if ((state & (P_MEMSTAT_TERMINATED | P_MEMSTAT_LOCKED | P_MEMSTAT_FROZEN)) || !(state & P_MEMSTAT_SUSPENDED)) {
 
                aPid = p->p_pid;
                state = p->p_memstat_state;
 
                /* Ensure the process is eligible for freezing */
                if ((state & (P_MEMSTAT_TERMINATED | P_MEMSTAT_LOCKED | P_MEMSTAT_FROZEN)) || !(state & P_MEMSTAT_SUSPENDED)) {
-                       continue; // with lock held
+                       proc_list_unlock();
+                       goto exit;
                }
                }
-                                       
+
                /* Only freeze processes meeting our minimum resident page criteria */
                /* Only freeze processes meeting our minimum resident page criteria */
-               memorystatus_get_task_page_counts(p->task, &pages, NULL);
+               memorystatus_get_task_page_counts(p->task, &pages, NULL, NULL, NULL);
                if (pages < memorystatus_freeze_pages_min) {
                if (pages < memorystatus_freeze_pages_min) {
-                       continue; // with lock held
-               } 
+                       proc_list_unlock();
+                       goto exit;
+               }
+
+               if (DEFAULT_FREEZER_IS_ACTIVE || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPBACKED) {
+
+                       unsigned int avail_swap_space = 0; /* in pages. */
+
+                       if (DEFAULT_FREEZER_IS_ACTIVE) {
+                               /*
+                                * Freezer backed by default pager and swap file(s).
+                                */
+                               avail_swap_space = default_pager_swap_pages_free();
+                       } else {
+                               /*
+                                * Freezer backed by the compressor and swap file(s)
+                                * while will hold compressed data.
+                                */
+                               avail_swap_space = vm_swap_get_free_space() / PAGE_SIZE_64;
+                       }
+
+                       max_pages = MIN(avail_swap_space, memorystatus_freeze_pages_max);
 
 
-               if (DEFAULT_FREEZER_IS_ACTIVE || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE) {
-                       /* Ensure there's enough free space to freeze this process. */                  
-                       max_pages = MIN(default_pager_swap_pages_free(), memorystatus_freeze_pages_max);
                        if (max_pages < memorystatus_freeze_pages_min) {
                        if (max_pages < memorystatus_freeze_pages_min) {
-                               *memorystatus_freeze_swap_low = TRUE;
                                proc_list_unlock();
                                goto exit;
                        }
                } else {
                                proc_list_unlock();
                                goto exit;
                        }
                } else {
+                       /*
+                        * We only have the compressor without any swap.
+                        */
                        max_pages = UINT32_MAX - 1;
                }
                        max_pages = UINT32_MAX - 1;
                }
-               
+
                /* Mark as locked temporarily to avoid kill */
                p->p_memstat_state |= P_MEMSTAT_LOCKED;
                /* Mark as locked temporarily to avoid kill */
                p->p_memstat_state |= P_MEMSTAT_LOCKED;
+               proc_list_unlock();
 
 
-               p = proc_ref_locked(p);
-               proc_list_unlock();        
-               if (!p) {
+               ret = task_freeze(p->task, &purgeable, &wired, &clean, &dirty, max_pages, &shared, FALSE);
+
+               MEMORYSTATUS_DEBUG(1, "memorystatus_freeze_process_sync: task_freeze %s for pid %d [%s] - "
+                       "memorystatus_pages: %d, purgeable: %d, wired: %d, clean: %d, dirty: %d, shared %d, free swap: %d\n",
+                       (ret == KERN_SUCCESS) ? "SUCCEEDED" : "FAILED", aPid, (p->p_comm ? p->p_comm : "(unknown)"),
+                       memorystatus_available_pages, purgeable, wired, clean, dirty, shared, default_pager_swap_pages_free());
+
+               proc_list_lock();
+               p->p_memstat_state &= ~P_MEMSTAT_LOCKED;
+
+               if (ret == KERN_SUCCESS) {
+                       memorystatus_freeze_entry_t data = { aPid, TRUE, dirty };
+
+                       memorystatus_frozen_count++;
+
+                       p->p_memstat_state |= (P_MEMSTAT_FROZEN | (shared ? 0: P_MEMSTAT_NORECLAIM));
+
+                       if (DEFAULT_FREEZER_IS_ACTIVE || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPBACKED) {
+                               /* Update stats */
+                               for (i = 0; i < sizeof(throttle_intervals) / sizeof(struct throttle_interval_t); i++) {
+                                       throttle_intervals[i].pageouts += dirty;
+                               }
+                       }
+
+                       memorystatus_freeze_pageouts += dirty;
+                       memorystatus_freeze_count++;
+
+                       proc_list_unlock();
+
+                       memorystatus_send_note(kMemorystatusFreezeNote, &data, sizeof(data));
+               } else {
+                       proc_list_unlock();
+               }
+       }
+
+exit:
+       lck_mtx_unlock(&freezer_mutex);
+       KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_FREEZE) | DBG_FUNC_END,
+               memorystatus_available_pages, aPid, 0, 0, 0);
+
+       return ret;
+}
+
+static int
+memorystatus_freeze_top_process(boolean_t *memorystatus_freeze_swap_low)
+{
+       pid_t aPid = 0;
+       int ret = -1;
+       proc_t p = PROC_NULL, next_p = PROC_NULL;
+       unsigned int i = 0;
+
+       KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_FREEZE) | DBG_FUNC_START,
+               memorystatus_available_pages, 0, 0, 0, 0);
+
+       proc_list_lock();
+       
+       next_p = memorystatus_get_first_proc_locked(&i, TRUE);
+       while (next_p) {
+               kern_return_t kr;
+               uint32_t purgeable, wired, clean, dirty;
+               boolean_t shared;
+               uint32_t pages;
+               uint32_t max_pages = 0;
+               uint32_t state;
+               
+               p = next_p;
+               next_p = memorystatus_get_next_proc_locked(&i, p, TRUE);
+
+               aPid = p->p_pid;
+               state = p->p_memstat_state;
+
+               /* Ensure the process is eligible for freezing */
+               if ((state & (P_MEMSTAT_TERMINATED | P_MEMSTAT_LOCKED | P_MEMSTAT_FROZEN)) || !(state & P_MEMSTAT_SUSPENDED)) {
+                       continue; // with lock held
+               }
+                                       
+               /* Only freeze processes meeting our minimum resident page criteria */
+               memorystatus_get_task_page_counts(p->task, &pages, NULL, NULL, NULL);
+               if (pages < memorystatus_freeze_pages_min) {
+                       continue; // with lock held
+               } 
+
+               if (DEFAULT_FREEZER_IS_ACTIVE || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPBACKED) {
+
+                       /* Ensure there's enough free space to freeze this process. */
+
+                       unsigned int avail_swap_space = 0; /* in pages. */
+
+                       if (DEFAULT_FREEZER_IS_ACTIVE) {
+                               /*
+                                * Freezer backed by default pager and swap file(s).
+                                */
+                               avail_swap_space = default_pager_swap_pages_free();
+                       } else {
+                               /*
+                                * Freezer backed by the compressor and swap file(s)
+                                * while will hold compressed data.
+                                */
+                               avail_swap_space = vm_swap_get_free_space() / PAGE_SIZE_64;
+                       }
+
+                       max_pages = MIN(avail_swap_space, memorystatus_freeze_pages_max);
+
+                       if (max_pages < memorystatus_freeze_pages_min) {
+                               *memorystatus_freeze_swap_low = TRUE;
+                               proc_list_unlock();
+                               goto exit;
+                       }
+               } else {
+                       /*
+                        * We only have the compressor pool.
+                        */
+                       max_pages = UINT32_MAX - 1;
+               }
+               
+               /* Mark as locked temporarily to avoid kill */
+               p->p_memstat_state |= P_MEMSTAT_LOCKED;
+
+               p = proc_ref_locked(p);
+               proc_list_unlock();        
+               if (!p) {
                        goto exit;
                }
         
                        goto exit;
                }
         
@@ -2240,11 +4051,13 @@ memorystatus_freeze_top_process(boolean_t *memorystatus_freeze_swap_low)
                        
                        p->p_memstat_state |= (P_MEMSTAT_FROZEN | (shared ? 0: P_MEMSTAT_NORECLAIM));
                
                        
                        p->p_memstat_state |= (P_MEMSTAT_FROZEN | (shared ? 0: P_MEMSTAT_NORECLAIM));
                
-                       /* Update stats */
-                       for (i = 0; i < sizeof(throttle_intervals) / sizeof(struct throttle_interval_t); i++) {
-                                       throttle_intervals[i].pageouts += dirty;
+                       if (DEFAULT_FREEZER_IS_ACTIVE || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPBACKED) {
+                               /* Update stats */
+                               for (i = 0; i < sizeof(throttle_intervals) / sizeof(struct throttle_interval_t); i++) {
+                                       throttle_intervals[i].pageouts += dirty;
+                               }
                        }
                        }
-               
+
                        memorystatus_freeze_pageouts += dirty;
                        memorystatus_freeze_count++;
 
                        memorystatus_freeze_pageouts += dirty;
                        memorystatus_freeze_count++;
 
@@ -2252,8 +4065,8 @@ memorystatus_freeze_top_process(boolean_t *memorystatus_freeze_swap_low)
 
                        memorystatus_send_note(kMemorystatusFreezeNote, &data, sizeof(data));
 
 
                        memorystatus_send_note(kMemorystatusFreezeNote, &data, sizeof(data));
 
-                       /* Return the number of reclaimed pages */
-                       ret = dirty;
+                       /* Return KERN_SUCESS */
+                       ret = kr;
 
                } else {
                        proc_list_unlock();
 
                } else {
                        proc_list_unlock();
@@ -2314,6 +4127,8 @@ memorystatus_can_freeze_processes(void)
 static boolean_t 
 memorystatus_can_freeze(boolean_t *memorystatus_freeze_swap_low)
 {
 static boolean_t 
 memorystatus_can_freeze(boolean_t *memorystatus_freeze_swap_low)
 {
+       boolean_t can_freeze = TRUE;
+
        /* Only freeze if we're sufficiently low on memory; this holds off freeze right
           after boot,  and is generally is a no-op once we've reached steady state. */
        if (memorystatus_available_pages > memorystatus_freeze_threshold) {
        /* Only freeze if we're sufficiently low on memory; this holds off freeze right
           after boot,  and is generally is a no-op once we've reached steady state. */
        if (memorystatus_available_pages > memorystatus_freeze_threshold) {
@@ -2325,27 +4140,68 @@ memorystatus_can_freeze(boolean_t *memorystatus_freeze_swap_low)
                return FALSE;
        }
 
                return FALSE;
        }
 
-       /* Is swap running low? */
-       if (*memorystatus_freeze_swap_low) {
-               /* If there's been no movement in free swap pages since we last attempted freeze, return. */
-               if (default_pager_swap_pages_free() < memorystatus_freeze_pages_min) {
-                       return FALSE;
+       if (COMPRESSED_PAGER_IS_SWAPLESS || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPLESS) {
+               /*
+                * In-core compressor used for freezing WITHOUT on-disk swap support.
+                */
+
+               if (vm_compressor_low_on_space()) {
+                       if (*memorystatus_freeze_swap_low) {
+                               *memorystatus_freeze_swap_low = TRUE;
+                       }
+
+                       can_freeze = FALSE;
+
+               } else {
+                       if (*memorystatus_freeze_swap_low) {
+                               *memorystatus_freeze_swap_low = FALSE;
+                       }
+
+                       can_freeze = TRUE;
+               }
+       } else {
+               /*
+                * Freezing WITH on-disk swap support.
+                */
+
+               if (DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPBACKED) {
+                       /*
+                        * In-core compressor fronts the swap.
+                        */
+                       if (vm_swap_low_on_space()) {
+                               if (*memorystatus_freeze_swap_low) {
+                                       *memorystatus_freeze_swap_low = TRUE;
+                               }
+
+                               can_freeze = FALSE;
+                       }
+
+               } else if (DEFAULT_FREEZER_IS_ACTIVE) {
+                       /*
+                        * Legacy freeze mode with no compressor support.
+                        */
+                       if (default_pager_swap_pages_free() < memorystatus_freeze_pages_min) {
+                               if (*memorystatus_freeze_swap_low) {
+                                       *memorystatus_freeze_swap_low = TRUE;
+                               }
+
+                               can_freeze = FALSE;
+                       }
+               } else {
+                       panic("Not a valid freeze configuration.\n");
                }
                }
-               
-               /* Pages have been freed - we can retry. */
-               *memorystatus_freeze_swap_low = FALSE;  
        }
        
        }
        
-       /* OK */
-       return TRUE;
+       return can_freeze;
 }
 
 static void
 memorystatus_freeze_update_throttle_interval(mach_timespec_t *ts, struct throttle_interval_t *interval)
 {
 }
 
 static void
 memorystatus_freeze_update_throttle_interval(mach_timespec_t *ts, struct throttle_interval_t *interval)
 {
+       unsigned int freeze_daily_pageouts_max = memorystatus_freeze_daily_mb_max * (1024 * 1024 / PAGE_SIZE);
        if (CMP_MACH_TIMESPEC(ts, &interval->ts) >= 0) {
                if (!interval->max_pageouts) {
        if (CMP_MACH_TIMESPEC(ts, &interval->ts) >= 0) {
                if (!interval->max_pageouts) {
-                       interval->max_pageouts = (interval->burst_multiple * (((uint64_t)interval->mins * FREEZE_DAILY_PAGEOUTS_MAX) / (24 * 60)));
+                       interval->max_pageouts = (interval->burst_multiple * (((uint64_t)interval->mins * freeze_daily_pageouts_max) / (24 * 60)));
                } else {
                        printf("memorystatus_freeze_update_throttle_interval: %d minute throttle timeout, resetting\n", interval->mins);
                }
                } else {
                        printf("memorystatus_freeze_update_throttle_interval: %d minute throttle timeout, resetting\n", interval->mins);
                }
@@ -2406,10 +4262,11 @@ static void
 memorystatus_freeze_thread(void *param __unused, wait_result_t wr __unused)
 {
        static boolean_t memorystatus_freeze_swap_low = FALSE;
 memorystatus_freeze_thread(void *param __unused, wait_result_t wr __unused)
 {
        static boolean_t memorystatus_freeze_swap_low = FALSE;
-       
+
+       lck_mtx_lock(&freezer_mutex);
        if (memorystatus_freeze_enabled) {
                if (memorystatus_can_freeze(&memorystatus_freeze_swap_low)) {
        if (memorystatus_freeze_enabled) {
                if (memorystatus_can_freeze(&memorystatus_freeze_swap_low)) {
-                       /* Only freeze if we've not exceeded our pageout budgets */
+                       /* Only freeze if we've not exceeded our pageout budgets.*/
                        if (!memorystatus_freeze_update_throttle()) {
                                memorystatus_freeze_top_process(&memorystatus_freeze_swap_low);
                        } else {
                        if (!memorystatus_freeze_update_throttle()) {
                                memorystatus_freeze_top_process(&memorystatus_freeze_swap_low);
                        } else {
@@ -2418,6 +4275,7 @@ memorystatus_freeze_thread(void *param __unused, wait_result_t wr __unused)
                        }
                }
        }
                        }
                }
        }
+       lck_mtx_unlock(&freezer_mutex);
 
        assert_wait((event_t) &memorystatus_freeze_wakeup, THREAD_UNINT);
        thread_block((thread_continue_t) memorystatus_freeze_thread);   
 
        assert_wait((event_t) &memorystatus_freeze_wakeup, THREAD_UNINT);
        thread_block((thread_continue_t) memorystatus_freeze_thread);   
@@ -2425,113 +4283,109 @@ memorystatus_freeze_thread(void *param __unused, wait_result_t wr __unused)
 
 #endif /* CONFIG_FREEZE */
 
 
 #endif /* CONFIG_FREEZE */
 
-#if CONFIG_JETSAM && VM_PRESSURE_EVENTS
+#if VM_PRESSURE_EVENTS
 
 
-boolean_t
-memorystatus_warn_process(pid_t pid) {
-       return (vm_dispatch_pressure_note_to_pid(pid, FALSE) == 0);
-}
+#if CONFIG_MEMORYSTATUS
 
 
-static inline boolean_t
-memorystatus_update_pressure_locked(boolean_t *pressured) {
-       vm_pressure_level_t old_level, new_level;
-       
-       old_level = memorystatus_vm_pressure_level;
-       
-       if (memorystatus_available_pages > memorystatus_available_pages_pressure) {
-               /* Too many free pages */
-               new_level = kVMPressureNormal;
-       }       
-#if CONFIG_FREEZE
-       else if (memorystatus_frozen_count > 0) {
-               /* Frozen processes exist */
-               new_level = kVMPressureNormal;          
-       }
-#endif
-       else if (memorystatus_suspended_count > MEMORYSTATUS_SUSPENDED_THRESHOLD) {
-               /* Too many supended processes */
-               new_level = kVMPressureNormal;
-       }
-       else if (memorystatus_suspended_count > 0) {
-               /* Some suspended processes - warn */
-               new_level = kVMPressureWarning;
-       }
-    else {
-               /* Otherwise, pressure level is urgent */
-               new_level = kVMPressureUrgent;
-       }
-       
-       *pressured = (new_level != kVMPressureNormal);
+static int
+memorystatus_send_note(int event_code, void *data, size_t data_length) {
+       int ret;
+       struct kev_msg ev_msg;
        
        
-       /* Did the pressure level change? */
-       if (old_level != new_level) {
-               MEMORYSTATUS_DEBUG(1, "memorystatus_update_pressure_locked(): memory pressure changed %d -> %d; memorystatus_available_pages: %d\n ", 
-                   old_level, new_level, memorystatus_available_pages);
-               memorystatus_vm_pressure_level = new_level;
-               return TRUE;
+       ev_msg.vendor_code    = KEV_VENDOR_APPLE;
+       ev_msg.kev_class      = KEV_SYSTEM_CLASS;
+       ev_msg.kev_subclass   = KEV_MEMORYSTATUS_SUBCLASS;
+
+       ev_msg.event_code     = event_code;
+
+       ev_msg.dv[0].data_length = data_length;
+       ev_msg.dv[0].data_ptr = data;
+       ev_msg.dv[1].data_length = 0;
+
+       ret = kev_post_msg(&ev_msg);
+       if (ret) {
+               printf("%s: kev_post_msg() failed, err %d\n", __func__, ret);
        }
        
        }
        
-       return FALSE;
+       return ret;
 }
 
 }
 
-kern_return_t
-memorystatus_update_vm_pressure(boolean_t target_foreground) {
-       boolean_t pressure_changed, pressured;
-       boolean_t warn = FALSE;
+boolean_t
+memorystatus_warn_process(pid_t pid, boolean_t critical) {
 
 
-       /* 
-        * Centralised pressure handling routine. Called from:
-        * - The main jetsam thread. In this case, we update the pressure level and dispatch warnings to the foreground 
-        *   process *only*, each time the available page % drops.
-        * - The pageout scan path. In this scenario, every other registered process is targeted in footprint order.
-        *
-        * This scheme guarantees delivery to the foreground app, while providing for warnings to the remaining processes
-        * driven by the pageout scan.
+       boolean_t ret = FALSE;
+       boolean_t found_knote = FALSE;
+       struct knote *kn = NULL;
+
+       /*
+        * See comment in sysctl_memorystatus_vm_pressure_send.
         */
 
         */
 
-       MEMORYSTATUS_DEBUG(1, "memorystatus_update_vm_pressure(): foreground %d; available %d, critical %d, pressure %d\n", 
-               target_foreground, memorystatus_available_pages, memorystatus_available_pages_critical, memorystatus_available_pages_pressure);
+       memorystatus_klist_lock();
 
 
-       proc_list_lock();
+       SLIST_FOREACH(kn, &memorystatus_klist, kn_selnext) {
+               proc_t knote_proc = kn->kn_kq->kq_p;
+               pid_t knote_pid = knote_proc->p_pid;
 
 
-       pressure_changed = memorystatus_update_pressure_locked(&pressured);
+               if (knote_pid == pid) {
+                       /*
+                        * By setting the "fflags" here, we are forcing
+                        * a process to deal with the case where it's
+                        * bumping up into its memory limits. If we don't
+                        * do this here, we will end up depending on the
+                        * system pressure snapshot evaluation in
+                        * filt_memorystatus().
+                        */
        
        
-       if (pressured) {
-               if (target_foreground) {
-                       if (memorystatus_available_pages != memorystatus_last_foreground_pressure_pages) {
-                               if (memorystatus_available_pages < memorystatus_last_foreground_pressure_pages) {
-                                       warn = TRUE;
+                       if (critical) {
+                               if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PRESSURE_CRITICAL) {
+                                       kn->kn_fflags = NOTE_MEMORYSTATUS_PRESSURE_CRITICAL;
+                               } else if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PRESSURE_WARN) {
+                                       kn->kn_fflags = NOTE_MEMORYSTATUS_PRESSURE_WARN;
+                               }
+                       } else {
+                               if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PRESSURE_WARN) {
+                                       kn->kn_fflags = NOTE_MEMORYSTATUS_PRESSURE_WARN;
                                }
                                }
-                               memorystatus_last_foreground_pressure_pages = memorystatus_available_pages;
                        }
                        }
-               } else {
-                       warn = TRUE;
+
+                       found_knote = TRUE;
                }
                }
-       } else if (pressure_changed) {
-               memorystatus_last_foreground_pressure_pages =  (unsigned int)-1;
        }
        }
-       
-       proc_list_unlock();
 
 
-       /* Target foreground processes if specified */
-       if (warn) {
-               if (target_foreground) {
-                       MEMORYSTATUS_DEBUG(1, "memorystatus_update_vm_pressure(): invoking vm_find_pressure_foreground_candidates()\n");
-                       vm_find_pressure_foreground_candidates();
-               } else {
-                       MEMORYSTATUS_DEBUG(1, "memorystatus_update_vm_pressure(): invoking vm_find_pressure_candidate()\n");
-                       /* Defer to VM code. This can race with the foreground priority, but
-                        * it's preferable to holding onto locks for an extended period. */
-                       vm_find_pressure_candidate();
+       if (found_knote) {
+               KNOTE(&memorystatus_klist, 0);
+               ret = TRUE;
+       } else {
+               if (vm_dispatch_pressure_note_to_pid(pid, FALSE) == 0) {
+                       ret = TRUE;
                }
        }
                }
        }
-    
-       /* Dispatch the global kevent to privileged listeners */
-       if (pressure_changed) {
-               memorystatus_issue_pressure_kevent(pressured);
+
+       memorystatus_klist_unlock();
+
+       return ret;
+}
+
+/*
+ * Can only be set by the current task on itself.
+ */
+int
+memorystatus_low_mem_privileged_listener(uint32_t op_flags)
+{
+       boolean_t set_privilege = FALSE;
+       /*
+        * Need an entitlement check here?
+        */
+       if (op_flags == MEMORYSTATUS_CMD_PRIVILEGED_LISTENER_ENABLE) {
+               set_privilege = TRUE;
+       } else if (op_flags == MEMORYSTATUS_CMD_PRIVILEGED_LISTENER_DISABLE) {
+               set_privilege = FALSE;
+       } else {
+               return EINVAL;
        }
 
        }
 
-       return KERN_SUCCESS;
+       return (task_low_mem_privileged_listener(current_task(), set_privilege, NULL));
 }
 
 int
 }
 
 int
@@ -2540,6 +4394,26 @@ memorystatus_send_pressure_note(pid_t pid) {
        return memorystatus_send_note(kMemorystatusPressureNote, &pid, sizeof(pid));
 }
 
        return memorystatus_send_note(kMemorystatusPressureNote, &pid, sizeof(pid));
 }
 
+void
+memorystatus_send_low_swap_note(void) {
+       
+       struct knote *kn = NULL;
+
+       memorystatus_klist_lock();
+       SLIST_FOREACH(kn, &memorystatus_klist, kn_selnext) {
+               /* We call is_knote_registered_modify_task_pressure_bits to check if the sfflags for the
+                * current note contain NOTE_MEMORYSTATUS_LOW_SWAP. Once we find one note in the memorystatus_klist
+                * that has the NOTE_MEMORYSTATUS_LOW_SWAP flags in its sfflags set, we call KNOTE with
+                * kMemoryStatusLowSwap as the hint to process and update all knotes on the memorystatus_klist accordingly. */
+               if (is_knote_registered_modify_task_pressure_bits(kn, NOTE_MEMORYSTATUS_LOW_SWAP, NULL, 0, 0) == TRUE) {
+                       KNOTE(&memorystatus_klist, kMemorystatusLowSwap);
+                       break;
+               }
+       }
+
+       memorystatus_klist_unlock();
+}
+
 boolean_t
 memorystatus_bg_pressure_eligible(proc_t p) {
        boolean_t eligible = FALSE;
 boolean_t
 memorystatus_bg_pressure_eligible(proc_t p) {
        boolean_t eligible = FALSE;
@@ -2563,8 +4437,7 @@ memorystatus_is_foreground_locked(proc_t p) {
         return ((p->p_memstat_effectivepriority == JETSAM_PRIORITY_FOREGROUND) || 
                 (p->p_memstat_effectivepriority == JETSAM_PRIORITY_FOREGROUND_SUPPORT));
 }
         return ((p->p_memstat_effectivepriority == JETSAM_PRIORITY_FOREGROUND) || 
                 (p->p_memstat_effectivepriority == JETSAM_PRIORITY_FOREGROUND_SUPPORT));
 }
-
-#else /* CONFIG_JETSAM && VM_PRESSURE_EVENTS */
+#endif /* CONFIG_MEMORYSTATUS */
 
 /*
  * Trigger levels to test the mechanism.
 
 /*
  * Trigger levels to test the mechanism.
@@ -2581,10 +4454,10 @@ boolean_t               memorystatus_manual_testing_on = FALSE;
 vm_pressure_level_t    memorystatus_manual_testing_level = kVMPressureNormal;
 
 extern struct knote *
 vm_pressure_level_t    memorystatus_manual_testing_level = kVMPressureNormal;
 
 extern struct knote *
-vm_pressure_select_optimal_candidate_to_notify(struct klist *, int);
+vm_pressure_select_optimal_candidate_to_notify(struct klist *, int, boolean_t);
 
 extern
 
 extern
-kern_return_t vm_pressure_notification_without_levels(void);
+kern_return_t vm_pressure_notification_without_levels(boolean_t);
 
 extern void vm_pressure_klist_lock(void);
 extern void vm_pressure_klist_unlock(void);
 
 extern void vm_pressure_klist_lock(void);
 extern void vm_pressure_klist_unlock(void);
@@ -2611,8 +4484,6 @@ void memorystatus_on_pageout_scan_end(void) {
  * pressure_level_to_set - the task is about to be notified of this new level. Update the task's bit notification information appropriately.
  *
  */
  * pressure_level_to_set - the task is about to be notified of this new level. Update the task's bit notification information appropriately.
  *
  */
-boolean_t
-is_knote_registered_modify_task_pressure_bits(struct knote*, int, task_t, vm_pressure_level_t, vm_pressure_level_t);
 
 boolean_t
 is_knote_registered_modify_task_pressure_bits(struct knote *kn_max, int knote_pressure_level, task_t task, vm_pressure_level_t pressure_level_to_clear, vm_pressure_level_t pressure_level_to_set)
 
 boolean_t
 is_knote_registered_modify_task_pressure_bits(struct knote *kn_max, int knote_pressure_level, task_t task, vm_pressure_level_t pressure_level_to_clear, vm_pressure_level_t pressure_level_to_set)
@@ -2631,19 +4502,57 @@ is_knote_registered_modify_task_pressure_bits(struct knote *kn_max, int knote_pr
        return FALSE;
 }
 
        return FALSE;
 }
 
-extern kern_return_t vm_pressure_notify_dispatch_vm_clients(void);
+extern kern_return_t vm_pressure_notify_dispatch_vm_clients(boolean_t target_foreground_process);
+
+#define VM_PRESSURE_DECREASED_SMOOTHING_PERIOD         5000    /* milliseconds */
 
 kern_return_t
 
 kern_return_t
-memorystatus_update_vm_pressure(boolean_t target_best_process) 
+memorystatus_update_vm_pressure(boolean_t target_foreground_process) 
 {
        struct knote                    *kn_max = NULL;
 {
        struct knote                    *kn_max = NULL;
+       struct knote                    *kn_cur = NULL, *kn_temp = NULL;  /* for safe list traversal */
         pid_t                          target_pid = -1;
         struct klist                   dispatch_klist = { NULL };
        proc_t                          target_proc = PROC_NULL;
         pid_t                          target_pid = -1;
         struct klist                   dispatch_klist = { NULL };
        proc_t                          target_proc = PROC_NULL;
-       static vm_pressure_level_t      level_snapshot = kVMPressureNormal;
        struct task                     *task = NULL;
        boolean_t                       found_candidate = FALSE;
 
        struct task                     *task = NULL;
        boolean_t                       found_candidate = FALSE;
 
+       static vm_pressure_level_t      level_snapshot = kVMPressureNormal;
+       static vm_pressure_level_t      prev_level_snapshot = kVMPressureNormal;
+       boolean_t                       smoothing_window_started = FALSE;
+       struct timeval                  smoothing_window_start_tstamp = {0, 0};
+       struct timeval                  curr_tstamp = {0, 0};
+       int                             elapsed_msecs = 0;
+
+#if !CONFIG_JETSAM
+#define MAX_IDLE_KILLS 100     /* limit the number of idle kills allowed */
+
+       int     idle_kill_counter = 0;
+
+       /*
+        * On desktop we take this opportunity to free up memory pressure
+        * by immediately killing idle exitable processes. We use a delay
+        * to avoid overkill.  And we impose a max counter as a fail safe
+        * in case daemons re-launch too fast.
+        */
+       while ((memorystatus_vm_pressure_level != kVMPressureNormal) && (idle_kill_counter < MAX_IDLE_KILLS)) {
+               if (memorystatus_idle_exit_from_VM() == FALSE) {
+                       /* No idle exitable processes left to kill */
+                       break;
+               }
+               idle_kill_counter++;
+
+               if (memorystatus_manual_testing_on == TRUE) {
+                       /*
+                        * Skip the delay when testing
+                        * the pressure notification scheme.
+                        */
+               } else {
+                       delay(1000000);    /* 1 second */
+               }
+       }
+#endif /* !CONFIG_JETSAM */
+
        while (1) {
        
                /*
        while (1) {
        
                /*
@@ -2652,8 +4561,33 @@ memorystatus_update_vm_pressure(boolean_t target_best_process)
                 */
                level_snapshot = memorystatus_vm_pressure_level;
 
                 */
                level_snapshot = memorystatus_vm_pressure_level;
 
+               if (prev_level_snapshot > level_snapshot) {
+                       /*
+                        * Pressure decreased? Let's take a little breather
+                        * and see if this condition stays.
+                        */
+                       if (smoothing_window_started == FALSE) {
+
+                               smoothing_window_started = TRUE;
+                               microuptime(&smoothing_window_start_tstamp);
+                       }
+
+                       microuptime(&curr_tstamp);
+                       timevalsub(&curr_tstamp, &smoothing_window_start_tstamp);
+                       elapsed_msecs = curr_tstamp.tv_sec * 1000 + curr_tstamp.tv_usec / 1000;
+
+                       if (elapsed_msecs < VM_PRESSURE_DECREASED_SMOOTHING_PERIOD) {
+                       
+                               delay(INTER_NOTIFICATION_DELAY);
+                               continue;
+                       }
+               }
+
+               prev_level_snapshot = level_snapshot;
+               smoothing_window_started = FALSE;
+
                memorystatus_klist_lock();
                memorystatus_klist_lock();
-               kn_max = vm_pressure_select_optimal_candidate_to_notify(&memorystatus_klist, level_snapshot);
+               kn_max = vm_pressure_select_optimal_candidate_to_notify(&memorystatus_klist, level_snapshot, target_foreground_process);
 
                if (kn_max == NULL) {
                        memorystatus_klist_unlock();
 
                if (kn_max == NULL) {
                        memorystatus_klist_unlock();
@@ -2687,7 +4621,6 @@ memorystatus_update_vm_pressure(boolean_t target_best_process)
                        continue;
                }
                proc_list_unlock();
                        continue;
                }
                proc_list_unlock();
-               memorystatus_klist_unlock();
                
                target_pid = target_proc->p_pid;
 
                
                target_pid = target_proc->p_pid;
 
@@ -2719,43 +4652,43 @@ memorystatus_update_vm_pressure(boolean_t target_best_process)
                }
 
                if (found_candidate == FALSE) {
                }
 
                if (found_candidate == FALSE) {
+                       proc_rele(target_proc);
+                       memorystatus_klist_unlock();
                        continue;
                }
 
                        continue;
                }
 
-               memorystatus_klist_lock();
-               KNOTE_DETACH(&memorystatus_klist, kn_max);
-               KNOTE_ATTACH(&dispatch_klist, kn_max);
-               memorystatus_klist_unlock();
+               SLIST_FOREACH_SAFE(kn_cur, &memorystatus_klist, kn_selnext, kn_temp) {
+                       proc_t knote_proc = kn_cur->kn_kq->kq_p;
+                       pid_t knote_pid = knote_proc->p_pid;
+                       if (knote_pid == target_pid) {
+                               KNOTE_DETACH(&memorystatus_klist, kn_cur);
+                               KNOTE_ATTACH(&dispatch_klist, kn_cur);
+                       }
+               }
 
                KNOTE(&dispatch_klist, (level_snapshot != kVMPressureNormal) ? kMemorystatusPressure : kMemorystatusNoPressure);
 
 
                KNOTE(&dispatch_klist, (level_snapshot != kVMPressureNormal) ? kMemorystatusPressure : kMemorystatusNoPressure);
 
-               memorystatus_klist_lock();
-               KNOTE_DETACH(&dispatch_klist, kn_max);
-               KNOTE_ATTACH(&memorystatus_klist, kn_max);
+               SLIST_FOREACH_SAFE(kn_cur, &dispatch_klist, kn_selnext, kn_temp) {
+                       KNOTE_DETACH(&dispatch_klist, kn_cur);
+                       KNOTE_ATTACH(&memorystatus_klist, kn_cur);
+               }
+
                memorystatus_klist_unlock();
 
                microuptime(&target_proc->vm_pressure_last_notify_tstamp);
                proc_rele(target_proc);
 
                memorystatus_klist_unlock();
 
                microuptime(&target_proc->vm_pressure_last_notify_tstamp);
                proc_rele(target_proc);
 
-               if (target_best_process == TRUE) {
+               if (memorystatus_manual_testing_on == TRUE && target_foreground_process == TRUE) {
                        break;
                }
 
 try_dispatch_vm_clients:
                        break;
                }
 
 try_dispatch_vm_clients:
-               if (level_snapshot != kVMPressureNormal) {
-                       /* 
-                        * Wake up idle-exit thread.
-                        * Targets one process per invocation.
-                        *
-                        * TODO: memorystatus_idle_exit_from_VM should return FALSE once it's
-                        * done with all idle-exitable processes. Currently, we will exit this
-                        * loop when we are done with notification clients (level and non-level based)
-                        * but we may still have some idle-exitable processes around.
-                        *
+               if (kn_max == NULL && level_snapshot != kVMPressureNormal) {
+                       /*
+                        * We will exit this loop when we are done with
+                        * notification clients (level and non-level based).
                         */
                         */
-                       memorystatus_idle_exit_from_VM();
-
-                       if ((vm_pressure_notify_dispatch_vm_clients() == KERN_FAILURE) && (kn_max == NULL)) {
+                       if ((vm_pressure_notify_dispatch_vm_clients(target_foreground_process) == KERN_FAILURE) && (kn_max == NULL)) {
                                /*
                                 * kn_max == NULL i.e. we didn't find any eligible clients for the level-based notifications
                                 * AND
                                /*
                                 * kn_max == NULL i.e. we didn't find any eligible clients for the level-based notifications
                                 * AND
@@ -2767,8 +4700,45 @@ try_dispatch_vm_clients:
                        }
                }
 
                        }
                }
 
-               if (memorystatus_manual_testing_on == FALSE) {
-                       delay(INTER_NOTIFICATION_DELAY);
+               /*
+                * LD: This block of code below used to be invoked in the older memory notification scheme on embedded everytime 
+                * a process was sent a memory pressure notification. The "memorystatus_klist" list was used to hold these
+                * privileged listeners. But now we have moved to the newer scheme and are trying to move away from the extra
+                * notifications. So the code is here in case we break compat. and need to send out notifications to the privileged
+                * apps.
+                */
+#if 0
+#endif /* 0 */
+
+               if (memorystatus_manual_testing_on == TRUE) {
+                       /*
+                        * Testing out the pressure notification scheme.
+                        * No need for delays etc.
+                        */
+               } else {
+
+                       uint32_t sleep_interval = INTER_NOTIFICATION_DELAY;
+#if CONFIG_JETSAM
+                       unsigned int page_delta = 0;
+                       unsigned int skip_delay_page_threshold = 0;
+
+                       assert(memorystatus_available_pages_pressure >= memorystatus_available_pages_critical_base);
+                       
+                       page_delta = (memorystatus_available_pages_pressure - memorystatus_available_pages_critical_base) / 2;
+                       skip_delay_page_threshold = memorystatus_available_pages_pressure - page_delta;
+
+                       if (memorystatus_available_pages <= skip_delay_page_threshold) {
+                               /*
+                                * We are nearing the critcal mark fast and can't afford to wait between
+                                * notifications.
+                                */
+                               sleep_interval = 0;
+                       }
+#endif /* CONFIG_JETSAM */
+                               
+                       if (sleep_interval) {
+                               delay(sleep_interval);
+                       }
                }
        }
 
                }
        }
 
@@ -2815,15 +4785,22 @@ static int
 sysctl_memorystatus_vm_pressure_level SYSCTL_HANDLER_ARGS
 {
 #pragma unused(arg1, arg2, oidp)
 sysctl_memorystatus_vm_pressure_level SYSCTL_HANDLER_ARGS
 {
 #pragma unused(arg1, arg2, oidp)
-       
        vm_pressure_level_t dispatch_level = convert_internal_pressure_level_to_dispatch_level(memorystatus_vm_pressure_level);
 
        return SYSCTL_OUT(req, &dispatch_level, sizeof(dispatch_level));
 }
 
        vm_pressure_level_t dispatch_level = convert_internal_pressure_level_to_dispatch_level(memorystatus_vm_pressure_level);
 
        return SYSCTL_OUT(req, &dispatch_level, sizeof(dispatch_level));
 }
 
+#if DEBUG || DEVELOPMENT
+
 SYSCTL_PROC(_kern, OID_AUTO, memorystatus_vm_pressure_level, CTLTYPE_INT|CTLFLAG_RD|CTLFLAG_LOCKED,
     0, 0, &sysctl_memorystatus_vm_pressure_level, "I", "");
 
 SYSCTL_PROC(_kern, OID_AUTO, memorystatus_vm_pressure_level, CTLTYPE_INT|CTLFLAG_RD|CTLFLAG_LOCKED,
     0, 0, &sysctl_memorystatus_vm_pressure_level, "I", "");
 
+#else /* DEBUG || DEVELOPMENT */
+
+SYSCTL_PROC(_kern, OID_AUTO, memorystatus_vm_pressure_level, CTLTYPE_INT|CTLFLAG_RD|CTLFLAG_LOCKED|CTLFLAG_MASKED,
+    0, 0, &sysctl_memorystatus_vm_pressure_level, "I", "");
+
+#endif /* DEBUG || DEVELOPMENT */
 
 extern int memorystatus_purge_on_warning;
 extern int memorystatus_purge_on_critical;
 
 extern int memorystatus_purge_on_warning;
 extern int memorystatus_purge_on_critical;
@@ -2928,7 +4905,7 @@ sysctl_memorypressure_manual_trigger SYSCTL_HANDLER_ARGS
        } else {
 
                vm_pressure_klist_lock();
        } else {
 
                vm_pressure_klist_lock();
-               vm_pressure_notification_without_levels();
+               vm_pressure_notification_without_levels(FALSE);
                vm_pressure_klist_unlock();
        }
 
                vm_pressure_klist_unlock();
        }
 
@@ -2948,7 +4925,7 @@ SYSCTL_INT(_kern, OID_AUTO, memorystatus_purge_on_urgent, CTLTYPE_INT|CTLFLAG_RW
 SYSCTL_INT(_kern, OID_AUTO, memorystatus_purge_on_critical, CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_purge_on_critical, 0, "");
 
 
 SYSCTL_INT(_kern, OID_AUTO, memorystatus_purge_on_critical, CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_LOCKED, &memorystatus_purge_on_critical, 0, "");
 
 
-#endif /* CONFIG_JETSAM && VM_PRESSURE_EVENTS */
+#endif /* VM_PRESSURE_EVENTS */
 
 /* Return both allocated and actual size, since there's a race between allocation and list compilation */
 static int
 
 /* Return both allocated and actual size, since there's a race between allocation and list compilation */
 static int
@@ -2991,12 +4968,18 @@ memorystatus_get_priority_list(memorystatus_priority_entry_t **list_ptr, size_t
                list_entry->priority = p->p_memstat_effectivepriority;
                list_entry->user_data = p->p_memstat_userdata;
 #if LEGACY_HIWATER
                list_entry->priority = p->p_memstat_effectivepriority;
                list_entry->user_data = p->p_memstat_userdata;
 #if LEGACY_HIWATER
-               if (((p->p_memstat_state & P_MEMSTAT_MEMLIMIT_BACKGROUND) && (p->p_memstat_effectivepriority >= JETSAM_PRIORITY_FOREGROUND)) ||
-                    (p->p_memstat_memlimit <= 0)) {
-                       task_get_phys_footprint_limit(p->task, &list_entry->limit);  
-               } else {
-                       list_entry->limit = p->p_memstat_memlimit;
-               }
+
+               /*
+                * No need to consider P_MEMSTAT_MEMLIMIT_BACKGROUND anymore.
+                * Background limits are described via the inactive limit slots.
+                * So, here, the cached limit should always be valid.
+                */
+
+               if (p->p_memstat_memlimit <= 0) {
+                        task_get_phys_footprint_limit(p->task, &list_entry->limit);
+                } else {
+                        list_entry->limit = p->p_memstat_memlimit;
+                }
 #else
                task_get_phys_footprint_limit(p->task, &list_entry->limit);
 #endif
 #else
                task_get_phys_footprint_limit(p->task, &list_entry->limit);
 #endif
@@ -3072,19 +5055,34 @@ memorystatus_clear_errors(void)
 
 static void
 memorystatus_update_levels_locked(boolean_t critical_only) {
 
 static void
 memorystatus_update_levels_locked(boolean_t critical_only) {
+
        memorystatus_available_pages_critical = memorystatus_available_pages_critical_base;
        memorystatus_available_pages_critical = memorystatus_available_pages_critical_base;
-#if !LATENCY_JETSAM
-       {
-               // If there's an entry in the first bucket, we have idle processes
-               memstat_bucket_t *first_bucket = &memstat_bucket[JETSAM_PRIORITY_IDLE];
-               if (first_bucket->count) {
-                       memorystatus_available_pages_critical += memorystatus_available_pages_critical_idle_offset;
+
+       /*
+        * If there's an entry in the first bucket, we have idle processes.
+        */
+       memstat_bucket_t *first_bucket = &memstat_bucket[JETSAM_PRIORITY_IDLE];
+       if (first_bucket->count) {
+               memorystatus_available_pages_critical += memorystatus_available_pages_critical_idle_offset;
+
+               if (memorystatus_available_pages_critical  > memorystatus_available_pages_pressure ) {
+                       /* 
+                        * The critical threshold must never exceed the pressure threshold
+                        */
+                       memorystatus_available_pages_critical = memorystatus_available_pages_pressure;
                }
        }
                }
        }
-#endif
+
 #if DEBUG || DEVELOPMENT
        if (memorystatus_jetsam_policy & kPolicyDiagnoseActive) {
                memorystatus_available_pages_critical += memorystatus_jetsam_policy_offset_pages_diagnostic;
 #if DEBUG || DEVELOPMENT
        if (memorystatus_jetsam_policy & kPolicyDiagnoseActive) {
                memorystatus_available_pages_critical += memorystatus_jetsam_policy_offset_pages_diagnostic;
+
+               if (memorystatus_available_pages_critical > memorystatus_available_pages_pressure ) {
+                       /* 
+                        * The critical threshold must never exceed the pressure threshold
+                        */
+                       memorystatus_available_pages_critical = memorystatus_available_pages_pressure;
+               }
        }
 #endif
         
        }
 #endif
         
@@ -3102,10 +5100,94 @@ memorystatus_update_levels_locked(boolean_t critical_only) {
 #endif
 }
 
 #endif
 }
 
+/*
+ * Get the at_boot snapshot
+ */
+static int
+memorystatus_get_at_boot_snapshot(memorystatus_jetsam_snapshot_t **snapshot, size_t *snapshot_size, boolean_t size_only) {
+       size_t input_size = *snapshot_size;
+
+       /*
+        * The at_boot snapshot has no entry list.
+        */
+       *snapshot_size = sizeof(memorystatus_jetsam_snapshot_t);
+
+       if (size_only) {
+               return 0;
+       }
+
+       /*
+        * Validate the size of the snapshot buffer
+        */
+       if (input_size < *snapshot_size) {
+               return EINVAL;
+       }
+
+       /*
+        * Update the notification_time only
+        */
+       memorystatus_at_boot_snapshot.notification_time = mach_absolute_time();
+       *snapshot = &memorystatus_at_boot_snapshot;
+
+       MEMORYSTATUS_DEBUG(7, "memorystatus_get_at_boot_snapshot: returned inputsize (%ld), snapshot_size(%ld), listcount(%d)\n",
+                          (long)input_size, (long)*snapshot_size, 0);
+       return 0;
+}
+
 static int
 static int
-memorystatus_get_snapshot(memorystatus_jetsam_snapshot_t **snapshot, size_t *snapshot_size, boolean_t size_only) {
+memorystatus_get_on_demand_snapshot(memorystatus_jetsam_snapshot_t **snapshot, size_t *snapshot_size, boolean_t size_only) {
        size_t input_size = *snapshot_size;
        size_t input_size = *snapshot_size;
+       uint32_t ods_list_count = memorystatus_list_count;
+       memorystatus_jetsam_snapshot_t *ods = NULL;     /* The on_demand snapshot buffer */
+
+       *snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) + (sizeof(memorystatus_jetsam_snapshot_entry_t) * (ods_list_count));
+
+       if (size_only) {
+               return 0;
+       }
+
+       /*
+        * Validate the size of the snapshot buffer.
+        * This is inherently racey. May want to revisit
+        * this error condition and trim the output when
+        * it doesn't fit.
+        */
+       if (input_size < *snapshot_size) {
+               return EINVAL;
+       }
+
+       /*
+        * Allocate and initialize a snapshot buffer.
+        */
+       ods = (memorystatus_jetsam_snapshot_t *)kalloc(*snapshot_size);
+       if (!ods) {
+               return (ENOMEM);
+       }
+
+       memset(ods, 0, *snapshot_size);
+
+       proc_list_lock();
+       memorystatus_init_jetsam_snapshot_locked(ods, ods_list_count);
+       proc_list_unlock();
+
+       /*
+        * Return the kernel allocated, on_demand buffer.
+        * The caller of this routine will copy the data out
+        * to user space and then free the kernel allocated
+        * buffer.
+        */
+       *snapshot = ods;
+
+       MEMORYSTATUS_DEBUG(7, "memorystatus_get_on_demand_snapshot: returned inputsize (%ld), snapshot_size(%ld), listcount(%ld)\n",
+                                  (long)input_size, (long)*snapshot_size, (long)ods_list_count);
        
        
+       return 0;
+}
+
+static int
+memorystatus_get_jetsam_snapshot(memorystatus_jetsam_snapshot_t **snapshot, size_t *snapshot_size, boolean_t size_only) {
+       size_t input_size = *snapshot_size;
+
        if (memorystatus_jetsam_snapshot_count > 0) {
                *snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) + (sizeof(memorystatus_jetsam_snapshot_entry_t) * (memorystatus_jetsam_snapshot_count));
        } else {
        if (memorystatus_jetsam_snapshot_count > 0) {
                *snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) + (sizeof(memorystatus_jetsam_snapshot_entry_t) * (memorystatus_jetsam_snapshot_count));
        } else {
@@ -3121,29 +5203,99 @@ memorystatus_get_snapshot(memorystatus_jetsam_snapshot_t **snapshot, size_t *sna
        }
 
        *snapshot = memorystatus_jetsam_snapshot;
        }
 
        *snapshot = memorystatus_jetsam_snapshot;
-       
-       MEMORYSTATUS_DEBUG(1, "memorystatus_snapshot: returning %ld for size\n", (long)*snapshot_size);
-       
+
+       MEMORYSTATUS_DEBUG(7, "memorystatus_get_jetsam_snapshot: returned inputsize (%ld), snapshot_size(%ld), listcount(%ld)\n",
+                                  (long)input_size, (long)*snapshot_size, (long)memorystatus_jetsam_snapshot_count);
+
        return 0;
 }
 
        return 0;
 }
 
+
 static int
 static int
-memorystatus_cmd_get_jetsam_snapshot(user_addr_t buffer, size_t buffer_size, int32_t *retval) {
+memorystatus_cmd_get_jetsam_snapshot(int32_t flags, user_addr_t buffer, size_t buffer_size, int32_t *retval) {
        int error = EINVAL;
        boolean_t size_only;
        int error = EINVAL;
        boolean_t size_only;
+       boolean_t is_default_snapshot = FALSE;
+       boolean_t is_on_demand_snapshot = FALSE;
+       boolean_t is_at_boot_snapshot = FALSE;
        memorystatus_jetsam_snapshot_t *snapshot;
        memorystatus_jetsam_snapshot_t *snapshot;
-       
+
        size_only = ((buffer == USER_ADDR_NULL) ? TRUE : FALSE);
        size_only = ((buffer == USER_ADDR_NULL) ? TRUE : FALSE);
-       
-       error = memorystatus_get_snapshot(&snapshot, &buffer_size, size_only);
+
+       if (flags == 0) {
+               /* Default */
+               is_default_snapshot = TRUE;
+               error = memorystatus_get_jetsam_snapshot(&snapshot, &buffer_size, size_only);
+       } else {
+               if (flags & ~(MEMORYSTATUS_SNAPSHOT_ON_DEMAND | MEMORYSTATUS_SNAPSHOT_AT_BOOT)) {
+                       /*
+                        * Unsupported bit set in flag.
+                        */
+                       return EINVAL;
+               }
+
+               if ((flags & (MEMORYSTATUS_SNAPSHOT_ON_DEMAND | MEMORYSTATUS_SNAPSHOT_AT_BOOT)) ==
+                   (MEMORYSTATUS_SNAPSHOT_ON_DEMAND |  MEMORYSTATUS_SNAPSHOT_AT_BOOT)) {
+                       /*
+                        * Can't have both set at the same time.
+                        */
+                       return EINVAL;
+               }
+
+               if (flags & MEMORYSTATUS_SNAPSHOT_ON_DEMAND) {
+                       is_on_demand_snapshot = TRUE;
+                       /*
+                        * When not requesting the size only, the following call will allocate
+                        * an on_demand snapshot buffer, which is freed below.
+                        */
+                       error = memorystatus_get_on_demand_snapshot(&snapshot, &buffer_size, size_only);
+
+               } else if (flags & MEMORYSTATUS_SNAPSHOT_AT_BOOT) {
+                       is_at_boot_snapshot = TRUE;
+                       error = memorystatus_get_at_boot_snapshot(&snapshot, &buffer_size, size_only);
+               } else {
+                       /*
+                        * Invalid flag setting.
+                        */
+                       return EINVAL;
+               }
+       }
+
        if (error) {
                goto out;
        }
 
        if (error) {
                goto out;
        }
 
-       /* Copy out and reset */
+       /*
+        * Copy the data out to user space and clear the snapshot buffer.
+        * If working with the jetsam snapshot,
+        *      clearing the buffer means, reset the count.
+        * If working with an on_demand snapshot
+        *      clearing the buffer means, free it.
+        * If working with the at_boot snapshot
+        *      there is nothing to clear or update.
+        */
        if (!size_only) {
                if ((error = copyout(snapshot, buffer, buffer_size)) == 0) {
        if (!size_only) {
                if ((error = copyout(snapshot, buffer, buffer_size)) == 0) {
-                       snapshot->entry_count = memorystatus_jetsam_snapshot_count = 0;
+                       if (is_default_snapshot) {
+                               /*
+                                * The jetsam snapshot is never freed, its count is simply reset.
+                                */
+                               snapshot->entry_count = memorystatus_jetsam_snapshot_count = 0;
+
+                               proc_list_lock();
+                               memorystatus_jetsam_snapshot_last_timestamp = 0;
+                               proc_list_unlock();
+                       }
+               }
+
+               if (is_on_demand_snapshot) {
+                       /*
+                        * The on_demand snapshot is always freed,
+                        * even if the copyout failed.
+                        */
+                       if(snapshot) {
+                               kfree(snapshot, buffer_size);
+                       }
                }
        }
 
                }
        }
 
@@ -3154,58 +5306,313 @@ out:
        return error;
 }
 
        return error;
 }
 
+/*
+ *     Routine:        memorystatus_cmd_grp_set_properties
+ *     Purpose:        Update properties for a group of processes.
+ *
+ *     Supported Properties:
+ *     [priority]
+ *             Move each process out of its effective priority
+ *             band and into a new priority band.
+ *             Maintains relative order from lowest to highest priority.
+ *             In single band, maintains relative order from head to tail.
+ *
+ *             eg: before      [effectivepriority | pid]
+ *                             [18 | p101              ]
+ *                             [17 | p55, p67, p19     ]
+ *                             [12 | p103 p10          ]
+ *                             [ 7 | p25               ]
+ *                             [ 0 | p71, p82,         ]
+ *
+ *             after   [ new band | pid]
+ *                     [ xxx | p71, p82, p25, p103, p10, p55, p67, p19, p101]
+ *
+ *     Returns:  0 on success, else non-zero.
+ *
+ *     Caveat:   We know there is a race window regarding recycled pids.
+ *               A process could be killed before the kernel can act on it here.
+ *               If a pid cannot be found in any of the jetsam priority bands,
+ *               then we simply ignore it.  No harm.
+ *               But, if the pid has been recycled then it could be an issue.
+ *               In that scenario, we might move an unsuspecting process to the new
+ *               priority band. It's not clear how the kernel can safeguard
+ *               against this, but it would be an extremely rare case anyway.
+ *               The caller of this api might avoid such race conditions by
+ *               ensuring that the processes passed in the pid list are suspended.
+ */
+
+
+/* This internal structure can expand when we add support for more properties */
+typedef        struct memorystatus_internal_properties
+{
+       proc_t proc;
+       int32_t priority;  /* see memorytstatus_priority_entry_t : priority */
+} memorystatus_internal_properties_t;
+       
+
 static int
 static int
-memorystatus_cmd_set_priority_properties(pid_t pid, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval) {
-       const uint32_t MAX_ENTRY_COUNT = 2; /* Cap the entry count */
+memorystatus_cmd_grp_set_properties(int32_t flags, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval) {
+
+#pragma unused (flags)
+
+       /*
+        * We only handle setting priority
+        * per process
+        */
+
+       int error = 0;
+       memorystatus_priority_entry_t *entries = NULL;
+       uint32_t entry_count = 0;
+
+       /* This will be the ordered proc list */
+       memorystatus_internal_properties_t *table = NULL;
+       size_t table_size = 0;
+       uint32_t table_count = 0;
+
+       uint32_t i = 0;
+       uint32_t bucket_index = 0;
+       boolean_t head_insert;
+       int32_t new_priority;
        
        
-       int error;
-       uint32_t i;
-       uint32_t entry_count;
-       memorystatus_priority_properties_t *entries;
+       proc_t p;
+
+       /* Verify inputs */
+       if ((buffer == USER_ADDR_NULL) || (buffer_size == 0) || ((buffer_size % sizeof(memorystatus_priority_entry_t)) != 0)) {
+               error = EINVAL;
+               goto out;
+       }
+
+       entry_count = (buffer_size / sizeof(memorystatus_priority_entry_t));
+       if ((entries = (memorystatus_priority_entry_t *)kalloc(buffer_size)) == NULL) {
+               error = ENOMEM;
+               goto out;
+       }
+
+       KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_GRP_SET_PROP) | DBG_FUNC_START, entry_count, 0, 0, 0, 0);
+
+       if ((error = copyin(buffer, entries, buffer_size)) != 0) {
+               goto out;
+       }
+
+       /* Verify sanity of input priorities */
+       for (i=0; i < entry_count; i++) {
+               if (entries[i].priority == -1) {
+                       /* Use as shorthand for default priority */
+                       entries[i].priority = JETSAM_PRIORITY_DEFAULT;
+               } else if (entries[i].priority == JETSAM_PRIORITY_IDLE_DEFERRED) {
+                       /* JETSAM_PRIORITY_IDLE_DEFERRED is reserved for internal use;
+                        * if requested, adjust to JETSAM_PRIORITY_IDLE. */
+                       entries[i].priority = JETSAM_PRIORITY_IDLE;
+               } else if (entries[i].priority == JETSAM_PRIORITY_IDLE_HEAD) {
+                       /* JETSAM_PRIORITY_IDLE_HEAD inserts at the head of the idle
+                        * queue */
+                       /* Deal with this later */
+               } else if ((entries[i].priority < 0) || (entries[i].priority >= MEMSTAT_BUCKET_COUNT)) {
+                       /* Sanity check */
+                       error = EINVAL;
+                       goto out;
+               }
+       }
+
+       table_size = sizeof(memorystatus_internal_properties_t) * entry_count;
+       if ( (table = (memorystatus_internal_properties_t *)kalloc(table_size)) == NULL) {
+               error = ENOMEM;
+               goto out;
+       }
+       memset(table, 0, table_size);
+
+
+       /*
+        * For each jetsam bucket entry, spin through the input property list.
+        * When a matching pid is found, populate an adjacent table with the
+        * appropriate proc pointer and new property values.
+        * This traversal automatically preserves order from lowest
+        * to highest priority.
+        */
+
+       bucket_index=0;
        
        
-       /* Validate inputs */
-       if ((pid == 0) || (buffer == USER_ADDR_NULL) || (buffer_size == 0)) {
-               return EINVAL;
+       proc_list_lock();
+
+       /* Create the ordered table */
+       p = memorystatus_get_first_proc_locked(&bucket_index, TRUE);    
+       while (p && (table_count < entry_count)) {
+               for (i=0; i < entry_count; i++ ) {
+                       if (p->p_pid == entries[i].pid) {
+                               /* Build the table data  */
+                               table[table_count].proc = p;
+                               table[table_count].priority = entries[i].priority;
+                               table_count++;
+                               break;
+                       }
+               }
+               p = memorystatus_get_next_proc_locked(&bucket_index, p, TRUE);
+       }
+       
+       /* We now have ordered list of procs ready to move */
+       for (i=0; i < table_count; i++) {
+               p = table[i].proc;
+               assert(p != NULL);
+
+               /* Allow head inserts -- but relative order is now  */
+               if (table[i].priority == JETSAM_PRIORITY_IDLE_HEAD) {
+                       new_priority = JETSAM_PRIORITY_IDLE;
+                       head_insert = true;
+               } else {
+                       new_priority = table[i].priority;
+                       head_insert = false;
+               }
+               
+               /* Not allowed */
+               if (p->p_memstat_state & P_MEMSTAT_INTERNAL) {
+                       continue;
+               }
+
+               /*
+                * Take appropriate steps if moving proc out of the
+                * JETSAM_PRIORITY_IDLE_DEFERRED band.
+                */
+               if (p->p_memstat_effectivepriority == JETSAM_PRIORITY_IDLE_DEFERRED) {
+                       memorystatus_invalidate_idle_demotion_locked(p, TRUE);
+               }
+
+               memorystatus_update_priority_locked(p, new_priority, head_insert);
        }
        }
+
+       proc_list_unlock();
+
+       /*
+        * if (table_count != entry_count)
+        * then some pids were not found in a jetsam band.
+        * harmless but interesting...
+        */
+       KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_GRP_SET_PROP) | DBG_FUNC_END, entry_count, table_count, 0, 0, 0);
+       
+out:
+       if (entries)
+               kfree(entries, buffer_size);
+       if (table)
+               kfree(table, table_size);
+
+       return (error);
+}
+
+
+/*
+ * This routine is used to update a process's jetsam priority position and stored user_data.
+ * It is not used for the setting of memory limits, which is why the last 6 args to the
+ * memorystatus_update() call are 0 or FALSE.
+ */
        
        
-       /* Make sure the buffer is a multiple of the entry size, and that an excessive size isn't specified */
-       entry_count = (buffer_size / sizeof(memorystatus_priority_properties_t));
-       if (((buffer_size % sizeof(memorystatus_priority_properties_t)) != 0) || (entry_count > MAX_ENTRY_COUNT)) {
+static int
+memorystatus_cmd_set_priority_properties(pid_t pid, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval) {
+       int error = 0;
+       memorystatus_priority_properties_t mpp_entry;
+
+       /* Validate inputs */
+       if ((pid == 0) || (buffer == USER_ADDR_NULL) || (buffer_size != sizeof(memorystatus_priority_properties_t))) {
                return EINVAL;
        }
                return EINVAL;
        }
-               
-       entries = (memorystatus_priority_properties_t *)kalloc(buffer_size);
-               
-       error = copyin(buffer, entries, buffer_size);
        
        
-       for (i = 0; i < entry_count; i++) {
+       error = copyin(buffer, &mpp_entry, buffer_size);
+
+       if (error == 0) {
                proc_t p;
                 
                proc_t p;
                 
-               if (error) {
-                       break;
-               }
-               
                p = proc_find(pid);
                if (!p) {
                p = proc_find(pid);
                if (!p) {
-                       error = ESRCH;
-                       break;         
+                       return ESRCH;
                }
                
                if (p->p_memstat_state & P_MEMSTAT_INTERNAL) {
                }
                
                if (p->p_memstat_state & P_MEMSTAT_INTERNAL) {
-                       error = EPERM;
                        proc_rele(p);
                        proc_rele(p);
-                       break;          
+                       return EPERM;
                }
                }
-               
-               error = memorystatus_update(p, entries[i].priority, entries[i].user_data, FALSE, FALSE, 0, 0);
+       
+               error = memorystatus_update(p, mpp_entry.priority, mpp_entry.user_data, FALSE, FALSE, 0, 0, FALSE, FALSE, FALSE);
                proc_rele(p);
        }
        
                proc_rele(p);
        }
        
-       kfree(entries, buffer_size);
-       
-       return error;
+       return(error);
 }
 
 }
 
+static int
+memorystatus_cmd_set_memlimit_properties(pid_t pid, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval) {
+       int error = 0;
+       memorystatus_memlimit_properties_t mmp_entry;
+
+       /* Validate inputs */
+       if ((pid == 0) || (buffer == USER_ADDR_NULL) || (buffer_size != sizeof(memorystatus_memlimit_properties_t))) {
+               return EINVAL;
+       }
+
+       error = copyin(buffer, &mmp_entry, buffer_size);
+
+       if (error == 0) {
+               error = memorystatus_set_memlimit_properties(pid, &mmp_entry);
+       }
+
+       return(error);
+}
+
+/*
+ * When getting the memlimit settings, we can't simply call task_get_phys_footprint_limit().
+ * That gets the proc's cached memlimit and there is no guarantee that the active/inactive
+ * limits will be the same in the no-limit case.  Instead we convert limits <= 0 using
+ * task_convert_phys_footprint_limit(). It computes the same limit value that would be written
+ * to the task's ledgers via task_set_phys_footprint_limit().
+ */
+static int
+memorystatus_cmd_get_memlimit_properties(pid_t pid, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval) {
+       int error = 0;
+       memorystatus_memlimit_properties_t mmp_entry;
+
+       /* Validate inputs */
+       if ((pid == 0) || (buffer == USER_ADDR_NULL) || (buffer_size != sizeof(memorystatus_memlimit_properties_t))) {
+               return EINVAL;
+       }
+
+       memset (&mmp_entry, 0, sizeof(memorystatus_memlimit_properties_t));
+
+       proc_t p = proc_find(pid);
+       if (!p) {
+               return ESRCH;
+       }
+
+       /*
+        * Get the active limit and attributes.
+        * No locks taken since we hold a reference to the proc.
+        */
+
+       if (p->p_memstat_memlimit_active > 0 ) {
+               mmp_entry.memlimit_active = p->p_memstat_memlimit_active;
+       } else {
+               task_convert_phys_footprint_limit(-1, &mmp_entry.memlimit_active);
+       }
+
+       if (p->p_memstat_state & P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL) {
+               mmp_entry.memlimit_active_attr |= MEMORYSTATUS_MEMLIMIT_ATTR_FATAL;
+       }
+
+       /*
+        * Get the inactive limit and attributes
+        */
+       if (p->p_memstat_memlimit_inactive <= 0) {
+               task_convert_phys_footprint_limit(-1, &mmp_entry.memlimit_inactive);
+       } else {
+               mmp_entry.memlimit_inactive = p->p_memstat_memlimit_inactive;
+       }
+       if (p->p_memstat_state & P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL) {
+               mmp_entry.memlimit_inactive_attr |= MEMORYSTATUS_MEMLIMIT_ATTR_FATAL;
+       }
+       proc_rele(p);
+
+       error = copyout(&mmp_entry, buffer, buffer_size);
+
+       return(error);
+}
+
+
 static int
 memorystatus_cmd_get_pressure_status(int32_t *retval) {        
        int error;
 static int
 memorystatus_cmd_get_pressure_status(int32_t *retval) {        
        int error;
@@ -3222,42 +5629,235 @@ memorystatus_cmd_get_pressure_status(int32_t *retval) {
        return error;
 }
 
        return error;
 }
 
+int
+memorystatus_get_pressure_status_kdp() {
+       return (kVMPressureNormal != memorystatus_vm_pressure_level) ? 1 : 0;
+}
+
+/*
+ * Every process, including a P_MEMSTAT_INTERNAL process (currently only pid 1), is allowed to set a HWM.
+ *
+ * This call is inflexible -- it does not distinguish between active/inactive, fatal/non-fatal
+ * So, with 2-level HWM preserving previous behavior will map as follows.
+ *      - treat the limit passed in as both an active and inactive limit.
+ *      - treat the is_fatal_limit flag as though it applies to both active and inactive limits.
+ *
+ * When invoked via MEMORYSTATUS_CMD_SET_JETSAM_HIGH_WATER_MARK
+ *      - the is_fatal_limit is FALSE, meaning the active and inactive limits are non-fatal/soft
+ *      - so mapping is (active/non-fatal, inactive/non-fatal)
+ *
+ * When invoked via MEMORYSTATUS_CMD_SET_JETSAM_TASK_LIMIT
+ *      - the is_fatal_limit is TRUE, meaning the process's active and inactive limits are fatal/hard
+ *      - so mapping is (active/fatal, inactive/fatal)
+ */
+
 static int
 static int
-memorystatus_cmd_set_jetsam_high_water_mark(pid_t pid, int32_t high_water_mark, __unused int32_t *retval) {
+memorystatus_cmd_set_jetsam_memory_limit(pid_t pid, int32_t high_water_mark, __unused int32_t *retval, boolean_t is_fatal_limit) {
        int error = 0;
        int error = 0;
+       memorystatus_memlimit_properties_t entry;
+
+       entry.memlimit_active = high_water_mark;
+       entry.memlimit_active_attr = 0;
+       entry.memlimit_inactive = high_water_mark;
+       entry.memlimit_inactive_attr = 0;
+
+       if (is_fatal_limit == TRUE) {
+               entry.memlimit_active_attr   |= MEMORYSTATUS_MEMLIMIT_ATTR_FATAL;
+               entry.memlimit_inactive_attr |= MEMORYSTATUS_MEMLIMIT_ATTR_FATAL;
+       }
+
+       error = memorystatus_set_memlimit_properties(pid, &entry);
+       return (error);
+}
+
+static int
+memorystatus_set_memlimit_properties(pid_t pid, memorystatus_memlimit_properties_t *entry) {
+
+       int32_t  memlimit_active;
+       boolean_t memlimit_active_is_fatal;
+       int32_t  memlimit_inactive;
+       boolean_t memlimit_inactive_is_fatal;
+       uint32_t valid_attrs = 0;
+       int       error = 0;
         
        proc_t p = proc_find(pid);
        if (!p) {
                return ESRCH;
        }
         
        proc_t p = proc_find(pid);
        if (!p) {
                return ESRCH;
        }
-        
-       if (high_water_mark <= 0) {
-               high_water_mark = -1; /* Disable */
+
+       /*
+        * Check for valid attribute flags.
+        */
+       valid_attrs |= (MEMORYSTATUS_MEMLIMIT_ATTR_FATAL);
+       if ((entry->memlimit_active_attr & (~valid_attrs)) != 0) {
+               proc_rele(p);
+               return EINVAL;
        }
        }
-    
-       proc_list_lock();
-    
-       if (p->p_memstat_state & P_MEMSTAT_INTERNAL) {
-               error = EPERM;
-               goto exit;              
+       if ((entry->memlimit_inactive_attr & (~valid_attrs)) != 0) {
+               proc_rele(p);
+               return EINVAL;
        }
        }
-    
-       p->p_memstat_memlimit = high_water_mark;
+
+       /*
+        * Setup the active memlimit properties
+        */
+       memlimit_active = entry->memlimit_active;
+       if (entry->memlimit_active_attr & MEMORYSTATUS_MEMLIMIT_ATTR_FATAL) {
+               memlimit_active_is_fatal = TRUE;
+       } else {
+               memlimit_active_is_fatal = FALSE;
+       }
+
+       /*
+        * Setup the inactive memlimit properties
+        */
+       memlimit_inactive = entry->memlimit_inactive;
+       if (entry->memlimit_inactive_attr & MEMORYSTATUS_MEMLIMIT_ATTR_FATAL) {
+               memlimit_inactive_is_fatal = TRUE;
+       } else {
+               memlimit_inactive_is_fatal = FALSE;
+       }
+
+       /*
+        * Setting a limit of <= 0 implies that the process has no
+        * high-water-mark and has no per-task-limit.  That means
+        * the system_wide task limit is in place, which by the way,
+        * is always fatal.
+        */
+
+       if (memlimit_active <= 0) {
+               /*
+                * Enforce the fatal system_wide task limit while process is active.
+                */
+               memlimit_active = -1;
+               memlimit_active_is_fatal = TRUE;
+       }
+
+       if (memlimit_inactive <= 0) {
+               /*
+                * Enforce the fatal system_wide task limit while process is inactive.
+                */
+               memlimit_inactive = -1;
+               memlimit_inactive_is_fatal = TRUE;
+       }
+
+       proc_list_lock();
+
+       /*
+        * Store the active limit variants in the proc.
+        */
+       SET_ACTIVE_LIMITS_LOCKED(p, memlimit_active, memlimit_active_is_fatal);
+
+       /*
+        * Store the inactive limit variants in the proc.
+        */
+       SET_INACTIVE_LIMITS_LOCKED(p, memlimit_inactive, memlimit_inactive_is_fatal);
+
+       /*
+        * Enforce appropriate limit variant by updating the cached values
+        * and writing the ledger.
+        * Limit choice is based on process active/inactive state.
+        */
+
        if (memorystatus_highwater_enabled) {
        if (memorystatus_highwater_enabled) {
-               if (p->p_memstat_state & P_MEMSTAT_MEMLIMIT_BACKGROUND) {
-                       memorystatus_update_priority_locked(p, p->p_memstat_effectivepriority);
-               } else {
-                       error = (task_set_phys_footprint_limit_internal(p->task, high_water_mark, NULL, TRUE) == 0) ? 0 : EINVAL;
-               }
+               boolean_t trigger_exception;
+               /*
+                * No need to consider P_MEMSTAT_MEMLIMIT_BACKGROUND anymore.
+                * Background limits are described via the inactive limit slots.
+                */
+
+               if (proc_jetsam_state_is_active_locked(p) == TRUE) {
+                       CACHE_ACTIVE_LIMITS_LOCKED(p, trigger_exception);
+               } else {
+                       CACHE_INACTIVE_LIMITS_LOCKED(p, trigger_exception);
+               }
+
+               /* Enforce the limit by writing to the ledgers */
+               assert(trigger_exception == TRUE);
+               error = (task_set_phys_footprint_limit_internal(p->task, ((p->p_memstat_memlimit > 0) ? p->p_memstat_memlimit : -1), NULL, trigger_exception) == 0) ? 0 : EINVAL;
+
+               MEMORYSTATUS_DEBUG(3, "memorystatus_set_memlimit_properties: new limit on pid %d (%dMB %s) current priority (%d) dirty_state?=0x%x %s\n",
+                                  p->p_pid, (p->p_memstat_memlimit > 0 ? p->p_memstat_memlimit : -1),
+                                  (p->p_memstat_state & P_MEMSTAT_FATAL_MEMLIMIT ? "F " : "NF"), p->p_memstat_effectivepriority, p->p_memstat_dirty,
+                                  (p->p_memstat_dirty ? ((p->p_memstat_dirty & P_DIRTY) ? "isdirty" : "isclean") : ""));
        }
 
        }
 
-exit:
        proc_list_unlock();
        proc_rele(p);
        
        return error;
 }
 
        proc_list_unlock();
        proc_rele(p);
        
        return error;
 }
 
+/*
+ * Returns the jetsam priority (effective or requested) of the process
+ * associated with this task.
+ */
+int
+proc_get_memstat_priority(proc_t p, boolean_t effective_priority)
+{
+       if (p) {
+               if (effective_priority) {
+                       return p->p_memstat_effectivepriority;
+               } else {
+                       return p->p_memstat_requestedpriority;
+               }
+       }
+       return 0;
+}
+
+/*
+ * Description:
+ *     Evaluates active vs. inactive process state.
+ *     Processes that opt into dirty tracking are evaluated
+ *     based on clean vs dirty state.
+ *     dirty ==> active
+ *     clean ==> inactive
+ *
+ *     Process that do not opt into dirty tracking are
+ *     evalulated based on priority level.
+ *     Foreground or above ==> active
+ *     Below Foreground    ==> inactive
+ *
+ *     Return: TRUE if active
+ *             False if inactive
+ */
+
+static boolean_t
+proc_jetsam_state_is_active_locked(proc_t p) {
+
+       if (p->p_memstat_dirty & P_DIRTY_TRACK) {
+               /*
+                * process has opted into dirty tracking
+                * active state is based on dirty vs. clean
+                */
+               if (p->p_memstat_dirty & P_DIRTY_IS_DIRTY) {
+                       /*
+                        * process is dirty
+                        * implies active state
+                        */
+                       return TRUE;
+               } else {
+                       /*
+                        * process is clean
+                        * implies inactive state
+                        */
+                       return FALSE;
+               }
+       } else if (p->p_memstat_effectivepriority >= JETSAM_PRIORITY_FOREGROUND) {
+               /*
+                * process is Foreground or higher
+                * implies active state
+                */
+               return TRUE;
+       } else {
+               /*
+                * process found below Foreground
+                * implies inactive state
+                */
+               return FALSE;
+       }
+}
+
 #endif /* CONFIG_JETSAM */
 
 int
 #endif /* CONFIG_JETSAM */
 
 int
@@ -3288,26 +5888,71 @@ memorystatus_control(struct proc *p __unused, struct memorystatus_control_args *
        case MEMORYSTATUS_CMD_SET_PRIORITY_PROPERTIES:
                error = memorystatus_cmd_set_priority_properties(args->pid, args->buffer, args->buffersize, ret);
                break;
        case MEMORYSTATUS_CMD_SET_PRIORITY_PROPERTIES:
                error = memorystatus_cmd_set_priority_properties(args->pid, args->buffer, args->buffersize, ret);
                break;
+       case MEMORYSTATUS_CMD_SET_MEMLIMIT_PROPERTIES:
+               error = memorystatus_cmd_set_memlimit_properties(args->pid, args->buffer, args->buffersize, ret);
+               break;
+       case MEMORYSTATUS_CMD_GET_MEMLIMIT_PROPERTIES:
+               error = memorystatus_cmd_get_memlimit_properties(args->pid, args->buffer, args->buffersize, ret);
+               break;
+       case MEMORYSTATUS_CMD_GRP_SET_PROPERTIES:
+               error = memorystatus_cmd_grp_set_properties((int32_t)args->flags, args->buffer, args->buffersize, ret);
+               break;          
        case MEMORYSTATUS_CMD_GET_JETSAM_SNAPSHOT:
        case MEMORYSTATUS_CMD_GET_JETSAM_SNAPSHOT:
-               error = memorystatus_cmd_get_jetsam_snapshot(args->buffer, args->buffersize, ret);
+               error = memorystatus_cmd_get_jetsam_snapshot((int32_t)args->flags, args->buffer, args->buffersize, ret);
                break;
        case MEMORYSTATUS_CMD_GET_PRESSURE_STATUS:
                error = memorystatus_cmd_get_pressure_status(ret);
                break;
        case MEMORYSTATUS_CMD_SET_JETSAM_HIGH_WATER_MARK:
                break;
        case MEMORYSTATUS_CMD_GET_PRESSURE_STATUS:
                error = memorystatus_cmd_get_pressure_status(ret);
                break;
        case MEMORYSTATUS_CMD_SET_JETSAM_HIGH_WATER_MARK:
-               /* TODO: deprecate. Keeping it in as there's no pid based way to set the ledger limit right now. */
-               error = memorystatus_cmd_set_jetsam_high_water_mark(args->pid, (int32_t)args->flags, ret);
+               /*
+                * This call does not distinguish between active and inactive limits.
+                * Default behavior in 2-level HWM world is to set both.
+                * Non-fatal limit is also assumed for both.
+                */
+               error = memorystatus_cmd_set_jetsam_memory_limit(args->pid, (int32_t)args->flags, ret, FALSE);
+               break;
+       case MEMORYSTATUS_CMD_SET_JETSAM_TASK_LIMIT:
+               /*
+                * This call does not distinguish between active and inactive limits.
+                * Default behavior in 2-level HWM world is to set both.
+                * Fatal limit is also assumed for both.
+                */
+               error = memorystatus_cmd_set_jetsam_memory_limit(args->pid, (int32_t)args->flags, ret, TRUE);
                break;
        /* Test commands */
 #if DEVELOPMENT || DEBUG
        case MEMORYSTATUS_CMD_TEST_JETSAM:
                error = memorystatus_kill_process_sync(args->pid, kMemorystatusKilled) ? 0 : EINVAL;
                break;
                break;
        /* Test commands */
 #if DEVELOPMENT || DEBUG
        case MEMORYSTATUS_CMD_TEST_JETSAM:
                error = memorystatus_kill_process_sync(args->pid, kMemorystatusKilled) ? 0 : EINVAL;
                break;
+       case MEMORYSTATUS_CMD_TEST_JETSAM_SORT:
+               error = memorystatus_cmd_test_jetsam_sort(args->pid, (int32_t)args->flags);
+               break;
        case MEMORYSTATUS_CMD_SET_JETSAM_PANIC_BITS:
                error = memorystatus_cmd_set_panic_bits(args->buffer, args->buffersize);
                break;
 #endif /* DEVELOPMENT || DEBUG */
        case MEMORYSTATUS_CMD_SET_JETSAM_PANIC_BITS:
                error = memorystatus_cmd_set_panic_bits(args->buffer, args->buffersize);
                break;
 #endif /* DEVELOPMENT || DEBUG */
+       case MEMORYSTATUS_CMD_AGGRESSIVE_JETSAM_LENIENT_MODE_ENABLE:
+               if (memorystatus_aggressive_jetsam_lenient_allowed == FALSE) {
+#if DEVELOPMENT || DEBUG
+                       printf("Enabling Lenient Mode\n");
+#endif /* DEVELOPMENT || DEBUG */
+
+                       memorystatus_aggressive_jetsam_lenient_allowed = TRUE;
+                       memorystatus_aggressive_jetsam_lenient = TRUE;
+               }
+               break;
+       case MEMORYSTATUS_CMD_AGGRESSIVE_JETSAM_LENIENT_MODE_DISABLE:
+#if DEVELOPMENT || DEBUG
+               printf("Disabling Lenient mode\n");
+#endif /* DEVELOPMENT || DEBUG */
+               memorystatus_aggressive_jetsam_lenient_allowed = FALSE;
+               memorystatus_aggressive_jetsam_lenient = FALSE;
+               break;
 #endif /* CONFIG_JETSAM */
 #endif /* CONFIG_JETSAM */
+       case MEMORYSTATUS_CMD_PRIVILEGED_LISTENER_ENABLE:
+       case MEMORYSTATUS_CMD_PRIVILEGED_LISTENER_DISABLE:
+               error = memorystatus_low_mem_privileged_listener(args->command);
+               break;
        default:
                break;
        }
        default:
                break;
        }
@@ -3337,21 +5982,26 @@ filt_memorystatus(struct knote *kn __unused, long hint)
                switch (hint) {
                case kMemorystatusNoPressure:
                        if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PRESSURE_NORMAL) {
                switch (hint) {
                case kMemorystatusNoPressure:
                        if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PRESSURE_NORMAL) {
-                               kn->kn_fflags |= NOTE_MEMORYSTATUS_PRESSURE_NORMAL;
+                               kn->kn_fflags = NOTE_MEMORYSTATUS_PRESSURE_NORMAL;
                        }
                        break;
                case kMemorystatusPressure:
                        if (memorystatus_vm_pressure_level == kVMPressureWarning || memorystatus_vm_pressure_level == kVMPressureUrgent) {
                                if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PRESSURE_WARN) {
                        }
                        break;
                case kMemorystatusPressure:
                        if (memorystatus_vm_pressure_level == kVMPressureWarning || memorystatus_vm_pressure_level == kVMPressureUrgent) {
                                if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PRESSURE_WARN) {
-                                       kn->kn_fflags |= NOTE_MEMORYSTATUS_PRESSURE_WARN;
+                                       kn->kn_fflags = NOTE_MEMORYSTATUS_PRESSURE_WARN;
                                }
                        } else if (memorystatus_vm_pressure_level == kVMPressureCritical) {
 
                                if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PRESSURE_CRITICAL) {
                                }
                        } else if (memorystatus_vm_pressure_level == kVMPressureCritical) {
 
                                if (kn->kn_sfflags & NOTE_MEMORYSTATUS_PRESSURE_CRITICAL) {
-                                       kn->kn_fflags |= NOTE_MEMORYSTATUS_PRESSURE_CRITICAL;
+                                       kn->kn_fflags = NOTE_MEMORYSTATUS_PRESSURE_CRITICAL;
                                }
                        }
                        break;
                                }
                        }
                        break;
+               case kMemorystatusLowSwap:
+                       if (kn->kn_sfflags & NOTE_MEMORYSTATUS_LOW_SWAP) {
+                               kn->kn_fflags = NOTE_MEMORYSTATUS_LOW_SWAP;
+                       }
+                       break;
                default:
                        break;
                }
                default:
                        break;
                }
@@ -3382,16 +6032,10 @@ memorystatus_knote_register(struct knote *kn) {
        
        memorystatus_klist_lock();
        
        
        memorystatus_klist_lock();
        
-       if (kn->kn_sfflags & (NOTE_MEMORYSTATUS_PRESSURE_NORMAL | NOTE_MEMORYSTATUS_PRESSURE_WARN | NOTE_MEMORYSTATUS_PRESSURE_CRITICAL)) {
+       if (kn->kn_sfflags & (NOTE_MEMORYSTATUS_PRESSURE_NORMAL | NOTE_MEMORYSTATUS_PRESSURE_WARN | NOTE_MEMORYSTATUS_PRESSURE_CRITICAL | NOTE_MEMORYSTATUS_LOW_SWAP)) {
 
 
-#if CONFIG_JETSAM && VM_PRESSURE_EVENTS
-               /* Need a privilege to register */
-               error = priv_check_cred(kauth_cred_get(), PRIV_VM_PRESSURE, 0);
-#endif /* CONFIG_JETSAM && VM_PRESSURE_EVENTS */
+               KNOTE_ATTACH(&memorystatus_klist, kn);
 
 
-               if (!error) {
-                       KNOTE_ATTACH(&memorystatus_klist, kn);
-               }
        } else {          
                error = ENOTSUP;
        }
        } else {          
                error = ENOTSUP;
        }
@@ -3408,6 +6052,8 @@ memorystatus_knote_unregister(struct knote *kn __unused) {
        memorystatus_klist_unlock();
 }
 
        memorystatus_klist_unlock();
 }
 
+
+#if 0
 #if CONFIG_JETSAM && VM_PRESSURE_EVENTS
 static boolean_t
 memorystatus_issue_pressure_kevent(boolean_t pressured) {
 #if CONFIG_JETSAM && VM_PRESSURE_EVENTS
 static boolean_t
 memorystatus_issue_pressure_kevent(boolean_t pressured) {
@@ -3416,5 +6062,269 @@ memorystatus_issue_pressure_kevent(boolean_t pressured) {
        memorystatus_klist_unlock();
        return TRUE;
 }
        memorystatus_klist_unlock();
        return TRUE;
 }
-
 #endif /* CONFIG_JETSAM && VM_PRESSURE_EVENTS */
 #endif /* CONFIG_JETSAM && VM_PRESSURE_EVENTS */
+#endif /* 0 */
+
+#if CONFIG_JETSAM
+/* Coalition support */
+
+/* sorting info for a particular priority bucket */
+typedef struct memstat_sort_info {
+       coalition_t     msi_coal;
+       uint64_t        msi_page_count;
+       pid_t           msi_pid;
+       int             msi_ntasks;
+} memstat_sort_info_t;
+
+/* 
+ * qsort from smallest page count to largest page count
+ *
+ * return < 0 for a < b
+ *          0 for a == b
+ *        > 0 for a > b
+ */
+static int memstat_asc_cmp(const void *a, const void *b)
+{
+        const memstat_sort_info_t *msA = (const memstat_sort_info_t *)a;
+        const memstat_sort_info_t *msB = (const memstat_sort_info_t *)b;
+
+        return (int)((uint64_t)msA->msi_page_count - (uint64_t)msB->msi_page_count);
+}
+
+/*
+ * Return the number of pids rearranged during this sort.
+ */
+static int
+memorystatus_sort_by_largest_coalition_locked(unsigned int bucket_index, int coal_sort_order)
+{
+#define MAX_SORT_PIDS          80
+#define MAX_COAL_LEADERS       10
+
+       unsigned int b = bucket_index;
+       int nleaders = 0;
+       int ntasks = 0;
+       proc_t p = NULL;
+       coalition_t coal = COALITION_NULL;
+       int pids_moved = 0;
+       int total_pids_moved = 0;
+       int i;
+
+       /* 
+        * The system is typically under memory pressure when in this
+        * path, hence, we want to avoid dynamic memory allocation.
+        */
+       memstat_sort_info_t leaders[MAX_COAL_LEADERS];
+       pid_t pid_list[MAX_SORT_PIDS];
+
+       if (bucket_index >= MEMSTAT_BUCKET_COUNT) {
+                return(0);
+        }
+
+       /*
+        * Clear the array that holds coalition leader information
+        */
+       for (i=0; i < MAX_COAL_LEADERS; i++) {
+               leaders[i].msi_coal = COALITION_NULL;
+               leaders[i].msi_page_count = 0;          /* will hold total coalition page count */
+               leaders[i].msi_pid = 0;                 /* will hold coalition leader pid */
+               leaders[i].msi_ntasks = 0;              /* will hold the number of tasks in a coalition */
+       }
+
+        p = memorystatus_get_first_proc_locked(&b, FALSE);
+        while (p) {
+                if (coalition_is_leader(p->task, COALITION_TYPE_JETSAM, &coal)) {
+                       if (nleaders < MAX_COAL_LEADERS) {
+                               int coal_ntasks = 0;
+                               uint64_t coal_page_count = coalition_get_page_count(coal, &coal_ntasks);
+                               leaders[nleaders].msi_coal = coal;
+                               leaders[nleaders].msi_page_count = coal_page_count;
+                               leaders[nleaders].msi_pid = p->p_pid;           /* the coalition leader */
+                               leaders[nleaders].msi_ntasks = coal_ntasks;
+                               nleaders++;
+                       } else {
+                               /* 
+                                * We've hit MAX_COAL_LEADERS meaning we can handle no more coalitions.
+                                * Abandoned coalitions will linger at the tail of the priority band 
+                                * when this sort session ends.
+                                * TODO:  should this be an assert?
+                                */
+                               printf("%s: WARNING: more than %d leaders in priority band [%d]\n",
+                                      __FUNCTION__, MAX_COAL_LEADERS, bucket_index);
+                               break;
+                       }
+                }
+                p=memorystatus_get_next_proc_locked(&b, p, FALSE);
+        }
+
+       if (nleaders == 0) {
+               /* Nothing to sort */
+               return(0);
+       }
+
+       /* 
+        * Sort the coalition leader array, from smallest coalition page count
+        * to largest coalition page count.  When inserted in the priority bucket,
+        * smallest coalition is handled first, resulting in the last to be jetsammed.
+        */
+       if (nleaders > 1) {
+               qsort(leaders, nleaders, sizeof(memstat_sort_info_t), memstat_asc_cmp);
+       }
+
+#if 0
+       for (i = 0; i < nleaders; i++) {
+               printf("%s: coal_leader[%d of %d] pid[%d] pages[%llu] ntasks[%d]\n",
+                      __FUNCTION__, i, nleaders, leaders[i].msi_pid, leaders[i].msi_page_count,
+                       leaders[i].msi_ntasks);
+       }
+#endif
+
+       /*
+        * During coalition sorting, processes in a priority band are rearranged
+        * by being re-inserted at the head of the queue.  So, when handling a
+        * list, the first process that gets moved to the head of the queue,
+        * ultimately gets pushed toward the queue tail, and hence, jetsams last.
+        *
+        * So, for example, the coalition leader is expected to jetsam last,
+        * after its coalition members.  Therefore, the coalition leader is
+        * inserted at the head of the queue first.
+        *
+        * After processing a coalition, the jetsam order is as follows:
+        *   undefs(jetsam first), extensions, xpc services, leader(jetsam last)
+        */
+
+       /*
+        * Coalition members are rearranged in the priority bucket here,
+        * based on their coalition role.
+        */
+       total_pids_moved = 0;
+       for (i=0; i < nleaders; i++) {
+               
+               /* a bit of bookkeeping */
+               pids_moved = 0;
+
+               /* Coalition leaders are jetsammed last, so move into place first */
+               pid_list[0] = leaders[i].msi_pid;
+               pids_moved += memorystatus_move_list_locked(bucket_index, pid_list, 1);
+
+               /* xpc services should jetsam after extensions */
+               ntasks = coalition_get_pid_list (leaders[i].msi_coal, COALITION_ROLEMASK_XPC,
+                                                coal_sort_order, pid_list, MAX_SORT_PIDS);
+
+               if (ntasks > 0) {
+                       pids_moved += memorystatus_move_list_locked(bucket_index, pid_list, 
+                                                                   (ntasks <= MAX_SORT_PIDS ? ntasks : MAX_SORT_PIDS));
+               }
+
+               /* extensions should jetsam after unmarked processes */
+               ntasks = coalition_get_pid_list (leaders[i].msi_coal, COALITION_ROLEMASK_EXT,
+                                                coal_sort_order, pid_list, MAX_SORT_PIDS);
+
+               if (ntasks > 0) {
+                       pids_moved += memorystatus_move_list_locked(bucket_index, pid_list,
+                                                                   (ntasks <= MAX_SORT_PIDS ? ntasks : MAX_SORT_PIDS));
+               }
+
+               /* undefined coalition members should be the first to jetsam */
+               ntasks = coalition_get_pid_list (leaders[i].msi_coal, COALITION_ROLEMASK_UNDEF,
+                                                coal_sort_order, pid_list, MAX_SORT_PIDS);
+
+               if (ntasks > 0) {
+                       pids_moved += memorystatus_move_list_locked(bucket_index, pid_list, 
+                                                                   (ntasks <= MAX_SORT_PIDS ? ntasks : MAX_SORT_PIDS));
+               }
+
+#if 0
+               if (pids_moved == leaders[i].msi_ntasks) {
+                       /*
+                        * All the pids in the coalition were found in this band.
+                        */
+                       printf("%s: pids_moved[%d]  equal  total coalition ntasks[%d] \n", __FUNCTION__,
+                              pids_moved, leaders[i].msi_ntasks);
+               } else if (pids_moved > leaders[i].msi_ntasks) {
+                       /*
+                        * Apparently new coalition members showed up during the sort?
+                        */
+                       printf("%s: pids_moved[%d] were greater than expected coalition ntasks[%d] \n", __FUNCTION__,
+                              pids_moved, leaders[i].msi_ntasks);
+               } else {
+                       /*
+                        * Apparently not all the pids in the coalition were found in this band?
+                        */
+                       printf("%s: pids_moved[%d] were less than  expected coalition ntasks[%d] \n", __FUNCTION__,
+                              pids_moved, leaders[i].msi_ntasks);
+               }
+#endif
+
+               total_pids_moved += pids_moved;
+
+       } /* end for */
+
+       return(total_pids_moved);
+}
+
+
+/*
+ * Traverse a list of pids, searching for each within the priority band provided.
+ * If pid is found, move it to the front of the priority band.
+ * Never searches outside the priority band provided.
+ * 
+ * Input:
+ *     bucket_index - jetsam priority band.
+ *     pid_list - pointer to a list of pids.
+ *     list_sz  - number of pids in the list.
+ *
+ * Pid list ordering is important in that, 
+ * pid_list[n] is expected to jetsam ahead of pid_list[n+1].
+ * The sort_order is set by the coalition default.
+ *
+ * Return: 
+ *     the number of pids found and hence moved within the priority band.
+ */
+static int
+memorystatus_move_list_locked(unsigned int bucket_index, pid_t *pid_list, int list_sz)
+{
+       memstat_bucket_t *current_bucket;
+       int i;
+       int found_pids = 0;
+
+       if ((pid_list == NULL) || (list_sz <= 0)) {
+               return(0);
+       }
+
+       if (bucket_index >= MEMSTAT_BUCKET_COUNT) {
+                return(0);
+        }
+
+       current_bucket = &memstat_bucket[bucket_index];
+       for (i=0; i < list_sz; i++) {
+               unsigned int b = bucket_index;
+               proc_t p = NULL;
+               proc_t aProc = NULL;
+               pid_t  aPid;
+               int list_index;
+
+               list_index = ((list_sz - 1) - i);
+                aPid = pid_list[list_index];
+
+                /* never search beyond bucket_index provided */
+                p = memorystatus_get_first_proc_locked(&b, FALSE);
+                while (p) {
+                        if (p->p_pid == aPid) {
+                                aProc = p;
+                                break;
+                        }
+                        p = memorystatus_get_next_proc_locked(&b, p, FALSE);
+                }
+
+                if (aProc == NULL) {
+                       /* pid not found in this band, just skip it */
+                        continue;
+                } else {
+                        TAILQ_REMOVE(&current_bucket->list, aProc, p_memstat_list);
+                        TAILQ_INSERT_HEAD(&current_bucket->list, aProc, p_memstat_list);
+                       found_pids++;
+                }
+        }
+       return(found_pids);
+}
+#endif  /* CONFIG_JETSAM */