Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp3038609ybt; Mon, 29 Jun 2020 13:37:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy+OUNYwpwZ1k35xAsh2na3az/JfKvIs4bpaPNUo4awl85+b9W5HhjOmLt4IZLYqjDnvHUv X-Received: by 2002:a17:906:90b:: with SMTP id i11mr15186277ejd.343.1593463021079; Mon, 29 Jun 2020 13:37:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593463021; cv=none; d=google.com; s=arc-20160816; b=xcxNjKUrhG+XyxyOO+I7Y/yUTwhL1jigVpmGBEUcgF8ZJAjVXGWFIdCRDxs+pZ+lBy Asr8vOqRoFPSEnb6PhViwwVW9lseMVlHaJjh1Qv3s8UjYO8HIQNUdetTjovh94wwbs7r R2Vw6+d5We31Zv9DRlg/rr+2U4s6uYhUatcEu4dqMZRlhVEJuVsg53cGF99s5rNTmHef QAXexm4xpPZMxSu12NF1DDG6a4clcNe5WGoXUr2HmVP3Nk3woaURhgM56BEYpqLyGgQJ wy8B+eYeNigSa/1h4nZV+GMqvmfr2+GKABUpAFbO5k/rlSZagiP4ccMJgSWmSl7kuUiu uwzw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=xES//uqOCPStX3aykAvLo6fwp6iB7kGQaX7aeLYCA8A=; b=iPYgjrt0+mTHS+1FC5ug1/Jou2lIG70QgwHfMroTRHilpBGA2W7mXmGCc9lweaa0zS mfEIeeo+SNd/LRFoT3DNYMHGvANBNN6KjbmsYD2m8LOg379ltJehJkGsjs0QpZhu4j0X k1GCvUOGbe2MQaLI6o+RSf+F7+gKP/vEeqSJULC8QHUiuXs3b68YWQ7I9Argk9G6UxPK FCRZoP6+FUF8f+H584cRDXJ7G4iVk/hNfVD7lVGGtEkV7PzRXM+8MJxQWmAlYkKg4je7 ax5+Ass1GgKoqubn7LrVurvxWkPAY9noXWMs96ZLlDGUCsYsGaA6aw57nFP+v2kUH980 7iRA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dp1si556573ejc.505.2020.06.29.13.36.38; Mon, 29 Jun 2020 13:37:01 -0700 (PDT) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389411AbgF2UfR (ORCPT + 99 others); Mon, 29 Jun 2020 16:35:17 -0400 Received: from mx2.suse.de ([195.135.220.15]:59512 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732207AbgF2TVe (ORCPT ); Mon, 29 Jun 2020 15:21:34 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 63211ADB5; Mon, 29 Jun 2020 14:04:46 +0000 (UTC) Date: Mon, 29 Jun 2020 16:04:45 +0200 From: Petr Mladek To: John Ogness Cc: Peter Zijlstra , Sergey Senozhatsky , Sergey Senozhatsky , Steven Rostedt , Linus Torvalds , Greg Kroah-Hartman , Andrea Parri , Thomas Gleixner , Paul McKenney , kexec@lists.infradead.org, linux-kernel@vger.kernel.org Subject: Re: buffer allocation: was: [PATCH v3 3/3] printk: use the lockless ringbuffer Message-ID: <20200629140445.GK6156@alley> References: <20200618144919.9806-1-john.ogness@linutronix.de> <20200618144919.9806-4-john.ogness@linutronix.de> <20200625082838.GF6156@alley> <87sgeh3m5j.fsf@jogness.linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <87sgeh3m5j.fsf@jogness.linutronix.de> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri 2020-06-26 17:02:48, John Ogness wrote: > On 2020-06-25, Petr Mladek wrote: > >> --- a/kernel/printk/printk.c > >> +++ b/kernel/printk/printk.c > >> + free = __LOG_BUF_LEN; > >> + prb_for_each_record(0, &printk_rb_static, seq, &r) > >> + free -= add_to_rb(&printk_rb_dynamic, &r); > >> + > >> + prb = &printk_rb_dynamic; > > > > This might deserve a comment that this is safe (no lost message) > > because it is called early enough when everything is still running > > on the boot CPU. > > I will add a comment and an extra check to make sure. > > Once everything is lockless, new messages could appear (for example, due > to NMI messages). The simple check should probably change to a loop. But > let us not worry about that at this point. Yup. > Below is a new version of the relevant patch snippets against mainline > just to show you how I plan to make it look for the next version. > > John Ogness > > --- a/kernel/printk/printk.c > +++ b/kernel/printk/printk.c > @@ @@ > +#define PRB_AVGBITS 5 /* 32 character average length */ > + > +#if CONFIG_LOG_BUF_SHIFT <= PRB_AVGBITS > +#error CONFIG_LOG_BUF_SHIFT value too small. > +#endif > +_DEFINE_PRINTKRB(printk_rb_static, CONFIG_LOG_BUF_SHIFT - PRB_AVGBITS, > + PRB_AVGBITS, PRB_AVGBITS, &__log_buf[0]); > + > @@ @@ > void __init setup_log_buf(int early) > { > + unsigned int new_descs_count; > + struct prb_desc *new_descs; > + struct printk_info info; > + struct printk_record r; > + size_t new_descs_size; > unsigned long flags; > + char *new_dict_buf; > char *new_log_buf; > unsigned int free; > + u64 seq; > > /* > * Some archs call setup_log_buf() multiple times - first is very > @@ @@ void __init setup_log_buf(int early) > if (!new_log_buf_len) > return; > > + new_descs_count = new_log_buf_len >> PRB_AVGBITS; > + if (new_descs_count == 0) { > + pr_err("new_log_buf_len: %lu too small\n", new_log_buf_len); > + return; > + } > + > new_log_buf = memblock_alloc(new_log_buf_len, LOG_ALIGN); > if (unlikely(!new_log_buf)) { > - pr_err("log_buf_len: %lu bytes not available\n", > - new_log_buf_len); > + pr_err("log_buf_len: %lu text bytes not available\n", > + new_log_buf_len); > + return; > + } > + > + new_dict_buf = memblock_alloc(new_log_buf_len, LOG_ALIGN); > + if (unlikely(!new_dict_buf)) { > + pr_err("log_buf_len: %lu dict bytes not available\n", > + new_log_buf_len); > + memblock_free(__pa(new_log_buf), new_log_buf_len); > return; > } > > + new_descs_size = new_descs_count * sizeof(struct prb_desc); > + new_descs = memblock_alloc(new_descs_size, LOG_ALIGN); > + if (unlikely(!new_descs)) { > + pr_err("log_buf_len: %lu desc bytes not available\n", > + new_descs_size); > + memblock_free(__pa(new_dict_buf), new_log_buf_len); > + memblock_free(__pa(new_log_buf), new_log_buf_len); > + return; > + } > + > + prb_rec_init_rd(&r, &info, > + &setup_text_buf[0], sizeof(setup_text_buf), > + &setup_dict_buf[0], sizeof(setup_dict_buf)); > + > + prb_init(&printk_rb_dynamic, > + new_log_buf, order_base_2(new_log_buf_len), > + new_dict_buf, order_base_2(new_log_buf_len), > + new_descs, order_base_2(new_descs_count)); order_base_2() is safe. But the result might be tat some allocated space is not used. I would prefer to make sure that new_log_buf_len is rounded, e.g. by roundup_pow_of_two(), at the beginning of the function. Then we could use ilog2() here. Otherwise, it looks fine to me. Best Regards, Petr