*/
        atomic64_t                      counter;
        atomic64_t                      limit;
+       int64_t                         watermark;
 
        /* Handle for "pids.events" */
        struct cgroup_file              events_file;
        kfree(css_pids(css));
 }
 
+static void pids_update_watermark(struct pids_cgroup *p, int64_t nr_pids)
+{
+       /*
+        * This is racy, but we don't need perfectly accurate tallying of
+        * the watermark, and this lets us avoid extra atomic overhead.
+        */
+       if (nr_pids > READ_ONCE(p->watermark))
+               WRITE_ONCE(p->watermark, nr_pids);
+}
+
 /**
  * pids_cancel - uncharge the local pid count
  * @pids: the pid cgroup state
 {
        struct pids_cgroup *p;
 
-       for (p = pids; parent_pids(p); p = parent_pids(p))
-               atomic64_add(num, &p->counter);
+       for (p = pids; parent_pids(p); p = parent_pids(p)) {
+               int64_t new = atomic64_add_return(num, &p->counter);
+
+               pids_update_watermark(p, new);
+       }
 }
 
 /**
                 */
                if (new > limit)
                        goto revert;
+
+               /*
+                * Not technically accurate if we go over limit somewhere up
+                * the hierarchy, but that's tolerable for the watermark.
+                */
+               pids_update_watermark(p, new);
        }
 
        return 0;
        return atomic64_read(&pids->counter);
 }
 
+static s64 pids_peak_read(struct cgroup_subsys_state *css,
+                         struct cftype *cft)
+{
+       struct pids_cgroup *pids = css_pids(css);
+
+       return READ_ONCE(pids->watermark);
+}
+
 static int pids_events_show(struct seq_file *sf, void *v)
 {
        struct pids_cgroup *pids = css_pids(seq_css(sf));
                .read_s64 = pids_current_read,
                .flags = CFTYPE_NOT_ON_ROOT,
        },
+       {
+               .name = "peak",
+               .flags = CFTYPE_NOT_ON_ROOT,
+               .read_s64 = pids_peak_read,
+       },
        {
                .name = "events",
                .seq_show = pids_events_show,