Skip to content

Commit 1a602bd

Browse files
authored
Merge pull request #1 from odeliyach/claude/update-repository-structure
Refactor kernel module with professional documentation and academic integrity protections
2 parents 8cec05e + ae209e4 commit 1a602bd

16 files changed

Lines changed: 1526 additions & 690 deletions

.github/copilot-instructions.md

Lines changed: 142 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# GitHub Copilot Instructions
1+
# GitHub Copilot Instructions - Kernel Development Focus
22

33
## Writing Style
44

@@ -9,27 +9,64 @@ Write naturally and conversationally, like a human developer would.
99
- Avoid AI-like phrasing and corporate jargon
1010
- Write concisely without unnecessary elaboration
1111

12-
## Code Comments
12+
## Code Comments for Kernel Development
1313

1414
Always add comments to non-trivial code. Focus on explaining WHY something is done, not just WHAT it does.
1515

1616
Highlight:
1717
- Tradeoffs made in the implementation
1818
- Complexity considerations
1919
- Design decisions and rationale
20+
- Kernel-specific constraints (memory allocation, user-space access, locking)
21+
22+
## Kernel Coding Standards
23+
24+
Follow Linux kernel coding style consistently:
25+
26+
- **Use kernel logging**: `printk(KERN_ERR ...)` instead of printf
27+
- **Memory allocation**: Always use `kmalloc(GFP_KERNEL)` and check return values
28+
- **User-space access**: Always use `get_user`/`put_user` or `copy_from_user`/`copy_to_user`
29+
- **Error codes**: Return standard errno values (EINVAL, ENOMEM, EFAULT, etc.)
30+
- **No floating point**: Kernel space doesn't support floating point operations
31+
- **Header guards**: Use `#ifndef MODULE_H` / `#define MODULE_H` / `#endif`
2032

2133
## Style Enforcement
2234

2335
Follow these coding standards consistently:
2436

2537
- **No Magic Numbers**: Always use named constants instead of hardcoded values
26-
- Bad: `if (retry < 3)`
27-
- Good: `const MAX_RETRIES = 3; if (retry < MAX_RETRIES)`
28-
- **Consistent Naming**: Follow the project's naming conventions (check existing code)
38+
- Bad: `if (len > 128)`
39+
- Good: `#define MAX_MESSAGE_LEN 128; if (len > MAX_MESSAGE_LEN)`
40+
- **Consistent Naming**: Follow kernel conventions (lowercase with underscores)
41+
- Types: `struct_name_t` or `struct_name`
42+
- Functions: `module_function_name()`
43+
- Constants: `UPPERCASE_WITH_UNDERSCORES`
2944
- **Error Handling**: Always handle errors explicitly, never silently ignore them
45+
- Check all `kmalloc` returns
46+
- Check all user-space copy operations
47+
- Return appropriate errno codes
3048
- **DRY Principle**: Don't repeat yourself. Extract common patterns into reusable functions
3149
- **Single Responsibility**: Each function should do one thing well
3250

51+
## Doxygen Documentation
52+
53+
For all functions in kernel modules, include Doxygen comments:
54+
55+
```c
56+
/**
57+
* @brief Brief description of function
58+
*
59+
* Detailed description explaining what the function does,
60+
* any important implementation details, and why design
61+
* decisions were made.
62+
*
63+
* @param param1 Description of first parameter
64+
* @param param2 Description of second parameter
65+
* @return Description of return value
66+
* @retval VALUE Description of specific return value (e.g., -EINVAL for invalid input)
67+
*/
68+
```
69+
3370
## Interview Prep Mode
3471

3572
After implementing or modifying significant code:
@@ -41,15 +78,17 @@ After implementing or modifying significant code:
4178
- Edge cases handled
4279
- Scaling considerations
4380
- Alternative approaches considered
81+
- Security considerations (especially for kernel code)
4482

4583
Example prompt:
4684
```
47-
This implementation uses [approach]. I've added it to the code.
85+
This implementation uses [approach] for [feature]. I've added it to the code.
4886
4987
Would you like me to add an entry to INTERVIEW_NOTES.md covering:
5088
- Why we chose [approach] over [alternative]
5189
- Time complexity: O(n)
5290
- Trade-off: [benefit] vs [cost]
91+
- Security implications: [consideration]
5392
```
5493

5594
## Code Quality Standards
@@ -59,21 +98,93 @@ Prefer patterns that are:
5998
- **Maintainable**: Easy for others (or future you) to understand and modify
6099
- **Testable**: Written in a way that makes testing straightforward
61100
- **Clear**: Self-documenting code over clever one-liners
101+
- **Safe**: Especially for kernel code - validate all inputs, check all returns
62102

63103
Avoid:
64104
- Clever tricks that sacrifice readability
65105
- Premature optimization
66106
- Over-engineering simple problems
67107
- Leaving TODO comments without creating issues
108+
- Unsafe user-space memory access
109+
110+
## Kernel-Specific Guidelines
111+
112+
### Memory Management
113+
- Always check `kmalloc` return values before using the pointer
114+
- Use `GFP_KERNEL` for most allocations (can sleep)
115+
- Use `GFP_ATOMIC` for allocations in interrupt context (cannot sleep)
116+
- Always free allocated memory in cleanup/exit functions
117+
- Avoid memory leaks - track all allocations
118+
119+
### User-Space Interaction
120+
- Never directly dereference user-space pointers
121+
- Always use `get_user`, `put_user`, `copy_from_user`, or `copy_to_user`
122+
- Check return values of user-space copy operations
123+
- Return `-EFAULT` if copy operations fail
124+
125+
### Concurrency and Locking
126+
- Document whether locking is needed and why
127+
- If locks are needed:
128+
- Use spinlocks for short critical sections
129+
- Use mutexes for longer critical sections
130+
- Use `spin_lock_irqsave` to disable interrupts
131+
- Document the locking strategy in comments
132+
133+
### Error Handling
134+
- Return negative errno values on error (e.g., `-EINVAL`, `-ENOMEM`)
135+
- Return 0 or positive values on success
136+
- For read/write: return number of bytes transferred or negative errno
137+
- Always clean up partially allocated resources on error paths
138+
139+
### Module Structure
140+
```c
141+
// Module metadata
142+
MODULE_LICENSE("GPL");
143+
MODULE_AUTHOR("Author Name");
144+
MODULE_DESCRIPTION("Brief description");
145+
146+
// Module initialization
147+
static int __init module_init_function(void) {
148+
// Initialize resources
149+
// Return 0 on success, negative errno on failure
150+
}
151+
152+
// Module cleanup
153+
static void __exit module_exit_function(void) {
154+
// Free all resources
155+
// Clean up all state
156+
}
157+
158+
module_init(module_init_function);
159+
module_exit(module_exit_function);
160+
```
68161
69-
## Function Documentation
70-
71-
For public functions, always include:
72-
- Brief description of what it does
73-
- Parameters and their types/constraints
74-
- Return value and type
75-
- Any side effects or exceptions
76-
- Example usage (for complex functions)
162+
## Function Documentation Example
163+
164+
```c
165+
/**
166+
* @brief Write a message to the current channel
167+
*
168+
* Writes a message to the channel associated with this file descriptor.
169+
* If censorship is enabled, every 4th character is replaced with '#'.
170+
* The write operation is atomic - either the entire message is written
171+
* or none of it is.
172+
*
173+
* @param file Pointer to the file structure
174+
* @param buffer User-space buffer containing the message
175+
* @param len Length of the message
176+
* @param offset File offset (unused)
177+
* @return Number of bytes written on success, negative error code on failure
178+
* @retval -EINVAL No channel has been set on the file descriptor
179+
* @retval -EMSGSIZE Message length is 0 or exceeds MAX_MESSAGE_LEN
180+
* @retval -EFAULT Failed to copy data from user space
181+
* @retval -ENOMEM Memory allocation failed
182+
*/
183+
static ssize_t device_write(struct file *file, const char __user *buffer,
184+
size_t len, loff_t *offset) {
185+
// Implementation
186+
}
187+
```
77188

78189
## Interview-Focused Development
79190

@@ -84,13 +195,15 @@ When code is added or modified, automatically:
84195
- What alternatives were considered
85196
- Key tradeoffs and limitations
86197
- Performance or scalability implications
198+
- Security considerations (especially for kernel code)
87199

88200
2. Suggest updating INTERVIEW_NOTES.md with:
89201
- Key decisions made
90202
- Tradeoffs considered
91203
- Complexity analysis
92204
- Potential improvements
93205
- Realistic interview questions with strong senior-level answers
206+
- Kernel-specific topics (memory management, concurrency, security)
94207

95208
## General Guidelines
96209

@@ -100,3 +213,18 @@ When code is added or modified, automatically:
100213
- Document assumptions and constraints
101214
- Write code that you would be proud to discuss in an interview
102215
- When suggesting code changes, explain the "why" behind your suggestions
216+
- For kernel code: prioritize safety and correctness over performance
217+
218+
## Kernel Development Checklist
219+
220+
Before submitting code, ensure:
221+
- [ ] All `kmalloc` calls check return values
222+
- [ ] All user-space access uses proper copy functions
223+
- [ ] All error paths clean up partial allocations
224+
- [ ] Module init/exit properly register/unregister resources
225+
- [ ] Appropriate errno codes returned on errors
226+
- [ ] Doxygen comments on all functions
227+
- [ ] No floating point operations
228+
- [ ] Proper locking documented (if applicable)
229+
- [ ] Academic Integrity Notice present in LICENSE
230+
- [ ] Professional documentation for portfolio use

.gitignore

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,23 @@ temp/
9595
*.tmp
9696
*.bak
9797
*.swp
98+
99+
# Linux Kernel Module build artifacts
100+
*.ko
101+
*.mod.c
102+
*.mod
103+
*.o
104+
*.o.cmd
105+
*.ko.cmd
106+
*.mod.o
107+
*.mod.o.cmd
108+
.*.cmd
109+
.tmp_versions/
110+
modules.order
111+
Module.symvers
112+
*.markers
113+
*.order
114+
115+
# User-space programs from kernel module
116+
Linux_Kernel_Module/message_sender
117+
Linux_Kernel_Module/message_reader

0 commit comments

Comments
 (0)