Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 45d921c1 authored by T.J. Mercier's avatar T.J. Mercier
Browse files

Fix race applying SystemMemoryProcess for system_server

When UsePerAppMemcg is true, we intend for system_server to be placed in
the root of the system memcg instead of its own per-app memcg. The
current implementation suffers from a race, since the memcg migration is
performed by the parent process (Zygote) when there is no guarantee
cgroup setup (createProcessGroup) has been completed in the child. This
can result in system_server being migrated to the system memcg initially
only to be migrated again later to a per-app memcg.

Fix this by moving the system_server migration from Zygote to
system_server after cgroup setup is completed.

Test: $ adb wait-for-device shell 'grep memory /proc/`pidof system_server`/cgroup'
Test: 4:memory:/system
Flag: EXEMPT bugfix
Bug: 434785097
Change-Id: I0952e747325a4cdeefe7491566293fd33230d2b4
parent ce79db49
Loading
Loading
Loading
Loading
+9 −9
Original line number Diff line number Diff line
@@ -1976,6 +1976,15 @@ static void SpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArray gids,
                                 : CREATE_ERROR("createProcessGroup(%d, %d) failed: %s", uid,
                                                /* pid= */ 0, strerror(-rc)));
        }

        if (is_system_server && UsePerAppMemcg()) {
            // Assign system_server to the correct memory cgroup.
            // Not all devices mount memcg so check if it is mounted first
            // to avoid unnecessarily printing errors and denials in the logs.
            if (!SetTaskProfiles(getpid(), std::vector<std::string>{"SystemMemoryProcess"})) {
                ALOGE("couldn't add process %d into system memcg group", getpid());
            }
        }
    }

    SetGids(env, gids, is_child_zygote, fail_fn);
@@ -2629,15 +2638,6 @@ static jint com_android_internal_os_Zygote_nativeForkSystemServer(
          ALOGE("System server process %d has died. Restarting Zygote!", pid);
          RuntimeAbort(env, __LINE__, "System server process has died. Restarting Zygote!");
      }

      if (UsePerAppMemcg()) {
          // Assign system_server to the correct memory cgroup.
          // Not all devices mount memcg so check if it is mounted first
          // to avoid unnecessarily printing errors and denials in the logs.
          if (!SetTaskProfiles(pid, std::vector<std::string>{"SystemMemoryProcess"})) {
              ALOGE("couldn't add process %d into system memcg group", pid);
          }
      }
  }
  return pid;
}