Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp3737191ybl; Tue, 20 Aug 2019 01:16:32 -0700 (PDT) X-Google-Smtp-Source: APXvYqyXOfTle9lDmkP9AmmWxpqNXA5GmYjL73fxmqJm9TQ18NGoPfClpjxFmM/XEPEeAKF8O2Jn X-Received: by 2002:aa7:9a86:: with SMTP id w6mr28437223pfi.60.1566288992038; Tue, 20 Aug 2019 01:16:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566288992; cv=none; d=google.com; s=arc-20160816; b=AAnEMEjfnQAZPPBt+cZTf8xOcGJSchhYTluEMS4X4/hX1hU1QBfQ9ALknW6zkVcNt1 kWLjxPnD3qc4ORc0xgWpmQyAu766ACbwowGVovcgMjFdPuBTYwkCuLIy96kTOdv7C0Os or9LqIRdiikjhJuLzgfxb6om8UPDY6MGIZPk4KkhTl7o28v4gCkKxfY/vOlENfz5iulV Ds1ZlEU9jHIKEmEmfuBTT1M/0AQVsDsS3O1B638RKzOk/KElttXM5VHf6ok1RMBSRI9c 0C/jNYfeOKqt//bofGj/nAK0ndF9Z5cMFZn4cChFIysbbPysQgMtufRjjifGV96DiGiv vOGw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=liUoVCvH2j2bqHK18mcuxWKJHvhQwvNs3L/7AZofoRc=; b=RlSfwfHVdDU3cD8V+qgPjIyxBNUMCGKkusVXhowek2NdhtKTsC2pMSNUyKm2MELEq1 5kQXRT9GstGEHWkuWOzEnU+VDGA7bC8VL3f0SGSc/nQRjh4UuYOz1ectAwQQd52DdJCc /4Mri/7JK7k2jxVitwXipCHu3mcLeDuMwYlbhjb7PK9tlhQkgkN5NdfakcAo1zYpP2ja OS+z0VyzZ2YczY73hG43bQdB4VPWyx/4kdr2iGO+Jqn6/415+DH8wrFZXd0MxptggQ0E SatTMAhgXCcuKTp/DaeaRYbtLMcgXfFq1Z/dU+L7qr0Iz/Q1hJH7q1gAwhWkQkj2254x j8IA== 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 e2si11895270pgm.99.2019.08.20.01.16.16; Tue, 20 Aug 2019 01:16:32 -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 S1729352AbfHTIPV (ORCPT + 99 others); Tue, 20 Aug 2019 04:15:21 -0400 Received: from mx2.suse.de ([195.135.220.15]:50320 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1729150AbfHTIPV (ORCPT ); Tue, 20 Aug 2019 04:15:21 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id C8DF7AEA1; Tue, 20 Aug 2019 08:15:19 +0000 (UTC) Date: Tue, 20 Aug 2019 10:15:18 +0200 From: Petr Mladek To: John Ogness Cc: linux-kernel@vger.kernel.org, Andrea Parri , Sergey Senozhatsky , Sergey Senozhatsky , Steven Rostedt , Brendan Higgins , Peter Zijlstra , Thomas Gleixner , Linus Torvalds , Greg Kroah-Hartman Subject: numlist_pop(): Re: [RFC PATCH v4 1/9] printk-rb: add a new printk ringbuffer implementation Message-ID: <20190820081518.3r3cagzggtifsvhz@pathway.suse.cz> References: <20190807222634.1723-1-john.ogness@linutronix.de> <20190807222634.1723-2-john.ogness@linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190807222634.1723-2-john.ogness@linutronix.de> User-Agent: NeoMutt/20170912 (1.9.0) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu 2019-08-08 00:32:26, John Ogness wrote: > --- /dev/null > +++ b/kernel/printk/numlist.c > +/** > + * numlist_pop() - Remove the oldest node from the list. > + * > + * @nl: The numbered list from which to remove the tail node. > + * > + * The tail node can only be removed if two conditions are satisfied: > + * > + * * The node is not the only node on the list. > + * * The node is not busy. > + * > + * If, during this function, another task removes the tail, this function > + * will try again with the new tail. > + * > + * Return: The removed node or NULL if the tail node cannot be removed. > + */ > +struct nl_node *numlist_pop(struct numlist *nl) > +{ > + unsigned long tail_id; > + unsigned long next_id; > + unsigned long r; > + > + /* cA: #1 */ > + tail_id = atomic_long_read(&nl->tail_id); > + > + for (;;) { > + /* cB */ > + while (!numlist_read(nl, tail_id, NULL, &next_id)) { > + /* > + * @tail_id is invalid. Try again with an > + * updated value. > + */ > + > + cpu_relax(); > + > + /* cA: #2 */ > + tail_id = atomic_long_read(&nl->tail_id); > + } The above while-cycle basically does the same as the upper for-cycle. It tries again with freshly loaded nl->tail_id. The following code looks easier to follow: do { tail_id = atomic_long_read(&nl->tail_id); /* * Read might fail when the tail node has been removed * and reused in parallel. */ if (!numlist_read(nl, tail_id, NULL, &next_id)) continue; /* Make sure the node is not the only node on the list. */ if (next_id == tail_id) return NULL; /* cC: Make sure the node is not busy. */ if (nl->busy(tail_id, nl->busy_arg)) return NULL; while (atomic_long_cmpxchg_relaxed(&nl->tail_id, tail_id, next_id) != tail_id); /* This should never fail. The node is ours. */ return nl->node(tail_id, nl->node_arg); > + /* Make sure the node is not the only node on the list. */ > + if (next_id == tail_id) > + return NULL; > + > + /* > + * cC: > + * > + * Make sure the node is not busy. > + */ > + if (nl->busy(tail_id, nl->busy_arg)) > + return NULL; > + > + r = atomic_long_cmpxchg_relaxed(&nl->tail_id, > + tail_id, next_id); > + if (r == tail_id) > + break; > + > + /* cA: #3 */ > + tail_id = r; > + } > + > + return nl->node(tail_id, nl->node_arg); If I get it correctly, the above nl->node() call should never fail. The node has been removed from the list and nobody else could touch it. It is pretty useful information and it might be worth mention it in a comment. Best Regards, Petr PS: I am scratching my head around the patchset. I'll try Peter's approach and comment independent things is separate mails.