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=-17.6 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,NICE_REPLY_A, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_1 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 9678FC4708A for ; Thu, 27 May 2021 06:53:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 797AE613AA for ; Thu, 27 May 2021 06:53:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229635AbhE0Gyt (ORCPT ); Thu, 27 May 2021 02:54:49 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:23566 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229838AbhE0Gyr (ORCPT ); Thu, 27 May 2021 02:54:47 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1622098394; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=tbeJSxErj2lDV+ffr5QaXbXOERJDDIrzIVBIxT+6PUQ=; b=KOF0OJOmc/Y3eSi1E4hGHYXRtqa7xf4n2HLB/Ud01HLzbAGeU4713k5LCQW2dexL2BUQpY Vv7/cn+GWb8Qc2Rob3RQzplQLpWUKLDybt0nBwEWetAL+te6A2lSXBiw505ZrroBUd+QKu HgMJPKHQqhWhJ6XNR0GsZCtAfiY0JGU= Received: from mail-pj1-f71.google.com (mail-pj1-f71.google.com [209.85.216.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-532-OdDBa7JhPCi8q9jwyaLoTg-1; Thu, 27 May 2021 02:53:12 -0400 X-MC-Unique: OdDBa7JhPCi8q9jwyaLoTg-1 Received: by mail-pj1-f71.google.com with SMTP id w12-20020a17090a528cb029015d7f990752so1989858pjh.0 for ; Wed, 26 May 2021 23:53:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=tbeJSxErj2lDV+ffr5QaXbXOERJDDIrzIVBIxT+6PUQ=; b=HmWMICMbvM4QnxejEl8z3IsBQZ+6bln8+S/B3q5V3bb5FAceXTO1jODXInazHqm7Tl orWNKWfFjvyy4x2/h+nJ9bSJmSCJXlHL+TcCg9dz1JGfuf8J2VA1RNUXVkZO8d65tgZZ FTQN9WBrCZt9eHVgnXzC8DNzFn0url7FnOsSwWVB5b+QULsQQJPFlPZArGj/itaiVGkW +WOi6xzPPx4pTjGYesFd/oYt/mNbPhh3I7GEXJSsF74luP8DcsnNhZe7N0EBF0HmYjnv QcpftybZzxAITsqYbp06fVswS+HjBuVn7i1pvIBvidHig19sjg740AtlBlZLQRnpCKnR ymRA== X-Gm-Message-State: AOAM53350SOVFeSPBi0lq+mNqGkBR2zPO/Ms5wlxrWHPJD9Kcb8fWpaS 3vyRA6lD29z1mz3YWBQgh9Mk4tREuL9/ESlpw5y4bhV12GuwDEQ7P8MmygteXjgxMfX4VjoeOms QGrCAbZac630DJwAHPsAXL4y2 X-Received: by 2002:a63:935b:: with SMTP id w27mr2403406pgm.264.1622098391341; Wed, 26 May 2021 23:53:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwbxRpvRjQPr6v5uVQ848wKew2CiohrQQ7CN8mymILSXGnMXBRVDCOnrMW6FbkBE6RAKnPk9A== X-Received: by 2002:a63:935b:: with SMTP id w27mr2403380pgm.264.1622098391079; Wed, 26 May 2021 23:53:11 -0700 (PDT) Received: from wangxiaodeMacBook-Air.local ([209.132.188.80]) by smtp.gmail.com with ESMTPSA id b7sm957003pfv.149.2021.05.26.23.53.07 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 26 May 2021 23:53:10 -0700 (PDT) Subject: Re: [PATCH net-next] ptr_ring: make __ptr_ring_empty() checking more reliable To: Yunsheng Lin , davem@davemloft.net, kuba@kernel.org Cc: will@kernel.org, peterz@infradead.org, paulmck@kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, mst@redhat.com, brouer@redhat.com References: <1622032173-11883-1-git-send-email-linyunsheng@huawei.com> <25a6b73d-06ec-fe07-b34c-10fea709e055@huawei.com> From: Jason Wang Message-ID: <51bc1c38-da20-1090-e3ef-1972f28adfee@redhat.com> Date: Thu, 27 May 2021 14:53:02 +0800 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.10.2 MIME-Version: 1.0 In-Reply-To: <25a6b73d-06ec-fe07-b34c-10fea709e055@huawei.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 在 2021/5/27 下午2:07, Yunsheng Lin 写道: > On 2021/5/27 12:57, Jason Wang wrote: >> 在 2021/5/26 下午8:29, Yunsheng Lin 写道: >>> Currently r->queue[] is cleared after r->consumer_head is moved >>> forward, which makes the __ptr_ring_empty() checking called in >>> page_pool_refill_alloc_cache() unreliable if the checking is done >>> after the r->queue clearing and before the consumer_head moving >>> forward. >>> >>> Move the r->queue[] clearing after consumer_head moving forward >>> to make __ptr_ring_empty() checking more reliable. >> >> If I understand this correctly, this can only happens if you run __ptr_ring_empty() in parallel with ptr_ring_discard_one(). > Yes. > >> I think those two needs to be serialized. Or did I miss anything? > As the below comment in __ptr_ring_discard_one, if the above is true, I > do not think we need to keep consumer_head valid at all times, right? > > > /* Note: we must keep consumer_head valid at all times for __ptr_ring_empty > * to work correctly. > */ I'm not sure I understand. But my point is that you need to synchronize the __ptr_ring_discard_one() and __ptr_empty() as explained in the comment above __ptr_ring_empty(): /*  * Test ring empty status without taking any locks.  *  * NB: This is only safe to call if ring is never resized.  *  * However, if some other CPU consumes ring entries at the same time, the value  * returned is not guaranteed to be correct.  *  * In this case - to avoid incorrectly detecting the ring  * as empty - the CPU consuming the ring entries is responsible  * for either consuming all ring entries until the ring is empty,  * or synchronizing with some other CPU and causing it to  * re-test __ptr_ring_empty and/or consume the ring enteries  * after the synchronization point.  *  * Note: callers invoking this in a loop must use a compiler barrier,  * for example cpu_relax().  */ Thanks >> Thanks >> >> >>> Signed-off-by: Yunsheng Lin >>> --- >>> include/linux/ptr_ring.h | 26 +++++++++++++++++--------- >>> 1 file changed, 17 insertions(+), 9 deletions(-) >>> >>> diff --git a/include/linux/ptr_ring.h b/include/linux/ptr_ring.h >>> index 808f9d3..f32f052 100644 >>> --- a/include/linux/ptr_ring.h >>> +++ b/include/linux/ptr_ring.h >>> @@ -261,8 +261,7 @@ static inline void __ptr_ring_discard_one(struct ptr_ring *r) >>> /* Note: we must keep consumer_head valid at all times for __ptr_ring_empty >>> * to work correctly. >>> */ >>> - int consumer_head = r->consumer_head; >>> - int head = consumer_head++; >>> + int consumer_head = r->consumer_head + 1; >>> /* Once we have processed enough entries invalidate them in >>> * the ring all at once so producer can reuse their space in the ring. >>> @@ -271,19 +270,28 @@ static inline void __ptr_ring_discard_one(struct ptr_ring *r) >>> */ >>> if (unlikely(consumer_head - r->consumer_tail >= r->batch || >>> consumer_head >= r->size)) { >>> + int tail = r->consumer_tail; >>> + int head = consumer_head; >>> + >>> + if (unlikely(consumer_head >= r->size)) { >>> + r->consumer_tail = 0; >>> + WRITE_ONCE(r->consumer_head, 0); >>> + } else { >>> + r->consumer_tail = consumer_head; >>> + WRITE_ONCE(r->consumer_head, consumer_head); >>> + } >>> + >>> /* Zero out entries in the reverse order: this way we touch the >>> * cache line that producer might currently be reading the last; >>> * producer won't make progress and touch other cache lines >>> * besides the first one until we write out all entries. >>> */ >>> - while (likely(head >= r->consumer_tail)) >>> - r->queue[head--] = NULL; >>> - r->consumer_tail = consumer_head; >>> - } >>> - if (unlikely(consumer_head >= r->size)) { >>> - consumer_head = 0; >>> - r->consumer_tail = 0; >>> + while (likely(--head >= tail)) >>> + r->queue[head] = NULL; >>> + >>> + return; >>> } >>> + >>> /* matching READ_ONCE in __ptr_ring_empty for lockless tests */ >>> WRITE_ONCE(r->consumer_head, consumer_head); >>> } >> >> . >>