From patchwork Fri Jul 20 08:33:02 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wang, Wei W" X-Patchwork-Id: 10536269 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id EF09D6029B for ; Fri, 20 Jul 2018 09:00:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E57D429147 for ; Fri, 20 Jul 2018 09:00:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D89F529155; Fri, 20 Jul 2018 09:00:45 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2CBD129147 for ; Fri, 20 Jul 2018 09:00:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9FAAC6B026B; Fri, 20 Jul 2018 05:00:43 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 9D2776B026C; Fri, 20 Jul 2018 05:00:43 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8C2E26B026D; Fri, 20 Jul 2018 05:00:43 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pg1-f200.google.com (mail-pg1-f200.google.com [209.85.215.200]) by kanga.kvack.org (Postfix) with ESMTP id 4E0F36B026B for ; Fri, 20 Jul 2018 05:00:43 -0400 (EDT) Received: by mail-pg1-f200.google.com with SMTP id y16-v6so5503061pgv.23 for ; Fri, 20 Jul 2018 02:00:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=aTSiq9jIzZK8jhZEKfbr6g0OHHuK99X6OURdPXqdDWY=; b=HMCfpNCU3+cGrIMwyF/doVBClrH545yfjOD7ZckuOODtQRIhdcG5VTzkmju7S0ca4U gZAGyu8vGjV35qb473jgpNJ4jOdgmUkiF+NmXyNeprVahNKA+fazJMEh2HgfCwQFM9/6 oZUiT29zVcbeWVyRoWOJln40gJch2BUquPQt/Qk/+/GtcR7KUn+v6+IAHa4YpuT81NyB YNEsHQYvCDGleVQiAQf9wPAeSUyUHB4nTc74lC2ScZFUazWy95N+xccqCFsddY19RKyK CqYDM1DLHsBAWlox7htUqy2GgLzv6JAUyjj2h/0gTaU4HNgrdMYKITIVKWkTsww36LjV xpzg== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of wei.w.wang@intel.com designates 192.55.52.120 as permitted sender) smtp.mailfrom=wei.w.wang@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Gm-Message-State: AOUpUlEsSE6dDvz+mCsmLWsqjK+nDI98g0AZgfPrjd2OsgInsIFJm3v7 KsMvnoCArKrX2DwleOt4rEDZyVciOXaRQ/BH4Wb/5sMliUugRT0aLxu7YozawnXP+N3fmDij6It sN29WqHbnmvzQIDhV6Te10K997Pn11EKG5qKSwP6Izt0tueeFzEif0rS4ipniup522A== X-Received: by 2002:a17:902:a981:: with SMTP id bh1-v6mr1294768plb.2.1532077242903; Fri, 20 Jul 2018 02:00:42 -0700 (PDT) X-Google-Smtp-Source: AAOMgpc3tCO5d6Ec8g/jSPYIDFY9UmpsYSXmg4cXRowa6u/O/4UFh8iKpvATJKgW6bm/3PHNzQ0T X-Received: by 2002:a17:902:a981:: with SMTP id bh1-v6mr1294705plb.2.1532077242078; Fri, 20 Jul 2018 02:00:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1532077242; cv=none; d=google.com; s=arc-20160816; b=KRLN1d5SzcxvkaOOyA9FS+4dIEuEkgmsy5ymaNSz+EzhXb6d8VGFjdh/0p7spJMkwq rvgddytPmwE4uItfAtauiqr47oVkKH0IoeUa+p6MCx04R2n9ZjqDC9gmIVxUBhxj8/Bg ow/kv8goHHb0HlBOdMUSdr6muphmMch+BYICIcp19cy9VLyPMMPVVr+T0x49Yj5Ia6BJ iIetuhb7hqAAHNRakbND1K2xIRdthSjmwy7knJ250sT1XGdbo5Tpoq/h31IV1hVvj+zM h/ATkaybu6SOljMSBu3zy1GztcH+spxANAOGtr6nM9E8QMN2z16ULxnZJkL4w8DvEbtV lOog== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=aTSiq9jIzZK8jhZEKfbr6g0OHHuK99X6OURdPXqdDWY=; b=X2bD3v9/iRgc7OIgKzAGYuAD0MVgUnqKoOHYel8U38rY21PuFM4+z85FeUwQAHhJ2Y wYEML2HuEsHGP/Ig6Rz/c0nZBsk++Ss7xm6F8bl4+S3L9DYkomsSm/b15H6t/zjVE2hf Ldc9g0aJRRP8kJfAqmMP2kl7kGzx8xaACrBLkPklWtUL7EKbY5doubevHXzW2r2WQ091 js9/GatHVEGv4GIF5zk50+H9JMUYuWL3rhNDMcp73l9sIW+A02mD8ErzUU/wB9kruv1i PbrDOIAYz3ug92pSs+IpfGzH0y7vDl7fybWVOD8iBUlhobq0DzU3Ao+reersdy4uNE37 KwQw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of wei.w.wang@intel.com designates 192.55.52.120 as permitted sender) smtp.mailfrom=wei.w.wang@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from mga04.intel.com (mga04.intel.com. [192.55.52.120]) by mx.google.com with ESMTPS id w15-v6si1381027pga.30.2018.07.20.02.00.41 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 20 Jul 2018 02:00:42 -0700 (PDT) Received-SPF: pass (google.com: domain of wei.w.wang@intel.com designates 192.55.52.120 as permitted sender) client-ip=192.55.52.120; Authentication-Results: mx.google.com; spf=pass (google.com: domain of wei.w.wang@intel.com designates 192.55.52.120 as permitted sender) smtp.mailfrom=wei.w.wang@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 20 Jul 2018 02:00:39 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,378,1526367600"; d="scan'208";a="66141356" Received: from devel-ww.sh.intel.com ([10.239.48.110]) by FMSMGA003.fm.intel.com with ESMTP; 20 Jul 2018 02:00:25 -0700 From: Wei Wang To: virtio-dev@lists.oasis-open.org, linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org, kvm@vger.kernel.org, linux-mm@kvack.org, mst@redhat.com, mhocko@kernel.org, akpm@linux-foundation.org, torvalds@linux-foundation.org Cc: pbonzini@redhat.com, wei.w.wang@intel.com, liliang.opensource@gmail.com, yang.zhang.wz@gmail.com, quan.xu0@gmail.com, nilal@redhat.com, riel@redhat.com, peterx@redhat.com Subject: [PATCH v36 2/5] virtio_balloon: replace oom notifier with shrinker Date: Fri, 20 Jul 2018 16:33:02 +0800 Message-Id: <1532075585-39067-3-git-send-email-wei.w.wang@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1532075585-39067-1-git-send-email-wei.w.wang@intel.com> References: <1532075585-39067-1-git-send-email-wei.w.wang@intel.com> X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP The OOM notifier is getting deprecated to use for the reasons mentioned here by Michal Hocko: https://lkml.org/lkml/2018/7/12/314 This patch replaces the virtio-balloon oom notifier with a shrinker to release balloon pages on memory pressure. In addition, the bug in the replaced virtballoon_oom_notify that only VIRTIO_BALLOON_ARRAY_PFNS_MAX (i.e 256) balloon pages can be freed though the user has specified more than that number is fixed in the shrinker_scan function. Signed-off-by: Wei Wang Cc: Michael S. Tsirkin Cc: Michal Hocko Cc: Andrew Morton Cc: Linus Torvalds --- drivers/virtio/virtio_balloon.c | 113 +++++++++++++++++++++++----------------- 1 file changed, 65 insertions(+), 48 deletions(-) diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c index 9356a1a..c6fd406 100644 --- a/drivers/virtio/virtio_balloon.c +++ b/drivers/virtio/virtio_balloon.c @@ -27,7 +27,6 @@ #include #include #include -#include #include #include #include @@ -40,12 +39,12 @@ */ #define VIRTIO_BALLOON_PAGES_PER_PAGE (unsigned)(PAGE_SIZE >> VIRTIO_BALLOON_PFN_SHIFT) #define VIRTIO_BALLOON_ARRAY_PFNS_MAX 256 -#define OOM_VBALLOON_DEFAULT_PAGES 256 +#define DEFAULT_BALLOON_PAGES_TO_SHRINK 256 #define VIRTBALLOON_OOM_NOTIFY_PRIORITY 80 -static int oom_pages = OOM_VBALLOON_DEFAULT_PAGES; -module_param(oom_pages, int, S_IRUSR | S_IWUSR); -MODULE_PARM_DESC(oom_pages, "pages to free on OOM"); +static unsigned long balloon_pages_to_shrink = DEFAULT_BALLOON_PAGES_TO_SHRINK; +module_param(balloon_pages_to_shrink, ulong, 0600); +MODULE_PARM_DESC(balloon_pages_to_shrink, "pages to free on memory presure"); #ifdef CONFIG_BALLOON_COMPACTION static struct vfsmount *balloon_mnt; @@ -86,8 +85,8 @@ struct virtio_balloon { /* Memory statistics */ struct virtio_balloon_stat stats[VIRTIO_BALLOON_S_NR]; - /* To register callback in oom notifier call chain */ - struct notifier_block nb; + /* To register a shrinker to shrink memory upon memory pressure */ + struct shrinker shrinker; }; static struct virtio_device_id id_table[] = { @@ -365,38 +364,6 @@ static void update_balloon_size(struct virtio_balloon *vb) &actual); } -/* - * virtballoon_oom_notify - release pages when system is under severe - * memory pressure (called from out_of_memory()) - * @self : notifier block struct - * @dummy: not used - * @parm : returned - number of freed pages - * - * The balancing of memory by use of the virtio balloon should not cause - * the termination of processes while there are pages in the balloon. - * If virtio balloon manages to release some memory, it will make the - * system return and retry the allocation that forced the OOM killer - * to run. - */ -static int virtballoon_oom_notify(struct notifier_block *self, - unsigned long dummy, void *parm) -{ - struct virtio_balloon *vb; - unsigned long *freed; - unsigned num_freed_pages; - - vb = container_of(self, struct virtio_balloon, nb); - if (!virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM)) - return NOTIFY_OK; - - freed = parm; - num_freed_pages = leak_balloon(vb, oom_pages); - update_balloon_size(vb); - *freed += num_freed_pages; - - return NOTIFY_OK; -} - static void update_balloon_stats_func(struct work_struct *work) { struct virtio_balloon *vb; @@ -548,6 +515,61 @@ static struct file_system_type balloon_fs = { #endif /* CONFIG_BALLOON_COMPACTION */ +static unsigned long virtio_balloon_shrinker_scan(struct shrinker *shrinker, + struct shrink_control *sc) +{ + unsigned long pages_to_free = balloon_pages_to_shrink, + pages_freed = 0; + struct virtio_balloon *vb = container_of(shrinker, + struct virtio_balloon, shrinker); + + /* + * One invocation of leak_balloon can deflate at most + * VIRTIO_BALLOON_ARRAY_PFNS_MAX balloon pages, so we call it + * multiple times to deflate pages till reaching + * balloon_pages_to_shrink pages. + */ + while (vb->num_pages && pages_to_free) { + pages_to_free = balloon_pages_to_shrink - pages_freed; + pages_freed += leak_balloon(vb, pages_to_free); + } + update_balloon_size(vb); + + return pages_freed / VIRTIO_BALLOON_PAGES_PER_PAGE; +} + +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); + + /* + * We continue to use VIRTIO_BALLOON_F_DEFLATE_ON_OOM to handle the + * case when shrinker needs to be invoked to relieve memory pressure. + */ + if (!virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM)) + return 0; + + return min_t(unsigned long, vb->num_pages, balloon_pages_to_shrink) / + VIRTIO_BALLOON_PAGES_PER_PAGE; +} + +static void virtio_balloon_unregister_shrinker(struct virtio_balloon *vb) +{ + unregister_shrinker(&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.batch = 0; + vb->shrinker.seeks = DEFAULT_SEEKS; + + return register_shrinker(&vb->shrinker); +} + static int virtballoon_probe(struct virtio_device *vdev) { struct virtio_balloon *vb; @@ -580,17 +602,10 @@ static int virtballoon_probe(struct virtio_device *vdev) if (err) goto out_free_vb; - vb->nb.notifier_call = virtballoon_oom_notify; - vb->nb.priority = VIRTBALLOON_OOM_NOTIFY_PRIORITY; - err = register_oom_notifier(&vb->nb); - if (err < 0) - goto out_del_vqs; - #ifdef CONFIG_BALLOON_COMPACTION balloon_mnt = kern_mount(&balloon_fs); if (IS_ERR(balloon_mnt)) { err = PTR_ERR(balloon_mnt); - unregister_oom_notifier(&vb->nb); goto out_del_vqs; } @@ -599,12 +614,14 @@ static int virtballoon_probe(struct virtio_device *vdev) if (IS_ERR(vb->vb_dev_info.inode)) { err = PTR_ERR(vb->vb_dev_info.inode); kern_unmount(balloon_mnt); - unregister_oom_notifier(&vb->nb); vb->vb_dev_info.inode = NULL; goto out_del_vqs; } vb->vb_dev_info.inode->i_mapping->a_ops = &balloon_aops; #endif + err = virtio_balloon_register_shrinker(vb); + if (err) + goto out_del_vqs; virtio_device_ready(vdev); @@ -637,7 +654,7 @@ static void virtballoon_remove(struct virtio_device *vdev) { struct virtio_balloon *vb = vdev->priv; - unregister_oom_notifier(&vb->nb); + virtio_balloon_unregister_shrinker(vb); spin_lock_irq(&vb->stop_update_lock); vb->stop_update = true;