net/core/sock.c
Source file repositories/reference/linux-study-clean/net/core/sock.c
File Facts
- System
- Linux kernel
- Corpus path
net/core/sock.c- Extension
.c- Size
- 115318 bytes
- Lines
- 4613
- Domain
- Networking Core
- Bucket
- Sockets, Protocols, Packet Path, And Network Policy
- Inferred role
- Networking Core: operation-table or driver-model contract
- Status
- pattern implementation candidate
Why This File Exists
Networking stack implementation surface: socket APIs, protocol dispatch, packet flow, routing, filtering, and network namespaces.
- Networking stack implementation surface: socket APIs, protocol dispatch, packet flow, routing, filtering, and network namespaces.
- Defines an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- 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.
- Touches IRQ or DMA behavior; this matters for the representative real-device path.
- 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/unaligned.hlinux/capability.hlinux/errno.hlinux/errqueue.hlinux/types.hlinux/socket.hlinux/in.hlinux/kernel.hlinux/module.hlinux/proc_fs.hlinux/seq_file.hlinux/sched.hlinux/sched/mm.hlinux/timer.hlinux/string.hlinux/sockios.hlinux/net.hlinux/mm.hlinux/slab.hlinux/interrupt.hlinux/poll.hlinux/tcp.hlinux/udp.hlinux/init.hlinux/highmem.hlinux/user_namespace.hlinux/static_key.hlinux/memcontrol.hlinux/prefetch.hlinux/compat.hlinux/mroute.hlinux/mroute6.h
Detected Declarations
function sk_ns_capablefunction sk_capablefunction sk_net_capablefunction sk_set_memallocfunction sk_clear_memallocfunction __sk_backlog_rcvfunction sk_error_reportfunction sock_get_timeoutfunction sock_copy_user_timevalfunction sock_set_timeoutfunction sk_set_prio_allowedfunction sock_needs_netstampfunction sock_disable_timestampfunction __sock_queue_rcv_skbfunction sock_queue_rcv_skb_reasonfunction __sk_receive_skbfunction sock_bindtoindex_lockedfunction sock_bindtoindexfunction sock_setbindtodevicefunction sock_getbindtodevicefunction sk_mc_loopfunction sock_set_reuseaddrfunction sock_set_reuseportfunction sock_no_lingerfunction sock_set_priorityfunction sock_set_sndtimeofunction __sock_set_timestampsfunction sock_set_timestampfunction sock_timestamping_bind_phcfunction sock_set_timestampingfunction bpf_skops_tx_timestampingfunction sock_set_keepalivefunction __sock_set_rcvbuffunction sock_set_rcvbuffunction __sock_set_markfunction sock_set_markfunction sock_release_reserved_memoryfunction sock_reserve_memoryfunction sock_devmem_dontneedfunction sockopt_lock_sockfunction sockopt_release_sockfunction sockopt_ns_capablefunction sockopt_capablefunction sockopt_validate_clockidfunction sk_setsockoptfunction sock_setsockoptfunction cred_to_ucredfunction groups_to_user
Annotated Snippet
const struct proto_ops *ops = READ_ONCE(sock->ops);
if (ops->set_rcvbuf)
ops->set_rcvbuf(sk, sk->sk_rcvbuf);
}
}
void sock_set_rcvbuf(struct sock *sk, int val)
{
lock_sock(sk);
__sock_set_rcvbuf(sk, val);
release_sock(sk);
}
EXPORT_SYMBOL(sock_set_rcvbuf);
static void __sock_set_mark(struct sock *sk, u32 val)
{
if (val != sk->sk_mark) {
WRITE_ONCE(sk->sk_mark, val);
sk_dst_reset(sk);
}
}
void sock_set_mark(struct sock *sk, u32 val)
{
lock_sock(sk);
__sock_set_mark(sk, val);
release_sock(sk);
}
EXPORT_SYMBOL(sock_set_mark);
static void sock_release_reserved_memory(struct sock *sk, int bytes)
{
/* Round down bytes to multiple of pages */
bytes = round_down(bytes, PAGE_SIZE);
WARN_ON(bytes > sk->sk_reserved_mem);
WRITE_ONCE(sk->sk_reserved_mem, sk->sk_reserved_mem - bytes);
sk_mem_reclaim(sk);
}
static int sock_reserve_memory(struct sock *sk, int bytes)
{
long allocated;
bool charged;
int pages;
if (!mem_cgroup_sk_enabled(sk) || !sk_has_account(sk))
return -EOPNOTSUPP;
if (!bytes)
return 0;
pages = sk_mem_pages(bytes);
/* pre-charge to memcg */
charged = mem_cgroup_sk_charge(sk, pages,
GFP_KERNEL | __GFP_RETRY_MAYFAIL);
if (!charged)
return -ENOMEM;
if (sk->sk_bypass_prot_mem)
goto success;
/* pre-charge to forward_alloc */
sk_memory_allocated_add(sk, pages);
allocated = sk_memory_allocated(sk);
/* If the system goes into memory pressure with this
* precharge, give up and return error.
*/
if (allocated > sk_prot_mem_limits(sk, 1)) {
sk_memory_allocated_sub(sk, pages);
mem_cgroup_sk_uncharge(sk, pages);
return -ENOMEM;
}
success:
sk_forward_alloc_add(sk, pages << PAGE_SHIFT);
WRITE_ONCE(sk->sk_reserved_mem,
sk->sk_reserved_mem + (pages << PAGE_SHIFT));
return 0;
}
#ifdef CONFIG_PAGE_POOL
/* This is the number of tokens and frags that the user can SO_DEVMEM_DONTNEED
* in 1 syscall. The limit exists to limit the amount of memory the kernel
Annotation
- Immediate include surface: `linux/unaligned.h`, `linux/capability.h`, `linux/errno.h`, `linux/errqueue.h`, `linux/types.h`, `linux/socket.h`, `linux/in.h`, `linux/kernel.h`.
- Detected declarations: `function sk_ns_capable`, `function sk_capable`, `function sk_net_capable`, `function sk_set_memalloc`, `function sk_clear_memalloc`, `function __sk_backlog_rcv`, `function sk_error_report`, `function sock_get_timeout`, `function sock_copy_user_timeval`, `function sock_set_timeout`.
- Atlas domain: Networking Core / Sockets, Protocols, Packet Path, And Network Policy.
- Implementation status: pattern 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.
- IRQ or DMA behavior appears here, which is relevant to the selected PCIe/NVMe device path.
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.