Re: [PATCH v2 001/101] lib: Add option iterator

From: Thomas Zimmermann
Date: Fri Mar 10 2023 - 07:39:01 EST


Hi

Am 10.03.23 um 09:21 schrieb Geert Uytterhoeven:
Hi Thomas,

On Thu, Mar 9, 2023 at 5:02 PM Thomas Zimmermann <tzimmermann@xxxxxxx> wrote:
Add struct option_iter and helpers that walk over individual options
of an option string. Add documentation.

Kernel parameters often have the format of

param=opt1,opt2:val,opt3

where the option string contains a number of comma-separated options.
Drivers usually use strsep() in a loop to extract individual options
from the string. Each call to strsep() modifies the given string, so
callers have to duplicate kernel parameters that are to be parsed
multiple times.

The new struct option_iter and its helpers wrap this code behind a
clean interface. Drivers can iterate over the options without having
to know the details of the option-string format. The iterator handles
string memory internally without modifying the original options.

v2:
* improve documentation (Randy)
* implement option_iter_next() as function
* allow modification of the returned options

Signed-off-by: Thomas Zimmermann <tzimmermann@xxxxxxx>

Thanks for your patch!

I> ---
Documentation/core-api/kernel-api.rst | 9 +++
include/linux/cmdline.h | 36 +++++++++
lib/Makefile | 2 +-
lib/cmdline_iter.c | 109 ++++++++++++++++++++++++++
4 files changed, 155 insertions(+), 1 deletion(-)
create mode 100644 include/linux/cmdline.h
create mode 100644 lib/cmdline_iter.c

Just wondering, is there any code that can be shared with/reused from
the existing lib/cmdline.c?

AFAIU, cmdline.o is linked into early-boot code, which prevents it from using dynamic memory allocation, such as kstrdup() and kfree(). The linker warns about this:

LD arch/x86/boot/compressed/vmlinux
ld: arch/x86/boot/compressed/kaslr.o: in function `get_option':
kaslr.c:(.text+0x532): undefined reference to `kstrdup'
ld: arch/x86/boot/compressed/vmlinux: hidden symbol `kstrdup' isn't
defined
ld: final link failed: bad value

So the iterator is in a separate source file.

The code in cmdline.c mostly deals with parsing individual values or key-value pairs. Nothing moves over the complete string and returns options one by one. There's not much to share AFAICT.

Best regards
Thomas



Gr{oetje,eeting}s,

Geert


--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Maxfeldstr. 5, 90409 Nürnberg, Germany
(HRB 36809, AG Nürnberg)
Geschäftsführer: Ivo Totev

Attachment: OpenPGP_signature
Description: OpenPGP digital signature