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 X-Spam-Level: X-Spam-Status: No, score=-1.0 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1BD04C04AB6 for ; Fri, 31 May 2019 18:58:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 004BF26E25 for ; Fri, 31 May 2019 18:58:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726683AbfEaS6V (ORCPT ); Fri, 31 May 2019 14:58:21 -0400 Received: from mx2.suse.de ([195.135.220.15]:49128 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726439AbfEaS6U (ORCPT ); Fri, 31 May 2019 14:58:20 -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 8C600AEA3; Fri, 31 May 2019 18:58:19 +0000 (UTC) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit Date: Fri, 31 May 2019 20:58:19 +0200 From: Roman Penyaev To: Peter Zijlstra Cc: azat@libevent.org, akpm@linux-foundation.org, viro@zeniv.linux.org.uk, torvalds@linux-foundation.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v3 06/13] epoll: introduce helpers for adding/removing events to uring In-Reply-To: <20190531165144.GE2606@hirez.programming.kicks-ass.net> References: <20190516085810.31077-1-rpenyaev@suse.de> <20190516085810.31077-7-rpenyaev@suse.de> <20190531095607.GC17637@hirez.programming.kicks-ass.net> <274e29d102133f3be1f309c66cb0af36@suse.de> <20190531125636.GZ2606@hirez.programming.kicks-ass.net> <98e74ceeefdffc9b50fb33e597d270f7@suse.de> <20190531165144.GE2606@hirez.programming.kicks-ass.net> Message-ID: <9e13f80872e5b6c96e9cd3343e27b1f1@suse.de> X-Sender: rpenyaev@suse.de User-Agent: Roundcube Webmail Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org On 2019-05-31 18:51, Peter Zijlstra wrote: > On Fri, May 31, 2019 at 04:21:30PM +0200, Roman Penyaev wrote: > >> The ep_add_event_to_uring() is lockless, thus I can't increase tail >> after, >> I need to reserve the index slot, where to write to. I can use shadow >> tail, >> which is not seen by userspace, but I have to guarantee that tail is >> updated >> with shadow tail *after* all callers of ep_add_event_to_uring() are >> left. >> That is possible, please see the code below, but it adds more >> complexity: >> >> (code was tested on user side, thus has c11 atomics) >> >> static inline void add_event__kernel(struct ring *ring, unsigned bit) >> { >> unsigned i, cntr, commit_cntr, *item_idx, tail, old; >> >> i = __atomic_fetch_add(&ring->cntr, 1, __ATOMIC_ACQUIRE); >> item_idx = &ring->user_itemsindex[i % ring->nr]; >> >> /* Update data */ >> *item_idx = bit; >> >> commit_cntr = __atomic_add_fetch(&ring->commit_cntr, 1, >> __ATOMIC_RELEASE); >> >> tail = ring->user_header->tail; >> rmb(); >> do { >> cntr = ring->cntr; >> if (cntr != commit_cntr) >> /* Someone else will advance tail */ >> break; >> >> old = tail; >> >> } while ((tail = >> __sync_val_compare_and_swap(&ring->user_header->tail, old, cntr)) != >> old); >> } > > Yes, I'm well aware of that particular problem (see > kernel/events/ring_buffer.c:perf_output_put_handle for instance). I'll take a look, thanks. > But like you show, it can be done. It also makes the thing wait-free, > as > opposed to merely lockless. You think it's better? I did not like this variant from the very beginning because of the unnecessary complexity. But maybe you're right. No busy loops on user side makes it wait-free. And also I can avoid c11 in kernel using cmpxchg along with atomic_t. -- Roman