e23cb04
diff --git a/build/cmake/CondorConfigure.cmake b/build/cmake/CondorConfigure.cmake
e23cb04
index e61fb4f..1094cb3 100644
e23cb04
--- a/build/cmake/CondorConfigure.cmake
e23cb04
+++ b/build/cmake/CondorConfigure.cmake
e23cb04
@@ -164,6 +164,7 @@ if( NOT WINDOWS)
e23cb04
 	check_function_exists("setlinebuf" HAVE_SETLINEBUF)
e23cb04
 	check_function_exists("snprintf" HAVE_SNPRINTF)
e23cb04
 	check_function_exists("snprintf" HAVE_WORKING_SNPRINTF)
e23cb04
+	check_function_exists("eventfd" HAVE_EVENTFD)
e23cb04
 
e23cb04
 	check_function_exists("stat64" HAVE_STAT64)
e23cb04
 	check_function_exists("_stati64" HAVE__STATI64)
e23cb04
diff --git a/src/condor_includes/config.h.cmake b/src/condor_includes/config.h.cmake
e23cb04
index b083945..3bd92b0 100644
e23cb04
--- a/src/condor_includes/config.h.cmake
e23cb04
+++ b/src/condor_includes/config.h.cmake
e23cb04
@@ -438,6 +438,9 @@
e23cb04
 /* Define to 1 if you have the 'snprintf' function. (USED)*/
e23cb04
 #cmakedefine HAVE_SNPRINTF 1
e23cb04
 
e23cb04
+/* Define to 1 if you have the 'eventfd' function. (USED)*/
e23cb04
+#cmakedefine HAVE_EVENTFD 1
e23cb04
+
e23cb04
 /* Define to 1 if you have the 'stat64' function. (USED)*/
e23cb04
 #cmakedefine HAVE_STAT64 1
e23cb04
 
e23cb04
diff --git a/src/condor_starter.V6.1/vanilla_proc.cpp b/src/condor_starter.V6.1/vanilla_proc.cpp
e23cb04
index 2e5538f..fe02dd3 100644
e23cb04
--- a/src/condor_starter.V6.1/vanilla_proc.cpp
e23cb04
+++ b/src/condor_starter.V6.1/vanilla_proc.cpp
e23cb04
@@ -42,9 +42,16 @@
e23cb04
 extern dynuser* myDynuser;
e23cb04
 #endif
e23cb04
 
e23cb04
+#if defined(HAVE_EVENTFD)
e23cb04
+#include <sys/eventfd.h>
e23cb04
+#endif
e23cb04
+
e23cb04
 extern CStarter *Starter;
e23cb04
 
e23cb04
-VanillaProc::VanillaProc(ClassAd* jobAd) : OsProc(jobAd)
e23cb04
+VanillaProc::VanillaProc(ClassAd* jobAd) : OsProc(jobAd),
e23cb04
+	m_memory_limit(-1),
e23cb04
+	m_oom_fd(-1),
e23cb04
+	m_oom_efd(-1)
e23cb04
 {
e23cb04
 #if !defined(WIN32)
e23cb04
 	m_escalation_tid = -1;
e23cb04
@@ -215,6 +222,12 @@ VanillaProc::StartJob()
e23cb04
 		}
e23cb04
 		fi.group_ptr = &tracking_gid;
e23cb04
 	}
e23cb04
+
e23cb04
+	// Increase the OOM score of this process; the child will inherit it.
e23cb04
+	// This way, the job will be heavily preferred to be killed over a normal process.
e23cb04
+	// OOM score is currently exponential - a score of 4 is a factor-16 increase in
e23cb04
+	// the OOM score.
e23cb04
+	setupOOMScore(4);
e23cb04
 #endif
e23cb04
 
e23cb04
 #if defined(HAVE_EXT_LIBCGROUP)
e23cb04
@@ -406,6 +419,7 @@ VanillaProc::StartJob()
e23cb04
 			int MemMb;
e23cb04
 			if (MachineAd->LookupInteger(ATTR_MEMORY, MemMb)) {
e23cb04
 				uint64_t MemMb_big = MemMb;
e23cb04
+				m_memory_limit = MemMb_big;
e23cb04
 				climits.set_memory_limit_bytes(1024*1024*MemMb_big, mem_is_soft);
e23cb04
 			} else {
e23cb04
 				dprintf(D_ALWAYS, "Not setting memory soft limit in cgroup because "
e23cb04
@@ -425,6 +439,14 @@ VanillaProc::StartJob()
e23cb04
 		} else {
e23cb04
 			dprintf(D_FULLDEBUG, "Invalid value of SlotWeight in machine ClassAd; ignoring.\n");
e23cb04
 		}
e23cb04
+		setupOOMEvent(cgroup);
e23cb04
+	}
e23cb04
+
e23cb04
+	// Now that the job is started, decrease the likelihood that the starter
e23cb04
+	// is killed instead of the job itself.
e23cb04
+	if (retval)
e23cb04
+	{
e23cb04
+		setupOOMScore(-4);
e23cb04
 	}
e23cb04
 
e23cb04
 #endif
e23cb04
@@ -611,5 +633,224 @@ VanillaProc::finishShutdownFast()
e23cb04
 	//   -gquinn, 2007-11-14
e23cb04
 	daemonCore->Kill_Family(JobPid);
e23cb04
 
e23cb04
+	if (m_oom_efd >= 0) {
e23cb04
+		dprintf(D_FULLDEBUG, "Closing event FD pipe in shutdown %d.\n", m_oom_efd);
e23cb04
+		daemonCore->Close_Pipe(m_oom_efd);
e23cb04
+		m_oom_efd = -1;
e23cb04
+	}
e23cb04
+	if (m_oom_fd >= 0) {
e23cb04
+		close(m_oom_fd);
e23cb04
+		m_oom_fd = -1;
e23cb04
+	}
e23cb04
+
e23cb04
 	return false;	// shutdown is pending, so return false
e23cb04
 }
e23cb04
+
e23cb04
+/*
e23cb04
+ * This will be called when the event fd fires, indicating an OOM event.
e23cb04
+ */
e23cb04
+int
e23cb04
+VanillaProc::outOfMemoryEvent(int /* fd */)
e23cb04
+{
e23cb04
+	std::stringstream ss;
e23cb04
+	if (m_memory_limit >= 0) {
e23cb04
+		ss << "Job has gone over memory limit of " << m_memory_limit << " megabytes.";
e23cb04
+	} else {
e23cb04
+		ss << "Job has encountered an out-of-memory event.";
e23cb04
+	}
e23cb04
+	Starter->jic->holdJob(ss.str().c_str(), CONDOR_HOLD_CODE_JobOutOfResources, 0);
e23cb04
+
e23cb04
+	// this will actually clean up the job
e23cb04
+	if ( Starter->Hold( ) ) {
e23cb04
+		dprintf( D_FULLDEBUG, "All jobs were removed due to OOM event.\n" );
e23cb04
+		Starter->allJobsDone();
e23cb04
+	}
e23cb04
+
e23cb04
+	dprintf(D_FULLDEBUG, "Closing event FD pipe %d.\n", m_oom_efd);
e23cb04
+	daemonCore->Close_Pipe(m_oom_efd);
e23cb04
+	close(m_oom_fd);
e23cb04
+	m_oom_efd = -1;
e23cb04
+	m_oom_fd = -1;
e23cb04
+
e23cb04
+	Starter->ShutdownFast();
e23cb04
+
e23cb04
+	return 0;
e23cb04
+}
e23cb04
+
e23cb04
+int
e23cb04
+VanillaProc::setupOOMScore(int new_score)
e23cb04
+{
e23cb04
+#if !defined(LINUX)
e23cb04
+	if (new_score) // Done to suppress compiler warnings.
e23cb04
+		return 0;
e23cb04
+	return 0;
e23cb04
+#endif
e23cb04
+	TemporaryPrivSentry sentry(PRIV_ROOT);
e23cb04
+	// oom_adj is deprecated on modern kernels and causes a deprecation warning when used.
e23cb04
+	int oom_score_fd = open("/proc/self/oom_score_adj", O_WRONLY | O_CLOEXEC);
e23cb04
+	if (oom_score_fd == -1) {
e23cb04
+		if (errno != ENOENT) {
e23cb04
+			dprintf(D_ALWAYS,
e23cb04
+				"Unable to open oom_score_adj for the starter: (errno=%u, %s)\n",
e23cb04
+				errno, strerror(errno));
e23cb04
+			return 1;
e23cb04
+		} else {
e23cb04
+			int oom_score_fd = open("/proc/self/oom_adj", O_WRONLY | O_CLOEXEC);
e23cb04
+			if (oom_score_fd == -1) {
e23cb04
+				dprintf(D_ALWAYS,
e23cb04
+					"Unable to open oom_adj for the starter: (errno=%u, %s)\n",
e23cb04
+					errno, strerror(errno));
e23cb04
+				return 1;
e23cb04
+			}
e23cb04
+		}
e23cb04
+	} else {
e23cb04
+		// oom_score_adj is linear; oom_adj was exponential.
e23cb04
+		if (new_score > 0)
e23cb04
+			new_score = 1 << new_score;
e23cb04
+		else
e23cb04
+			new_score = -(1 << -new_score);
e23cb04
+	}
e23cb04
+
e23cb04
+	std::stringstream ss;
e23cb04
+	ss << new_score;
e23cb04
+	std::string new_score_str = ss.str();
e23cb04
+        ssize_t nwritten = full_write(oom_score_fd, new_score_str.c_str(), new_score_str.length());
e23cb04
+	if (nwritten < 0) {
e23cb04
+		dprintf(D_ALWAYS,
e23cb04
+			"Unable to write into oom_adj file for the starter: (errno=%u, %s)\n",
e23cb04
+			errno, strerror(errno));
e23cb04
+		close(oom_score_fd);
e23cb04
+		return 1;
e23cb04
+	}
e23cb04
+	close(oom_score_fd);
e23cb04
+	return 0;
e23cb04
+}
e23cb04
+
e23cb04
+int
e23cb04
+VanillaProc::setupOOMEvent(const std::string &cgroup_string)
e23cb04
+{
e23cb04
+#if !(defined(HAVE_EVENTFD) && defined(HAVE_EXT_LIBCGROUP))
e23cb04
+	return 0;
e23cb04
+#endif
e23cb04
+	// Initialize the event descriptor
e23cb04
+	m_oom_efd = eventfd(0, EFD_CLOEXEC);
e23cb04
+	if (m_oom_efd == -1) {
e23cb04
+		dprintf(D_ALWAYS,
e23cb04
+			"Unable to create new event FD for starter: %u %s\n",
e23cb04
+			errno, strerror(errno));
e23cb04
+		return 1;
e23cb04
+	}
e23cb04
+
e23cb04
+	// Find the memcg location on disk
e23cb04
+	void * handle = NULL;
e23cb04
+	struct cgroup_mount_point mount_info;
e23cb04
+	int ret = cgroup_get_controller_begin(&handle, &mount_info);
e23cb04
+	std::stringstream oom_control;
e23cb04
+	std::stringstream event_control;
e23cb04
+	bool found_memcg = false;
e23cb04
+	while (ret == 0) {
e23cb04
+		if (strcmp(mount_info.name, MEMORY_CONTROLLER_STR) == 0) {
e23cb04
+			found_memcg = true;
e23cb04
+			oom_control << mount_info.path << "/";
e23cb04
+			event_control << mount_info.path << "/";
e23cb04
+			break;
e23cb04
+		}
e23cb04
+		cgroup_get_controller_next(&handle, &mount_info);
e23cb04
+	}
e23cb04
+	if (!found_memcg && (ret != ECGEOF)) {
e23cb04
+		dprintf(D_ALWAYS,
e23cb04
+			"Error while locating memcg controller for starter: %u %s\n",
e23cb04
+			ret, cgroup_strerror(ret));
e23cb04
+		return 1;
e23cb04
+	}
e23cb04
+	cgroup_get_controller_end(&handle);
e23cb04
+	if (found_memcg == false) {
e23cb04
+		dprintf(D_ALWAYS,
e23cb04
+			"Memcg is not available; OOM notification disabled for starter.\n");
e23cb04
+		return 1;
e23cb04
+	}
e23cb04
+
e23cb04
+	// Finish constructing the location of the control files
e23cb04
+	oom_control << cgroup_string << "/memory.oom_control";
e23cb04
+	std::string oom_control_str = oom_control.str();
e23cb04
+	event_control << cgroup_string << "/cgroup.event_control";
e23cb04
+	std::string event_control_str = event_control.str();
e23cb04
+
e23cb04
+	// Open the oom_control and event control files
e23cb04
+	TemporaryPrivSentry sentry(PRIV_ROOT);
e23cb04
+	m_oom_fd = open(oom_control_str.c_str(), O_RDONLY | O_CLOEXEC);
e23cb04
+	if (m_oom_fd == -1) {
e23cb04
+		dprintf(D_ALWAYS,
e23cb04
+			"Unable to open the OOM control file for starter: %u %s\n",
e23cb04
+			errno, strerror(errno));
e23cb04
+		return 1;
e23cb04
+	}
e23cb04
+	int event_ctrl_fd = open(event_control_str.c_str(), O_WRONLY | O_CLOEXEC);
e23cb04
+	if (event_ctrl_fd == -1) {
e23cb04
+		dprintf(D_ALWAYS,
e23cb04
+			"Unable to open event control for starter: %u %s\n",
e23cb04
+			errno, strerror(errno));
e23cb04
+		return 1;
e23cb04
+	}
e23cb04
+
e23cb04
+	// Inform Linux we will be handling the OOM events for this container.
e23cb04
+	int oom_fd2 = open(oom_control_str.c_str(), O_WRONLY | O_CLOEXEC);
e23cb04
+	if (oom_fd2 == -1) {
e23cb04
+		dprintf(D_ALWAYS,
e23cb04
+			"Unable to open the OOM control file for writing for starter: %u %s\n",
e23cb04
+			errno, strerror(errno));
e23cb04
+		return 1;
e23cb04
+	}
e23cb04
+	const char limits [] = "1";
e23cb04
+        ssize_t nwritten = full_write(oom_fd2, &limits, 1);
e23cb04
+	if (nwritten < 0) {
e23cb04
+		dprintf(D_ALWAYS,
e23cb04
+			"Unable to set OOM control to %s for starter: %u %s\n",
e23cb04
+				limits, errno, strerror(errno));
e23cb04
+		close(event_ctrl_fd);
e23cb04
+		close(oom_fd2);
e23cb04
+		return 1;
e23cb04
+	}
e23cb04
+	close(oom_fd2);
e23cb04
+
e23cb04
+	// Create the subscription string:
e23cb04
+	std::stringstream sub_ss;
e23cb04
+	sub_ss << m_oom_efd << " " << m_oom_fd;
e23cb04
+	std::string sub_str = sub_ss.str();
e23cb04
+
e23cb04
+	if ((nwritten = full_write(event_ctrl_fd, sub_str.c_str(), sub_str.size())) < 0) {
e23cb04
+		dprintf(D_ALWAYS,
e23cb04
+			"Unable to write into event control file for starter: %u %s\n",
e23cb04
+			errno, strerror(errno));
e23cb04
+		close(event_ctrl_fd);
e23cb04
+		return 1;
e23cb04
+	}
e23cb04
+	close(event_ctrl_fd);
e23cb04
+
e23cb04
+	// Fool DC into talking to the eventfd
e23cb04
+	int pipes[2]; pipes[0] = -1; pipes[1] = -1;
e23cb04
+	int fd_to_replace = -1;
e23cb04
+	if (daemonCore->Create_Pipe(pipes, true) == -1 || pipes[0] == -1) {
e23cb04
+		dprintf(D_ALWAYS, "Unable to create a DC pipe\n");
e23cb04
+		close(m_oom_efd);
e23cb04
+		m_oom_efd = -1;
e23cb04
+		close(m_oom_fd);
e23cb04
+		m_oom_fd = -1;
e23cb04
+		return 1;
e23cb04
+	}
e23cb04
+	if ( daemonCore->Get_Pipe_FD(pipes[0], &fd_to_replace) == -1 || fd_to_replace == -1) {
e23cb04
+		dprintf(D_ALWAYS, "Unable to lookup pipe's FD\n");
e23cb04
+		close(m_oom_efd); m_oom_efd = -1;
e23cb04
+		close(m_oom_fd); m_oom_fd = -1;
e23cb04
+		daemonCore->Close_Pipe(pipes[0]);
e23cb04
+		daemonCore->Close_Pipe(pipes[1]);
e23cb04
+	}
e23cb04
+	dup3(m_oom_efd, fd_to_replace, O_CLOEXEC);
e23cb04
+	close(m_oom_efd);
e23cb04
+	m_oom_efd = pipes[0];
e23cb04
+
e23cb04
+	// Inform DC we want to recieve notifications from this FD.
e23cb04
+	daemonCore->Register_Pipe(pipes[0],"OOM event fd", static_cast<PipeHandlercpp>(&VanillaProc::outOfMemoryEvent),"OOM Event Handler",this,HANDLE_READ);
e23cb04
+	return 0;
e23cb04
+}
e23cb04
+
e23cb04
diff --git a/src/condor_starter.V6.1/vanilla_proc.h b/src/condor_starter.V6.1/vanilla_proc.h
e23cb04
index d524cf5..90b4741 100644
e23cb04
--- a/src/condor_starter.V6.1/vanilla_proc.h
e23cb04
+++ b/src/condor_starter.V6.1/vanilla_proc.h
e23cb04
@@ -74,6 +74,15 @@ private:
e23cb04
 #if !defined(WIN32)
e23cb04
 	int m_escalation_tid;
e23cb04
 #endif
e23cb04
+
e23cb04
+	// Configure OOM killer for this job
e23cb04
+	int m_memory_limit; // Memory limit, in MB.
e23cb04
+	int m_oom_fd; // The file descriptor which recieves events
e23cb04
+	int m_oom_efd; // The event FD to watch
e23cb04
+	int setupOOMScore(int new_score);
e23cb04
+	int outOfMemoryEvent(int fd);
e23cb04
+	int setupOOMEvent(const std::string & cgroup_string);
e23cb04
+
e23cb04
 };
e23cb04
 
e23cb04
 #endif
e23cb04
diff --git a/src/condor_utils/condor_holdcodes.h b/src/condor_utils/condor_holdcodes.h
e23cb04
index d788d6e..3083db3 100644
e23cb04
--- a/src/condor_utils/condor_holdcodes.h
e23cb04
+++ b/src/condor_utils/condor_holdcodes.h
e23cb04
@@ -128,4 +128,6 @@ const int CONDOR_HOLD_CODE_GlexecChownSandboxToCondor = 30;
e23cb04
 
e23cb04
 const int CONDOR_HOLD_CODE_PrivsepChownSandboxToCondor = 31;
e23cb04
 
e23cb04
+const int CONDOR_HOLD_CODE_JobOutOfResources = 32;
e23cb04
+
e23cb04
 #endif