user namespaces: bump idmap limits
PATCH 1/1 user namespaces: bump idmap limits
We have quite some use cases where users already run into the current limit for {g,u}id mappings. Consider a user requesting us to map everything but 999, and 1001 for a given range of 1000000000 with a sub{g,u}id layout of:
some-user:100000:1000000000
some-user:999:1
some-user:1000:1
some-user:1001:1
some-user:1002:1
This translates to:
MAPPING-TYPE CONTAINER HOST RANGE
uid 999 999 1
uid 1001 1001 1
uid 0 1000000 999 uid 1000 1001000 1 uid 1002 1001002 999998998
gid 999 999 1
gid 1001 1001 1
gid 0 1000000 999 gid 1000 1001000 1 gid 1002 1001002 999998998
which is already the current limit.
Design Notes:
As discussed at LPC simply bumping the number of limits is not going to work since this would mean that struct uid_gid_map won't fit into a single cache-line anymore thereby regressing performance for the base-cases. The same problem seems to arise when using a single pointer. So the idea is to keep the base cases (0-3 mappings) directly in struct uid_gid_map so they fit into a single cache-line of 64 byte. For the two removed mappings we place three pointers in the struct that mock the behavior of traditional filesystems:
1. a direct pointer to a struct uid_gid_extent of 5 mappings of 60 bytes
2. an indirect pointer to an array of 64 byte of direct pointers to struct uid_gid_extent of 5 mappings a 60 bytes each
3. a double indirect pointer to an array of 64 bytes of indirect pointers each to an array of 64 bytes of direct pointers (and so on)
Fixing a pointer size of 8 byte this gives us 3 + 5 + (8 * 5) + (8 * (8 * 5)) = 368 mappings which should really be enough. The idea of this approach is to always have each extent of idmaps (struct uid_gid_extent) be 60 bytes (5 * (4 + 4 + 4) and thus 4 bytes smaller than the size of a single cache line. This should only see a (i.e. linear) performance impact caused by iterating through the idmappings in a for-loop. Note that the base cases shouldn't see any performance degradation which is the most important part.
Performance Testing:
When Eric introduced the extent-based struct uid_gid_map approach he measured the performanc impact of his idmap changes:
Quoted 9 lines
My benchmark consisted of going to single user mode where nothing else was running. On an ext4 filesystem opening 1,000,000 files and looping through all of the files 1000 times and calling fstat on the individuals files. This was to ensure I was benchmarking stat times where the inodes were in the kernels cache, but the inode values were not in the processors cache. My results:
v3.4-rc1: ~= 156ns (unmodified v3.4-rc1 with user namespace support disabled) v3.4-rc1-userns-: ~= 155ns (v3.4-rc1 with my user namespace patches and user namespace support disabled)v3.4-rc1-userns+: ~= 164ns (v3.4-rc1 with my user namespace patches and user namespace support enabled)
I used an identical approach on my laptop. Here's a thorough description of what I did. I built three kernels and used an additional "control" kernel:
1. v4.14-rc2-vanilla (unmodified v4.14-rc2)
2. v4.14-rc2-userns+ (v4.14-rc2 with my new user namespace idmap limits patch)
3. v4.14-rc2-userns- (v4.14-rc2 without my new user namespace idmap limits patch)
4. v4.12.0-12-generic (v4.12.0-12 standard Ubuntu kernel)
I booted into single user mode (systemd rescue target in newspeak) and used an ext4 filesystem to open/create 1,000,000 files. Then I looped through all of the files calling fstat() on each of them 1000 times and calculated the mean fstat() time for a single file. (The test program can be found below.)
For kernels v4.14-rc2-vanilla, v4.12.0-12-generic I tested the following cases:
0 mappings
1 mapping
2 mappings
3 mappings
5 mappings
For kernel v4.4-rc2-userns+ I tested:
0 mappings
1 mapping
2 mappings
3 mappings
5 mappings
10 mappings
50 mappings
100 mappings
200 mappings
300 mappings
Here are the results:
- v4.14-rc2-vanilla (unmodified v4.14-rc2)
# no unshare: 312 ns unshare -U # write 0 mappings: 307 ns unshare -U # write 1 mappings: 328 ns unshare -U # write 2 mappings: 328 ns unshare -U # write 3 mappings: 328 ns unshare -U # write 5 mappings: 338 ns
- v4.14-rc2-userns+ (v4.14-rc2 with my new user namespace idmap limits patch)
# no unshare: 158 ns unshare -U # write 0 mappings: 158 ns unshare -U # write 1 mappings: 164 ns unshare -U # write 2 mappings: 170 ns unshare -U # write 3 mappings: 175 ns unshare -U # write 5 mappings: 187 ns unshare -U # write 10 mappings: 218 ns unshare -U # write 50 mappings: 528 ns unshare -U # write 100 mappings: 980 ns unshare -U # write 200 mappings: 1880 ns unshare -U # write 300 mappings: 2760 ns
- v3.4-rc1-userns-: ~= 155ns (v3.4-rc1 with my user namespace patches and user namespace support disabled)
# no unshare: 161 ns
- 4.12.0-12-generic Ubuntu Kernel:
# no unshare: 328 ns unshare -U # write 0 mappings: 327 ns unshare -U # write 1 mappings: 328 ns unshare -U # write 2 mappings: 328 ns unshare -U # write 3 mappings: 328 ns unshare -U # write 5 mappings: 338 ns
I've tested this multiple times and the numbers hold up. All v4.14-rc2 kernels were built on the same machine with the same .config, the same options and a simple call to make -j 11 bindeb-pkg. The 4.12 kernel was simply installed from the Ubuntu archives.
The most import part seems to me that my idmap patches don't regress performance for the base-cases. I'd actually only consider 0 and 1 mapping to be the proper base cases since this is the standard layout that users usually expect when using user namespaces. However, the numbers show that even up to 5 mappings there's no real performance regression.
When writing more than 10 mappings the numbers nicely show that in the worst case the performance degrades linearly which is what I was aiming for. To me this even seems unrelated to cache misses but is rather related to how for-loops usually scale which is a good sign (I think.).
I expect most users will usually go slightly over the 5 mappings mark up to 10 mappings. For example, when the administrator runs a system container that serves as a number of users where each of them will want to be able to mount their home directory in the container and create files with their own uid and gid. I've used this specific system container setup before on HPC clusters.
The really high idmap numbers are good to have for specific use cases. I have a bunch of containers that punch various complex holes into idmap ranges to guarantee safe access for a high number of programs that run with their own uid on the host and in the container. LXD's idmapping algorithm is able to calculate complex idmaps that are handed down to the LIBLXC driver.
Here's the test program I used. I asked Eric what he did and this is a more "advanced" implementation of the idea. It's pretty straight-forward:
/*
* Copyright © 2017 Christian Brauner <[email protected]>. *
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
int main(int argc, char *argv[])
{
int ret;
size_t i, k;
int fd[1000000];
int times[1000];
char pathname[4096];
struct stat st;
struct timeval t1, t2;
uint64_t time_in_mcs;
uint64_t sum = 0;
if (argc != 2) {
fprintf(stderr, "Please specify a directory where to create "
"the test files\n");
exit(EXIT_FAILURE);
}
for (i = 0; i < sizeof(fd) / sizeof(fd[0]); i++) {
sprintf(pathname, "%s/idmap_test_%zu", argv[1], i);
fd[i]= open(pathname, O_RDWR | O_CREAT, S_IXUSR | S_IXGRP | S_IXOTH);
if (fd[i] < 0) {
ssize_t j;
for (j = i; j >= 0; j--)
close(fd[j]);
exit(EXIT_FAILURE);
}
}
for (k = 0; k < 1000; k++) {
ret = gettimeofday(&t1, NULL);
if (ret < 0)
goto close_all;
for (i = 0; i < sizeof(fd) / sizeof(fd[0]); i++) {
ret = fstat(fd[i], &st);
if (ret < 0)
goto close_all;
}
ret = gettimeofday(&t2, NULL);
if (ret < 0)
goto close_all;
time_in_mcs = (1000000 * t2.tv_sec + t2.tv_usec) -
(1000000 * t1.tv_sec + t1.tv_usec);
printf("Total time in micro seconds: %" PRIu64 "\n",
time_in_mcs);
printf("Total time in nanoseconds: %" PRIu64 "\n",
time_in_mcs * 1000);
printf("Time per file in nanoseconds: %" PRIu64 "\n",
(time_in_mcs * 1000) / 1000000);
times[k] = (time_in_mcs * 1000) / 1000000;
}close_all:
for (i = 0; i < sizeof(fd) / sizeof(fd[0]); i++)
close(fd[i]);
if (ret < 0)
exit(EXIT_FAILURE);
for (k = 0; k < 1000; k++)
sum += times[k];
printf("Mean time per file in nanoseconds: %" PRIu64 "\n", sum / 1000);
exit(EXIT_SUCCESS);;
}--- include/linux/user_namespace.h | 18 ++- kernel/user_namespace.c | 264 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 263 insertions(+), 19 deletions(-)
include/linux/user_namespace.h +16 -2
@@ -10,15 +10,29 @@#include <linux/sysctl.h> #include <linux/err.h> +#define UID_GID_MAP_PTR_SIZE 8#define UID_GID_MAP_MAX_EXTENTS 5 +#define UID_GID_MAP_BASE 3 +#define UID_GID_MAP_DIRECT UID_GID_MAP_MAX_EXTENTS +#define UID_GID_MAP_IDIRECT (UID_GID_MAP_MAX_EXTENTS * UID_GID_MAP_PTR_SIZE) +#define UID_GID_MAP_DIDIRECT (UID_GID_MAP_IDIRECT * UID_GID_MAP_PTR_SIZE) +#define UID_GID_MAP_MAX \ + (UID_GID_MAP_BASE + UID_GID_MAP_DIRECT + UID_GID_MAP_IDIRECT + \ + UID_GID_MAP_DIDIRECT)struct uid_gid_map {/* 64 bytes -- 1 cache line */ -u32 nr_extents; +/* Pointer to an extent of size UID_GID_MAP_MAX_EXTENTS */ +struct uid_gid_extent *direct; +/* Pointer to a 64 byte array of 8 direct pointers. */ +struct uid_gid_extent **idirect; +/* Pointer to a 64 byte array of 8 idirect pointers. */ +struct uid_gid_extent ***didirect;struct uid_gid_extent {u32 first;u32 lower_first;u32 count; - } extent[UID_GID_MAP_MAX_EXTENTS ]; + } extent[3 ]; +u32 nr_extents; };#define USERNS_SETGROUPS_ALLOWED 1UL
kernel/user_namespace.c +247 -17
394 lines not on this page · raw message
Signature · 1 line
2.14.1
Quoted 39 lines · Christian Brauner
We have quite some use cases where users already run into the current limit for {g,u}id mappings. Consider a user requesting us to map everything but 999, and 1001 for a given range of 1000000000 with a sub{g,u}id layout of:
some-user:100000:1000000000
some-user:999:1
some-user:1000:1
some-user:1001:1
some-user:1002:1This translates to:
MAPPING-TYPE CONTAINER HOST RANGE
uid 999 999 1
uid 1001 1001 1uid 0 1000000 999 uid 1000 1001000 1 uid 1002 1001002 999998998gid 999 999 1
gid 1001 1001 1gid 0 1000000 999 gid 1000 1001000 1 gid 1002 1001002 999998998which is already the current limit.
Design Notes:
As discussed at LPC simply bumping the number of limits is not going to work since this would mean that structuid_gid_mapwon't fit into a single cache-line anymore thereby regressing performance for the base-cases. The same problem seems to arise when using a single pointer. So the idea is to keep the base cases (0-3 mappings) directly in structuid_gid_mapso they fit into a single cache-line of 64 byte. For the two removed mappings we place three pointers in the struct that mock the behavior of traditional filesystems:
1. a direct pointer to a structuid_gid_extentof 5 mappings of 60 bytes
2. an indirect pointer to an array of 64 byte of direct pointers to structuid_gid_extentof 5 mappings a 60 bytes each
3. a double indirect pointer to an array of 64 bytes of indirect pointers each
Quoted 8 lines
to an array of 64 bytes of direct pointers (and so on)
Fixing a pointer size of 8 byte this gives us 3 + 5 + (8 * 5) + (8 * (8 * 5)) = 368 mappings which should really be enough. The idea of this approach is to always have each extent of idmaps (structuid_gid_extent) be 60 bytes (5 * (4 + 4 + 4) and thus 4 bytes smaller than the size of a single cache line. This should only see a (i.e. linear) performance impact caused by iterating through the idmappings in a for-loop. Note that the base cases shouldn't see any performance degradation which is the most important part.
Sounds like a good plan.
Quoted 14 lines
Performance Testing:
When Eric introduced the extent-based structuid_gid_mapapproach he measured the performanc impact of his idmap changes:Quoted 9 lines
My benchmark consisted of going to single user mode where nothing else was running. On an ext4 filesystem opening 1,000,000 files and looping through all of the files 1000 times and calling fstat on the individuals files. This was to ensure I was benchmarking stat times where the inodes were in the kernels cache, but the inode values were not in the processors cache. My results:
v3.4-rc1: ~= 156ns (unmodified v3.4-rc1 with user namespace support disabled) v3.4-rc1-userns-: ~= 155ns (v3.4-rc1 with my user namespace patches and user namespace support disabled)v3.4-rc1-userns+: ~= 164ns (v3.4-rc1 with my user namespace patches and user namespace support enabled)
Quoted 6 lines
I used an identical approach on my laptop. Here's a thorough description of what I did. I built three kernels and used an additional "control" kernel:
1. v4.14-rc2-vanilla (unmodified v4.14-rc2)
2. v4.14-rc2-userns+ (v4.14-rc2 with my new user namespace idmap limits patch)
3. v4.14-rc2-userns- (v4.14-rc2 without my new user namespace idmap limits patch)
^ you mean *withYou your patch but with CONFIG_USER_NS=n ?
Quoted 1 line
4. v4.12.0-12-generic (v4.12.0-12 standard Ubuntu kernel)
^ Just curious, why did you include this? To show that other factors have a much larger impact? This does not include your patch, right?
Quoted 51 lines
I booted into single user mode (systemd rescue target in newspeak) and used an ext4 filesystem to open/create 1,000,000 files. Then I looped through all of the files calling
fstat()on each of them 1000 times and calculated the meanfstat()time for a single file. (The test program can be found below.)For kernels v4.14-rc2-vanilla, v4.12.0-12-generic I tested the following cases:
0 mappings
1 mapping
2 mappings
3 mappings
5 mappingsFor kernel v4.4-rc2-userns+ I tested:
0 mappings
1 mapping
2 mappings
3 mappings
5 mappings
10 mappings
50 mappings
100 mappings
200 mappings
300 mappingsHere are the results:
- v4.14-rc2-vanilla (unmodified v4.14-rc2)
# no unshare: 312 ns unshare -U # write 0 mappings: 307 ns unshare -U # write 1 mappings: 328 ns unshare -U # write 2 mappings: 328 ns unshare -U # write 3 mappings: 328 ns unshare -U # write 5 mappings: 338 ns- v4.14-rc2-userns+ (v4.14-rc2 with my new user namespace idmap limits patch)
# no unshare: 158 ns unshare -U # write 0 mappings: 158 ns unshare -U # write 1 mappings: 164 ns unshare -U # write 2 mappings: 170 ns unshare -U # write 3 mappings: 175 ns unshare -U # write 5 mappings: 187 ns unshare -U # write 10 mappings: 218 ns unshare -U # write 50 mappings: 528 ns unshare -U # write 100 mappings: 980 ns unshare -U # write 200 mappings: 1880 ns unshare -U # write 300 mappings: 2760 ns- v3.4-rc1-userns-: ~= 155ns (v3.4-rc1 with my user namespace patches and user namespace support disabled)
# no unshare: 161 ns
Quoted 8 lines
- 4.12.0-12-generic Ubuntu Kernel:
# no unshare: 328 ns unshare -U # write 0 mappings: 327 ns unshare -U # write 1 mappings: 328 ns unshare -U # write 2 mappings: 328 ns unshare -U # write 3 mappings: 328 ns unshare -U # write 5 mappings: 338 ns
^ This is really weird. Why does Ubuntu kernel have near-constant (horrible) time?
Quoted 7 lines
I've tested this multiple times and the numbers hold up. All v4.14-rc2 kernels were built on the same machine with the same .config, the same options and a simple call to make -j 11 bindeb-pkg. The 4.12 kernel was simply installed from the Ubuntu archives.
The most import part seems to me that my idmap patches don't regress performance for the base-cases. I'd actually only consider 0 and 1 mapping to be the proper
Agreed. Now personally I probably would have kept 4 direct pointers then make the 5+ case hurt more, but I'm not saying that's the right thing.
(haven't looked at the patch itself yet)
thanks,
-serge
Quoted 65 lines · Serge Hallyn
Quoted 39 lines · Christian Brauner
We have quite some use cases where users already run into the current limit for {g,u}id mappings. Consider a user requesting us to map everything but 999, and 1001 for a given range of 1000000000 with a sub{g,u}id layout of:
some-user:100000:1000000000
some-user:999:1
some-user:1000:1
some-user:1001:1
some-user:1002:1This translates to:
MAPPING-TYPE CONTAINER HOST RANGE
uid 999 999 1
uid 1001 1001 1uid 0 1000000 999 uid 1000 1001000 1 uid 1002 1001002 999998998gid 999 999 1
gid 1001 1001 1gid 0 1000000 999 gid 1000 1001000 1 gid 1002 1001002 999998998which is already the current limit.
Design Notes:
As discussed at LPC simply bumping the number of limits is not going to work since this would mean that structuid_gid_mapwon't fit into a single cache-line anymore thereby regressing performance for the base-cases. The same problem seems to arise when using a single pointer. So the idea is to keep the base cases (0-3 mappings) directly in structuid_gid_mapso they fit into a single cache-line of 64 byte. For the two removed mappings we place three pointers in the struct that mock the behavior of traditional filesystems:
1. a direct pointer to a structuid_gid_extentof 5 mappings of 60 bytes
2. an indirect pointer to an array of 64 byte of direct pointers to structuid_gid_extentof 5 mappings a 60 bytes each
3. a double indirect pointer to an array of 64 bytes of indirect pointers eachQuoted 8 lines
to an array of 64 bytes of direct pointers (and so on)
Fixing a pointer size of 8 byte this gives us 3 + 5 + (8 * 5) + (8 * (8 * 5)) = 368 mappings which should really be enough. The idea of this approach is to always have each extent of idmaps (structuid_gid_extent) be 60 bytes (5 * (4 + 4 + 4) and thus 4 bytes smaller than the size of a single cache line. This should only see a (i.e. linear) performance impact caused by iterating through the idmappings in a for-loop. Note that the base cases shouldn't see any performance degradation which is the most important part.Sounds like a good plan.
Quoted 14 lines
Performance Testing:
When Eric introduced the extent-based structuid_gid_mapapproach he measured the performanc impact of his idmap changes:Quoted 9 lines
My benchmark consisted of going to single user mode where nothing else was running. On an ext4 filesystem opening 1,000,000 files and looping through all of the files 1000 times and calling fstat on the individuals files. This was to ensure I was benchmarking stat times where the inodes were in the kernels cache, but the inode values were not in the processors cache. My results:
v3.4-rc1: ~= 156ns (unmodified v3.4-rc1 with user namespace support disabled) v3.4-rc1-userns-: ~= 155ns (v3.4-rc1 with my user namespace patches and user namespace support disabled)v3.4-rc1-userns+: ~= 164ns (v3.4-rc1 with my user namespace patches and user namespace support enabled)
Quoted 8 lines
Quoted 6 lines
I used an identical approach on my laptop. Here's a thorough description of what I did. I built three kernels and used an additional "control" kernel:
1. v4.14-rc2-vanilla (unmodified v4.14-rc2)
2. v4.14-rc2-userns+ (v4.14-rc2 with my new user namespace idmap limits patch)
3. v4.14-rc2-userns- (v4.14-rc2 without my new user namespace idmap limits patch)^ you mean *withYou your patch but with
CONFIG_USER_NS=n?
Yes, exactly. Sorry, that was unclear here.
Quoted 5 lines
Quoted 2 lines
4. v4.12.0-12-generic (v4.12.0-12 standard Ubuntu kernel)
^ Just curious, why did you include this? To show that other factors have a much larger impact? This does not include your patch, right?
Basically I wanted something which I didn't compile and see if the numbers somehow line-up. In terms of experimentation you could think of this as a second "control condition".
Quoted 61 lines
Quoted 52 lines
I booted into single user mode (systemd rescue target in newspeak) and used an ext4 filesystem to open/create 1,000,000 files. Then I looped through all of the files calling
fstat()on each of them 1000 times and calculated the meanfstat()time for a single file. (The test program can be found below.)For kernels v4.14-rc2-vanilla, v4.12.0-12-generic I tested the following cases:
0 mappings
1 mapping
2 mappings
3 mappings
5 mappingsFor kernel v4.4-rc2-userns+ I tested:
0 mappings
1 mapping
2 mappings
3 mappings
5 mappings
10 mappings
50 mappings
100 mappings
200 mappings
300 mappingsHere are the results:
- v4.14-rc2-vanilla (unmodified v4.14-rc2)
# no unshare: 312 ns unshare -U # write 0 mappings: 307 ns unshare -U # write 1 mappings: 328 ns unshare -U # write 2 mappings: 328 ns unshare -U # write 3 mappings: 328 ns unshare -U # write 5 mappings: 338 ns- v4.14-rc2-userns+ (v4.14-rc2 with my new user namespace idmap limits patch)
# no unshare: 158 ns unshare -U # write 0 mappings: 158 ns unshare -U # write 1 mappings: 164 ns unshare -U # write 2 mappings: 170 ns unshare -U # write 3 mappings: 175 ns unshare -U # write 5 mappings: 187 ns unshare -U # write 10 mappings: 218 ns unshare -U # write 50 mappings: 528 ns unshare -U # write 100 mappings: 980 ns unshare -U # write 200 mappings: 1880 ns unshare -U # write 300 mappings: 2760 ns- v3.4-rc1-userns-: ~= 155ns (v3.4-rc1 with my user namespace patches and user namespace support disabled)
# no unshare: 161 nsQuoted 8 lines
- 4.12.0-12-generic Ubuntu Kernel:
# no unshare: 328 ns unshare -U # write 0 mappings: 327 ns unshare -U # write 1 mappings: 328 ns unshare -U # write 2 mappings: 328 ns unshare -U # write 3 mappings: 328 ns unshare -U # write 5 mappings: 338 ns
Quoted 2 lines
^ This is really weird. Why does Ubuntu kernel have near-constant (horrible) time?
I actually think - even in single user mode - with the same number of processes running and so on - that there's a lot of fluctuation going on. That's why I ran the tests multiple times. It might also depend on compilation since I compiled the three kernels myself and just downloaded the binaries for the ubuntu kernel. The tests clearly show that there's an increase with the number of mappings which is what I expected.
Quoted 11 lines
Quoted 8 lines
I've tested this multiple times and the numbers hold up. All v4.14-rc2 kernels were built on the same machine with the same .config, the same options and a simple call to make -j 11 bindeb-pkg. The 4.12 kernel was simply installed from the Ubuntu archives.
The most import part seems to me that my idmap patches don't regress performance for the base-cases. I'd actually only consider 0 and 1 mapping to be the proper
Agreed. Now personally I probably would have kept 4 direct pointers then make the 5+ case hurt more, but I'm not saying that's the right thing.
Yeah, I thought about that as well but my goal was to basically ramp up the number of mappings into the hundreds to settle this "once and for all". I actually don't expect us to go any higher than this. Tbh, users that have a requirement to have many mappings should be prepared to take the performance hit. Also, I think that the direct pointers won't necessarily give you more speed since - I'd guess - that the slowdown simply comes from the number of iterations through the map you have to do and not necessarily from cache misses. But I might be thinking nonsense here. Thanks!
Quoted 5 lines
(haven't looked at the patch itself yet)
thanks,
-serge
Quoted 105 lines · Christian Brauner
We have quite some use cases where users already run into the current limit for {g,u}id mappings. Consider a user requesting us to map everything but 999, and 1001 for a given range of 1000000000 with a sub{g,u}id layout of:
some-user:100000:1000000000
some-user:999:1
some-user:1000:1
some-user:1001:1
some-user:1002:1This translates to:
MAPPING-TYPE CONTAINER HOST RANGE
uid 999 999 1
uid 1001 1001 1uid 0 1000000 999 uid 1000 1001000 1 uid 1002 1001002 999998998gid 999 999 1
gid 1001 1001 1gid 0 1000000 999 gid 1000 1001000 1 gid 1002 1001002 999998998which is already the current limit.
Design Notes:
As discussed at LPC simply bumping the number of limits is not going to work since this would mean that structuid_gid_mapwon't fit into a single cache-line anymore thereby regressing performance for the base-cases. The same problem seems to arise when using a single pointer. So the idea is to keep the base cases (0-3 mappings) directly in structuid_gid_mapso they fit into a single cache-line of 64 byte. For the two removed mappings we place three pointers in the struct that mock the behavior of traditional filesystems:
1. a direct pointer to a structuid_gid_extentof 5 mappings of 60 bytes
2. an indirect pointer to an array of 64 byte of direct pointers to structuid_gid_extentof 5 mappings a 60 bytes each
3. a double indirect pointer to an array of 64 bytes of indirect pointers each to an array of 64 bytes of direct pointers (and so on)
Fixing a pointer size of 8 byte this gives us 3 + 5 + (8 * 5) + (8 * (8 * 5)) = 368 mappings which should really be enough. The idea of this approach is to always have each extent of idmaps (structuid_gid_extent) be 60 bytes (5 * (4 + 4 + 4) and thus 4 bytes smaller than the size of a single cache line. This should only see a (i.e. linear) performance impact caused by iterating through the idmappings in a for-loop. Note that the base cases shouldn't see any performance degradation which is the most important part.Performance Testing:
When Eric introduced the extent-based structuid_gid_mapapproach he measured the performanc impact of his idmap changes:Quoted 9 lines
My benchmark consisted of going to single user mode where nothing else was running. On an ext4 filesystem opening 1,000,000 files and looping through all of the files 1000 times and calling fstat on the individuals files. This was to ensure I was benchmarking stat times where the inodes were in the kernels cache, but the inode values were not in the processors cache. My results:
v3.4-rc1: ~= 156ns (unmodified v3.4-rc1 with user namespace support disabled) v3.4-rc1-userns-: ~= 155ns (v3.4-rc1 with my user namespace patches and user namespace support disabled)v3.4-rc1-userns+: ~= 164ns (v3.4-rc1 with my user namespace patches and user namespace support enabled)
I used an identical approach on my laptop. Here's a thorough description of what I did. I built three kernels and used an additional "control" kernel:
1. v4.14-rc2-vanilla (unmodified v4.14-rc2)
2. v4.14-rc2-userns+ (v4.14-rc2 with my new user namespace idmap limits patch)
3. v4.14-rc2-userns- (v4.14-rc2 without my new user namespace idmap limits patch)
4. v4.12.0-12-generic (v4.12.0-12 standard Ubuntu kernel)I booted into single user mode (systemd rescue target in newspeak) and used an ext4 filesystem to open/create 1,000,000 files. Then I looped through all of the files calling
fstat()on each of them 1000 times and calculated the meanfstat()time for a single file. (The test program can be found below.)For kernels v4.14-rc2-vanilla, v4.12.0-12-generic I tested the following cases:
0 mappings
1 mapping
2 mappings
3 mappings
5 mappingsFor kernel v4.4-rc2-userns+ I tested:
0 mappings
1 mapping
2 mappings
3 mappings
5 mappings
10 mappings
50 mappings
100 mappings
200 mappings
300 mappingsHere are the results:
- v4.14-rc2-vanilla (unmodified v4.14-rc2)
# no unshare: 312 ns unshare -U # write 0 mappings: 307 ns unshare -U # write 1 mappings: 328 ns unshare -U # write 2 mappings: 328 ns unshare -U # write 3 mappings: 328 ns unshare -U # write 5 mappings: 338 ns- v4.14-rc2-userns+ (v4.14-rc2 with my new user namespace idmap limits patch)
Quoted 11 lines
# no unshare: 158 ns unshare -U # write 0 mappings: 158 ns unshare -U # write 1 mappings: 164 ns unshare -U # write 2 mappings: 170 ns unshare -U # write 3 mappings: 175 ns unshare -U # write 5 mappings: 187 ns unshare -U # write 10 mappings: 218 ns unshare -U # write 50 mappings: 528 ns unshare -U # write 100 mappings: 980 ns unshare -U # write 200 mappings: 1880 ns unshare -U # write 300 mappings: 2760 ns
These numbers look very good.
Eric, does that address your performance concern?
(If so I'll take a closer look at the patch itself, I've not yet done so)
thanks,
-serge
Quoted 118 lines · Serge E. Hallyn
Quoted 105 lines · Christian Brauner
We have quite some use cases where users already run into the current limit for {g,u}id mappings. Consider a user requesting us to map everything but 999, and 1001 for a given range of 1000000000 with a sub{g,u}id layout of:
some-user:100000:1000000000
some-user:999:1
some-user:1000:1
some-user:1001:1
some-user:1002:1This translates to:
MAPPING-TYPE CONTAINER HOST RANGE
uid 999 999 1
uid 1001 1001 1uid 0 1000000 999 uid 1000 1001000 1 uid 1002 1001002 999998998gid 999 999 1
gid 1001 1001 1gid 0 1000000 999 gid 1000 1001000 1 gid 1002 1001002 999998998which is already the current limit.
Design Notes:
As discussed at LPC simply bumping the number of limits is not going to work since this would mean that structuid_gid_mapwon't fit into a single cache-line anymore thereby regressing performance for the base-cases. The same problem seems to arise when using a single pointer. So the idea is to keep the base cases (0-3 mappings) directly in structuid_gid_mapso they fit into a single cache-line of 64 byte. For the two removed mappings we place three pointers in the struct that mock the behavior of traditional filesystems:
1. a direct pointer to a structuid_gid_extentof 5 mappings of 60 bytes
2. an indirect pointer to an array of 64 byte of direct pointers to structuid_gid_extentof 5 mappings a 60 bytes each
3. a double indirect pointer to an array of 64 bytes of indirect pointers each to an array of 64 bytes of direct pointers (and so on)
Fixing a pointer size of 8 byte this gives us 3 + 5 + (8 * 5) + (8 * (8 * 5)) = 368 mappings which should really be enough. The idea of this approach is to always have each extent of idmaps (structuid_gid_extent) be 60 bytes (5 * (4 + 4 + 4) and thus 4 bytes smaller than the size of a single cache line. This should only see a (i.e. linear) performance impact caused by iterating through the idmappings in a for-loop. Note that the base cases shouldn't see any performance degradation which is the most important part.Performance Testing:
When Eric introduced the extent-based structuid_gid_mapapproach he measured the performanc impact of his idmap changes:Quoted 9 lines
My benchmark consisted of going to single user mode where nothing else was running. On an ext4 filesystem opening 1,000,000 files and looping through all of the files 1000 times and calling fstat on the individuals files. This was to ensure I was benchmarking stat times where the inodes were in the kernels cache, but the inode values were not in the processors cache. My results:
v3.4-rc1: ~= 156ns (unmodified v3.4-rc1 with user namespace support disabled) v3.4-rc1-userns-: ~= 155ns (v3.4-rc1 with my user namespace patches and user namespace support disabled)v3.4-rc1-userns+: ~= 164ns (v3.4-rc1 with my user namespace patches and user namespace support enabled)
I used an identical approach on my laptop. Here's a thorough description of what I did. I built three kernels and used an additional "control" kernel:
1. v4.14-rc2-vanilla (unmodified v4.14-rc2)
2. v4.14-rc2-userns+ (v4.14-rc2 with my new user namespace idmap limits patch)
3. v4.14-rc2-userns- (v4.14-rc2 without my new user namespace idmap limits patch)
4. v4.12.0-12-generic (v4.12.0-12 standard Ubuntu kernel)I booted into single user mode (systemd rescue target in newspeak) and used an ext4 filesystem to open/create 1,000,000 files. Then I looped through all of the files calling
fstat()on each of them 1000 times and calculated the meanfstat()time for a single file. (The test program can be found below.)For kernels v4.14-rc2-vanilla, v4.12.0-12-generic I tested the following cases:
0 mappings
1 mapping
2 mappings
3 mappings
5 mappingsFor kernel v4.4-rc2-userns+ I tested:
0 mappings
1 mapping
2 mappings
3 mappings
5 mappings
10 mappings
50 mappings
100 mappings
200 mappings
300 mappingsHere are the results:
- v4.14-rc2-vanilla (unmodified v4.14-rc2)
# no unshare: 312 ns unshare -U # write 0 mappings: 307 ns unshare -U # write 1 mappings: 328 ns unshare -U # write 2 mappings: 328 ns unshare -U # write 3 mappings: 328 ns unshare -U # write 5 mappings: 338 ns- v4.14-rc2-userns+ (v4.14-rc2 with my new user namespace idmap limits patch)
Quoted 11 lines
# no unshare: 158 ns unshare -U # write 0 mappings: 158 ns unshare -U # write 1 mappings: 164 ns unshare -U # write 2 mappings: 170 ns unshare -U # write 3 mappings: 175 ns unshare -U # write 5 mappings: 187 ns unshare -U # write 10 mappings: 218 ns unshare -U # write 50 mappings: 528 ns unshare -U # write 100 mappings: 980 ns unshare -U # write 200 mappings: 1880 ns unshare -U # write 300 mappings: 2760 ns
Quoted 5 lines
These numbers look very good.
Eric, does that address your performance concern?
(If so I'll take a closer look at the patch itself, I've not yet done so)
Apologies it has been a busy couple of days. I really meant to write my reply a while ago.
With the current strategy I have no concerns about the new implementation regressing performance in existing use cases.
I meant to mention that one way I got out variability when I was timing things was to turn off cpu power management. I don't know if that works on the newest cpus that can occassionally speed up.
I am concerned about an implementation that scales linearly with the number of mappings in the large. When it is know we should be able to scale at log(N) of the number of mappings.
So perhaps we want the implementation to look something like:
struct uid_gid_extent {
u32 first;
u32 lower_first;
u32 count;
};
struct uid_gid_map {
u32 nr_extents;
union {
extent[UID_GID_MAP_MAX_INLINE_EXTENTS];
struct {
struct uid_gid_extent *forward;
struct uid_gid_extent *reverse;
};
};
};So perhaps we want the implementation to be a union with nr_extents being the constant member and having two pointers one for a forward and one for the reverse mappings.
Then limiting ourselves to a single kmalloc (about 4K) of extents, and performing a binary search through them for the large case.
That will take twice the memory but it will be fast, and it will scale to about 341 extents with only only 8 or 9 comparisons/cache line hits.
Eric
Quoted 132 lines · Eric W. Biederman
Quoted 118 lines · Serge E. Hallyn
Quoted 105 lines · Christian Brauner
We have quite some use cases where users already run into the current limit for {g,u}id mappings. Consider a user requesting us to map everything but 999, and 1001 for a given range of 1000000000 with a sub{g,u}id layout of:
some-user:100000:1000000000
some-user:999:1
some-user:1000:1
some-user:1001:1
some-user:1002:1This translates to:
MAPPING-TYPE CONTAINER HOST RANGE
uid 999 999 1
uid 1001 1001 1uid 0 1000000 999 uid 1000 1001000 1 uid 1002 1001002 999998998gid 999 999 1
gid 1001 1001 1gid 0 1000000 999 gid 1000 1001000 1 gid 1002 1001002 999998998which is already the current limit.
Design Notes:
As discussed at LPC simply bumping the number of limits is not going to work since this would mean that structuid_gid_mapwon't fit into a single cache-line anymore thereby regressing performance for the base-cases. The same problem seems to arise when using a single pointer. So the idea is to keep the base cases (0-3 mappings) directly in structuid_gid_mapso they fit into a single cache-line of 64 byte. For the two removed mappings we place three pointers in the struct that mock the behavior of traditional filesystems:
1. a direct pointer to a structuid_gid_extentof 5 mappings of 60 bytes
2. an indirect pointer to an array of 64 byte of direct pointers to structuid_gid_extentof 5 mappings a 60 bytes each
3. a double indirect pointer to an array of 64 bytes of indirect pointers each to an array of 64 bytes of direct pointers (and so on)
Fixing a pointer size of 8 byte this gives us 3 + 5 + (8 * 5) + (8 * (8 * 5)) = 368 mappings which should really be enough. The idea of this approach is to always have each extent of idmaps (structuid_gid_extent) be 60 bytes (5 * (4 + 4 + 4) and thus 4 bytes smaller than the size of a single cache line. This should only see a (i.e. linear) performance impact caused by iterating through the idmappings in a for-loop. Note that the base cases shouldn't see any performance degradation which is the most important part.Performance Testing:
When Eric introduced the extent-based structuid_gid_mapapproach he measured the performanc impact of his idmap changes:Quoted 9 lines
My benchmark consisted of going to single user mode where nothing else was running. On an ext4 filesystem opening 1,000,000 files and looping through all of the files 1000 times and calling fstat on the individuals files. This was to ensure I was benchmarking stat times where the inodes were in the kernels cache, but the inode values were not in the processors cache. My results:
v3.4-rc1: ~= 156ns (unmodified v3.4-rc1 with user namespace support disabled) v3.4-rc1-userns-: ~= 155ns (v3.4-rc1 with my user namespace patches and user namespace support disabled)v3.4-rc1-userns+: ~= 164ns (v3.4-rc1 with my user namespace patches and user namespace support enabled)
I used an identical approach on my laptop. Here's a thorough description of what I did. I built three kernels and used an additional "control" kernel:
1. v4.14-rc2-vanilla (unmodified v4.14-rc2)
2. v4.14-rc2-userns+ (v4.14-rc2 with my new user namespace idmap limits patch)
3. v4.14-rc2-userns- (v4.14-rc2 without my new user namespace idmap limits patch)
4. v4.12.0-12-generic (v4.12.0-12 standard Ubuntu kernel)I booted into single user mode (systemd rescue target in newspeak) and used an ext4 filesystem to open/create 1,000,000 files. Then I looped through all of the files calling
fstat()on each of them 1000 times and calculated the meanfstat()time for a single file. (The test program can be found below.)For kernels v4.14-rc2-vanilla, v4.12.0-12-generic I tested the following cases:
0 mappings
1 mapping
2 mappings
3 mappings
5 mappingsFor kernel v4.4-rc2-userns+ I tested:
0 mappings
1 mapping
2 mappings
3 mappings
5 mappings
10 mappings
50 mappings
100 mappings
200 mappings
300 mappingsHere are the results:
- v4.14-rc2-vanilla (unmodified v4.14-rc2)
# no unshare: 312 ns unshare -U # write 0 mappings: 307 ns unshare -U # write 1 mappings: 328 ns unshare -U # write 2 mappings: 328 ns unshare -U # write 3 mappings: 328 ns unshare -U # write 5 mappings: 338 ns- v4.14-rc2-userns+ (v4.14-rc2 with my new user namespace idmap limits patch)
Quoted 11 lines
# no unshare: 158 ns unshare -U # write 0 mappings: 158 ns unshare -U # write 1 mappings: 164 ns unshare -U # write 2 mappings: 170 ns unshare -U # write 3 mappings: 175 ns unshare -U # write 5 mappings: 187 ns unshare -U # write 10 mappings: 218 ns unshare -U # write 50 mappings: 528 ns unshare -U # write 100 mappings: 980 ns unshare -U # write 200 mappings: 1880 ns unshare -U # write 300 mappings: 2760 nsQuoted 5 lines
These numbers look very good.
Eric, does that address your performance concern?
(If so I'll take a closer look at the patch itself, I've not yet done so)
Apologies it has been a busy couple of days. I really meant to write my reply a while ago.
With the current strategy I have no concerns about the new implementation regressing performance in existing use cases.
Quoted 7 lines
I meant to mention that one way I got out variability when I was timing things was to turn off cpu power management. I don't know if that works on the newest cpus that can occassionally speed up.
I am concerned about an implementation that scales linearly with the number of mappings in the large. When it is know we should be able to scale at log(N) of the number of mappings.
Oh, I was under the impression that what you cared about was slowing down the base cases. Thanks!
Quoted 19 lines
So perhaps we want the implementation to look something like:
structuid_gid_extent{u32 first; u32 lower_first; u32 count; }; struct uid_gid_map { u32 nr_extents; union { extent[UID_GID_MAP_MAX_INLINE_EXTENTS]; struct { struct uid_gid_extent *forward; struct uid_gid_extent *reverse; }; }; };
Quoted 3 lines
So perhaps we want the implementation to be a union with
nr_extentsbeing the constant member and having two pointers one for a forward and one for the reverse mappings.
That looks reasonable. I'll code that up soon.
Just to clarify, what do you mean by forward mappings and reverse mappings? Are you just saying that struct uid_gid_extent *forward points to the beginning of the sorted 4k kmalloc()ed extent array and struct uid_gid_extent *reverse points to the end of the sorted 4k kmalloc()ed extent array for the binary search? Not that I'm missing some intricacy here that might be crucial.
Christian
Quoted 137 lines · Christian Brauner
Quoted 136 lines · Eric W. Biederman
Quoted 118 lines · Serge E. Hallyn
Quoted 105 lines · Christian Brauner
We have quite some use cases where users already run into the current limit for {g,u}id mappings. Consider a user requesting us to map everything but 999, and 1001 for a given range of 1000000000 with a sub{g,u}id layout of:
some-user:100000:1000000000
some-user:999:1
some-user:1000:1
some-user:1001:1
some-user:1002:1This translates to:
MAPPING-TYPE CONTAINER HOST RANGE
uid 999 999 1
uid 1001 1001 1uid 0 1000000 999 uid 1000 1001000 1 uid 1002 1001002 999998998gid 999 999 1
gid 1001 1001 1gid 0 1000000 999 gid 1000 1001000 1 gid 1002 1001002 999998998which is already the current limit.
Design Notes:
As discussed at LPC simply bumping the number of limits is not going to work since this would mean that structuid_gid_mapwon't fit into a single cache-line anymore thereby regressing performance for the base-cases. The same problem seems to arise when using a single pointer. So the idea is to keep the base cases (0-3 mappings) directly in structuid_gid_mapso they fit into a single cache-line of 64 byte. For the two removed mappings we place three pointers in the struct that mock the behavior of traditional filesystems:
1. a direct pointer to a structuid_gid_extentof 5 mappings of 60 bytes
2. an indirect pointer to an array of 64 byte of direct pointers to structuid_gid_extentof 5 mappings a 60 bytes each
3. a double indirect pointer to an array of 64 bytes of indirect pointers each to an array of 64 bytes of direct pointers (and so on)
Fixing a pointer size of 8 byte this gives us 3 + 5 + (8 * 5) + (8 * (8 * 5)) = 368 mappings which should really be enough. The idea of this approach is to always have each extent of idmaps (structuid_gid_extent) be 60 bytes (5 * (4 + 4 + 4) and thus 4 bytes smaller than the size of a single cache line. This should only see a (i.e. linear) performance impact caused by iterating through the idmappings in a for-loop. Note that the base cases shouldn't see any performance degradation which is the most important part.Performance Testing:
When Eric introduced the extent-based structuid_gid_mapapproach he measured the performanc impact of his idmap changes:Quoted 9 lines
My benchmark consisted of going to single user mode where nothing else was running. On an ext4 filesystem opening 1,000,000 files and looping through all of the files 1000 times and calling fstat on the individuals files. This was to ensure I was benchmarking stat times where the inodes were in the kernels cache, but the inode values were not in the processors cache. My results:
v3.4-rc1: ~= 156ns (unmodified v3.4-rc1 with user namespace support disabled) v3.4-rc1-userns-: ~= 155ns (v3.4-rc1 with my user namespace patches and user namespace support disabled)v3.4-rc1-userns+: ~= 164ns (v3.4-rc1 with my user namespace patches and user namespace support enabled)
I used an identical approach on my laptop. Here's a thorough description of what I did. I built three kernels and used an additional "control" kernel:
1. v4.14-rc2-vanilla (unmodified v4.14-rc2)
2. v4.14-rc2-userns+ (v4.14-rc2 with my new user namespace idmap limits patch)
3. v4.14-rc2-userns- (v4.14-rc2 without my new user namespace idmap limits patch)
4. v4.12.0-12-generic (v4.12.0-12 standard Ubuntu kernel)I booted into single user mode (systemd rescue target in newspeak) and used an ext4 filesystem to open/create 1,000,000 files. Then I looped through all of the files calling
fstat()on each of them 1000 times and calculated the meanfstat()time for a single file. (The test program can be found below.)For kernels v4.14-rc2-vanilla, v4.12.0-12-generic I tested the following cases:
0 mappings
1 mapping
2 mappings
3 mappings
5 mappingsFor kernel v4.4-rc2-userns+ I tested:
0 mappings
1 mapping
2 mappings
3 mappings
5 mappings
10 mappings
50 mappings
100 mappings
200 mappings
300 mappingsHere are the results:
- v4.14-rc2-vanilla (unmodified v4.14-rc2)
# no unshare: 312 ns unshare -U # write 0 mappings: 307 ns unshare -U # write 1 mappings: 328 ns unshare -U # write 2 mappings: 328 ns unshare -U # write 3 mappings: 328 ns unshare -U # write 5 mappings: 338 ns- v4.14-rc2-userns+ (v4.14-rc2 with my new user namespace idmap limits patch)
Quoted 11 lines
# no unshare: 158 ns unshare -U # write 0 mappings: 158 ns unshare -U # write 1 mappings: 164 ns unshare -U # write 2 mappings: 170 ns unshare -U # write 3 mappings: 175 ns unshare -U # write 5 mappings: 187 ns unshare -U # write 10 mappings: 218 ns unshare -U # write 50 mappings: 528 ns unshare -U # write 100 mappings: 980 ns unshare -U # write 200 mappings: 1880 ns unshare -U # write 300 mappings: 2760 nsQuoted 5 lines
These numbers look very good.
Eric, does that address your performance concern?
(If so I'll take a closer look at the patch itself, I've not yet done so)
Apologies it has been a busy couple of days. I really meant to write my reply a while ago.
With the current strategy I have no concerns about the new implementation regressing performance in existing use cases.
I meant to mention that one way I got out variability when I was timing things was to turn off cpu power management. I don't know if that works on the newest cpus that can occassionally speed up.
Quoted 6 lines
Quoted 3 lines
I am concerned about an implementation that scales linearly with the number of mappings in the large. When it is know we should be able to scale at log(N) of the number of mappings.
Oh, I was under the impression that what you cared about was slowing down the base cases. Thanks!
I care very much about the base case because if that slows down we have a performance regression that must be fixed. At the same time it doesn't makes sense to extend the data structure and with something that has poor performance when used heavily. We will just have to fix that later.
Quoted 22 lines
Quoted 18 lines
So perhaps we want the implementation to look something like:
structuid_gid_extent{u32 first; u32 lower_first; u32 count; }; struct uid_gid_map { u32 nr_extents; union { extent[UID_GID_MAP_MAX_INLINE_EXTENTS]; struct { struct uid_gid_extent *forward; struct uid_gid_extent *reverse; }; }; };Quoted 3 lines
So perhaps we want the implementation to be a union with
nr_extentsbeing the constant member and having two pointers one for a forward and one for the reverse mappings.
Quoted 6 lines
That looks reasonable. I'll code that up soon.
Just to clarify, what do you mean by forward mappings and reverse mappings? Are you just saying that structuid_gid_extent*forward points to the beginning of the sorted 4k kmalloc()ed extent array and structuid_gid_extent*reverse points to the end of the sorted 4k kmalloc()ed extent array for the binary search? Not that I'm missing some intricacy here that might be crucial.
No. Intricacies missed. The sort for forward and reverse is different so we need two different arrays. One sorted on first the other sorted on lower_first. If the data was larger we could put in a pointer to their common elements but as only a u32 is different a pointer to a common element would be a waste of space.
Eric