Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp3802455pxb; Mon, 1 Feb 2021 05:16:15 -0800 (PST) X-Google-Smtp-Source: ABdhPJyuvxC6lDZww0fP/hzf53I66wnyHABJ5kINe/P8E5EOGbimMXvUDiPaSqi4RP9K/WZCMYtV X-Received: by 2002:aa7:d511:: with SMTP id y17mr18994675edq.112.1612185375405; Mon, 01 Feb 2021 05:16:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612185375; cv=none; d=google.com; s=arc-20160816; b=TM6P6MHqWrtuBhmgObFUnjy2GowNUIn8VJ21M5p0s0bgSfChdWh9Eqxy/407MQ45mq fB/0dtjx9W+dZpIABNP6paHishpTgZcfHawXqZ0CX04ZuE6Uyn/aKIsQQg+xN6G9Sx1H yEK/MiCWUdTuBb5Puqkcf/y/19iL6xYkyKZy4SfFyNY0qEq6opyaN4ZfnIySmvDe6IR4 RiVGrBgtYHPq7BMP4Mu3NQHJI8CIbH+JDgeD9yR/QaMSXBmGNB8jZwS4ijmrWI8jHgfI vGgaXVN8gYOgTocRf4lsdHRVkp6gAN5AfSaP3hmo+wV+QMX4vJDNXu6tks0Gkz28zqAv u/Gw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:references :in-reply-to:subject:cc:to:dkim-signature:dkim-signature:from; bh=eUaCyr3F7lqUdpg4F0N9kWvTDUoAE0/ywIy7lgPol1Y=; b=b9s+6i3GCVZE7wVCs619QxtKginRZTnIk/Vos7v+dvA9iVXkfDX54MQYzpZisIwk8j ZLYwL+jsvNO6bEHaSry2YsjYxpDyiEMZfczr+mOnl10a49I6c8foYlGVxfflYda/FKpj G9cjTs/zP5DBaTC4a96UC2foNBxtehLDdUgkL7j4eBVU6QhZX2sII0/oRN7/HfSSoShY bX+995PdWfHAQhRLGqeIXJ8cUm8499pX9bt2LZh7FqnsZl6sUQMNLvlLoeRm1rucGqtk E5bOjSiKxsvs7PXazUoJeylw+N6JB9Htn/OeSjANMVEJnqrW+xIj4TdxYKRnmVMI1LVy fxuA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=LKQ9SVA7; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; 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=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y4si10852962edw.240.2021.02.01.05.15.50; Mon, 01 Feb 2021 05:16:15 -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=@linutronix.de header.s=2020 header.b=LKQ9SVA7; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; 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=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231622AbhBANMm (ORCPT + 99 others); Mon, 1 Feb 2021 08:12:42 -0500 Received: from Galois.linutronix.de ([193.142.43.55]:57534 "EHLO galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229707AbhBANMj (ORCPT ); Mon, 1 Feb 2021 08:12:39 -0500 From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1612185116; h=from:from:reply-to:subject:subject: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=eUaCyr3F7lqUdpg4F0N9kWvTDUoAE0/ywIy7lgPol1Y=; b=LKQ9SVA7OazpDeEL++kzx1lrPvmddTdlJM7N2q+ye8vftUs5cuTPlFrZDaUaEBL3RDz3cy dqq+H0pc/B8k6CIgTU7lwYvIJnUmvRHN1Xce8GvmQN0gLhnB34lfvp0ndPDJ2n7njohe9g G+xN1vB34qwUyD7gLGSKKRftA5IGTLpD80pmOROWP5uqQI4LOOUAHjKvumgCseDwcvQSJN lSS8cpjNzKvlpk0BT/eQt+YYumUacENOhfKX5iC+QdyaGtgAC8tdhH6NBqskyqhniVK57R AzuK9Vn/aXC4qty74m474jwIsYKei2k1qQL7J2L4xJ5tzS+VRe7D0ZBiBQ5qFw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1612185116; h=from:from:reply-to:subject:subject: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=eUaCyr3F7lqUdpg4F0N9kWvTDUoAE0/ywIy7lgPol1Y=; b=i9BKcFmNuVf6k65hRZktN7NvsYtG3J2GoFgLW1oaMvvTBZ2x8jIcffHob9awGz2I+ee24U rFN6qoO2/pcymyCg== To: Petr Mladek Cc: Sergey Senozhatsky , Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: Re: [PATCH printk-rework 07/12] printk: add syslog_lock In-Reply-To: References: <20210126211551.26536-1-john.ogness@linutronix.de> <20210126211551.26536-8-john.ogness@linutronix.de> Date: Mon, 01 Feb 2021 14:17:55 +0106 Message-ID: <87h7mwj5k4.fsf@jogness.linutronix.de> MIME-Version: 1.0 Content-Type: text/plain Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2021-02-01, Petr Mladek wrote: >> The global variables @syslog_seq, @syslog_partial, @syslog_time >> and write access to @clear_seq are protected by @logbuf_lock. >> Once @logbuf_lock is removed, these variables will need their >> own synchronization method. Introduce @syslog_lock for this >> purpose. > >> --- a/kernel/printk/printk.c >> +++ b/kernel/printk/printk.c >> @@ -390,8 +390,12 @@ DEFINE_RAW_SPINLOCK(logbuf_lock); >> printk_safe_exit_irqrestore(flags); \ >> } while (0) >> >> +/* syslog_lock protects syslog_* variables and write access to clear_seq. */ >> +static DEFINE_RAW_SPINLOCK(syslog_lock); > > I am not expert on RT code but I think that it prefers the generic > spinlocks. syslog_lock seems to be used in a normal context. > IMHO, it does not need to be a raw spinlock. > > Note that using normal spinlock would require switching the locking > order. logbuf_lock is a raw lock. Normal spinlock must not be taken > under a raw spinlock. > > Or we could switch syslog_lock to the normal spinlock later > after logbuf_lock is removed. I was planning on this last option because I think it is the simplest. There are places such as syslog_print_all() where the printk_safe_enter() and logbuf_lock locking are not at the same place as the syslog_lock locking (and syslog_lock is inside). Once the safe buffers are removed, syslog_lock can transition to a spinlock. (spinlock's must not be under local_irq_save().) >> + >> #ifdef CONFIG_PRINTK >> DECLARE_WAIT_QUEUE_HEAD(log_wait); >> +/* All 3 protected by @syslog_lock. */ >> /* the next printk record to read by syslog(READ) or /proc/kmsg */ >> static u64 syslog_seq; >> static size_t syslog_partial; >> @@ -1631,6 +1643,7 @@ int do_syslog(int type, char __user *buf, int len, int source) >> bool clear = false; >> static int saved_console_loglevel = LOGLEVEL_DEFAULT; >> int error; >> + u64 seq; > > This allows to remove definition of the same temporary variable > for case SYSLOG_ACTION_SIZE_UNREAD. Right. I missed that. >> >> error = check_syslog_permissions(type, source); >> if (error) >> @@ -1648,8 +1661,14 @@ int do_syslog(int type, char __user *buf, int len, int source) >> return 0; >> if (!access_ok(buf, len)) >> return -EFAULT; >> + >> + /* Get a consistent copy of @syslog_seq. */ >> + raw_spin_lock_irq(&syslog_lock); >> + seq = syslog_seq; >> + raw_spin_unlock_irq(&syslog_lock); >> + >> error = wait_event_interruptible(log_wait, >> - prb_read_valid(prb, syslog_seq, NULL)); >> + prb_read_valid(prb, seq, NULL)); > > Hmm, this will not detect when syslog_seq gets cleared in parallel. > I hope that nobody rely on this behavior. But who knows? > > A solution might be to have also syslog_seq latched. But I am > not sure if it is worth it. > > I am for taking the risk and use the patch as it is now. Let's keep > the code for now. We could always use the latched variable when > anyone complains. Just keep it in mind. We could add a simple helper: /* Get a consistent copy of @syslog_seq. */ static u64 syslog_seq_read(void) { unsigned long flags; raw_spin_lock_irqsave(&syslog_lock, flags); seq = syslog_seq; raw_spin_unlock_irqrestore(&syslog_lock, flags); return seq; } Then change the code to: error = wait_event_interruptible(log_wait, prb_read_valid(prb, read_syslog_seq(), NULL)); register_console() could also make use of the function. (That is why I am suggesting the flags variant.) John Ogness