From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 53E75C433EF for ; Fri, 8 Apr 2022 00:49:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=U7iyOwBt+vun8yBBNqeFX/+HoKmLjcL8FGXlFbI3QjM=; b=lIv6eI6++g071xA183pnDTvi6G kuZSJoKF/w/j5EciancPPrU3g+JorcPlghOrPjeonMefu5wUFScsTQbfv3X4TKM0nSADXD1YCp0v7 q7GKC/yQ4g7FUbSppKkHMED+Aq38TWBFtIzxx6A1Q6YBhlnYYRC3hsd+dlBjXWF6jhaDNH5yCpo1J sDMge3DIogDupKutVPPHHz8BlJnKyusKPsug2FpUXcnnpCfJWujosCwRBJW6KrAWHdlDdbH5GPMTB +ajlNmNVdiE8NtbUOnIrbt0xZZeIDqWDOg6UiCa7bsJTTLCkvLK9GppYthPd188dfNO4rsWpDkQvg DdQAL9Bw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nccni-00ESXe-L0; Fri, 08 Apr 2022 00:48:34 +0000 Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nccne-00ESWJ-HN for linux-arm-kernel@lists.infradead.org; Fri, 08 Apr 2022 00:48:32 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id y32-20020a25ad20000000b006339fb8e18cso5495500ybi.9 for ; Thu, 07 Apr 2022 17:48:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=ithdVlozh5jFC5LDPPJEaLYRLxUsUDLKrVaBbck8woI=; b=omXiV0brS6SC4fAsszjr3vh7SFO8aEPobqoxOFDY5gSXD4t7YUUrTf7kvD2voWFdGR 0zfKeLrkPKhiVG1D/WWtxb29Iv343o7BnI/AvfEqwHDPJmrMzF1O3Vnpao8P0/fXyCgx nY3TY7bOcVwFNQFpshyTnzdQgZOwX7Q1RBQCrzsncOpPhEbEyqD2cFwzhTN4wfmDFPs5 MekcHDrPOPryJQWDeii7ymb0dhrvpcoLFAtJ70AuD4FM5oNiUWAjY4U2/aOY8j1hxnrv 2Ha/emP+PaKxv0PdcVrgerBl0AxigiyEeU0+NcNeFRn8UX2MwLAuPhkTGPDKgIXxlgUI 6olQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=ithdVlozh5jFC5LDPPJEaLYRLxUsUDLKrVaBbck8woI=; b=HudoKssakfKcA49yMf0LsYpyIia80Pa9CY+l2gRyVVarqYzJo8tdYFkIca8KOl5BfK 1J9xNHGPqfRaNWVhC12/rKkVQE7ocCEOMo7I43BlotJ9ho0jtspdgQ+lnBA1+MHuctR+ oRrQn6ZkwA6zdStPbR9xklnr76kifdjNeThqR9hU2QEq+G4mQLlMfgS0cZX+MY6uC+bI iyWbySet1/5nf8ZgWn9UsS9mUg6RolQZGmzUi523aeYq8R/vRz7LKvJzlod1JvnsJVrd RGXl1faAEJWyA982HXXrYUTDdNg3ofT/PhTLLXlstAWo/9g7Sm1NqvYUgqA+BB20GqG6 PeNQ== X-Gm-Message-State: AOAM530eBx0No+qG/ZEfc7trg5FVR8MFFXmZmJ9djLijisI6DnSOA20U 98TCtdw0ZUYmy1grjO769rB/9d8mbIjGjnc= X-Google-Smtp-Source: ABdhPJxZfTDhCmiUWxemMxtmOYKdXt8UIDGz1EMPXmcT+4UwQKXx3DIeNZNwfDy6tGznTHCK1StYatP5YLU97sQ= X-Received: from saravanak.san.corp.google.com ([2620:15c:2d:3:b203:eb20:896d:ef2f]) (user=saravanak job=sendgmr) by 2002:a05:6902:1d1:b0:636:fa07:4b9a with SMTP id u17-20020a05690201d100b00636fa074b9amr12044080ybh.590.1649378905528; Thu, 07 Apr 2022 17:48:25 -0700 (PDT) Date: Thu, 7 Apr 2022 17:48:21 -0700 In-Reply-To: Message-Id: <20220408004821.4010969-1-saravanak@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.35.1.1178.g4f1659d476-goog Subject: Re: [PATCH v2] printk: ringbuffer: Improve prb_next_seq() performance From: Saravana Kannan To: pmladek@suse.com, Matthias Brugger Cc: Chunlei.wang@mediatek.com, john.ogness@linutronix.de, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mediatek@lists.infradead.org, rostedt@goodmis.org, senozhatsky@chromium.org, kernel-team@android.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220407_174830_621257_83258CD1 X-CRM114-Status: GOOD ( 34.86 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org > Petr Mladek wrote: > > prb_next_seq() always iterates from the first known sequence number. > In the worst case, it might loop 8k times for 256kB buffer, > 15k times for 512kB buffer, and 64k times for 2MB buffer. > > It was reported that pooling and reading using syslog interface > might occupy 50% of CPU. > > Speedup the search by storing @id of the last finalized descriptor. > > The loop is still needed because the @id is stored and read in the best > effort way. An atomic variable is used to keep the @id consistent. > But the stores and reads are not serialized against each other. > The descriptor could get reused in the meantime. The related sequence > number will be used only when it is still valid. > > An invalid value should be read _only_ when there is a flood of messages > and the ringbuffer is rapidly reused. The performance is the least > problem in this case. > > Reported-by: Chunlei Wang > Signed-off-by: Petr Mladek > --- > Changes against v1: > > + Read @seq by the last finalized @id directly in prb_next_seq() (John) > > kernel/printk/printk_ringbuffer.c | 49 ++++++++++++++++++++++++++++--- > kernel/printk/printk_ringbuffer.h | 2 ++ > 2 files changed, 47 insertions(+), 4 deletions(-) > > diff --git a/kernel/printk/printk_ringbuffer.c b/kernel/printk/printk_ringbuffer.c > index 8a7b7362c0dd..24f47fbefbb5 100644 > --- a/kernel/printk/printk_ringbuffer.c > +++ b/kernel/printk/printk_ringbuffer.c > @@ -474,8 +474,10 @@ static enum desc_state desc_read(struct prb_desc_ring *desc_ring, > * state has been re-checked. A memcpy() for all of @desc > * cannot be used because of the atomic_t @state_var field. > */ > - memcpy(&desc_out->text_blk_lpos, &desc->text_blk_lpos, > - sizeof(desc_out->text_blk_lpos)); /* LMM(desc_read:C) */ > + if (desc_out) { > + memcpy(&desc_out->text_blk_lpos, &desc->text_blk_lpos, > + sizeof(desc_out->text_blk_lpos)); /* LMM(desc_read:C) */ > + } > if (seq_out) > *seq_out = info->seq; /* also part of desc_read:C */ > if (caller_id_out) > @@ -1449,6 +1451,9 @@ static void desc_make_final(struct prb_desc_ring *desc_ring, unsigned long id) > > atomic_long_cmpxchg_relaxed(&d->state_var, prev_state_val, > DESC_SV(id, desc_finalized)); /* LMM(desc_make_final:A) */ > + > + /* Best effort to remember the last finalized @id. */ > + atomic_long_set(&desc_ring->last_finalized_id, id); > } > > /** > @@ -1657,7 +1662,12 @@ void prb_commit(struct prb_reserved_entry *e) > */ > void prb_final_commit(struct prb_reserved_entry *e) > { > + struct prb_desc_ring *desc_ring = &e->rb->desc_ring; > + > _prb_commit(e, desc_finalized); > + > + /* Best effort to remember the last finalized @id. */ > + atomic_long_set(&desc_ring->last_finalized_id, e->id); > } > > /* > @@ -2005,9 +2015,39 @@ u64 prb_first_valid_seq(struct printk_ringbuffer *rb) > */ > u64 prb_next_seq(struct printk_ringbuffer *rb) > { > - u64 seq = 0; > + struct prb_desc_ring *desc_ring = &rb->desc_ring; > + enum desc_state d_state; > + unsigned long id; > + u64 seq; > + > + /* Check if the cached @id still points to a valid @seq. */ > + id = atomic_long_read(&desc_ring->last_finalized_id); > + d_state = desc_read(desc_ring, id, NULL, &seq, NULL); > > - /* Search forward from the oldest descriptor. */ > + if (d_state == desc_finalized || d_state == desc_reusable) { > + /* > + * Begin searching after the last finalized record. > + * > + * On 0, the search must begin at 0 because of hack#2 > + * of the bootstrapping phase it is not known if a > + * record at index 0 exists. > + */ > + if (seq != 0) > + seq++; > + } else { > + /* > + * The information about the last finalized sequence number > + * has gone. It should happen only when there is a flood of > + * new messages and the ringbuffer is rapidly recycled. > + * Give up and start from the beginning. > + */ > + seq = 0; > + } > + > + /* > + * The information about the last finalized @seq might be inaccurate. > + * Search forward to find the current one. > + */ > while (_prb_read_valid(rb, &seq, NULL, NULL)) > seq++; > > @@ -2044,6 +2084,7 @@ void prb_init(struct printk_ringbuffer *rb, > rb->desc_ring.infos = infos; > atomic_long_set(&rb->desc_ring.head_id, DESC0_ID(descbits)); > atomic_long_set(&rb->desc_ring.tail_id, DESC0_ID(descbits)); > + atomic_long_set(&rb->desc_ring.last_finalized_id, DESC0_ID(descbits)); > > rb->text_data_ring.size_bits = textbits; > rb->text_data_ring.data = text_buf; > diff --git a/kernel/printk/printk_ringbuffer.h b/kernel/printk/printk_ringbuffer.h > index 73cc80e01cef..18cd25e489b8 100644 > --- a/kernel/printk/printk_ringbuffer.h > +++ b/kernel/printk/printk_ringbuffer.h > @@ -75,6 +75,7 @@ struct prb_desc_ring { > struct printk_info *infos; > atomic_long_t head_id; > atomic_long_t tail_id; > + atomic_long_t last_finalized_id; > }; I really know nothing about this code, but while looking around kernel/printk/ I noticed kernel/printk/printk.c has these lines in log_buf_vmcoreinfo_setup(). VMCOREINFO_STRUCT_SIZE(prb_desc_ring); VMCOREINFO_OFFSET(prb_desc_ring, count_bits); VMCOREINFO_OFFSET(prb_desc_ring, descs); VMCOREINFO_OFFSET(prb_desc_ring, infos); VMCOREINFO_OFFSET(prb_desc_ring, head_id); VMCOREINFO_OFFSET(prb_desc_ring, tail_id); Would this header file change also require a change to that location? Something like VMCOREINFO_OFFSET(prb_desc_ring, head_id); VMCOREINFO_OFFSET(prb_desc_ring, tail_id); + VMCOREINFO_OFFSET(prb_desc_ring, last_finalized_id); -Saravana > > /* > @@ -258,6 +259,7 @@ static struct printk_ringbuffer name = { \ > .infos = &_##name##_infos[0], \ > .head_id = ATOMIC_INIT(DESC0_ID(descbits)), \ > .tail_id = ATOMIC_INIT(DESC0_ID(descbits)), \ > + .last_finalized_id = ATOMIC_INIT(DESC0_ID(descbits)), \ > }, \ > .text_data_ring = { \ > .size_bits = (avgtextbits) + (descbits), \ > -- > 2.26.2 > _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel