systems programming - How to handle memory obtained by module_param when writing a Linux kernel module? -


when writing kernel module linux, module_param , variants can take string module parameter, , memory space required string allocated behind scenes without having explicitly. question is, how should handle piece of memory? have explicitly free if don't need anymore? should if want change string inside module?

memory allocated module_param string (charp type) argument maintained callbacks parameter's type, see param_set_charp, param_get_charp , param_free_charp functions defined in kernel/params.c.

  1. the easiest way operate such param declare read only, , accessing module read. in case 1 needn't bother locking on access or freeing parameter's value.

  2. if want write module read only parameter, need:

    1) free old value of parameter via param_free_charp.

    2) assign new value parameter. if value allocated, need manually deallocate in module_exit or in next writting parameter.

    both these actions should performed inside single critical section kernel_param_lock(this_module)/kernel_param_unlock(this_module), possible reading parameter's value user space found value initialized.

  3. if declare parameter writable, reading inside module requires critical section kernel_param_lock(this_module)/kernel_param_unlock(this_module).

  4. writing writable parameter allowed when new value stored somewhere else, if parameter rewritten user space, previous value can found , freed if needed.

    writing , reading such parameter inside module should follow same requiremens cases 2 , 3 cases correspondingly.


Comments