ipc/shm.c
Source file repositories/reference/linux-study-clean/ipc/shm.c
File Facts
- System
- Linux kernel
- Corpus path
ipc/shm.c- Extension
.c- Size
- 46245 bytes
- Lines
- 1883
- Domain
- Core OS
- Bucket
- IPC
- Inferred role
- Core OS: syscall or user/kernel boundary
- Status
- core implementation candidate
Why This File Exists
Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Defines or participates in a user/kernel boundary; inspect argument validation, copy_from_user/copy_to_user, credentials, and dispatch target.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/slab.hlinux/mm.hlinux/hugetlb.hlinux/shm.huapi/linux/shm.hlinux/init.hlinux/file.hlinux/mman.hlinux/shmem_fs.hlinux/security.hlinux/syscalls.hlinux/audit.hlinux/capability.hlinux/ptrace.hlinux/seq_file.hlinux/rwsem.hlinux/nsproxy.hlinux/mount.hlinux/ipc_namespace.hlinux/rhashtable.hlinux/nstree.hlinux/uaccess.hutil.h
Detected Declarations
syscall shmgetsyscall shmctlsyscall old_shmctlsyscall shmatsyscall shmdtstruct shm_file_datastruct compat_shmid_dsstruct compat_shminfo64struct compat_shm_infofunction shm_init_nsfunction do_shm_rmidfunction shm_exit_nsfunction ipc_ns_initfunction shm_initfunction shm_lock_function ipc_rmidfunction shm_lock_by_ptrfunction shm_rcu_freefunction ipc_rmidfunction list_del_initfunction shm_rmidfunction __shm_openfunction shm_openfunction shm_destroyfunction shm_may_destroyfunction kfreefunction shm_closefunction shm_try_destroy_orphanedfunction shm_destroy_orphanedfunction exit_shmfunction shm_faultfunction shm_may_splitfunction shm_pagesizefunction shm_set_policyfunction shm_mmapfunction shm_releasefunction shm_fsyncfunction shm_fallocatefunction shm_get_unmapped_areafunction newsegfunction shm_more_checksfunction ksys_shmgetfunction copy_shmid_to_userfunction copy_shmid_from_userfunction copy_shminfo_to_userfunction shm_add_rss_swapfunction shm_get_statfunction shmctl_down
Annotated Snippet
SYSCALL_DEFINE3(shmget, key_t, key, size_t, size, int, shmflg)
{
return ksys_shmget(key, size, shmflg);
}
static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
{
switch (version) {
case IPC_64:
return copy_to_user(buf, in, sizeof(*in));
case IPC_OLD:
{
struct shmid_ds out;
memset(&out, 0, sizeof(out));
ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
out.shm_segsz = in->shm_segsz;
out.shm_atime = in->shm_atime;
out.shm_dtime = in->shm_dtime;
out.shm_ctime = in->shm_ctime;
out.shm_cpid = in->shm_cpid;
out.shm_lpid = in->shm_lpid;
out.shm_nattch = in->shm_nattch;
return copy_to_user(buf, &out, sizeof(out));
}
default:
return -EINVAL;
}
}
static inline unsigned long
copy_shmid_from_user(struct shmid64_ds *out, void __user *buf, int version)
{
switch (version) {
case IPC_64:
if (copy_from_user(out, buf, sizeof(*out)))
return -EFAULT;
return 0;
case IPC_OLD:
{
struct shmid_ds tbuf_old;
if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
return -EFAULT;
out->shm_perm.uid = tbuf_old.shm_perm.uid;
out->shm_perm.gid = tbuf_old.shm_perm.gid;
out->shm_perm.mode = tbuf_old.shm_perm.mode;
return 0;
}
default:
return -EINVAL;
}
}
static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
{
switch (version) {
case IPC_64:
return copy_to_user(buf, in, sizeof(*in));
case IPC_OLD:
{
struct shminfo out;
if (in->shmmax > INT_MAX)
out.shmmax = INT_MAX;
else
out.shmmax = (int)in->shmmax;
out.shmmin = in->shmmin;
out.shmmni = in->shmmni;
out.shmseg = in->shmseg;
out.shmall = in->shmall;
return copy_to_user(buf, &out, sizeof(out));
}
default:
return -EINVAL;
}
}
/*
* Calculate and add used RSS and swap pages of a shm.
* Called with shm_ids.rwsem held as a reader
*/
static void shm_add_rss_swap(struct shmid_kernel *shp,
unsigned long *rss_add, unsigned long *swp_add)
{
Annotation
- Immediate include surface: `linux/slab.h`, `linux/mm.h`, `linux/hugetlb.h`, `linux/shm.h`, `uapi/linux/shm.h`, `linux/init.h`, `linux/file.h`, `linux/mman.h`.
- Detected declarations: `syscall shmget`, `syscall shmctl`, `syscall old_shmctl`, `syscall shmat`, `syscall shmdt`, `struct shm_file_data`, `struct compat_shmid_ds`, `struct compat_shminfo64`, `struct compat_shm_info`, `function shm_init_ns`.
- Atlas domain: Core OS / IPC.
- Implementation status: core implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.