Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp494537pxu; Fri, 4 Dec 2020 08:13:28 -0800 (PST) X-Google-Smtp-Source: ABdhPJxjX9X3vmIXOsdC8lHkAGFETNq4d3MlejR7gnZxAAEW+Hg+7uQbszElKbRixuLSSLl7Vlv/ X-Received: by 2002:a05:6402:1421:: with SMTP id c1mr8339807edx.247.1607098408085; Fri, 04 Dec 2020 08:13:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607098408; cv=none; d=google.com; s=arc-20160816; b=YjtIclEmkG6DOB8d7sbtYENE86uqyZcJY/zdqFTDPTOyLj5n6X8rml6wkC2VB6sJsQ nGcREwLN1rjYTsAN47B1RlAhSySQJJMm309DwkmNuSxvpSvSSeADJedXH1P1JNl6hEaV +2orrMvxByPd8HRCEwPJliakIu/XCr8tYhk1OVLhMg+p/ZBESMmYt41VvEbxbmFJvINX O+AcwJ0CRc7jFa7HS3ZinVFIgQEyPPN/shMg0Sg20VbkJKNtcccpVCf0A2TLAgGk0t5u IsY5QHyfEupxGlO0/wj3UBp67i7R4wzA2mUXegGRtgK7+gtOx8vJ3VJHZuCWwSWpz173 6X7g== 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=D33SdhwZNJi2uBaYJpFAFfeHS3fk9aEn8alrEz4biwo=; b=anIE4ERkpTL4pTpSPipQ4N5Yu9ghoQcSrn8mmYW+cCqtQcghEmAMai/FVrErXaxqF4 q1IOXGBbkdrVipruLtXhiKcqMgjxafnZlSBUYru1zg/eBjsYbmfzeDWz4TT+9J93nf7h /o5XLeolcC+TFRNQgn9/5r11a9Y96CuDBdLG4l+HU0t6HvNZc1o0ZlXCobOs5V8P2SFz kPScEWXSQPgeVuNkVW9nXWKtZ3yimR0R2+A5PPcwdhSPVwbpt0+Z7WuojhpUH+lUf4t2 M/v6ksmLbB6EPV6wX7j+RF6aygargLMuqUUu5yepIkyo+18x/we0ETms785ekYWsQ0s7 k/MA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=R4nG2Rhj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=suse.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r10si1584300ejg.233.2020.12.04.08.13.02; Fri, 04 Dec 2020 08:13:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=R4nG2Rhj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=suse.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730670AbgLDQKy (ORCPT + 99 others); Fri, 4 Dec 2020 11:10:54 -0500 Received: from mx2.suse.de ([195.135.220.15]:35462 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727096AbgLDQKy (ORCPT ); Fri, 4 Dec 2020 11:10:54 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1607098207; 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=D33SdhwZNJi2uBaYJpFAFfeHS3fk9aEn8alrEz4biwo=; b=R4nG2RhjBXbm0SfRECtr8SQfRq+9lcVM56MVjR5KL1t3L6IlXzTgIxsu3BwXuEFoZC4bvm edF09KND73+4y/drqmjOe6NkMKrRUVAhHPDTlfEVK7JJ8tXebIAjiKrUDGqxMJKL+NFT8p ZDHlLQfGNfa9u1rsEknSMnZGXFfQRiQ= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 81BDCAC9A; Fri, 4 Dec 2020 16:10:07 +0000 (UTC) Date: Fri, 4 Dec 2020 17:10:07 +0100 From: Petr Mladek To: John Ogness Cc: Sergey Senozhatsky , Sergey Senozhatsky , Steven Rostedt , Linus Torvalds , Greg Kroah-Hartman , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: recursion handling: Re: [PATCH next v2 3/3] printk: remove logbuf_lock, add syslog_lock Message-ID: References: <20201201205341.3871-1-john.ogness@linutronix.de> <20201201205341.3871-4-john.ogness@linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20201201205341.3871-4-john.ogness@linutronix.de> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue 2020-12-01 21:59:41, John Ogness wrote: > Since the ringbuffer is lockless, there is no need for it to be > protected by @logbuf_lock. Remove @logbuf_lock. > > --- a/kernel/printk/printk.c > +++ b/kernel/printk/printk.c > @@ -1847,6 +1811,65 @@ static void call_console_drivers(const char *ext_text, size_t ext_len, > } > } The recursion-related code needs some explanation or we should do it another way. I spent quite some time on it and I am still not sure that I understand it. Let me describe how I understand it. > +#ifdef CONFIG_PRINTK_NMI > +#define NUM_RECURSION_CTX 2 > +#else > +#define NUM_RECURSION_CTX 1 > +#endif OK, the number of context is limited because interrupts are disabled inside print_enter()/printk_exit(). It is basically the same reason why we have only two printk_safe buffers (NNI + other contexts). What is the exact reason to disable interrupts around the entire vprintk_store(), please? It should get documented. One reason is the use of per-cpu variables. Alternative solution would be to store printk_context into task_struct. Well, I am not sure if "current" task is available during early boot. But it might solve problems with per-cpu variables that are not working during early boot. That said, I am not sure if it is worth it. > + > +struct printk_recursion { > + char count[NUM_RECURSION_CTX]; > +}; > > +static DEFINE_PER_CPU(struct printk_recursion, percpu_printk_recursion); > +static char printk_recursion_count[NUM_RECURSION_CTX]; This is pretty confusing. The array is hidden in a struct when per-cpu variables are used. And a naked array is used for early boot. Is the structure really needed? What about? static DEFINE_PER_CPU(char [PRINTK_CTX_NUM], printk_count); static char printk_count_early[NUM_RECURSION_CTX]; > + > +static char *get_printk_count(void) > +{ > + struct printk_recursion *rec; > + char *count; > + > + if (!printk_percpu_data_ready()) { > + count = &printk_recursion_count[0]; I see why you avoided per-cpu variables for early boot. I am just curious how printk_context variable works these days. It is used by any printk(), including early code, see vprintk_func(). > + } else { > + rec = this_cpu_ptr(&percpu_printk_recursion); > + > + count = &rec->count[0]; > + } > + > +#ifdef CONFIG_PRINTK_NMI > + if (in_nmi()) > + count++; > +#endif This is extremely confusing. It is far from obvious that the pointer and not the value is incremented. If we really need this to avoid per-cpu variables during early boot then a more clear implementation would be: char *get_printk_counter_by_ctx() { int ctx = 0; if (in_nmi) ctx = 1; if (!printk_percpu_data_ready()) return &printk_count_early[ctx]; return this_cpu_ptr(printk_count[ctx]); } > + > + return count; > +} > + > +static bool printk_enter(unsigned long *flags) > +{ > + char *count; > + > + local_irq_save(*flags); > + count = get_printk_count(); > + /* Only 1 level of recursion allowed. */ We should allow at least some level of recursion. Otherwise, we would not see warnings printed from vsprintf code. > + if (*count > 1) { > + local_irq_restore(*flags); > + return false; > + } > + (*count)++; > + > + return true; > +} This should be unified with printk_context, printk_nmi_enter(), printk_nmi_exit(). It does not make sense to have two separate printk context counters. Or is there any plan to remove printk_safe and printk_context? BTW: I prefer to use the bitmask approach. It allows to check the normal context by a single operation (no bit is set). There is no need to go through all counters. Note that we need at least one more context for gdb. Best Regards, Petr