From patchwork Thu Mar 2 19:56:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Ogness X-Patchwork-Id: 63544 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp42655wrd; Thu, 2 Mar 2023 12:00:59 -0800 (PST) X-Google-Smtp-Source: AK7set9fiaqUqf1YTDD3OUiv7YntUdoNCjfeagLn1AsPBhOHp2l823o8giQf5eofsNrqMEeO/oci X-Received: by 2002:a05:6a20:69a3:b0:cc:1996:9853 with SMTP id t35-20020a056a2069a300b000cc19969853mr17658441pzk.47.1677787258983; Thu, 02 Mar 2023 12:00:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677787258; cv=none; d=google.com; s=arc-20160816; b=EHd5stVpZLNDjpLZEf3m0w8DymVEtsjBxMlDs7vz2C0WCSMgJLZRbgri/zedJNqyoa AqeNOvMChVo0g2BuoGLwORTpKRaZQwjv3dh2DMqJAH5leNoX4LXzS+d7NQTuT7QHj+k2 2N8rGqUWrzoSatxToXhSw+JhdIcoHEB7TQwQ596fSQeM99rjBHwR5UHe9smhAWdqw8D1 CHoT5//3A+CukfFrT1VtJOnqx0rlJdoLnNnhm6pDQcjCU8qoZKMoujxptCfjWuQ+x5ny vHNaSmx0mUFbQ7XaIEASY4cjcEzdMT+jYXyZvvrv71e6k5WxLMFzn6yBlA0bw/XyI2Iu aNSg== 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=j2fMZu7G+YK9eneabidoKIltYZn7KfVS0Zt0k2ab0y0=; b=n4thnK63LbGM/+UmNcnXhU0axShACGO00VuauUFsWcCfHRfvq/Wi0j3tlBxO3TN6lr W3FwtAIjhBDXj+yQFB0MES9kCNJTIAjfVTyMr2SDcozAcWIakXwpB47thMbjWH9IwpOs kTRu7hTFMy2lg6dcTCmeUYi5lOx6heGtYcKR+ExyPlmq6Uq42o3HabCcm3Zw6exJhSL2 Buo701KrJJUb+d99dTtHoNx2YmmfSHxYE14yDyVBwIUhNjsNXeBe3Dyv/LnsIUgQjoNl 8dyLeHP+oGYt6wIfjQTlF8vygR/GGtJxgR9VjlEU/2+2q56bN1J9TOa8cK9HrkwG1Z0t wxxQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=3nCVLeHq; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i65-20020a625444000000b005941664b506si192356pfb.224.2023.03.02.12.00.45; Thu, 02 Mar 2023 12:00:58 -0800 (PST) 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=3nCVLeHq; 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 S230210AbjCBT6U (ORCPT + 99 others); Thu, 2 Mar 2023 14:58:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54846 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230000AbjCBT5u (ORCPT ); Thu, 2 Mar 2023 14:57:50 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EA2CF474E8 for ; Thu, 2 Mar 2023 11:57:47 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1677787065; 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=j2fMZu7G+YK9eneabidoKIltYZn7KfVS0Zt0k2ab0y0=; b=3nCVLeHqSTGTltSXJiVNkoE1q8fdIPyjeuYWyzSd4c7S1+AztCDDJIZRwc0oNa7iLmCJQm k1Er7/aw3qWpZ5X9i3hP/KFf5rWAlU1SVOB9NQiRAwwJFny8SQTVfRzkBeGk4Tk1GBEeL9 +R6vWhYToMqDU/WTBI2pnHuRXnFZ8xwCV+0FIV1mnDERyiqPfG8Itln0C9xQM0Av/Kn8Ij jvbWmqO7Ukglg/lBmB3MTAUNFLn4gYrqvQlJwL3lw7+gFlvXL5UhB8rA42LBRfSdCmyyxI thEG1ez8IbCg8z+lLVV51R7KoWAVTGhI1v6bPzmDNeL4n+F36DR5mxSgxHQl9A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1677787065; 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=j2fMZu7G+YK9eneabidoKIltYZn7KfVS0Zt0k2ab0y0=; b=NbLLJtoEj1oiWEL7cz5tkc94m1maoG84ijey5UzBqQUkgzAHCQZ1fbPMsrx18wvdzgYufs BPYawCdSurW+GCBw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v1 09/18] printk: nobkl: Add print state functions Date: Thu, 2 Mar 2023 21:02:09 +0106 Message-Id: <20230302195618.156940-10-john.ogness@linutronix.de> In-Reply-To: <20230302195618.156940-1-john.ogness@linutronix.de> References: <20230302195618.156940-1-john.ogness@linutronix.de> MIME-Version: 1.0 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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759287453060660455?= X-GMAIL-MSGID: =?utf-8?q?1759287453060660455?= From: Thomas Gleixner Provide three functions which are related to the safe handover mechanism and allow console drivers to denote takeover unsafe sections: - console_can_proceed() Invoked by a console driver to check whether a handover request is pending or whether the console was taken over in a hostile fashion. - console_enter/exit_unsafe() Invoked by a console driver to denote that the driver output function is about to enter or to leave an critical region where a hostile take over is unsafe. These functions are also cancellation points. The unsafe state is stored in the console state and allows a takeover attempt to make informed decisions whether to take over and/or output on such a console at all. The unsafe state is also available to the driver in the write context for the atomic_write() output function so the driver can make informed decisions about the required actions or take a special emergency path. Co-developed-by: John Ogness Signed-off-by: John Ogness Signed-off-by: Thomas Gleixner (Intel) --- include/linux/console.h | 3 + kernel/printk/printk_nobkl.c | 139 +++++++++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+) diff --git a/include/linux/console.h b/include/linux/console.h index 942cc7f57798..0779757cb917 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -464,6 +464,9 @@ static inline bool console_is_registered(const struct console *con) lockdep_assert_console_list_lock_held(); \ hlist_for_each_entry(con, &console_list, node) +extern bool console_can_proceed(struct cons_write_context *wctxt); +extern bool console_enter_unsafe(struct cons_write_context *wctxt); +extern bool console_exit_unsafe(struct cons_write_context *wctxt); extern bool console_try_acquire(struct cons_write_context *wctxt); extern bool console_release(struct cons_write_context *wctxt); diff --git a/kernel/printk/printk_nobkl.c b/kernel/printk/printk_nobkl.c index 7184a93a5b0d..3318a79a150a 100644 --- a/kernel/printk/printk_nobkl.c +++ b/kernel/printk/printk_nobkl.c @@ -947,6 +947,145 @@ static void cons_free_percpu_data(struct console *con) con->pcpu_data = NULL; } +/** + * console_can_proceed - Check whether printing can proceed + * @wctxt: The write context that was handed to the write function + * + * Returns: True if the state is correct. False if a handover + * has been requested or if the console was taken + * over. + * + * Must be invoked after the record was dumped into the assigned record + * buffer and at appropriate safe places in the driver. For unsafe driver + * sections see console_enter_unsafe(). + * + * When this function returns false then the calling context is not allowed + * to go forward and has to back out immediately and carefully. The buffer + * content is no longer trusted either and the console lock is no longer + * held. + */ +bool console_can_proceed(struct cons_write_context *wctxt) +{ + struct cons_context *ctxt = &ACCESS_PRIVATE(wctxt, ctxt); + struct console *con = ctxt->console; + struct cons_state state; + + cons_state_read(con, CON_STATE_CUR, &state); + /* Store it for analysis or reuse */ + copy_full_state(ctxt->old_state, state); + + /* Make sure this context is still the owner. */ + if (!cons_state_full_match(state, ctxt->state)) + return false; + + /* + * Having a safe point for take over and eventually a few + * duplicated characters or a full line is way better than a + * hostile takeover. Post processing can take care of the garbage. + * Continue if the requested priority is not sufficient. + */ + if (state.req_prio <= state.cur_prio) + return true; + + /* + * A console printer within an unsafe region is allowed to continue. + * It can perform the handover when exiting the safe region. Otherwise + * a hostile takeover will be necessary. + */ + if (state.unsafe) + return true; + + /* Release and hand over */ + cons_release(ctxt); + /* + * This does not check whether the handover succeeded. The + * outermost callsite has to make the final decision whether printing + * should continue or not (via reacquire, possibly hostile). The + * console is unlocked already so go back all the way instead of + * trying to implement heuristics in tons of places. + */ + return false; +} + +/** + * __console_update_unsafe - Update the unsafe bit in @con->atomic_state + * @wctxt: The write context that was handed to the write function + * + * Returns: True if the state is correct. False if a handover + * has been requested or if the console was taken + * over. + * + * Must be invoked before an unsafe driver section is entered. + * + * When this function returns false then the calling context is not allowed + * to go forward and has to back out immediately and carefully. The buffer + * content is no longer trusted either and the console lock is no longer + * held. + * + * Internal helper to avoid duplicated code + */ +static bool __console_update_unsafe(struct cons_write_context *wctxt, bool unsafe) +{ + struct cons_context *ctxt = &ACCESS_PRIVATE(wctxt, ctxt); + struct console *con = ctxt->console; + struct cons_state new; + + do { + if (!console_can_proceed(wctxt)) + return false; + /* + * console_can_proceed() saved the real state in + * ctxt->old_state + */ + copy_full_state(new, ctxt->old_state); + new.unsafe = unsafe; + + } while (!cons_state_try_cmpxchg(con, CON_STATE_CUR, &ctxt->old_state, &new)); + + copy_full_state(ctxt->state, new); + return true; +} + +/** + * console_enter_unsafe - Enter an unsafe region in the driver + * @wctxt: The write context that was handed to the write function + * + * Returns: True if the state is correct. False if a handover + * has been requested or if the console was taken + * over. + * + * Must be invoked before an unsafe driver section is entered. + * + * When this function returns false then the calling context is not allowed + * to go forward and has to back out immediately and carefully. The buffer + * content is no longer trusted either and the console lock is no longer + * held. + */ +bool console_enter_unsafe(struct cons_write_context *wctxt) +{ + return __console_update_unsafe(wctxt, true); +} + +/** + * console_exit_unsafe - Exit an unsafe region in the driver + * @wctxt: The write context that was handed to the write function + * + * Returns: True if the state is correct. False if a handover + * has been requested or if the console was taken + * over. + * + * Must be invoked before an unsafe driver section is exited. + * + * When this function returns false then the calling context is not allowed + * to go forward and has to back out immediately and carefully. The buffer + * content is no longer trusted either and the console lock is no longer + * held. + */ +bool console_exit_unsafe(struct cons_write_context *wctxt) +{ + return __console_update_unsafe(wctxt, false); +} + /** * cons_nobkl_init - Initialize the NOBKL console specific data * @con: Console to initialize