Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp3004031iog; Mon, 20 Jun 2022 09:09:13 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sTCP6AGm0Zjsw2dDMFcI+sOp7KpY19XMD3bULnlAs2fcFOawjJBb1faiaitkVs1OeD5q9P X-Received: by 2002:a17:906:73d4:b0:715:701c:ae96 with SMTP id n20-20020a17090673d400b00715701cae96mr20829586ejl.50.1655741353096; Mon, 20 Jun 2022 09:09:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655741353; cv=none; d=google.com; s=arc-20160816; b=JpDfhiMpbcjbl+9mv8DmD7g/ndwo6f8uIxe/tcuJvKcnk6/74jt2q8Km9m/x6+FVpw 3OFzAD7aA/8gTCPS6XmoXJSmvVHnLSMUavXDUXW8OnAcRMG803K2DwjQDxYXKf+8OJ9G fxzVyDzjjoBPUO8D8hF2ucqI1kZYAMJNsYKuhSFdZLa0T8QH3qjJ6jbQwut8SOG0bPC/ pN+ESkYsqW0cQS7LUtdD/dvjs1iPxFUm27tvIuzRR/3EE3l6Gu5YzivhqDZfWzyncQb4 GF/90xWVvTC2Oj6zv8zz6s53s0/VPm94clUY8PlZRYyToK7GOdAOtT1g31oQFPyufyCG 5jpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=HLrdK2cHg8a+WfynFvGYZTk9JjHbb/Ti/PUAmWDEXmQ=; b=b2VKxooBEZam4vK5bfYu/LPriJAYlA5kigYn6cWgXUJ7bY2ppzN+8a45kSu3TLBxye Kv2QT+yyHGFkDN0jOlhQ2O3IqhIWwh47utWo7L2TlgSkTqcv/G+KaWIrTkuRX4BcIN/Y yuNtfFsl+ZffFdbmqHMrTQ6q1lFCnmSYctQd2abkzOhFW96mnjBPIe94QHn9jQQY7rkc FDW6DaGpHjkX2DL1ISaSMlJOZU830+/T5Ez1SGG7y/QUlshuAbKKQ7a0dfrD2bkt3Z76 v7bmFGoFsh/mdjPKvzPVF0XbRuGgULL4k2yXNL+omdWAsaUIQ+KaNvt4sj7NScVTVCmS 5PsQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=qK7K5LcM; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i19-20020a1709067a5300b006e6f361280fsi13612888ejo.724.2022.06.20.09.08.45; Mon, 20 Jun 2022 09:09:13 -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=@gmail.com header.s=20210112 header.b=qK7K5LcM; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243055AbiFTPXK (ORCPT + 99 others); Mon, 20 Jun 2022 11:23:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50830 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244363AbiFTPWt (ORCPT ); Mon, 20 Jun 2022 11:22:49 -0400 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5D1F3635B; Mon, 20 Jun 2022 08:18:00 -0700 (PDT) Received: by mail-wr1-x430.google.com with SMTP id q9so15133269wrd.8; Mon, 20 Jun 2022 08:18:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=HLrdK2cHg8a+WfynFvGYZTk9JjHbb/Ti/PUAmWDEXmQ=; b=qK7K5LcM7dyr13i14ftD313nslG8YxjHQmfv8LjV7Tk1r8ybUhZ5AI0jn5rTqQhgmm i59iso5WlVRzUdhMyAPWt7eltJkNSfRGyu0jYjEV010WarFWgUS3zKaSaYaJfYgDz//T pid5V6gslaCrjWsjULnAXS530hiyltR47EnpCo8fvT4ma6uRB5F+KGSfmkgW+IZhEhRU OHPH8eR5Bncckxj2xyb0u6lbCoknZypydnAtyGNuGpMe8xIqnmVyJNbKWPMhsWl15p7Z MAziNE52C7w3j7WtmrVuXvwuEgzAYrB48BIKNyK7u2dralOWnRbA4it6ts7mRM3p0T0z PUdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=HLrdK2cHg8a+WfynFvGYZTk9JjHbb/Ti/PUAmWDEXmQ=; b=uHSUtpnpJZlS/Io/BZSsWi+9vi95Lr7t1T8WwRuEjtDAz9Waq8Az/9BaKMp4Gjd9Ei 7u1GVaGcml0XhTv5Xx+UdrwCp4UcvPu6G4aG6lwglN1zAWVyBaDYGmKo8mC/dtUhgeTs FRhXxWtNN0UmU5ICaVsCkpn/wh1g57vwpsdEILBuOviaGP/AwYizCIybCkRvSlOyBJee WwiEe9DUjclD956mSwt1bvqqHFrtPL+PPaLZqktJUkA/Kg5aMappsvTmaokcsjB02q10 IgpZBGrb8sIaOOQsJlzQMz8jqcymk4m9yRf3GpNy+SH8TjtyooR2D0Ckiz6Ed4UXksRQ cnig== X-Gm-Message-State: AJIora+ml+vXvRGeu1Nn7fb9+Y0azSIbZMqicjoEsngSl9kwMWmMQvEb 9J89bG4GskYlrVz+ACgkbmmIQXo+myKalFwod2c= X-Received: by 2002:adf:eb45:0:b0:21a:efae:4b9f with SMTP id u5-20020adfeb45000000b0021aefae4b9fmr17379320wrn.585.1655738278721; Mon, 20 Jun 2022 08:17:58 -0700 (PDT) MIME-Version: 1.0 References: <20220526235040.678984-1-dmitry.osipenko@collabora.com> <20220526235040.678984-18-dmitry.osipenko@collabora.com> <3bb3dc53-69fc-8cdb-ae37-583b9b2660a3@collabora.com> In-Reply-To: <3bb3dc53-69fc-8cdb-ae37-583b9b2660a3@collabora.com> From: Rob Clark Date: Mon, 20 Jun 2022 08:18:04 -0700 Message-ID: Subject: Re: [PATCH v6 17/22] drm/shmem-helper: Add generic memory shrinker To: Dmitry Osipenko Cc: David Airlie , Gerd Hoffmann , Gurchetan Singh , Chia-I Wu , Daniel Vetter , Daniel Almeida , Gert Wollny , Gustavo Padovan , Daniel Stone , Tomeu Vizoso , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , Rob Herring , Steven Price , Alyssa Rosenzweig , Emil Velikov , Robin Murphy , Qiang Yu , Sumit Semwal , =?UTF-8?Q?Christian_K=C3=B6nig?= , "Pan, Xinhui" , Thierry Reding , Tomasz Figa , Marek Szyprowski , Mauro Carvalho Chehab , Alex Deucher , Jani Nikula , Joonas Lahtinen , Rodrigo Vivi , Tvrtko Ursulin , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org, Dmitry Osipenko , linux-tegra@vger.kernel.org, linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org, amd-gfx@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, kernel@collabora.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 Mon, Jun 20, 2022 at 7:09 AM Dmitry Osipenko wrote: > > On 6/19/22 20:53, Rob Clark wrote: > ... > >> +static unsigned long > >> +drm_gem_shmem_shrinker_count_objects(struct shrinker *shrinker, > >> + struct shrink_control *sc) > >> +{ > >> + struct drm_gem_shmem_shrinker *gem_shrinker = to_drm_shrinker(shrinker); > >> + struct drm_gem_shmem_object *shmem; > >> + unsigned long count = 0; > >> + > >> + if (!mutex_trylock(&gem_shrinker->lock)) > >> + return 0; > >> + > >> + list_for_each_entry(shmem, &gem_shrinker->lru_evictable, madv_list) { > >> + count += shmem->base.size; > >> + > >> + if (count >= SHRINK_EMPTY) > >> + break; > >> + } > >> + > >> + mutex_unlock(&gem_shrinker->lock); > > > > As I mentioned on other thread, count_objects, being approximate but > > lockless and fast is the important thing. Otherwise when you start > > hitting the shrinker on many threads, you end up serializing them all, > > even if you have no pages to return to the system at that point. > > Daniel's point for dropping the lockless variant was that we're already > in trouble if we're hitting shrinker too often and extra optimizations > won't bring much benefits to us. At least with zram swap (which I highly recommend using even if you are not using a physical swap file/partition), swapin/out is actually quite fast. And if you are leaning on zram swap to fit 8GB of chrome browser on a 4GB device, the shrinker gets hit quite a lot. Lower spec (4GB RAM) chromebooks can be under constant memory pressure and can quite easily get into a situation where you are hitting the shrinker on many threads simultaneously. So it is pretty important for all shrinkers in the system (not just drm driver) to be as concurrent as possible. As long as you avoid serializing reclaim on all the threads, performance can still be quite good, but if you don't performance will fall off a cliff. jfwiw, we are seeing pretty good results (iirc 40-70% increase in open tab counts) with the combination of eviction + multigen LRU[1] + sizing zram swap to be 2x physical RAM [1] https://lwn.net/Articles/856931/ > Alright, I'll add back the lockless variant (or will use yours > drm_gem_lru) in the next revision. The code difference is very small > after all. > > ... > >> + /* prevent racing with the dma-buf importing/exporting */ > >> + if (!mutex_trylock(&gem_shrinker->dev->object_name_lock)) { > >> + *lock_contention |= true; > >> + goto resv_unlock; > >> + } > > > > I'm not sure this is a good idea to serialize on object_name_lock. > > Purgeable buffers should never be shared (imported or exported). So > > at best you are avoiding evicting and immediately swapping back in, in > > a rare case, at the cost of serializing multiple threads trying to > > reclaim pages in parallel. > > The object_name_lock shouldn't cause contention in practice. But objects > are also pinned on attachment, hence maybe this lock is indeed > unnecessary.. I'll re-check it. I'm not worried about contention with export/import/etc, but contention between multiple threads hitting the shrinker in parallel. I guess since you are using trylock, it won't *block* the other threads hitting shrinker, but they'll just end up looping in do_shrink_slab() because they are hitting contention. I'd have to do some experiments to see how it works out in practice, but my gut feel is that it isn't a good idea BR, -R > -- > Best regards, > Dmitry