Received: by 2002:a05:6358:53a8:b0:117:f937:c515 with SMTP id z40csp5420692rwe; Tue, 18 Apr 2023 06:44:54 -0700 (PDT) X-Google-Smtp-Source: AKy350b+Rz4wisbqdbGNNLaZoEwagMSv+mHJdLICHFOl6bb5nw+Hkffz0rDLaxNOd9vcfihtJiGz X-Received: by 2002:a05:6a20:a10d:b0:f0:7bd5:cc06 with SMTP id q13-20020a056a20a10d00b000f07bd5cc06mr3617665pzk.16.1681825493903; Tue, 18 Apr 2023 06:44:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681825493; cv=none; d=google.com; s=arc-20160816; b=Fi5ZBaVB7yyMlp01syqhuv/4v5ok8g0IMvtkq6KUqB8K3WM2X0UiXQ6bQxFWyGeOzq wAtr5n5vIE7Dp7UvdegqpFRNiUi0UMw3V7rL6J0mWBWbrzum0Rj1pbhU5UYJPyYayspi c8XCjXI6D4gDlC9rYr/58r646Q5i+wKAYqioQ0YG+GCqOQJHcfRWGaZGhRtWi/16RE3u Ey9hDzxHmbGh7gJRE23WYWPfAVnxTNNXk1CJQroq9Z4KMOo/OEBMCGO/2CIhUebqEZHE r2KTk8VZJLYOVn/Hx7FRmqqhtkYZ7O5XQQ1I66qvY7ka3NXcIW8GtOhsFEg2kxdbw3AT 9rfA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=iAAQ0frQqJj6843+qn+YG/AGqrm/NXD2JEWPvg2PLWM=; b=Jxn0SywxCOHJ/YGmkbvvFvix4QNyW6n9G1iaHgQEaGVbrRe27aGF5xhBtFjKXKUvBP vyOzX1bvuCx4mocZAvrcFRGMqiu0aQQRqLj3ytztoQd6dI1LIN1KyP8gr+M1fu/sM2wh wfcpwmFPIy6bTSAlO7CjxD1TGXkAngLNwYIlJe+sm60BI+F6e9AR1A8BCe6mnLNSU2zw ksNn3D1+ISaYS3wSy4u+jdWK6SEnBMY7K8VG/SndIA/0tBag/LUw3WR5XkZKtMKgCAcv 32AwOWHZY1/4Hm8+QMyzUZ6ukdyJKAfl4s5bvkzDE/OUtOL80YqDNR9IOlQqk3kLLThE jLog== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=kDGIMux5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w184-20020a6382c1000000b0050f736a8d3dsi14777541pgd.64.2023.04.18.06.44.40; Tue, 18 Apr 2023 06:44:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=kDGIMux5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231635AbjDRNlm (ORCPT + 99 others); Tue, 18 Apr 2023 09:41:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56732 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230477AbjDRNll (ORCPT ); Tue, 18 Apr 2023 09:41:41 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8756A210B for ; Tue, 18 Apr 2023 06:41:39 -0700 (PDT) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 2BB871F8D7; Tue, 18 Apr 2023 13:41:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1681825298; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=iAAQ0frQqJj6843+qn+YG/AGqrm/NXD2JEWPvg2PLWM=; b=kDGIMux5X/IdSRGLRwvqLK/weGYG8HzbycZzmuGCDPXvpEysKE294uhfVRVC7FQ3hxOJjq DkGuudn4XrlRc1fZwu1VgT5aftQw9LOXUjQGuo1ANkLIHoGGeoY+jUQUe+4ba6Jp3PqYOR Od3UOFEtapsAUHFRSCOOQyBflO3zjUk= Received: from suse.cz (unknown [10.100.201.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id A317B2C141; Tue, 18 Apr 2023 13:41:37 +0000 (UTC) Date: Tue, 18 Apr 2023 15:41:31 +0200 From: Petr Mladek To: Chris Down Cc: linux-kernel@vger.kernel.org, Greg Kroah-Hartman , Sergey Senozhatsky , Steven Rostedt , John Ogness , Geert Uytterhoeven , kernel-team@fb.com Subject: Re: [PATCH v3 1/2] printk: console: Create console= parser that supports named options Message-ID: References: <732ee897b2bd49ada3f7dee396475c5a2195071b.1658339046.git.chris@chrisdown.name> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon 2023-04-17 16:04:27, Chris Down wrote: > (To others on this thread wondering about this patchset, Petr and I have had > some discussions offlist about v4 and it should be up soon.) > > Petr Mladek writes: > > I thought a lot how to do it a clean way. IMHO, it would be great to > > parse everything at a single place but it might require updating > > all drivers. I am not sure if it is worth it. > > > > So, I suggest to do it another way. We could implement a generic > > function to find in the new key[:value] format. It would check > > if the given option (key) exists and read the optional value. > > > > The optional value would allow to define another new options > > that would not need any value, e.g. "kthread" or "atomic" that > > might be used in the upcoming code that allows to offload > > console handling to kthreads. > > Any thoughts on something simple like this that takes advantage of > memmove()? This should overcome the mmio/io concerns, and it's fairly > simple. > > --- > > static bool find_and_remove_console_option(char *buf, size_t size, > const char *wanted, char *options) Nit: I would change the ordering of the parameters. The above uses the semantic of copy functions (desc, src). But this function is more about searching or reading. I would use semantic like strchr() or read() (where, what, buf). Also I would use the key:value names. Something like: static bool find_and_remove_console_option(char *options, const char char *val_buf, size_t val_buf_size) > { > bool found = false, first = true; > char *item, *opt = options; Nit: I would rename these: + item -> option: the function is searching for an option that has the format value:key. + opt -> next: make it more clear that it points behind the currently proceed option (string token). > while ((item = strsep(&opt, ","))) { > char *key = item, *value; > > value = strchr(item, ':'); > if (value) > *(value++) = '\0'; > > if (strcmp(key, wanted) == 0) { > found = true; > if (value) { > if (strlen(value) > size - 1) { > pr_warn("Can't copy console option value for %s:%s: not enough space (%zu)\n", > key, value, size); > found = false; > } else { > strscpy(buf, value, size); > } > } else > *buf = '\0'; > } > > if (!found && opt) > *(opt - 1) = ','; > if (!found && value) > *(value - 1) = ':'; > if (!first) > *(item - 1) = ','; This last assigned should not be needed. The above code replaced at max one ',' and one ':'. It should be enough to restore just the two. > if (found) > break; > > first = false; > } > > if (found) { > if (opt) > memmove(item, opt, strlen(opt) + 1); > else if (first) > *item = '\0'; > else > *--item = '\0'; > } > > return found; > } Otherwise, it looks correct. Note: I though about using strnchr() and strncmp() instead of replacing/restoring the two delimiters by '\0'. But the code looks more hairy in the end. Just for record, here is my attempt: static bool find_and_remove_console_option(char *options, const char *key, char *val_buf, size_t val_buf_size) { char *start, *next, *val; int option_len, key_len, found_key_len; bool found = false; if (val_buf && val_buf_size) *val_buf = '\0'; key_len = strlen(key); next = options; do { start = next; next = strchr(start, ','); if (next) { option_len = next - start; next++; } else { option_len = strlen(start); } val = strnchr(start, option_len, ':'); if (val) { found_key_len = val - start; val++; } else { found_key_len = option_len; } if (key_len != found_key_len) continue; if (!strncmp(start, key, key_len)) { found = true; break; } } while (next); if (found && val) { int val_len = option_len - key_len - 1; if (!val_buf || val_buf_size < val_len + 1) { pr_err("Can't copy value for the console option key: %s:%.*s\n", key, val_len, val); return false; } strscpy(val_buf, val, val_len + 1); } /* Remove the found value[:key][,] */ if (found) { if (next) memmove(start, next, strlen(next) + 1); else if (start == options) *options = '\0'; else *(start - 1) = '\0'; } return found; } Best Regards, Petr