Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp12468760rwd; Fri, 23 Jun 2023 06:36:54 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ60V51huUMF8BYvWh0bIzqTcJ5XPyl6lARTBoHvPXUEZYV1exSuSAq9ta0STUvTgmJ5Ui15 X-Received: by 2002:a17:90a:2f06:b0:25b:bae6:3c67 with SMTP id s6-20020a17090a2f0600b0025bbae63c67mr20650154pjd.43.1687527414324; Fri, 23 Jun 2023 06:36:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687527414; cv=none; d=google.com; s=arc-20160816; b=aPx3IAvBP0XwD+FdbM/q/fVkDW0mBG3b2F1vA+uSwFMv8bqgxfLJMARkBX58/5/j2r e/8TsiLR7ULyR0OJSyrTG6CieH3Gu+a7KCdEdtNUcaxD91mDAZnACfrfkCa815sJ1nBL jNLK0JNr+uaKx9uQfpxahGL01VETfhCQjefbTk1eNuMtzsNCAbZOYIx0oNQz0J3/tsXl OLCyhCnbq+EUdiDEMWRBZNUaQ+Eki6NnqySdNAwv984SiFtsXzgJLuuXP74bCAYqLGV/ pBNU/B/rPYCbg/EW09eJ+GDogQ1Ls+/jGZtXoS4u//mcoQJJRAoVLhF0v0jYBP4emuwS tvlQ== 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:dkim-signature; bh=ACoox6lHywQa9wO0MGPPVAOCZzDjfudqnGd2G0lw0os=; b=AuYnJupH1Ho9tYsy/gGjfYnvSy6B3m5g06VEbLrhJc72GZtpqu+7MymhX+XP0Od2H9 4W3KphjPYnUNH2IpF5ukIDbbjlNS2u3LMzaE57p5mv4AqmVsRbGZl60UdUPKR3JKpV6x WgiAyPxgIGQt/hdmF0Irw1+8WpLpWY3cGDByZw7ZXNkYXoU3u/uGapERqIfWXFUPPFlF /NFMem6rtvMzQk9EFQtoOwYsPbqvY7mnxmcEJ6NmHWThg5XNsL3RybmIuN0vcUL9K39v YoCIfTlDPzb+zDHvzv9hUhPTXtSPFtWG4h3jDCrSciKAb0wwkQZQLRhMrqJQO3pwhi9t fLpA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=fa1yyl1X; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 1-20020a17090a198100b00261326aa56esi1972388pji.5.2023.06.23.06.36.36; Fri, 23 Jun 2023 06:36:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4-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=@bytedance.com header.s=google header.b=fa1yyl1X; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231293AbjFWNdh (ORCPT + 99 others); Fri, 23 Jun 2023 09:33:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55702 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229611AbjFWNdf (ORCPT ); Fri, 23 Jun 2023 09:33:35 -0400 Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6F22410F6 for ; Fri, 23 Jun 2023 06:33:34 -0700 (PDT) Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-1b52418c25bso936905ad.0 for ; Fri, 23 Jun 2023 06:33:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1687527214; x=1690119214; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=ACoox6lHywQa9wO0MGPPVAOCZzDjfudqnGd2G0lw0os=; b=fa1yyl1XuChlZVeuHnAOkkfkq2hZyLQlU7Qx7FIzebWZlha3QE3pftRSrj+DJxI7Rv UhXvlkHo3Vcdolq+ME68HhnTz0k5XIGoqWorIAsqCzinXOgMm/YBV2p6rPjxxhB9xA+p CKbtixmq104K/XaeP7c/FRQEzfBhOolOJ13XEqLz2Vl/3W9UCdfjI5o3p38fxwPZQ7jM f2Nv1spQskpxwNqmrAc0KV9Etlyt/W0NlU64HVCq3FTgTv7qi6xblmtVtEFrypVRLxTd swjUrDnQOk53j5e/5rffnzu2n8zAvNVx/4KpVQ/7vpT1VvkShyKZT45f4U4Mq3kIEUCc sA+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687527214; x=1690119214; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ACoox6lHywQa9wO0MGPPVAOCZzDjfudqnGd2G0lw0os=; b=Q7tlBn58mShUWMFx9gN7bzEbmtPGjI+ke8dXcKWgxg5iBNOzJ37Fyxx0nyFOHkwlrG z/iRKHHno4GkPR6P9BmUY4YbTmEnAcccjHtdfqyKU0c83LiiUt6JlrNRmALdcqRKb0Gh ODWGJT6w0JRMZ361pbzkzprnEJO77MtToyxtUILs43D4rQxogRlvObjQ4tCqNUgezl5z ofEFShwmhMmmSqTYJYK4mMAqBcs19HT/5u/qNZ+M4j45rOQ3lj/2c6SPEsnz3DHfZMn9 1GcfJ8M5EaYtfANcl7+wDIonpcqAoSf9Y0qBY/h6htAz7DAFwJRAnwRUxRgJwKFChh29 +8oQ== X-Gm-Message-State: AC+VfDyBI/SMxux1l0ECJuj4YYHCWuezAuqpARcG9kk3Tq6tzlGifRFh OwEU3QurNxI189lpMCy9rROcEw== X-Received: by 2002:a17:902:c945:b0:1ae:3ff8:7fa7 with SMTP id i5-20020a170902c94500b001ae3ff87fa7mr26069073pla.4.1687527213878; Fri, 23 Jun 2023 06:33:33 -0700 (PDT) Received: from [10.4.168.167] ([139.177.225.254]) by smtp.gmail.com with ESMTPSA id kg14-20020a170903060e00b001b6a27dff99sm4341406plb.159.2023.06.23.06.33.26 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 23 Jun 2023 06:33:33 -0700 (PDT) Message-ID: <43a07dbe-5049-8596-da58-51e0a0d6243c@bytedance.com> Date: Fri, 23 Jun 2023 21:33:24 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.12.0 Subject: Re: [PATCH 05/29] drm/panfrost: dynamically allocate the drm-panfrost shrinker Content-Language: en-US To: Steven Price Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, linux-arm-msm@vger.kernel.org, dm-devel@redhat.com, linux-raid@vger.kernel.org, linux-bcache@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-fsdevel@vger.kernel.org, linux-ext4@vger.kernel.org, linux-nfs@vger.kernel.org, linux-xfs@vger.kernel.org, linux-btrfs@vger.kernel.org, akpm@linux-foundation.org, david@fromorbit.com, tkhai@ya.ru, vbabka@suse.cz, roman.gushchin@linux.dev, djwong@kernel.org, brauner@kernel.org, paulmck@kernel.org, tytso@mit.edu References: <20230622085335.77010-1-zhengqi.arch@bytedance.com> <20230622085335.77010-6-zhengqi.arch@bytedance.com> From: Qi Zheng In-Reply-To: <20230622085335.77010-6-zhengqi.arch@bytedance.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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-ext4@vger.kernel.org Hi Steven, The email you replied to was the failed version (due to the error below), so I copied your reply and replied to you on this successful version. (4.7.1 Error: too many recipients from 49.7.199.173) On 2023/6/23 18:01, Steven Price wrote: > On 22/06/2023 09:39, Qi Zheng wrote: >> From: Qi Zheng >> >> In preparation for implementing lockless slab shrink, >> we need to dynamically allocate the drm-panfrost shrinker, >> so that it can be freed asynchronously using kfree_rcu(). >> Then it doesn't need to wait for RCU read-side critical >> section when releasing the struct panfrost_device. >> >> Signed-off-by: Qi Zheng >> --- >> drivers/gpu/drm/panfrost/panfrost_device.h | 2 +- >> .../gpu/drm/panfrost/panfrost_gem_shrinker.c | 24 ++++++++++--------- >> 2 files changed, 14 insertions(+), 12 deletions(-) >> >> diff --git a/drivers/gpu/drm/panfrost/panfrost_device.h b/drivers/gpu/drm/panfrost/panfrost_device.h >> index b0126b9fbadc..e667e5689353 100644 >> --- a/drivers/gpu/drm/panfrost/panfrost_device.h >> +++ b/drivers/gpu/drm/panfrost/panfrost_device.h >> @@ -118,7 +118,7 @@ struct panfrost_device { >> >> struct mutex shrinker_lock; >> struct list_head shrinker_list; >> - struct shrinker shrinker; >> + struct shrinker *shrinker; >> >> struct panfrost_devfreq pfdevfreq; >> }; >> diff --git a/drivers/gpu/drm/panfrost/panfrost_gem_shrinker.c b/drivers/gpu/drm/panfrost/panfrost_gem_shrinker.c >> index bf0170782f25..2a5513eb9e1f 100644 >> --- a/drivers/gpu/drm/panfrost/panfrost_gem_shrinker.c >> +++ b/drivers/gpu/drm/panfrost/panfrost_gem_shrinker.c >> @@ -18,8 +18,7 @@ >> static unsigned long >> panfrost_gem_shrinker_count(struct shrinker *shrinker, struct shrink_control *sc) >> { >> - struct panfrost_device *pfdev = >> - container_of(shrinker, struct panfrost_device, shrinker); >> + struct panfrost_device *pfdev = shrinker->private_data; >> struct drm_gem_shmem_object *shmem; >> unsigned long count = 0; >> >> @@ -65,8 +64,7 @@ static bool panfrost_gem_purge(struct drm_gem_object *obj) >> static unsigned long >> panfrost_gem_shrinker_scan(struct shrinker *shrinker, struct shrink_control *sc) >> { >> - struct panfrost_device *pfdev = >> - container_of(shrinker, struct panfrost_device, shrinker); >> + struct panfrost_device *pfdev = shrinker->private_data; >> struct drm_gem_shmem_object *shmem, *tmp; >> unsigned long freed = 0; >> >> @@ -100,10 +98,15 @@ panfrost_gem_shrinker_scan(struct shrinker *shrinker, struct shrink_control *sc) >> void panfrost_gem_shrinker_init(struct drm_device *dev) >> { >> struct panfrost_device *pfdev = dev->dev_private; >> - pfdev->shrinker.count_objects = panfrost_gem_shrinker_count; >> - pfdev->shrinker.scan_objects = panfrost_gem_shrinker_scan; >> - pfdev->shrinker.seeks = DEFAULT_SEEKS; >> - WARN_ON(register_shrinker(&pfdev->shrinker, "drm-panfrost")); >> + >> + pfdev->shrinker = shrinker_alloc_and_init(panfrost_gem_shrinker_count, >> + panfrost_gem_shrinker_scan, 0, >> + DEFAULT_SEEKS, 0, pfdev); >> + if (pfdev->shrinker && >> + register_shrinker(pfdev->shrinker, "drm-panfrost")) { >> + shrinker_free(pfdev->shrinker); >> + WARN_ON(1); >> + } > > So we didn't have good error handling here before, but this is > significantly worse. Previously if register_shrinker() failed then the > driver could safely continue without a shrinker - it would waste memory > but still function. > > However we now have two failure conditions: > * shrinker_alloc_init() returns NULL. No warning and NULL deferences > will happen later. > > * register_shrinker() fails, shrinker_free() will free pdev->shrinker > we get a warning, but followed by a use-after-free later. > > I think we need to modify panfrost_gem_shrinker_init() to be able to > return an error, so a change something like the below (untested) before > your change. Indeed. I will fix it in the v2. Thanks, Qi > > Steve > > ----8<--- > diff --git a/drivers/gpu/drm/panfrost/panfrost_drv.c > b/drivers/gpu/drm/panfrost/panfrost_drv.c > index bbada731bbbd..f705bbdea360 100644 > --- a/drivers/gpu/drm/panfrost/panfrost_drv.c > +++ b/drivers/gpu/drm/panfrost/panfrost_drv.c > @@ -598,10 +598,14 @@ static int panfrost_probe(struct platform_device > *pdev) > if (err < 0) > goto err_out1; > > - panfrost_gem_shrinker_init(ddev); > + err = panfrost_gem_shrinker_init(ddev); > + if (err) > + goto err_out2; > > return 0; > > +err_out2: > + drm_dev_unregister(ddev); > err_out1: > pm_runtime_disable(pfdev->dev); > panfrost_device_fini(pfdev); > diff --git a/drivers/gpu/drm/panfrost/panfrost_gem.h > b/drivers/gpu/drm/panfrost/panfrost_gem.h > index ad2877eeeccd..863d2ec8d4f0 100644 > --- a/drivers/gpu/drm/panfrost/panfrost_gem.h > +++ b/drivers/gpu/drm/panfrost/panfrost_gem.h > @@ -81,7 +81,7 @@ panfrost_gem_mapping_get(struct panfrost_gem_object *bo, > void panfrost_gem_mapping_put(struct panfrost_gem_mapping *mapping); > void panfrost_gem_teardown_mappings_locked(struct panfrost_gem_object *bo); > > -void panfrost_gem_shrinker_init(struct drm_device *dev); > +int panfrost_gem_shrinker_init(struct drm_device *dev); > void panfrost_gem_shrinker_cleanup(struct drm_device *dev); > > #endif /* __PANFROST_GEM_H__ */ > diff --git a/drivers/gpu/drm/panfrost/panfrost_gem_shrinker.c > b/drivers/gpu/drm/panfrost/panfrost_gem_shrinker.c > index bf0170782f25..90265b37636f 100644 > --- a/drivers/gpu/drm/panfrost/panfrost_gem_shrinker.c > +++ b/drivers/gpu/drm/panfrost/panfrost_gem_shrinker.c > @@ -97,13 +97,17 @@ panfrost_gem_shrinker_scan(struct shrinker > *shrinker, struct shrink_control *sc) > * > * This function registers and sets up the panfrost shrinker. > */ > -void panfrost_gem_shrinker_init(struct drm_device *dev) > +int panfrost_gem_shrinker_init(struct drm_device *dev) > { > struct panfrost_device *pfdev = dev->dev_private; > + int ret; > + > pfdev->shrinker.count_objects = panfrost_gem_shrinker_count; > pfdev->shrinker.scan_objects = panfrost_gem_shrinker_scan; > pfdev->shrinker.seeks = DEFAULT_SEEKS; > - WARN_ON(register_shrinker(&pfdev->shrinker, "drm-panfrost")); > + ret = register_shrinker(&pfdev->shrinker, "drm-panfrost"); > + > + return ret; > } > > /** >