Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp3113402pxm; Mon, 28 Feb 2022 12:17:26 -0800 (PST) X-Google-Smtp-Source: ABdhPJy58DE0c8BzZhaucz+cnhyS2tPJeQSQTvQWMAsK2ezfFUCv+aTAO5Rp6XEqidSw7Iyh+MzF X-Received: by 2002:a05:6a00:240c:b0:4e1:2d98:d2c9 with SMTP id z12-20020a056a00240c00b004e12d98d2c9mr23622226pfh.51.1646079446794; Mon, 28 Feb 2022 12:17:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646079446; cv=none; d=google.com; s=arc-20160816; b=N5s9lx0GAKT4isvqdn589dP8LLjPH4Aw+m1y33Fhue2vAIRls15kTft5OtCWSmPa9v tOLa8Ylki+/dx8Flxx6Cro1k5/V4eXjOEqObKRUteyejosnB7sqqpvxZJqMLRAzwVqOy kio1F+bhohW3Smo7XXKbH6cpW7M7nPMwbhWz0JrpcoyBmWb4jDDCDnaLQzYmqCoP+0SW L7jeUVK8qlJW5kEVVugTpDoBxwykhTo/A3l2mQ6H3nqHRqrmwNQjIo9Zwba5ebm0n7KA WJB685ljkVXaY0ifnkpu0Nv8/K6dyNjHhaKCdYLusWL+O92q8Z2KEqls5tKf14tsf6jG cZxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=ZwVSAIkPcqNzkCK2RKk+X2MMOBXgNgEIKVsLngELsNk=; b=a4KepeyDwY8uJuPvjE/IkiltvGOGZHpsNC8BN3Zwly2/xuQAk+22Aq7f7nW5iepbY0 2g1P6LBRYSb5e1g6AWG8R7ddCnlnkTcx/y4+7gwKmY7b/+5K5E179NNIAzLc1RYIfVUZ zJsYAcdq/GRokFHrehRjk18Mm6dv8BpOXiXC3PlYHTmrlAkedlL4hBwxotPbozO10vuy qc/uWFWGch7grEsO/PnV78cF0CbFRfrLlst+BGb+HYvRGSO4Xzmfm/752Je6wiueuiIL Owy1Yv99eFiHC+7Q4Qe0T15Zyzzbns3EhRp2jpVyG/DfZav3HiCdovv0Vo1TZSysOuc/ DLkw== ARC-Authentication-Results: i=1; mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id p17-20020a170902ead100b0014ed9d9b8d2si9811643pld.499.2022.02.28.12.17.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Feb 2022 12:17:26 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 343A118C7B4; Mon, 28 Feb 2022 11:38:48 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240644AbiB1SlH (ORCPT + 99 others); Mon, 28 Feb 2022 13:41:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59108 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240651AbiB1Skz (ORCPT ); Mon, 28 Feb 2022 13:40:55 -0500 Received: from out30-44.freemail.mail.aliyun.com (out30-44.freemail.mail.aliyun.com [115.124.30.44]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A065B45511; Mon, 28 Feb 2022 10:34:18 -0800 (PST) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R601e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04407;MF=haoxu@linux.alibaba.com;NM=1;PH=DS;RN=5;SR=0;TI=SMTPD_---0V5oMHbb_1646073255; Received: from 192.168.31.208(mailfrom:haoxu@linux.alibaba.com fp:SMTPD_---0V5oMHbb_1646073255) by smtp.aliyun-inc.com(127.0.0.1); Tue, 01 Mar 2022 02:34:16 +0800 Message-ID: <9954b806-c4a0-2448-1eac-c8fc5cf2ca2c@linux.alibaba.com> Date: Tue, 1 Mar 2022 02:34:15 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Subject: Re: [PATCH v1] io_uring: Add support for napi_busy_poll Content-Language: en-US To: Olivier Langlois , Jens Axboe Cc: Pavel Begunkov , io-uring , linux-kernel References: <2cedc9f21a1c89aa9fe1fa4dffc2ebeabeb761f5.camel@trillion01.com> From: Hao Xu In-Reply-To: <2cedc9f21a1c89aa9fe1fa4dffc2ebeabeb761f5.camel@trillion01.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,NICE_REPLY_A, RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2/25/22 23:32, Olivier Langlois wrote: > On Fri, 2022-02-25 at 00:32 -0500, Olivier Langlois wrote: >>>> +#ifdef CONFIG_NET_RX_BUSY_POLL >>>> +static void io_adjust_busy_loop_timeout(struct timespec64 *ts, >>>> +                                       struct io_wait_queue >>>> *iowq) >>>> +{ >>>> +       unsigned busy_poll_to = READ_ONCE(sysctl_net_busy_poll); >>>> +       struct timespec64 pollto = ns_to_timespec64(1000 * >>>> (s64)busy_poll_to); >>>> + >>>> +       if (timespec64_compare(ts, &pollto) > 0) { >>>> +               *ts = timespec64_sub(*ts, pollto); >>>> +               iowq->busy_poll_to = busy_poll_to; >>>> +       } else { >>>> +               iowq->busy_poll_to = timespec64_to_ns(ts) / 1000; >>> How about timespec64_tons(ts) >> 10, since we don't need accurate >>> number. >> Fantastic suggestion! The kernel test robot did also detect an issue >> with that statement. I did discover do_div() in the meantime but what >> you suggest is better, IMHO... > After having seen Jens patch (io_uring: don't convert to jiffies for > waiting on timeouts), I think that I'll stick with do_div(). > > I have a hard time considering removing timing accuracy when effort is > made to make the same function more accurate... I think they are different things. Jens' patch is to resolve the problem that jiffies possibly can not stand for time < 1ms (when HZ is 1000). For example, a user assigns 10us, turn out to be 1ms, it's big difference. But divided by 1000 or 1024 is not that quite different in this case. >> >>>> +                !io_busy_loop_end(iowq, start_time)); >>>> +} >>>> +#endif /* CONFIG_NET_RX_BUSY_POLL */ >>>> + >>>>   /* >>>>    * Wait until events become available, if we don't already have >>>> some. The >>>>    * application must reap them itself, as they reside on the >>>> shared cq ring. >>>> @@ -7729,12 +7906,20 @@ static int io_cqring_wait(struct >>>> io_ring_ctx *ctx, int min_events, >>>>                 if (!io_run_task_work()) >>>>                         break; >>>>         } while (1); >>>> - >>>> +#ifdef CONFIG_NET_RX_BUSY_POLL >>>> +       iowq.busy_poll_to = 0; >>>> +#endif >>>>         if (uts) { >>>>                 struct timespec64 ts; >>>> >>>>                 if (get_timespec64(&ts, uts)) >>>>                         return -EFAULT; >>>> +#ifdef CONFIG_NET_RX_BUSY_POLL >>>> +               if (!(ctx->flags & IORING_SETUP_SQPOLL) && >>>> +                   !list_empty(&ctx->napi_list)) { >>>> +                       io_adjust_busy_loop_timeout(&ts, &iowq); >>>> +               } >>>> +#endif >>>>                 timeout = timespec64_to_jiffies(&ts); >>>>         } >>>> >>>> @@ -7759,6 +7944,10 @@ static int io_cqring_wait(struct >>>> io_ring_ctx >>>> *ctx, int min_events, >>>>         iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + >>>> min_events; >>>> >>>>         trace_io_uring_cqring_wait(ctx, min_events); >>>> +#ifdef CONFIG_NET_RX_BUSY_POLL >>>> +       if (iowq.busy_poll_to) >>>> +               io_blocking_napi_busy_loop(ctx, &iowq); >>> We may not need locks for the napi_list, the reason is we don't >>> need >>> to >>> poll an accurate list, the busy polling/NAPI itself is kind of >>> speculation. So the deletion is not an emergency. >>> To say the least, we can probably delay the deletion to some safe >>> place >>> like the original task's task work though this may cause other >>> problems... >> There are 2 concerns here. >> >> 1. Iterating a list while another thread modify it is not thread-safe >> unless you use a lock. >> >> If we offer napi_busy_poll() without sqpoll with the modification in >> io_cqring_wait(), this is a real possibility. A thread could call >> io_uring_enter(IORING_ENTER_GETEVENTS) while another thread calls >> io_uring_enter() to submit new sqes that could trigger a call to >> io_add_napi(). >> >> If napi_busy_poll() is only offered through sqpoll thread, this >> becomes >> a non-issue since the only thread accessing/modifying the napi_list >> field is the sqpoll thread. >> >> Providing the patch benchmark result with v2 could help deciding what >> to do with this choice. >> >> 2. You are correct when you say that deletion is not an emergency. >> >> However, the design guideline that I did follow when writing the >> patch >> is that napi_busy_poll support should not impact users not using this >> feature. Doing the deletion where that patch is doing it fullfill >> this >> goal. >> >> Comparing a timeout value with the jiffies variable is very cheap and >> will only be performed when napi_busy_poll is used. >> >> The other option would be to add a refcount to each napi_entry and >> decrement it if needed everytime a request is discarded. Doing that >> that check for every requests that io_uring discards on completion, I >> am very confident that this would negatively impact various >> performance >> benchmarks that Jens routinely perform... >> > Another fact to consider, it is that I expect the content of napi_list > to be extremely stable. Regular entry deletion should not be a thing. > > postponing the deletion using task work is not an option too. How would > io_busy_loop_end() discern between a pending list entry deletion and > any other task work making the busy looping stop?