Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (C) 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <stddef.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <sys/mman.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <sys/vfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/magic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <os.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) /* Set by make_tempfile() during early boot. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) static char *tempdir = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) /* Check if dir is on tmpfs. Return 0 if yes, -1 if no or error. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) static int __init check_tmpfs(const char *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	struct statfs st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	os_info("Checking if %s is on tmpfs...", dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	if (statfs(dir, &st) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 		os_info("%s\n", strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	} else if (st.f_type != TMPFS_MAGIC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		os_info("no\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		os_info("OK\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * Choose the tempdir to use. We want something on tmpfs so that our memory is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  * not subject to the host's vm.dirty_ratio. If a tempdir is specified in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  * environment, we use that even if it's not on tmpfs, but we warn the user.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  * Otherwise, we try common tmpfs locations, and if no tmpfs directory is found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  * then we fall back to /tmp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static char * __init choose_tempdir(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	static const char * const vars[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		"TMPDIR",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		"TMP",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		"TEMP",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	static const char fallback_dir[] = "/tmp";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	static const char * const tmpfs_dirs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		"/dev/shm",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		fallback_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	const char *dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	os_info("Checking environment variables for a tempdir...");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	for (i = 0; vars[i]; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		dir = getenv(vars[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		if ((dir != NULL) && (*dir != '\0')) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 			os_info("%s\n", dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 			if (check_tmpfs(dir) >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 				goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 				goto warn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	os_info("none found\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	for (i = 0; tmpfs_dirs[i]; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		dir = tmpfs_dirs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		if (check_tmpfs(dir) >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	dir = fallback_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) warn:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	os_warn("Warning: tempdir %s is not on tmpfs\n", dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	/* Make a copy since getenv results may not remain valid forever. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	return strdup(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92)  * Create an unlinked tempfile in a suitable tempdir. template must be the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93)  * basename part of the template with a leading '/'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) static int __init make_tempfile(const char *template)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	char *tempname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	if (tempdir == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		tempdir = choose_tempdir();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		if (tempdir == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 			os_warn("Failed to choose tempdir: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 				strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) #ifdef O_TMPFILE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	fd = open(tempdir, O_CLOEXEC | O_RDWR | O_EXCL | O_TMPFILE, 0700);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	 * If the running system does not support O_TMPFILE flag then retry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	 * without it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	if (fd != -1 || (errno != EINVAL && errno != EISDIR &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 			errno != EOPNOTSUPP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		return fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	tempname = malloc(strlen(tempdir) + strlen(template) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (tempname == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	strcpy(tempname, tempdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	strcat(tempname, template);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	fd = mkstemp(tempname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		os_warn("open - cannot create %s: %s\n", tempname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 			strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	if (unlink(tempname) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		perror("unlink");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		goto close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	free(tempname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	return fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) close:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	free(tempname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) #define TEMPNAME_TEMPLATE "/vm_file-XXXXXX"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) static int __init create_tmp_file(unsigned long long len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	int fd, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	char zero;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	fd = make_tempfile(TEMPNAME_TEMPLATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	 * Seek to len - 1 because writing a character there will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	 * increase the file size by one byte, to the desired length.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	if (lseek64(fd, len - 1, SEEK_SET) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		perror("lseek64");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	zero = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	err = write(fd, &zero, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	if (err != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		perror("write");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	return fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) int __init create_mem_file(unsigned long long len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	int err, fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	fd = create_tmp_file(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	err = os_set_exec_close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		errno = -err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		perror("exec_close");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	return fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) void __init check_tmpexec(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	void *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	int err, fd = create_tmp_file(UM_KERN_PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	addr = mmap(NULL, UM_KERN_PAGE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		    PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	os_info("Checking PROT_EXEC mmap in %s...", tempdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	if (addr == MAP_FAILED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		err = errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		os_warn("%s\n", strerror(err));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		if (err == EPERM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 			os_warn("%s must be not mounted noexec\n", tempdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	os_info("OK\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	munmap(addr, UM_KERN_PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }