Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp726174pxb; Tue, 9 Feb 2021 10:46:59 -0800 (PST) X-Google-Smtp-Source: ABdhPJyHKVF2L4o7ElycbmXGV/n8RinEI+S471m6hVcM2VqGofsLyPKPMWzUIHnf98H9whoVTFF/ X-Received: by 2002:a17:906:f0c4:: with SMTP id dk4mr23291060ejb.6.1612896418832; Tue, 09 Feb 2021 10:46:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612896418; cv=none; d=google.com; s=arc-20160816; b=pkHa77XkpkFIJ353yyaR6UFiDI8HTScwFqSeyFZxbFINMgui3LWTbIcqSKMIRD/EL2 1a0xgBdESavEfOk36tcKhZAbdo3vcS1U7hDFOAM6V2v8MpLdQg6evDvNQbQRYaBalaks TbvetuXl9JT/lBjd8iniFcdEP3K4zSNXdFCjQ0cm3AGJLHcAlXeURyfRemmmLVhm0n16 6k+EtECpQsn2j9PyoE73Kbkv6jApIlI4e+r63gWYjcZ+PQYamgCvQlewwfhX55Jq8Lmc /tq9+fVfZ6oCbXW+X4yJp/pzl1PKuMxh2i9bRsg+KfLja6kfo7W9YvaHt027fpFu89XG UPyQ== 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=jni2W/IDi/GWvC25GgqGCfHqUn0ldqQ6jWnvtbK6ewk=; b=HmDMplga48PfZ1jQmRP7waExlbzgvWZiMFGJRMAcEgjzpwnPMHdwHLSh0h3Ypbm3lz SdtjvJxXio3amiASGDQ1JvmLc/kZaiKdXA8NE7XiF/CMCyd8oTf9ELWOFW77Cz0xRHGg KhYjXJGjDydOCs7mhCOG0/pdN940DihD3bFMcx80aR7qsf5Slld7jRAgzIfF6Q/9nBVg fkGtdm7Lu3XmxLUll4i1BFEwJ1iCZz6PAlmzZO52o9RJng8BAW/4Rt/KaMwFOjD+hplt JnejGOPkz1YEttZA1tXRu6AjyAv73GDZEkZ3XtYKt0ALCWHOOgcQyILK25fyevlNDV0X d4tg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=F2JizNrE; 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 cc22si15955695edb.80.2021.02.09.10.46.34; Tue, 09 Feb 2021 10:46:58 -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=F2JizNrE; 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 S233608AbhBISnh (ORCPT + 99 others); Tue, 9 Feb 2021 13:43:37 -0500 Received: from mx2.suse.de ([195.135.220.15]:58828 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233238AbhBIRtO (ORCPT ); Tue, 9 Feb 2021 12:49:14 -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=1612892861; 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=jni2W/IDi/GWvC25GgqGCfHqUn0ldqQ6jWnvtbK6ewk=; b=F2JizNrEBrmtY1YGQeraNmBLSU/uLcXhtkc9Kkm1/r1i7YqHlGtIxz+1YrVR8hL+/RHyFP D9NU1q4aV4m9gZ3H1lJkJ3ou69x0R47NmtXSho2Bde5OMlXCTzxo0AytQleALlEniyo5P0 JD4BK+8usFFcuMKUdLkPVO6UqxIW2KA= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 7DF4FAB71; Tue, 9 Feb 2021 17:47:41 +0000 (UTC) Date: Tue, 9 Feb 2021 18:47:40 +0100 From: Petr Mladek To: John Ogness Cc: Sergey Senozhatsky , Sergey Senozhatsky , Steven Rostedt , linux-kernel@vger.kernel.org, "J. Avila" Subject: Re: [PATCH] printk: avoid prb_first_valid_seq() where possible Message-ID: References: <20210205141728.18117-1-john.ogness@linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210205141728.18117-1-john.ogness@linutronix.de> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri 2021-02-05 15:23:28, John Ogness wrote: > If message sizes average larger than expected (more than 32 > characters), the data_ring will wrap before the desc_ring. Once the > data_ring wraps, it will start invalidating descriptors. These > invalid descriptors hang around until they are eventually recycled > when the desc_ring wraps. Readers do not care about invalid > descriptors, but they still need to iterate past them. If the > average message size is much larger than 32 characters, then there > will be many invalid descriptors preceding the valid descriptors. > > The function prb_first_valid_seq() always begins at the oldest > descriptor and searches for the first valid descriptor. This can > be rather expensive for the above scenario. And, in fact, because > of its heavy usage in /dev/kmsg, there have been reports of long > delays and even RCU stalls. > > For code that does not need to search from the oldest record, > replace prb_first_valid_seq() usage with prb_read_valid_*() > functions, which provide a start sequence number to search from. > > Fixes: 896fbe20b4e2333fb55 ("printk: use the lockless ringbuffer") > Reported-by: kernel test robot > Reported-by: J. Avila > Signed-off-by: John Ogness > --- > patch against next-20210205 > > kernel/printk/printk.c | 29 ++++++++++++++++++----------- > 1 file changed, 18 insertions(+), 11 deletions(-) > > diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c > index 5a95c688621f..035aae771ea1 100644 > --- a/kernel/printk/printk.c > +++ b/kernel/printk/printk.c > @@ -1559,6 +1560,7 @@ static void syslog_clear(void) > > int do_syslog(int type, char __user *buf, int len, int source) > { > + struct printk_info info; > bool clear = false; > static int saved_console_loglevel = LOGLEVEL_DEFAULT; > int error; > @@ -1629,9 +1631,13 @@ int do_syslog(int type, char __user *buf, int len, int source) > /* Number of chars in the log buffer */ > case SYSLOG_ACTION_SIZE_UNREAD: > logbuf_lock_irq(); > - if (syslog_seq < prb_first_valid_seq(prb)) { > - /* messages are gone, move to first one */ > - syslog_seq = prb_first_valid_seq(prb); > + if (prb_read_valid_info(prb, syslog_seq, &info, NULL)) { > + if (info.seq != syslog_seq) { > + /* messages are gone, move to first one */ > + syslog_seq = info.seq; > + syslog_partial = 0; > + } > + } else { > syslog_partial = 0; I am scratching my head when prb_read_valid_info(prb, syslog_seq, &info, NULL)) might fail. It might fail when syslog_seq points to the next message after the last valid one. In this case, we could return immediately (after releasing the lock) because there are zero unread messages. Anyway, syslog_partial must be zero in this case. syslog_seq should stay when the last read was partial. And there should always be at least one valid message in the log buffer be design. Do I get it correctly, please? IMHO, it would deserve a comment and maybe even a warning. What about something like? /* Number of chars in the log buffer */ case SYSLOG_ACTION_SIZE_UNREAD: logbuf_lock_irq(); if (!prb_read_valid_info(prb, syslog_seq, &info, NULL)) { /* No unread message */ if (syslog_partial) { /* This should never happen. */ pr_err_once("Unable to read any message even when the last syslog read was partial: %zu", syslog_partial); syslog_partial = 0; } logbuf_unlock_irq(); return 0; } if (info.seq != syslog_seq) { /* messages are gone, move to first one */ syslog_seq = info.seq; syslog_partial = 0; } if (source == SYSLOG_FROM_PROC) { /* * Short-cut for poll(/"proc/kmsg") which simply checks [...] Best Regards, Petr