Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp3428645rwb; Mon, 19 Sep 2022 22:21:23 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6OeO6rCFExQDaQCzL6W3FzxLEQZF4V5XH2NpkGQXzGD8sBdzzMFFnsmFfu+g/QT3aaG58n X-Received: by 2002:a17:907:3d94:b0:780:afb5:43b7 with SMTP id he20-20020a1709073d9400b00780afb543b7mr13787312ejc.546.1663651282718; Mon, 19 Sep 2022 22:21:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663651282; cv=none; d=google.com; s=arc-20160816; b=qGvVu9SngKAuYiVcfQKC4nx7tLiQAVg7hJBYinYOlWJ4GF93v2cT0k0mNaO655ECgJ DClZH1wY3T+I4Y2NQHb8luavF3ZZiE87bbIpbjghjiPX/U/ZuRwP0WhnvjzZGxqsDktU cM5+j4s8E7ZOoD1b2nWpO7Xq72QPdxiV3E67IGFCbre3qGHA3Vx/BjMVOwfigF08sX6D NMWHrX7R0jCBP1XLoJk5v4elGi8SNLvUR08rqR/76IUDZLprW9Ap04p+dsxJWBbtlkuy yrOVxQM4DRjqt5WQZXFW+Pzsy3J9T5+sJIMRmTiPtmVD+frbjqz3u3renGxstxF1BJG0 1GBg== 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 :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=Vyg2BS4OgoN4PABi0kgWr5r2OkJO3xdUk2Gib7RnQ68=; b=hebmyEDsOUGhbSFFgXnLAIzStKT+7/XyaVR4W2Vx4F8DERGFATzssTx8qkCLXeIuJb pcrL3wQOpbh1O2OPXwt+XgasRPWRNVMvc12TCJ/i3tyc8Gs7ELM0PpQpXTmh5S3g7Bkf UTQ5TjMz1StbBLl0K0LMqZ6x6eWlr5N6Jv68vvHC2WN/SjwYJLe5UYFQgv8nDgGybwKp ZB9DdQxpWA5UpiZhjuYaUxFj9yatVc1yGzOrcQ6XfMsufdIZ0d1n21fmgC+h5wK13Tm1 7cQc1WiAiFvpbKlGZcSxdVeeX1iBiRWDuS+8TNl64ilxoPo0KBPWS1SeHLxhQ13ppD+a j5gA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@colorfullife-com.20210112.gappssmtp.com header.s=20210112 header.b=pV5NEnzz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id fg9-20020a056402548900b004408bac1e2fsi515392edb.370.2022.09.19.22.20.55; Mon, 19 Sep 2022 22:21:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@colorfullife-com.20210112.gappssmtp.com header.s=20210112 header.b=pV5NEnzz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229529AbiITExy (ORCPT + 99 others); Tue, 20 Sep 2022 00:53:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35790 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229437AbiITExv (ORCPT ); Tue, 20 Sep 2022 00:53:51 -0400 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3F60E58DC9 for ; Mon, 19 Sep 2022 21:53:50 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id e16so2320257wrx.7 for ; Mon, 19 Sep 2022 21:53:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=colorfullife-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date; bh=Vyg2BS4OgoN4PABi0kgWr5r2OkJO3xdUk2Gib7RnQ68=; b=pV5NEnzzTDNCMhIzv9W9eopezRmZC6OUF2OXuGZ9QRaxsXFUqT/5cgWD4AGCTFPCTe TZim8aD+ldgXRmWVldy/lzKLR+IMaFm+0C190mV2ozj8V6y+ZSJSHpJ7IqXJJmosUFCe 4s8uLn1Bdg6rHPn6jCHn4OCyydLcbXzMGG8Z95MxVdlW+HPfIuqdrxfT+ZZ1c4VomHc5 NEuDuICS60ulIQzhJeDcTXqLVO3ycKcpMSZy7bsh4eRkUTJFGdMQaEhG5WhycshOQivE nnUAE2gwIcb396TpI3cZHfZJWe9Ly9qgiXY2cVxwIZvbIlM88m9tp5qVxNmE0YewNwmX 8rLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date; bh=Vyg2BS4OgoN4PABi0kgWr5r2OkJO3xdUk2Gib7RnQ68=; b=Ok4ZUntZfdwgkLWaPtstGJydtxwweI0mT9tRlrbJvFZ+lsBthK13S0U3Hcwyr83Bfj T/fGrVq6QWQqZpsZQgUPyogszZhIVEudToXoS1tsUj2qQCptTm5glYNF2qgoFbFXr6x5 mJZP0N81FUp3nhpUPMzlX7T5vhseOuPDAbt1xG80JK/d7Bot/GCkbkGtir/VX0hMK/HZ f5YkSFXQKyl/KUbfER5F4+C5UxstNIrYDMjTUoEUxy+HsRih9WtNRoq/cf+H4/QS1ouL kagAKZ1cvIFs70SuprehPv5os3oxEbq9p4vPqF8/vwsRlp87IZJQrDv8C3J0gedkQkNb BvYA== X-Gm-Message-State: ACrzQf3rW0OBnl6VDS2IdITQPy0ZuuBFz7Yxt1fYr8UZdPuSVcKVtOkj osY/LsGkMNQdyucX2fB/ZDbMCg== X-Received: by 2002:adf:d08d:0:b0:22a:4560:9c29 with SMTP id y13-20020adfd08d000000b0022a45609c29mr12541726wrh.579.1663649628800; Mon, 19 Sep 2022 21:53:48 -0700 (PDT) Received: from ?IPV6:2003:d9:970b:c00:6690:5cde:f1af:9517? (p200300d9970b0c0066905cdef1af9517.dip0.t-ipconnect.de. [2003:d9:970b:c00:6690:5cde:f1af:9517]) by smtp.googlemail.com with ESMTPSA id r13-20020adfa14d000000b0022af5e36981sm522866wrr.9.2022.09.19.21.53.47 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 19 Sep 2022 21:53:48 -0700 (PDT) Message-ID: <8d74a7d4-b80f-2a0f-ee95-243bdbd51ccd@colorfullife.com> Date: Tue, 20 Sep 2022 06:53:47 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.2.0 Subject: Re: [PATCH v6 2/2] ipc/msg: mitigate the lock contention with percpu counter To: "Sun, Jiebin" , akpm@linux-foundation.org, vasily.averin@linux.dev, shakeelb@google.com, dennis@kernel.org, tj@kernel.org, cl@linux.com, ebiederm@xmission.com, legion@kernel.org, alexander.mikhalitsyn@virtuozzo.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: tim.c.chen@intel.com, feng.tang@intel.com, ying.huang@intel.com, tianyou.li@intel.com, wangyang.guo@intel.com, Tim Chen References: <20220902152243.479592-1-jiebin.sun@intel.com> <20220913192538.3023708-1-jiebin.sun@intel.com> <20220913192538.3023708-3-jiebin.sun@intel.com> <6ed22478-0c89-92ea-a346-0349be2dd99c@intel.com> Content-Language: en-US From: Manfred Spraul In-Reply-To: <6ed22478-0c89-92ea-a346-0349be2dd99c@intel.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham 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 9/20/22 04:36, Sun, Jiebin wrote: > > On 9/18/2022 8:53 PM, Manfred Spraul wrote: >> Hi Jiebin, >> >> On 9/13/22 21:25, Jiebin Sun wrote: >>> The msg_bytes and msg_hdrs atomic counters are frequently >>> updated when IPC msg queue is in heavy use, causing heavy >>> cache bounce and overhead. Change them to percpu_counter >>> greatly improve the performance. Since there is one percpu >>> struct per namespace, additional memory cost is minimal. >>> Reading of the count done in msgctl call, which is infrequent. >>> So the need to sum up the counts in each CPU is infrequent. >>> >>> Apply the patch and test the pts/stress-ng-1.4.0 >>> -- system v message passing (160 threads). >>> >>> Score gain: 3.99x >>> >>> CPU: ICX 8380 x 2 sockets >>> Core number: 40 x 2 physical cores >>> Benchmark: pts/stress-ng-1.4.0 >>> -- system v message passing (160 threads) >>> >>> Signed-off-by: Jiebin Sun >>> Reviewed-by: Tim Chen >> Reviewed-by: Manfred Spraul >>> @@ -495,17 +496,18 @@ static int msgctl_info(struct ipc_namespace >>> *ns, int msqid, >>>       msginfo->msgssz = MSGSSZ; >>>       msginfo->msgseg = MSGSEG; >>>       down_read(&msg_ids(ns).rwsem); >>> -    if (cmd == MSG_INFO) { >>> +    if (cmd == MSG_INFO) >>>           msginfo->msgpool = msg_ids(ns).in_use; >>> -        msginfo->msgmap = atomic_read(&ns->msg_hdrs); >>> -        msginfo->msgtql = atomic_read(&ns->msg_bytes); >>> +    max_idx = ipc_get_maxidx(&msg_ids(ns)); >>> +    up_read(&msg_ids(ns).rwsem); >>> +    if (cmd == MSG_INFO) { >>> +        msginfo->msgmap = percpu_counter_sum(&ns->percpu_msg_hdrs); >>> +        msginfo->msgtql = percpu_counter_sum(&ns->percpu_msg_bytes); >> >> Not caused by your change, it just now becomes obvious: >> >> msginfo->msgmap and ->msgtql are type int, i.e. signed 32-bit, and >> the actual counters are 64-bit. >> This can overflow - and I think the code should handle this. Just >> clamp the values to INT_MAX. >> > Hi Manfred, > > Thanks for your advice. But I'm not sure if we could fix the overflow > issue in ipc/msg totally by > > clamp(val, low, INT_MAX). If the value is over s32, we might avoid the > reversal sign, but still could > > not get the accurate value. I think just clamping it to INT_MAX is the best approach. Reporting negative values is worse than clamping. If (and only if) there are real users that need to know the total amount of memory allocated for messages queues in one namespace, then we could add a MSG_INFO64 with long values. But I would not add that right now, I do not see a real use case where the value would be needed. Any other opinions? --     Manfred