Received: by 2002:a25:b794:0:0:0:0:0 with SMTP id n20csp6006555ybh; Wed, 7 Aug 2019 15:28:22 -0700 (PDT) X-Google-Smtp-Source: APXvYqzYqfUDzJgTmzfSMwBHuBZODhI1ZMoNPB/xQ4rbX9q7BWorn4JETDGq7cQOl07eqm0YuMVk X-Received: by 2002:a63:6d6:: with SMTP id 205mr9974628pgg.262.1565216901938; Wed, 07 Aug 2019 15:28:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565216901; cv=none; d=google.com; s=arc-20160816; b=sygz+0HUiiAOyZCFXOSasIqKLNd5lr5bq2Ttg+yojmHSVZfJrx318ju9EZnogkD1pd /FXtzNehdT7TFwat/6HnCbY+hbVNSpMWjMSz1KL6tPlFYVzHAuGTMTBDjP7hfhSvF1QI lGtDFxSBDwn0wnqp1GwPnVHYWl0XaXEtxbS0Sl3VSqeo9hCLFzSthSMPCLQ1KGdlLrnM 6ltuy3xR27xytFwo87nZW7bVfJ0JVoW7I1pu1xiJYvS71H2T1AuMPS5ud5vyRfL73Yi2 epDqRy47sv/w+RmXlE3PH3/i6l4+kp/KOkyWOXZA8XZa4qOvuPdvxSqwDIuqkzwHgX2x 0bFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=+TENZh3KHcu9vLxU+atvSKsxTuQiwKeXIv9VLHAH9Bo=; b=kNXe+dxzGr69rx4LEfPYoP//tm79s25GQz+hn2xjpOu6D0HWpb9/x1Qr6/xS+IbdCC BAd2QNadEMDdgkp5MAnopS/D3WHLZUTYFO0+p9hm15jmu1U7dOt+OUqLathOQbl2AzH3 2I4YRrIG+S+HUR2ZctdKIQdkrgQwt3q3nVtU2Di7qmCILluYuf+1epEpXWK9Fyba69xn Ti9E7w8mR7oS/UpfKKS0OgYIGvcDooK13SJgJGYw0/IzHdywbcpl2pdWwVZgsIyzMh4t 2NZ0mGmNKHJbmlWDlre24nUVuU8hQBjqF5ox2X2isbq/FqTQUcJ32H2feKoMu+PCLpSv N0yg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t15si51405544pgv.546.2019.08.07.15.28.07; Wed, 07 Aug 2019 15:28:21 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389469AbfHGW1S (ORCPT + 99 others); Wed, 7 Aug 2019 18:27:18 -0400 Received: from Galois.linutronix.de ([193.142.43.55]:51810 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388848AbfHGW1S (ORCPT ); Wed, 7 Aug 2019 18:27:18 -0400 Received: from [5.158.153.52] (helo=g2noscherz.tec.linutronix.de.) by Galois.linutronix.de with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1hvUOa-0007mg-KE; Thu, 08 Aug 2019 00:27:02 +0200 From: John Ogness To: linux-kernel@vger.kernel.org Cc: Peter Zijlstra , Petr Mladek , Sergey Senozhatsky , Steven Rostedt , Linus Torvalds , Greg Kroah-Hartman , Andrea Parri , Thomas Gleixner , Sergey Senozhatsky , Brendan Higgins Subject: [RFC PATCH v4 4/9] printk-rb: initialize new descriptors as invalid Date: Thu, 8 Aug 2019 00:32:29 +0206 Message-Id: <20190807222634.1723-5-john.ogness@linutronix.de> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190807222634.1723-1-john.ogness@linutronix.de> References: <20190807222634.1723-1-john.ogness@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Initialize never-used descriptors as permanently invalid so there is no risk of the descriptor unexpectedly being determined as valid due to dataring head overflowing/wrapping. Signed-off-by: John Ogness --- kernel/printk/dataring.c | 42 +++++++++++++++++++++++++++----------- kernel/printk/dataring.h | 12 +++++++++++ kernel/printk/ringbuffer.c | 5 +++++ kernel/printk/ringbuffer.h | 7 ++++++- 4 files changed, 53 insertions(+), 13 deletions(-) diff --git a/kernel/printk/dataring.c b/kernel/printk/dataring.c index 6642e085a05d..345c46dba5bb 100644 --- a/kernel/printk/dataring.c +++ b/kernel/printk/dataring.c @@ -316,8 +316,8 @@ static unsigned long _dataring_pop(struct dataring *dr, * If dB reads from gA, then dD reads from fH. * If dB reads from gA, then dE reads from fE. * - * Note that if dB reads from gA, then dC cannot read from fC. - * Note that if dB reads from gA, then dD cannot read from fD. + * Note that if dB reads from gA, then dC cannot read from fC->nA. + * Note that if dB reads from gA, then dD cannot read from fC->nB. * * Relies on: * @@ -489,6 +489,30 @@ static bool get_new_lpos(struct dataring *dr, unsigned int size, } } +/** + * dataring_desc_init() - Initialize a descriptor to be permanently invalid. + * + * @desc: The descriptor to initialize. + * + * Setting a descriptor to be permanently invalid means that there is no risk + * of the descriptor later unexpectedly being determined as valid due to + * overflowing/wrapping of @head_lpos. + */ +void dataring_desc_init(struct dr_desc *desc) +{ + /* + * An unaligned @begin_lpos can never point to a data block and + * having the same value for @begin_lpos and @next_lpos is also + * invalid. + */ + + /* nA: */ + WRITE_ONCE(desc->begin_lpos, 1); + + /* nB: */ + WRITE_ONCE(desc->next_lpos, 1); +} + /** * dataring_push() - Reserve a data block in the data array. * @@ -568,20 +592,14 @@ char *dataring_push(struct dataring *dr, unsigned int size, if (!ret) { /* + * fC: + * * Force @desc permanently invalid to minimize risk * of the descriptor later unexpectedly being * determined as valid due to overflowing/wrapping of - * @head_lpos. An unaligned @begin_lpos can never - * point to a data block and having the same value - * for @begin_lpos and @next_lpos is also invalid. + * @head_lpos. */ - - /* fC: */ - WRITE_ONCE(desc->begin_lpos, 1); - - /* fD: */ - WRITE_ONCE(desc->next_lpos, 1); - + dataring_desc_init(desc); return NULL; } /* fE: */ diff --git a/kernel/printk/dataring.h b/kernel/printk/dataring.h index 346a455a335a..413ee95f4dd6 100644 --- a/kernel/printk/dataring.h +++ b/kernel/printk/dataring.h @@ -43,6 +43,17 @@ struct dr_desc { unsigned long next_lpos; }; +/* + * Initialize a descriptor to be permanently invalid so there is no risk + * of the descriptor later unexpectedly being determined as valid due to + * overflowing/wrapping of @head_lpos. + */ +#define __DR_DESC_INITIALIZER \ + { \ + .begin_lpos = 1, \ + .next_lpos = 1, \ + } + /** * struct dataring - A data ringbuffer with support for entry IDs. * @@ -90,6 +101,7 @@ void dataring_datablock_setid(struct dataring *dr, struct dr_desc *desc, struct dr_datablock *dataring_getdatablock(struct dataring *dr, struct dr_desc *desc, int *size); bool dataring_datablock_isvalid(struct dataring *dr, struct dr_desc *desc); +void dataring_desc_init(struct dr_desc *desc); void dataring_desc_copy(struct dr_desc *dst, struct dr_desc *src); #endif /* _KERNEL_PRINTK_DATARING_H */ diff --git a/kernel/printk/ringbuffer.c b/kernel/printk/ringbuffer.c index b9fc13597b10..9be841761ea2 100644 --- a/kernel/printk/ringbuffer.c +++ b/kernel/printk/ringbuffer.c @@ -345,6 +345,11 @@ static bool assign_desc(struct prb_reserved_entry *e) if (i < DESCS_COUNT(rb)) { d = &rb->descs[i]; atomic_long_set(&d->id, i); + /* + * Initialize the new descriptor such that + * it is permanently invalid. + */ + dataring_desc_init(&d->desc); break; } } diff --git a/kernel/printk/ringbuffer.h b/kernel/printk/ringbuffer.h index 9fe54a09fbc2..462b4d3a3ee2 100644 --- a/kernel/printk/ringbuffer.h +++ b/kernel/printk/ringbuffer.h @@ -178,7 +178,12 @@ struct dr_desc *prb_getdesc(unsigned long id, void *arg); char _##name##_data[(1 << ((avgdatabits) + (descbits))) + \ sizeof(long)] \ __aligned(__alignof__(long)); \ -struct prb_desc _##name##_descs[1 << (descbits)]; \ +struct prb_desc _##name##_descs[1 << (descbits)] = { \ + { \ + .id = ATOMIC_LONG_INIT(0), \ + .desc = __DR_DESC_INITIALIZER, \ + }, \ + }; \ struct printk_ringbuffer name = { \ .desc_count_bits = descbits, \ .descs = &_##name##_descs[0], \ -- 2.20.1