From patchwork Thu Aug 24 03:42:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13363486 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9A3B0C71145 for ; Thu, 24 Aug 2023 03:50:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236657AbjHXDuQ (ORCPT ); Wed, 23 Aug 2023 23:50:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58382 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239782AbjHXDtx (ORCPT ); Wed, 23 Aug 2023 23:49:53 -0400 Received: from mail-oi1-x22c.google.com (mail-oi1-x22c.google.com [IPv6:2607:f8b0:4864:20::22c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2A50A1FC4 for ; Wed, 23 Aug 2023 20:48:08 -0700 (PDT) Received: by mail-oi1-x22c.google.com with SMTP id 5614622812f47-3a86b1114ceso349497b6e.1 for ; Wed, 23 Aug 2023 20:48:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1692848879; x=1693453679; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=fNxRXaSipmd30ualQGXe2v/kqoH5WODKyeol+P+IpTs=; b=S7PJEhTBlU2ptzdizLOxfCUewX7SiUVXbnwlSYN2yOHNvAoStUJTI1eiOSL7rG56BH Lkt4aTffxFuu0wUyb3306dIpVQZ165605uSpyYO/jhILMu6ZQDf4Tgv0Sz+dN089wZwi 9MiPQoX3n2iBc0xXxBVtNs+dbNzAVP1eVgBKzKWyi1Dqgq9N7qukpvI07cbnyHlHwwR6 rSYkx5l0RXfmz5M6SQxpzqA/q37t0/Fle2uHU7Mbba0q0AloR2InD9pLWhiEHZxOwLsU Fti+OAi7IhueuRvVApFDNMDYIN5jErjkaa4mxwyJFA4LjJ5dva6v5WoeRZyJ3mtuwz44 +sSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692848879; x=1693453679; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fNxRXaSipmd30ualQGXe2v/kqoH5WODKyeol+P+IpTs=; b=b5VeQ1EEMK6cRobwmtf3N0ECdFc2Zc9Awz9K/AB62rI76ORr4cAe3IRqBY7AN04+ZJ RZ//Fuou1he0f6mgQnHnMcOB7yj1kjTx07gu+95wHJcfhQ6BkKTIZ/5aJkCntjtfyvyc ikREgv+qxHqIDfWAtOhezXyS26Q8vJ46bBBQ6NC+9oWhtmdZSqBnPq7binvNRf8uW8Ut 8P5izByCm6iQMxSmgf6CQUS291SaCyx9mdX7GEqlGb3y/qRhyXSityn/1qTFzg5/dGvG fLarWXA04BsRZczT4cqFePz9vuM8sdLm459nL3Ihipv0IMJK3FfKcnEAQCm+X33AuEyM yTKg== X-Gm-Message-State: AOJu0YweWEpshdfCaZKjxMjkzbGTvlL65dBAV8sdx2Niy6Yc7EosPC6X KLiL2SJhDkvqZdcDA9nD5zvOcA== X-Google-Smtp-Source: AGHT+IE+WG5PH7wniJfO5JcJbhNOS2WEVVvbmj+rtjyhc6DhOoBfaOm021n0CLUAVkQksyHeF+Z9Ig== X-Received: by 2002:a05:6808:bc8:b0:3a8:f3e7:d66b with SMTP id o8-20020a0568080bc800b003a8f3e7d66bmr424249oik.4.1692848878915; Wed, 23 Aug 2023 20:47:58 -0700 (PDT) Received: from C02DW0BEMD6R.bytedance.net ([203.208.167.146]) by smtp.gmail.com with ESMTPSA id t6-20020a63b246000000b005579f12a238sm10533157pgo.86.2023.08.23.20.47.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Aug 2023 20:47:58 -0700 (PDT) From: Qi Zheng To: 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, steven.price@arm.com, cel@kernel.org, senozhatsky@chromium.org, yujie.liu@intel.com, gregkh@linuxfoundation.org, muchun.song@linux.dev Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, Qi Zheng , Muchun Song , "Michael S. Tsirkin" , David Hildenbrand , Jason Wang , Xuan Zhuo , virtualization@lists.linux-foundation.org Subject: [PATCH v5 28/45] virtio_balloon: dynamically allocate the virtio-balloon shrinker Date: Thu, 24 Aug 2023 11:42:47 +0800 Message-Id: <20230824034304.37411-29-zhengqi.arch@bytedance.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: <20230824034304.37411-1-zhengqi.arch@bytedance.com> References: <20230824034304.37411-1-zhengqi.arch@bytedance.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org In preparation for implementing lockless slab shrink, use new APIs to dynamically allocate the virtio-balloon shrinker, so that it can be freed asynchronously via RCU. Then it doesn't need to wait for RCU read-side critical section when releasing the struct virtio_balloon. Signed-off-by: Qi Zheng Reviewed-by: Muchun Song CC: "Michael S. Tsirkin" CC: David Hildenbrand CC: Jason Wang CC: Xuan Zhuo CC: virtualization@lists.linux-foundation.org --- drivers/virtio/virtio_balloon.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c index 5b15936a5214..82e6087073a9 100644 --- a/drivers/virtio/virtio_balloon.c +++ b/drivers/virtio/virtio_balloon.c @@ -111,7 +111,7 @@ struct virtio_balloon { struct virtio_balloon_stat stats[VIRTIO_BALLOON_S_NR]; /* Shrinker to return free pages - VIRTIO_BALLOON_F_FREE_PAGE_HINT */ - struct shrinker shrinker; + struct shrinker *shrinker; /* OOM notifier to deflate on OOM - VIRTIO_BALLOON_F_DEFLATE_ON_OOM */ struct notifier_block oom_nb; @@ -816,8 +816,7 @@ static unsigned long shrink_free_pages(struct virtio_balloon *vb, static unsigned long virtio_balloon_shrinker_scan(struct shrinker *shrinker, struct shrink_control *sc) { - struct virtio_balloon *vb = container_of(shrinker, - struct virtio_balloon, shrinker); + struct virtio_balloon *vb = shrinker->private_data; return shrink_free_pages(vb, sc->nr_to_scan); } @@ -825,8 +824,7 @@ static unsigned long virtio_balloon_shrinker_scan(struct shrinker *shrinker, static unsigned long virtio_balloon_shrinker_count(struct shrinker *shrinker, struct shrink_control *sc) { - struct virtio_balloon *vb = container_of(shrinker, - struct virtio_balloon, shrinker); + struct virtio_balloon *vb = shrinker->private_data; return vb->num_free_page_blocks * VIRTIO_BALLOON_HINT_BLOCK_PAGES; } @@ -847,16 +845,23 @@ static int virtio_balloon_oom_notify(struct notifier_block *nb, static void virtio_balloon_unregister_shrinker(struct virtio_balloon *vb) { - unregister_shrinker(&vb->shrinker); + shrinker_free(vb->shrinker); } static int virtio_balloon_register_shrinker(struct virtio_balloon *vb) { - vb->shrinker.scan_objects = virtio_balloon_shrinker_scan; - vb->shrinker.count_objects = virtio_balloon_shrinker_count; - vb->shrinker.seeks = DEFAULT_SEEKS; + vb->shrinker = shrinker_alloc(0, "virtio-balloon"); + if (!vb->shrinker) + return -ENOMEM; - return register_shrinker(&vb->shrinker, "virtio-balloon"); + vb->shrinker->scan_objects = virtio_balloon_shrinker_scan; + vb->shrinker->count_objects = virtio_balloon_shrinker_count; + vb->shrinker->seeks = DEFAULT_SEEKS; + vb->shrinker->private_data = vb; + + shrinker_register(vb->shrinker); + + return 0; } static int virtballoon_probe(struct virtio_device *vdev)