drivers/misc/lkdtm/refcount.c
Source file repositories/reference/linux-study-clean/drivers/misc/lkdtm/refcount.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/misc/lkdtm/refcount.c- Extension
.c- Size
- 11404 bytes
- Lines
- 436
- Domain
- Driver Families
- Bucket
- drivers/misc
- Inferred role
- Driver Families: implementation source
- Status
- source implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
lkdtm.hlinux/refcount.h
Detected Declarations
function bugsfunction refcount_incfunction lkdtm_REFCOUNT_ADD_OVERFLOWfunction lkdtm_REFCOUNT_INC_NOT_ZERO_OVERFLOWfunction lkdtm_REFCOUNT_ADD_NOT_ZERO_OVERFLOWfunction check_zerofunction refcount_decfunction check_negativefunction lkdtm_REFCOUNT_DEC_NEGATIVEfunction refcount_dec_and_testfunction refcount_sub_and_testfunction refcount_sub_and_testfunction check_from_zerofunction refcount_incfunction refcount_addfunction check_saturatedfunction refcount_incfunction lkdtm_REFCOUNT_DEC_SATURATEDfunction lkdtm_REFCOUNT_ADD_SATURATEDfunction lkdtm_REFCOUNT_INC_NOT_ZERO_SATURATEDfunction lkdtm_REFCOUNT_ADD_NOT_ZERO_SATURATEDfunction lkdtm_REFCOUNT_DEC_AND_TEST_SATURATEDfunction lkdtm_REFCOUNT_SUB_AND_TEST_SATURATEDfunction lkdtm_ATOMIC_TIMINGfunction lkdtm_REFCOUNT_TIMING
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
/*
* This is for all the tests related to refcount bugs (e.g. overflow,
* underflow, reaching zero untested, etc).
*/
#include "lkdtm.h"
#include <linux/refcount.h>
static void overflow_check(refcount_t *ref)
{
switch (refcount_read(ref)) {
case REFCOUNT_SATURATED:
pr_info("Overflow detected: saturated\n");
break;
case REFCOUNT_MAX:
pr_warn("Overflow detected: unsafely reset to max\n");
break;
default:
pr_err("Fail: refcount wrapped to %d\n", refcount_read(ref));
}
}
/*
* A refcount_inc() above the maximum value of the refcount implementation,
* should at least saturate, and at most also WARN.
*/
static void lkdtm_REFCOUNT_INC_OVERFLOW(void)
{
refcount_t over = REFCOUNT_INIT(REFCOUNT_MAX - 1);
pr_info("attempting good refcount_inc() without overflow\n");
refcount_dec(&over);
refcount_inc(&over);
pr_info("attempting bad refcount_inc() overflow\n");
refcount_inc(&over);
refcount_inc(&over);
overflow_check(&over);
}
/* refcount_add() should behave just like refcount_inc() above. */
static void lkdtm_REFCOUNT_ADD_OVERFLOW(void)
{
refcount_t over = REFCOUNT_INIT(REFCOUNT_MAX - 1);
pr_info("attempting good refcount_add() without overflow\n");
refcount_dec(&over);
refcount_dec(&over);
refcount_dec(&over);
refcount_dec(&over);
refcount_add(4, &over);
pr_info("attempting bad refcount_add() overflow\n");
refcount_add(4, &over);
overflow_check(&over);
}
/* refcount_inc_not_zero() should behave just like refcount_inc() above. */
static void lkdtm_REFCOUNT_INC_NOT_ZERO_OVERFLOW(void)
{
refcount_t over = REFCOUNT_INIT(REFCOUNT_MAX);
pr_info("attempting bad refcount_inc_not_zero() overflow\n");
if (!refcount_inc_not_zero(&over))
pr_warn("Weird: refcount_inc_not_zero() reported zero\n");
overflow_check(&over);
}
/* refcount_add_not_zero() should behave just like refcount_inc() above. */
static void lkdtm_REFCOUNT_ADD_NOT_ZERO_OVERFLOW(void)
{
refcount_t over = REFCOUNT_INIT(REFCOUNT_MAX);
pr_info("attempting bad refcount_add_not_zero() overflow\n");
if (!refcount_add_not_zero(6, &over))
pr_warn("Weird: refcount_add_not_zero() reported zero\n");
overflow_check(&over);
}
static void check_zero(refcount_t *ref)
{
switch (refcount_read(ref)) {
case REFCOUNT_SATURATED:
pr_info("Zero detected: saturated\n");
break;
case REFCOUNT_MAX:
Annotation
- Immediate include surface: `lkdtm.h`, `linux/refcount.h`.
- Detected declarations: `function bugs`, `function refcount_inc`, `function lkdtm_REFCOUNT_ADD_OVERFLOW`, `function lkdtm_REFCOUNT_INC_NOT_ZERO_OVERFLOW`, `function lkdtm_REFCOUNT_ADD_NOT_ZERO_OVERFLOW`, `function check_zero`, `function refcount_dec`, `function check_negative`, `function lkdtm_REFCOUNT_DEC_NEGATIVE`, `function refcount_dec_and_test`.
- Atlas domain: Driver Families / drivers/misc.
- Implementation status: source implementation candidate.
- 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.