Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp941089pxb; Thu, 19 Nov 2020 18:38:19 -0800 (PST) X-Google-Smtp-Source: ABdhPJymfQ2xmXc/YTxftxEnImLL/5LiSseNP1dbRod/lxdB1wVSVLoyR8irDKJV+aebNxLd9s06 X-Received: by 2002:a50:c30d:: with SMTP id a13mr10352170edb.89.1605839899476; Thu, 19 Nov 2020 18:38:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605839899; cv=none; d=google.com; s=arc-20160816; b=rhjhnXrFQBGBc/bsZabSUvkX6m2qqTBmIaB/EwI7fF16iZwAQua1+gzalt/ffAXCTe oA8QiY/rBWE75dyb6xAsZLpRad7Z1CfwE5IQ4yncxNEeqjoNUoYFpJ+9QHIMZItbmXEX p+WHt/cAO0K3YZoyHVGAFEyKOyTReow3xIVAOp8Q5BUTPDdaRSjiadkFyuc7UFKoAcbq BIUlx9wKymaIOoiKh1lKMhCVHtKH8149MgRDPJKlTPhJ8+4hYMuZAXHhZ7s9deSyyQ4A gAxYY8Pxf1lL6clAJ3V/zG0yMKgaTIsJF2QzzYatSuuxJhhecV65To8sDRBphgmAzl5K SNDw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:message-id:in-reply-to :date:references:subject:cc:to:from:ironport-sdr:ironport-sdr; bh=pY/eeE7iegkwV6KNyYX5+AcztX66UNdi2Cu3C5ObPdg=; b=f2+Nh588P4PKT9N1kds/ugnrXTd8D1Zg34e0zG4Ox23zNAg2abvt3xPQjrZZ3JyDz2 qBY+mWWW4bsghXLuqcOnjxFsfc4/7tdthIiit5D6SkZd9x0rDT4+bV0afNLO4V2jlOpB PC5INiG589s/Om9FE/6OVQdtbRAmZuMDqftiQFiOFaXFBDQTJTCbItVPNYzSz0ZmD0o4 MsvT6nCzIhiPfNwfRlCUKoA4y87FHiazAq1MqP8u3kMFmtFnySBi7UIpJVDMYukdITLS eyPxlHyhqlt9Zqg9id3MKgbIUj3wzEwd8pt9T/W2/DLnoiQ/b0tsWhbhwCUWTEdDBVAH Smlg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g13si1023269edn.525.2020.11.19.18.37.56; Thu, 19 Nov 2020 18:38:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726599AbgKTCeY (ORCPT + 99 others); Thu, 19 Nov 2020 21:34:24 -0500 Received: from mga02.intel.com ([134.134.136.20]:57018 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726468AbgKTCeX (ORCPT ); Thu, 19 Nov 2020 21:34:23 -0500 IronPort-SDR: JLSeyX5JTUAm4wygmSdvN61pnDaIhyOzQnzRrKXFOu67PDwjOs8fCm63bp+eHqxjFHRv+r1PJV YbTMN4/eJwug== X-IronPort-AV: E=McAfee;i="6000,8403,9810"; a="158440288" X-IronPort-AV: E=Sophos;i="5.78,354,1599548400"; d="scan'208";a="158440288" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Nov 2020 18:34:23 -0800 IronPort-SDR: pyr0PZg1wIIwvBczesvoSnPc0oCSt5HzcldCuTNhmi331eXLMOSXTJDPySoUzifOGPxrcoSXoQ Zz4fwbDT9WuQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.78,354,1599548400"; d="scan'208";a="535008625" Received: from yhuang-dev.sh.intel.com (HELO yhuang-dev) ([10.239.159.50]) by fmsmga005.fm.intel.com with ESMTP; 19 Nov 2020 18:34:20 -0800 From: "Huang\, Ying" To: Uladzislau Rezki Cc: huang ying , Andrew Morton , linux-mm@kvack.org, LKML , Hillf Danton , Michal Hocko , Matthew Wilcox , Oleksiy Avramchenko , Steven Rostedt , Christoph Hellwig Subject: Re: [PATCH 2/2] mm/vmalloc: rework the drain logic References: <20201116220033.1837-1-urezki@gmail.com> <20201116220033.1837-2-urezki@gmail.com> <20201117130434.GA10769@pc636> <20201118161623.GA21171@pc636> <87mtzeunsi.fsf@yhuang-dev.intel.com> <20201119173604.GA991@pc636> Date: Fri, 20 Nov 2020 10:34:19 +0800 In-Reply-To: <20201119173604.GA991@pc636> (Uladzislau Rezki's message of "Thu, 19 Nov 2020 18:36:04 +0100") Message-ID: <87zh3cu578.fsf@yhuang-dev.intel.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=ascii Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Uladzislau Rezki writes: > On Thu, Nov 19, 2020 at 09:40:29AM +0800, Huang, Ying wrote: >> Uladzislau Rezki writes: >> >> > On Wed, Nov 18, 2020 at 10:44:13AM +0800, huang ying wrote: >> >> On Tue, Nov 17, 2020 at 9:04 PM Uladzislau Rezki wrote: >> >> > >> >> > On Tue, Nov 17, 2020 at 10:37:34AM +0800, huang ying wrote: >> >> > > On Tue, Nov 17, 2020 at 6:00 AM Uladzislau Rezki (Sony) >> >> > > wrote: >> >> > > > >> >> > > > A current "lazy drain" model suffers from at least two issues. >> >> > > > >> >> > > > First one is related to the unsorted list of vmap areas, thus >> >> > > > in order to identify the [min:max] range of areas to be drained, >> >> > > > it requires a full list scan. What is a time consuming if the >> >> > > > list is too long. >> >> > > > >> >> > > > Second one and as a next step is about merging all fragments >> >> > > > with a free space. What is also a time consuming because it >> >> > > > has to iterate over entire list which holds outstanding lazy >> >> > > > areas. >> >> > > > >> >> > > > See below the "preemptirqsoff" tracer that illustrates a high >> >> > > > latency. It is ~24 676us. Our workloads like audio and video >> >> > > > are effected by such long latency: >> >> > > >> >> > > This seems like a real problem. But I found there's long latency >> >> > > avoidance mechanism in the loop in __purge_vmap_area_lazy() as >> >> > > follows, >> >> > > >> >> > > if (atomic_long_read(&vmap_lazy_nr) < resched_threshold) >> >> > > cond_resched_lock(&free_vmap_area_lock); >> >> > > >> >> > I have added that "resched threshold" because of on my tests i could >> >> > simply hit out of memory, due to the fact that a drain work is not up >> >> > to speed to process such long outstanding list of vmap areas. >> >> >> >> OK. Now I think I understand the problem. For free area purging, >> >> there are multiple "producers" but one "consumer", and it lacks enough >> >> mechanism to slow down the "producers" if "consumer" can not catch up. >> >> And your patch tries to resolve the problem via accelerating the >> >> "consumer". >> >> >> > Seems, correct. But just in case one more time: >> > >> > the cond_resched_lock was added once upon a time to get rid of long >> > preemption off time. Due to dropping the lock, "producers" can start >> > generate further vmap area, so "consumer" can not catch up. Seems >> >> Yes. And in theory there are vfree() storm, that is, thousands vfree() >> can be called in short time. But I don't think that's practical use >> case. >> >> > Later on, a resched threshold was added. It is just a simple protection >> > threshold, passing which, a freeing is prioritized back over allocation, >> > so we guarantee that we do not hit out of memory. >> >> Yes. That can accelerate freeing if necessary. >> >> >> >> >> That isn't perfect, but I think we may have quite some opportunities >> >> to merge the free areas, so it should just work. >> >> >> > Yes, merging opportunity should do the work. But of course there are >> > exceptions. >> > >> >> And I found the long latency avoidance logic in >> >> __purge_vmap_area_lazy() appears problematic, >> >> >> >> if (atomic_long_read(&vmap_lazy_nr) < resched_threshold) >> >> cond_resched_lock(&free_vmap_area_lock); >> >> >> >> Shouldn't it be something as follows? >> >> >> >> if (i >= BATCH && atomic_long_read(&vmap_lazy_nr) < >> >> resched_threshold) { >> >> cond_resched_lock(&free_vmap_area_lock); >> >> i = 0; >> >> } else >> >> i++; >> >> >> >> This will accelerate the purging via batching and slow down vmalloc() >> >> via holding free_vmap_area_lock. If it makes sense, can we try this? >> >> >> > Probably we can switch to just using "batch" methodology: >> > >> > >> > if (!(i++ % batch_threshold)) >> > cond_resched_lock(&free_vmap_area_lock); >> > >> >> That's the typical long latency avoidance method. >> >> > The question is, which value we should use as a batch_threshold: 100, 1000, etc. >> >> I think we can do some measurement to determine it? >> > Hmm.. looking at it one more time i do not see what batching solves. Without batch protection, we may release the lock and CPU anytime during looping if "vmap_lazy_nr < resched_threshold". Too many vmalloc/vfree may be done during that. So I think we can restrict it. Batching can improve the performance of purging itself too. > Anyway we need to have some threshold(what we do have), that regulates > a priority between vmalloc()/vfree(). > > What we can do more with it are: > > - purging should be just performed asynchronously in workqueue context. > Giving the fact, that now we also do a merge of outstanding areas, the > data structure(rb-tree) will not be so fragmented. Async works only if there are idle CPU time on other CPUs. And it may punish other innocent workloads instead of the heavy vmalloc/vfree users. So we should be careful about that. > - lazy_max_pages() can slightly be decreased. If there are existing > workloads which suffer from such long value. It would be good to get > real complains and evidence. > >> > Apart of it and in regard to CONFIG_KASAN_VMALLOC, it seems that we are not >> > allowed to drop the free_vmap_area_lock at all. Because any simultaneous >> > allocations are not allowed within a drain region, so it should occur in >> > disjoint regions. But i need to double check it. >> > >> >> >> >> And, can we reduce lazy_max_pages() to control the length of the >> >> purging list? It could be > 8K if the vmalloc/vfree size is small. >> >> >> > We can adjust it for sure. But it will influence on number of global >> > TLB flushes that must be performed. >> >> Em... For example, if we set it to 100, then the number of the TLB >> flushes can be reduced to 1% of the un-optimized implementation >> already. Do you think so? >> > If we set lazy_max_pages() to vague value such as 100, the performance > will be just destroyed. Sorry, my original words weren't clear enough. What I really want to suggest is to control the length of the purging list instead of reduce lazy_max_pages() directly. That is, we can have a "atomic_t nr_purge_item" to record the length of the purging list and start purging if (vmap_lazy_nr > lazy_max_pages && nr_purge_item > max_purge_item). vmap_lazy_nr is to control the virtual address space, nr_purge_item is to control the batching purging latency. "100" is just an example, the real value should be determined according to the test results. Best Regards, Huang, Ying