starnix_core/task/limits.rs
1// Copyright 2025 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::mm::PAGE_SIZE;
6use starnix_sync::{IcmpPingGidsLock, LockDepMutex};
7use std::ops::Range;
8use std::sync::atomic::{AtomicI32, AtomicUsize};
9
10/// Corresponds to files in /proc/sys/fs/inotify/, but cannot be negative.
11#[derive(Debug)]
12pub struct InotifyLimits {
13 // This value is used when creating an inotify instance.
14 // Updating this value does not affect already-created inotify instances.
15 pub max_queued_events: AtomicI32,
16
17 // TODO(b/297439734): Make this a real user limit on inotify instances.
18 pub max_user_instances: AtomicI32,
19
20 // TODO(b/297439734): Make this a real user limit on inotify watches.
21 pub max_user_watches: AtomicI32,
22}
23
24#[derive(Default, Debug)]
25pub struct SocketLimits {
26 /// The maximum backlog size for a socket.
27 pub max_connections: AtomicI32,
28
29 // Range of GIDs that can create ICMP Ping sockets.
30 pub icmp_ping_gids: LockDepMutex<Range<u32>, IcmpPingGidsLock>,
31}
32
33#[derive(Debug)]
34pub struct SystemLimits {
35 /// Limits applied to inotify objects.
36 pub inotify: InotifyLimits,
37
38 /// Limits applied to socket objects.
39 pub socket: SocketLimits,
40
41 /// The maximum size of pipes in the system.
42 pub pipe_max_size: AtomicUsize,
43
44 /// Whether IoUring is disabled.
45 ///
46 /// 0 -> io_uring is enabled (default)
47 /// 1 -> io_uring is enabled for processes in the io_uring_group
48 /// 2 -> io_uring is disabled
49 ///
50 /// See https://docs.kernel.org/admin-guide/sysctl/kernel.html#io-uring-disabled
51 pub io_uring_disabled: AtomicI32,
52
53 /// If io_uring_disabled is 1, then io_uring is enabled only for processes with CAP_SYS_ADMIN
54 /// or that are members of this group.
55 ///
56 /// See https://docs.kernel.org/admin-guide/sysctl/kernel.html#io-uring-group
57 pub io_uring_group: AtomicI32,
58}
59
60impl Default for SystemLimits {
61 fn default() -> SystemLimits {
62 SystemLimits {
63 inotify: InotifyLimits {
64 max_queued_events: AtomicI32::new(16384),
65 max_user_instances: AtomicI32::new(128),
66 max_user_watches: AtomicI32::new(1048576),
67 },
68 socket: SocketLimits {
69 max_connections: AtomicI32::new(4096),
70 icmp_ping_gids: LockDepMutex::new(1..1),
71 },
72 pipe_max_size: AtomicUsize::new((*PAGE_SIZE * 256) as usize),
73 io_uring_disabled: AtomicI32::new(0),
74 io_uring_group: AtomicI32::new(-1),
75 }
76 }
77}