5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2013, Joyent Inc. All rights reserved.
25 */
26
27 /*
28 * Zones
29 *
30 * A zone is a named collection of processes, namespace constraints,
31 * and other system resources which comprise a secure and manageable
32 * application containment facility.
33 *
34 * Zones (represented by the reference counted zone_t) are tracked in
35 * the kernel in the zonehash. Elsewhere in the kernel, Zone IDs
36 * (zoneid_t) are used to track zone association. Zone IDs are
37 * dynamically generated when the zone is created; if a persistent
38 * identifier is needed (core files, accounting logs, audit trail,
39 * etc.), the zone name should be used.
40 *
41 *
42 * Global Zone:
43 *
44 * The global zone (zoneid 0) is automatically associated with all
1820 static int
1821 zone_misc_kstat_update(kstat_t *ksp, int rw)
1822 {
1823 zone_t *zone = ksp->ks_private;
1824 zone_misc_kstat_t *zmp = ksp->ks_data;
1825 hrtime_t tmp;
1826
1827 if (rw == KSTAT_WRITE)
1828 return (EACCES);
1829
1830 tmp = zone->zone_utime;
1831 scalehrtime(&tmp);
1832 zmp->zm_utime.value.ui64 = tmp;
1833 tmp = zone->zone_stime;
1834 scalehrtime(&tmp);
1835 zmp->zm_stime.value.ui64 = tmp;
1836 tmp = zone->zone_wtime;
1837 scalehrtime(&tmp);
1838 zmp->zm_wtime.value.ui64 = tmp;
1839
1840 zmp->zm_avenrun1.value.ui32 = zone->zone_avenrun[0];
1841 zmp->zm_avenrun5.value.ui32 = zone->zone_avenrun[1];
1842 zmp->zm_avenrun15.value.ui32 = zone->zone_avenrun[2];
1843
1844 zmp->zm_ffcap.value.ui32 = zone->zone_ffcap;
1845 zmp->zm_ffnoproc.value.ui32 = zone->zone_ffnoproc;
1846 zmp->zm_ffnomem.value.ui32 = zone->zone_ffnomem;
1847 zmp->zm_ffmisc.value.ui32 = zone->zone_ffmisc;
1848
1849 return (0);
1850 }
1851
1852 static kstat_t *
1853 zone_misc_kstat_create(zone_t *zone)
1854 {
1855 kstat_t *ksp;
1856 zone_misc_kstat_t *zmp;
1857
1858 if ((ksp = kstat_create_zone("zones", zone->zone_id,
1859 zone->zone_name, "zone_misc", KSTAT_TYPE_NAMED,
1867 zmp = ksp->ks_data = kmem_zalloc(sizeof (zone_misc_kstat_t), KM_SLEEP);
1868 ksp->ks_data_size += strlen(zone->zone_name) + 1;
1869 ksp->ks_lock = &zone->zone_misc_lock;
1870 zone->zone_misc_stats = zmp;
1871
1872 /* The kstat "name" field is not large enough for a full zonename */
1873 kstat_named_init(&zmp->zm_zonename, "zonename", KSTAT_DATA_STRING);
1874 kstat_named_setstr(&zmp->zm_zonename, zone->zone_name);
1875 kstat_named_init(&zmp->zm_utime, "nsec_user", KSTAT_DATA_UINT64);
1876 kstat_named_init(&zmp->zm_stime, "nsec_sys", KSTAT_DATA_UINT64);
1877 kstat_named_init(&zmp->zm_wtime, "nsec_waitrq", KSTAT_DATA_UINT64);
1878 kstat_named_init(&zmp->zm_avenrun1, "avenrun_1min", KSTAT_DATA_UINT32);
1879 kstat_named_init(&zmp->zm_avenrun5, "avenrun_5min", KSTAT_DATA_UINT32);
1880 kstat_named_init(&zmp->zm_avenrun15, "avenrun_15min",
1881 KSTAT_DATA_UINT32);
1882 kstat_named_init(&zmp->zm_ffcap, "forkfail_cap", KSTAT_DATA_UINT32);
1883 kstat_named_init(&zmp->zm_ffnoproc, "forkfail_noproc",
1884 KSTAT_DATA_UINT32);
1885 kstat_named_init(&zmp->zm_ffnomem, "forkfail_nomem", KSTAT_DATA_UINT32);
1886 kstat_named_init(&zmp->zm_ffmisc, "forkfail_misc", KSTAT_DATA_UINT32);
1887
1888
1889 ksp->ks_update = zone_misc_kstat_update;
1890 ksp->ks_private = zone;
1891
1892 kstat_install(ksp);
1893 return (ksp);
1894 }
1895
1896 static void
1897 zone_kstat_create(zone_t *zone)
1898 {
1899 zone->zone_lockedmem_kstat = zone_kstat_create_common(zone,
1900 "lockedmem", zone_lockedmem_kstat_update);
1901 zone->zone_swapresv_kstat = zone_kstat_create_common(zone,
1902 "swapresv", zone_swapresv_kstat_update);
1903 zone->zone_nprocs_kstat = zone_kstat_create_common(zone,
1904 "nprocs", zone_nprocs_kstat_update);
1905
1906 if ((zone->zone_misc_ksp = zone_misc_kstat_create(zone)) == NULL) {
3948 pool_lock();
3949 if (zone->zone_defaultcid > 0)
3950 cid = zone->zone_defaultcid;
3951 else
3952 cid = pool_get_class(zone->zone_pool);
3953 if (cid == -1)
3954 cid = defaultcid;
3955
3956 /*
3957 * If this fails, zone_boot will ultimately fail. The
3958 * state of the zone will be set to SHUTTING_DOWN-- userland
3959 * will have to tear down the zone, and fail, or try again.
3960 */
3961 if ((zone->zone_boot_err = newproc(zone_start_init, NULL, cid,
3962 minclsyspri - 1, &ct, 0)) != 0) {
3963 mutex_enter(&zone_status_lock);
3964 zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
3965 mutex_exit(&zone_status_lock);
3966 } else {
3967 zone->zone_boot_time = gethrestime_sec();
3968 }
3969
3970 pool_unlock();
3971 }
3972
3973 /*
3974 * Wait for zone_destroy() to be called. This is what we spend
3975 * most of our life doing.
3976 */
3977 zone_status_wait_cpr(zone, ZONE_IS_DYING, "zsched");
3978
3979 if (ct)
3980 /*
3981 * At this point the process contract should be empty.
3982 * (Though if it isn't, it's not the end of the world.)
3983 */
3984 VERIFY(contract_abandon(ct, curproc, B_TRUE) == 0);
3985
3986 /*
3987 * Allow kcred to be freed when all referring processes
|
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2013, Joyent Inc. All rights reserved.
25 * Copyright 2016 Garrett D'Amore
26 */
27
28 /*
29 * Zones
30 *
31 * A zone is a named collection of processes, namespace constraints,
32 * and other system resources which comprise a secure and manageable
33 * application containment facility.
34 *
35 * Zones (represented by the reference counted zone_t) are tracked in
36 * the kernel in the zonehash. Elsewhere in the kernel, Zone IDs
37 * (zoneid_t) are used to track zone association. Zone IDs are
38 * dynamically generated when the zone is created; if a persistent
39 * identifier is needed (core files, accounting logs, audit trail,
40 * etc.), the zone name should be used.
41 *
42 *
43 * Global Zone:
44 *
45 * The global zone (zoneid 0) is automatically associated with all
1821 static int
1822 zone_misc_kstat_update(kstat_t *ksp, int rw)
1823 {
1824 zone_t *zone = ksp->ks_private;
1825 zone_misc_kstat_t *zmp = ksp->ks_data;
1826 hrtime_t tmp;
1827
1828 if (rw == KSTAT_WRITE)
1829 return (EACCES);
1830
1831 tmp = zone->zone_utime;
1832 scalehrtime(&tmp);
1833 zmp->zm_utime.value.ui64 = tmp;
1834 tmp = zone->zone_stime;
1835 scalehrtime(&tmp);
1836 zmp->zm_stime.value.ui64 = tmp;
1837 tmp = zone->zone_wtime;
1838 scalehrtime(&tmp);
1839 zmp->zm_wtime.value.ui64 = tmp;
1840
1841 zmp->zm_boot_hrtime.value.t = zone->zone_boot_hrtime;
1842
1843 zmp->zm_avenrun1.value.ui32 = zone->zone_avenrun[0];
1844 zmp->zm_avenrun5.value.ui32 = zone->zone_avenrun[1];
1845 zmp->zm_avenrun15.value.ui32 = zone->zone_avenrun[2];
1846
1847 zmp->zm_ffcap.value.ui32 = zone->zone_ffcap;
1848 zmp->zm_ffnoproc.value.ui32 = zone->zone_ffnoproc;
1849 zmp->zm_ffnomem.value.ui32 = zone->zone_ffnomem;
1850 zmp->zm_ffmisc.value.ui32 = zone->zone_ffmisc;
1851
1852 return (0);
1853 }
1854
1855 static kstat_t *
1856 zone_misc_kstat_create(zone_t *zone)
1857 {
1858 kstat_t *ksp;
1859 zone_misc_kstat_t *zmp;
1860
1861 if ((ksp = kstat_create_zone("zones", zone->zone_id,
1862 zone->zone_name, "zone_misc", KSTAT_TYPE_NAMED,
1870 zmp = ksp->ks_data = kmem_zalloc(sizeof (zone_misc_kstat_t), KM_SLEEP);
1871 ksp->ks_data_size += strlen(zone->zone_name) + 1;
1872 ksp->ks_lock = &zone->zone_misc_lock;
1873 zone->zone_misc_stats = zmp;
1874
1875 /* The kstat "name" field is not large enough for a full zonename */
1876 kstat_named_init(&zmp->zm_zonename, "zonename", KSTAT_DATA_STRING);
1877 kstat_named_setstr(&zmp->zm_zonename, zone->zone_name);
1878 kstat_named_init(&zmp->zm_utime, "nsec_user", KSTAT_DATA_UINT64);
1879 kstat_named_init(&zmp->zm_stime, "nsec_sys", KSTAT_DATA_UINT64);
1880 kstat_named_init(&zmp->zm_wtime, "nsec_waitrq", KSTAT_DATA_UINT64);
1881 kstat_named_init(&zmp->zm_avenrun1, "avenrun_1min", KSTAT_DATA_UINT32);
1882 kstat_named_init(&zmp->zm_avenrun5, "avenrun_5min", KSTAT_DATA_UINT32);
1883 kstat_named_init(&zmp->zm_avenrun15, "avenrun_15min",
1884 KSTAT_DATA_UINT32);
1885 kstat_named_init(&zmp->zm_ffcap, "forkfail_cap", KSTAT_DATA_UINT32);
1886 kstat_named_init(&zmp->zm_ffnoproc, "forkfail_noproc",
1887 KSTAT_DATA_UINT32);
1888 kstat_named_init(&zmp->zm_ffnomem, "forkfail_nomem", KSTAT_DATA_UINT32);
1889 kstat_named_init(&zmp->zm_ffmisc, "forkfail_misc", KSTAT_DATA_UINT32);
1890 kstat_named_init(&zmp->zm_boot_hrtime, "boot_hrtime", KSTAT_DATA_TIME);
1891
1892
1893 ksp->ks_update = zone_misc_kstat_update;
1894 ksp->ks_private = zone;
1895
1896 kstat_install(ksp);
1897 return (ksp);
1898 }
1899
1900 static void
1901 zone_kstat_create(zone_t *zone)
1902 {
1903 zone->zone_lockedmem_kstat = zone_kstat_create_common(zone,
1904 "lockedmem", zone_lockedmem_kstat_update);
1905 zone->zone_swapresv_kstat = zone_kstat_create_common(zone,
1906 "swapresv", zone_swapresv_kstat_update);
1907 zone->zone_nprocs_kstat = zone_kstat_create_common(zone,
1908 "nprocs", zone_nprocs_kstat_update);
1909
1910 if ((zone->zone_misc_ksp = zone_misc_kstat_create(zone)) == NULL) {
3952 pool_lock();
3953 if (zone->zone_defaultcid > 0)
3954 cid = zone->zone_defaultcid;
3955 else
3956 cid = pool_get_class(zone->zone_pool);
3957 if (cid == -1)
3958 cid = defaultcid;
3959
3960 /*
3961 * If this fails, zone_boot will ultimately fail. The
3962 * state of the zone will be set to SHUTTING_DOWN-- userland
3963 * will have to tear down the zone, and fail, or try again.
3964 */
3965 if ((zone->zone_boot_err = newproc(zone_start_init, NULL, cid,
3966 minclsyspri - 1, &ct, 0)) != 0) {
3967 mutex_enter(&zone_status_lock);
3968 zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
3969 mutex_exit(&zone_status_lock);
3970 } else {
3971 zone->zone_boot_time = gethrestime_sec();
3972 zone->zone_boot_hrtime = gethrtime();
3973 }
3974
3975 pool_unlock();
3976 }
3977
3978 /*
3979 * Wait for zone_destroy() to be called. This is what we spend
3980 * most of our life doing.
3981 */
3982 zone_status_wait_cpr(zone, ZONE_IS_DYING, "zsched");
3983
3984 if (ct)
3985 /*
3986 * At this point the process contract should be empty.
3987 * (Though if it isn't, it's not the end of the world.)
3988 */
3989 VERIFY(contract_abandon(ct, curproc, B_TRUE) == 0);
3990
3991 /*
3992 * Allow kcred to be freed when all referring processes
|