Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp327855pxb; Fri, 22 Apr 2022 01:32:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxIxCVXpY9zmLvFNEFbeO41n4PtgStu72h0OPQHbm3iIqyCtAho/2k8oEY+eMaAhYU9y+Em X-Received: by 2002:a17:906:6bcd:b0:6e8:93e4:b37f with SMTP id t13-20020a1709066bcd00b006e893e4b37fmr3015043ejs.529.1650616353867; Fri, 22 Apr 2022 01:32:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650616353; cv=none; d=google.com; s=arc-20160816; b=tC/feUwFbuDSAAQtX4C/zEe7HR1o+hFouqZLkfuCF3F0s+bqtxelpWq5Nt3jbHceET C4SI7nw26nbH8NhPPH4V9p+z/1kFER2txpAzZw9lucRlH3gTwzVYxwckeRZaGAcwUvNF zgy9sFBDQBm6C2Go/7mTLdRWlQ5MX+fKqKIQJGJR/r46aD32v3GlxCe2mTI820IlhyF9 ELVkSpMRosnMxlKFU4PJbEZ9nubJWHw3E+MKx0Q2VG2Xf2sJjLv4xvjq0WJsMFDGpFhR hYhUtsQ96ddXL7jbQwXgreAGFi4wIL9Ko0bZvzSvD4cwdiYdhDu7jLxwm3Ve9kwx8sM2 EsVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=Dz1VIM0uOb4OFT5vjsJmvFN5uJS7lSMD11xUufLa4SE=; b=RkgNnsMThBZLWXERMLkvQntCSkUr8lx8RzAi1SO14M1usPKuoKqkZQT8nw5rzTsbOy +8r4fyuYeGrWE0TT4adehM9ESEWBcBPe0XpHiY/Ek9/ocwcoAOsrWlNvUDJP8ghL8Pdi F+bNJy4CschQlTR+v1m8EvaUE5P14dHzx9MR2hD/mua6PJzogCPjKy4RvXomoHNeAS5X VhvE9g5+AIgmgw1ELmtiks7VX+rC/rMmozItCUitNKmnXn3yYGTbelRm5FbmifWX4JWt dvBNOF1uvkhytJcyDQ+F/flKJBlTeSlIYWZFDu1jqJUOp2ex28KK7NfCTW+nfkJ9dixI Z+uA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=OZCGW8R3; dkim=neutral (no key) header.i=@linutronix.de; 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=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id pg8-20020a170907204800b006e7f3b2ef70si5804283ejb.31.2022.04.22.01.32.07; Fri, 22 Apr 2022 01:32:33 -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=@linutronix.de header.s=2020 header.b=OZCGW8R3; dkim=neutral (no key) header.i=@linutronix.de; 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=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1358264AbiDSXta (ORCPT + 99 others); Tue, 19 Apr 2022 19:49:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39374 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352297AbiDSXtZ (ORCPT ); Tue, 19 Apr 2022 19:49:25 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 870471EED2 for ; Tue, 19 Apr 2022 16:46:41 -0700 (PDT) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1650412000; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Dz1VIM0uOb4OFT5vjsJmvFN5uJS7lSMD11xUufLa4SE=; b=OZCGW8R3rDrfoeJVcFtn9I3/oLnd92vxhRSesXB28UUHsW5tEb0skqxclQAjj1xIVHk/Jt LLb2u/SkuQPOMLDKG8EwYovwZTLQKOUVuBjqhyReZmcjjJYE1VndLJo9ZEQvBDIXdCEm2y 6CFWd3NVImL2t7PhQ133ZwBvEZLwX4Vv0sTdZsQzzidAVJBsaIcpRSalJe3xA7Bi8WwX3H cT9D/iBBvwmYaal9AOJJOZt/8OqTGRLBDSCrDljDChsOTvlC4oYnJk6vZRY228DZu0LTd7 8DtIiWoWL9ekOOP9X+KFr4fjvSifUJsxzKhc0eS1+HB2wYE/y5OPapfnJ/bkMA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1650412000; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Dz1VIM0uOb4OFT5vjsJmvFN5uJS7lSMD11xUufLa4SE=; b=WVBX/41uM/GPuFFdEt0BZ77NL828pAS2Z6koZCHhKakxzjVYPC7M6+d+RUV3sqL1hDMFrD KZJAiyHto31wn3CQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v3 03/15] printk: add missing memory barrier to wake_up_klogd() Date: Wed, 20 Apr 2022 01:52:25 +0206 Message-Id: <20220419234637.357112-4-john.ogness@linutronix.de> In-Reply-To: <20220419234637.357112-1-john.ogness@linutronix.de> References: <20220419234637.357112-1-john.ogness@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,INVALID_DATE_TZ_ABSURD, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 It is important that any new records are visible to preparing waiters before the waker checks if the wait queue is empty. Otherwise it is possible that: - there are new records available - the waker sees an empty wait queue and does not wake - the preparing waiter sees no new records and begins to wait This is exactly the problem that the function description of waitqueue_active() warns about. Use wq_has_sleeper() instead of waitqueue_active() because it includes the necessary full memory barrier. Signed-off-by: John Ogness --- kernel/printk/printk.c | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 13a1eebe72af..f817dfb4852d 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -746,8 +746,19 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf, goto out; } + /* + * Guarantee this task is visible on the waitqueue before + * checking the wake condition. + * + * The full memory barrier within set_current_state() of + * prepare_to_wait_event() pairs with the full memory barrier + * within wq_has_sleeper(). + * + * This pairs with wake_up_klogd:A. + */ ret = wait_event_interruptible(log_wait, - prb_read_valid(prb, atomic64_read(&user->seq), r)); + prb_read_valid(prb, + atomic64_read(&user->seq), r)); /* LMM(devkmsg_read:A) */ if (ret) goto out; } @@ -1513,7 +1524,18 @@ static int syslog_print(char __user *buf, int size) seq = syslog_seq; mutex_unlock(&syslog_lock); - len = wait_event_interruptible(log_wait, prb_read_valid(prb, seq, NULL)); + /* + * Guarantee this task is visible on the waitqueue before + * checking the wake condition. + * + * The full memory barrier within set_current_state() of + * prepare_to_wait_event() pairs with the full memory barrier + * within wq_has_sleeper(). + * + * This pairs with wake_up_klogd:A. + */ + len = wait_event_interruptible(log_wait, + prb_read_valid(prb, seq, NULL)); /* LMM(syslog_print:A) */ mutex_lock(&syslog_lock); if (len) @@ -3316,7 +3338,18 @@ void wake_up_klogd(void) return; preempt_disable(); - if (waitqueue_active(&log_wait)) { + /* + * Guarantee any new records can be seen by tasks preparing to wait + * before this context checks if the wait queue is empty. + * + * The full memory barrier within wq_has_sleeper() pairs with the full + * memory barrier within set_current_state() of + * prepare_to_wait_event(), which is called after ___wait_event() adds + * the waiter but before it has checked the wait condition. + * + * This pairs with devkmsg_read:A and syslog_print:A. + */ + if (wq_has_sleeper(&log_wait)) { /* LMM(wake_up_klogd:A) */ this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP); irq_work_queue(this_cpu_ptr(&wake_up_klogd_work)); } -- 2.30.2