Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp1038852rwb; Tue, 27 Sep 2022 07:42:08 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4V9E+Pxpadd0B+JagfNMGpeXOk8WDJ8POn4gjbjVF3cMEncLYGPUdfNibD0Nuc8f/QKW2L X-Received: by 2002:aa7:c617:0:b0:457:2d14:dc5e with SMTP id h23-20020aa7c617000000b004572d14dc5emr2196246edq.408.1664289728180; Tue, 27 Sep 2022 07:42:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664289728; cv=none; d=google.com; s=arc-20160816; b=bImdJayrPyHdRhKNstsUtgWs6hE4qNA/Hml4+8tENoslMPEuu1e93gH64iRkksLQxa Uix3sHOcvBb1Y3MEzU4H4205M0oskEYih7jdL2gG9E6gK9d5ud8JtgrxT3n3ZB1rYZbh TtZI5PKmDjjFXYifQnyjDqIO3EG8ge79lCdsVMOE9BB/B2Ocaxw0j8vI80lPboN8+VQI ii1BMQI15q7BamJS/ARZba6a4SKPn288NJ60zTb3LTgzDVH02YT28DbiT0eFFtmRCId0 IjJlN84huHumUZQ1B6XxD5RSIJEXDnf4bhhkb0e0cjg6M845+uvCGLq+M7qi9QnY4WEu jOvw== 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=doHCftpEX9Bx6E/ShqAhQuaabtZ/kVoIscZHD6AATYw=; b=kVzUknECp2p08NCgBH/VZUxrfxUgw92HvKj19GY5mtZHmJ3nZstZm0EnFpcXCHhKxY Aad+FJiFMYDYonvp17nIiY1q6KpW2vw/PwPsXsYiL4cKMhKt1hEXFO81OyTPnt01CiRd VtKMnbZLKg3GXGGJYxWsP5KjvDXy6tT4yqBWnIM7FblweMSSVHA+mPR5hBNU0hdFwP+F USRLkAhMXprQlSryyY7z0uaoOJ0u9nvXhtmProHtwjaai+8hsy0BKZsmyQghdMVjP5aw fVCUQ//ICmtfgx6Xbv5gXtS+xrQ3Km7Pp2OuUOYAJB7OTMEjWRDtH96iKxG3F40DkeUA kLIQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=PmGqo8G8; 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 cs20-20020a170906dc9400b0077b08d909fbsi1397034ejc.53.2022.09.27.07.41.40; Tue, 27 Sep 2022 07:42:08 -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=PmGqo8G8; 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 S232105AbiI0Ntc (ORCPT + 99 others); Tue, 27 Sep 2022 09:49:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37414 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232620AbiI0Nt2 (ORCPT ); Tue, 27 Sep 2022 09:49:28 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 75EA612477B for ; Tue, 27 Sep 2022 06:49:26 -0700 (PDT) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1664286565; 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=doHCftpEX9Bx6E/ShqAhQuaabtZ/kVoIscZHD6AATYw=; b=PmGqo8G8nmlq6Qad5m4UnR2UyRR4LcnFeFOQ+8meMgFbcFJH/Vh7CbOPoaTJrtwr908frv tJ5QbIAG7iAT0oJ99YlJJCpcPWCTj2z5+YJaY9RBZoJL63tm38PXqissUVTc/Ovj/Utk/B 2WmVpuMEeioqe1CPD+C60fPVnK0RCSmuyzSNf/NsAJySOC8tyH+iSqQyHVo4fkOWA5w4WN NwarhCoL93qXqy8+mPHV3nQl0wFcuhreYz+VPb1ssEa8kcAB3hhjmlk9ybMZQOyLf4B/yG dGaeitVPmvzW/AoybcCzk2NFN1fd9eKS8jkYDbWa72ArRJl6roRsJzW2L4AcQw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1664286565; 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=doHCftpEX9Bx6E/ShqAhQuaabtZ/kVoIscZHD6AATYw=; b=a04GMzPYCq8NE/6O1MMtwS6LZBlqAlYSYyc/rNOUoXr4XTOe13So0ftuBK5VP9Y/Quor4l P3deeF0oCQ3fykDA== To: Thomas Gleixner , LKML Cc: Petr Mladek , Sergey Senozhatsky , Steven Rostedt , Linus Torvalds , Peter Zijlstra , "Paul E. McKenney" , Daniel Vetter , Greg Kroah-Hartman , Helge Deller , Jason Wessel , Daniel Thompson Subject: Re: [patch RFC 20/29] printk: Add non-BKL console acquire/release logic In-Reply-To: <20220910222301.539158418@linutronix.de> References: <20220910221947.171557773@linutronix.de> <20220910222301.539158418@linutronix.de> Date: Tue, 27 Sep 2022 15:55:24 +0206 Message-ID: <87tu4tkjvf.fsf@jogness.linutronix.de> MIME-Version: 1.0 Content-Type: text/plain 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 Below is a fix that was used for the LPC2022 demo so that a hostile takeover upon timeout is possible. On 2022-09-11, Thomas Gleixner wrote: > --- a/kernel/printk/printk_nobkl.c > +++ b/kernel/printk/printk_nobkl.c > +/** > + * __cons_try_acquire - Try to acquire the console for printk output > + * @ctxt: Pointer to an aquire context which contains > + * all information about the acquire mode > + * > + * Returns: True if the acquire was successful. False on fail. > + * > + * In case of success @ctxt->state contains the acquisition > + * state. > + * > + * In case of fail @ctxt->old_state contains the state > + * which was read from @con->state for analysis by the caller. > + */ > +static bool __cons_try_acquire(struct cons_context *ctxt) > +{ > + struct console *con = ctxt->console; > + unsigned int cpu = smp_processor_id(); > + struct cons_state old, new; > + > + if (WARN_ON_ONCE(!(con->flags & CON_NO_BKL))) > + return false; > + > + cons_state_read(con, STATE_REAL, &old); > + > +again: > + if (cons_check_panic()) > + return false; > + > + /* Preserve it for the caller and for spinwait */ > + copy_full_state(ctxt->old_state, old); > + > + if (!cons_state_ok(old)) > + return false; > + > + /* Set up the new state for takeover */ > + copy_full_state(new, old); > + new.locked = 1; > + new.thread = ctxt->thread; > + new.cur_prio = ctxt->prio; > + new.req_prio = CONS_PRIO_NONE; > + new.cpu = cpu; > + > + /* Attempt to acquire it directly if unlocked */ > + if (!old.locked) { > + if (!cons_state_try_cmpxchg(con, STATE_REAL, &old, &new)) > + goto again; > + > + ctxt->hostile = false; > + copy_full_state(ctxt->state, new); > + goto success; > + } > + > + /* > + * Give up if the calling context is the printk thread. The > + * atomic writer will wake the thread when it is done with > + * the important output. > + */ > + if (ctxt->thread) > + return false; > + > + /* > + * If the active context is on the same CPU then there is > + * obviously no handshake possible. > + */ > + if (old.cpu == cpu) > + goto check_hostile; > + > + /* > + * If the caller did not request spin-waiting or a request with the > + * same or higher priority is pending then check whether a hostile > + * takeover is due. > + */ > + if (!ctxt->spinwait || old.req_prio >= ctxt->prio) > + goto check_hostile; > + > + /* Proceed further with spin acquire */ > + if (!cons_setup_handover(ctxt)) > + return false; > + > + /* > + * Setup the request in state[REAL]. Hand in the state, which was > + * used to make the decision to spinwait above, for comparison. > + */ > + if (!cons_setup_request(ctxt, old)) > + return false; > + > + /* Now spin on it */ > + if (!cons_try_acquire_spin(ctxt)) > + return false; Instead of returning false here, it needs to: goto check_hostile; cons_try_acquire_spin() may have failed due to a timeout, in which case a hostile takeover might be necessary. > +success: > + /* Common updates on success */ > + return true; > + > +check_hostile: > + if (!ctxt->hostile) > + return false; > + > + if (!cons_state_try_cmpxchg(con, STATE_REAL, &old, &new)) > + goto again; > + > + ctxt->hostile = true; > + copy_full_state(ctxt->state, new); > + goto success; > +} John Ogness