2022-06-27 12:36:47

by Alexander Atanasov

[permalink] [raw]
Subject: [PATCH 1/1] Create debugfs file with virtio balloon usage information

From: Alexander Atanasov <[email protected]>

Allow the guest to know how much it is ballooned by the host.
It is useful when debugging out of memory conditions.

When host gets back memory from the guest it is accounted
as used memory in the guest but the guest have no way to know
how much it is actually ballooned.

Signed-off-by: Alexander Atanasov <[email protected]>
---
drivers/virtio/virtio_balloon.c | 91 +++++++++++++++++++++++++++++++++
1 file changed, 91 insertions(+)

diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c
index b9737da6c4dd..a32a52c28a1f 100644
--- a/drivers/virtio/virtio_balloon.c
+++ b/drivers/virtio/virtio_balloon.c
@@ -10,6 +10,7 @@
#include <linux/virtio_balloon.h>
#include <linux/swap.h>
#include <linux/workqueue.h>
+#include <linux/debugfs.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/module.h>
@@ -731,6 +732,91 @@ static void report_free_page_func(struct work_struct *work)
}
}

+/*
+ * DEBUGFS Interface
+ */
+#ifdef CONFIG_DEBUG_FS
+
+/**
+ * virtio_balloon_debug_show - shows statistics of balloon operations.
+ * @f: pointer to the &struct seq_file.
+ * @offset: ignored.
+ *
+ * Provides the statistics that can be accessed in virtio-balloon in the debugfs.
+ *
+ * Return: zero on success or an error code.
+ */
+static int virtio_balloon_debug_show(struct seq_file *f, void *offset)
+{
+ struct virtio_balloon *b = f->private;
+ u32 num_pages;
+ struct sysinfo i;
+
+ si_meminfo(&i);
+
+ seq_printf(f, "%-22s:", "capabilities");
+
+ if (virtio_has_feature(b->vdev, VIRTIO_BALLOON_F_MUST_TELL_HOST))
+ seq_puts(f, " tell_host");
+
+ if (virtio_has_feature(b->vdev, VIRTIO_BALLOON_F_STATS_VQ))
+ seq_puts(f, " stats");
+
+ if (virtio_has_feature(b->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
+ seq_puts(f, " deflate_on_oom");
+
+ if (virtio_has_feature(b->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
+ seq_puts(f, " free_page_hint");
+
+ if (virtio_has_feature(b->vdev, VIRTIO_BALLOON_F_PAGE_POISON))
+ seq_puts(f, " page_poison");
+
+ if (virtio_has_feature(b->vdev, VIRTIO_BALLOON_F_REPORTING))
+ seq_puts(f, " reporting");
+
+ seq_puts(f, "\n");
+
+ seq_printf(f, "%-22s: %d\n", "page_size", 4096);
+
+ virtio_cread_le(b->vdev, struct virtio_balloon_config, actual,
+ &num_pages);
+ /* Memory returned to host or amount we can inflate if possible */
+ seq_printf(f, "%-22s: %u\n", "ballooned_pages", num_pages);
+
+ /* Total Memory for the guest from host */
+ seq_printf(f, "%-22s: %lu\n", "total_pages", i.totalram);
+
+ /* Current memory for the guest */
+ seq_printf(f, "%-22s: %lu\n", "current_pages", i.totalram-num_pages);
+
+ return 0;
+}
+
+DEFINE_SHOW_ATTRIBUTE(virtio_balloon_debug);
+
+static void virtio_balloon_debugfs_init(struct virtio_balloon *b)
+{
+ debugfs_create_file("virtio-balloon", 0444, NULL, b,
+ &virtio_balloon_debug_fops);
+}
+
+static void virtio_balloon_debugfs_exit(struct virtio_balloon *b)
+{
+ debugfs_remove(debugfs_lookup("virtio-balloon", NULL));
+}
+
+#else
+
+static inline void virtio_balloon_debugfs_init(struct virtio_balloon *b)
+{
+}
+
+static inline void virtio_balloon_debugfs_exit(struct virtio_balloon *b)
+{
+}
+
+#endif /* CONFIG_DEBUG_FS */
+
#ifdef CONFIG_BALLOON_COMPACTION
/*
* virtballoon_migratepage - perform the balloon page migration on behalf of
@@ -1019,6 +1105,9 @@ static int virtballoon_probe(struct virtio_device *vdev)

if (towards_target(vb))
virtballoon_changed(vdev);
+
+ virtio_balloon_debugfs_init(vb);
+
return 0;

out_unregister_oom:
@@ -1065,6 +1154,8 @@ static void virtballoon_remove(struct virtio_device *vdev)
{
struct virtio_balloon *vb = vdev->priv;

+ virtio_balloon_debugfs_exit(vb);
+
if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING))
page_reporting_unregister(&vb->pr_dev_info);
if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
--
2.25.1


2022-06-27 13:19:11

by Michael S. Tsirkin

[permalink] [raw]
Subject: Re: [PATCH 1/1] Create debugfs file with virtio balloon usage information

On Mon, Jun 27, 2022 at 12:20:38PM +0000, [email protected] wrote:
> From: Alexander Atanasov <[email protected]>
>
> Allow the guest to know how much it is ballooned by the host.
> It is useful when debugging out of memory conditions.
>
> When host gets back memory from the guest it is accounted
> as used memory in the guest but the guest have no way to know
> how much it is actually ballooned.
>
> Signed-off-by: Alexander Atanasov <[email protected]>
> ---
> drivers/virtio/virtio_balloon.c | 91 +++++++++++++++++++++++++++++++++
> 1 file changed, 91 insertions(+)
>
> diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c
> index b9737da6c4dd..a32a52c28a1f 100644
> --- a/drivers/virtio/virtio_balloon.c
> +++ b/drivers/virtio/virtio_balloon.c
> @@ -10,6 +10,7 @@
> #include <linux/virtio_balloon.h>
> #include <linux/swap.h>
> #include <linux/workqueue.h>
> +#include <linux/debugfs.h>
> #include <linux/delay.h>
> #include <linux/slab.h>
> #include <linux/module.h>
> @@ -731,6 +732,91 @@ static void report_free_page_func(struct work_struct *work)
> }
> }
>
> +/*
> + * DEBUGFS Interface
> + */
> +#ifdef CONFIG_DEBUG_FS
> +
> +/**
> + * virtio_balloon_debug_show - shows statistics of balloon operations.
> + * @f: pointer to the &struct seq_file.
> + * @offset: ignored.
> + *
> + * Provides the statistics that can be accessed in virtio-balloon in the debugfs.
> + *
> + * Return: zero on success or an error code.
> + */
> +static int virtio_balloon_debug_show(struct seq_file *f, void *offset)
> +{
> + struct virtio_balloon *b = f->private;
> + u32 num_pages;
> + struct sysinfo i;
> +
> + si_meminfo(&i);
> +
> + seq_printf(f, "%-22s:", "capabilities");

why aren't features in sysfs sufficient for this? pretty printing
can be done in userspace ...

> + if (virtio_has_feature(b->vdev, VIRTIO_BALLOON_F_MUST_TELL_HOST))
> + seq_puts(f, " tell_host");
> +
> + if (virtio_has_feature(b->vdev, VIRTIO_BALLOON_F_STATS_VQ))
> + seq_puts(f, " stats");
> +
> + if (virtio_has_feature(b->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
> + seq_puts(f, " deflate_on_oom");
> +
> + if (virtio_has_feature(b->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
> + seq_puts(f, " free_page_hint");
> +
> + if (virtio_has_feature(b->vdev, VIRTIO_BALLOON_F_PAGE_POISON))
> + seq_puts(f, " page_poison");
> +
> + if (virtio_has_feature(b->vdev, VIRTIO_BALLOON_F_REPORTING))
> + seq_puts(f, " reporting");
> +
> + seq_puts(f, "\n");
> +
> + seq_printf(f, "%-22s: %d\n", "page_size", 4096);
> +
> + virtio_cread_le(b->vdev, struct virtio_balloon_config, actual,
> + &num_pages);
> + /* Memory returned to host or amount we can inflate if possible */
> + seq_printf(f, "%-22s: %u\n", "ballooned_pages", num_pages);
> +
> + /* Total Memory for the guest from host */
> + seq_printf(f, "%-22s: %lu\n", "total_pages", i.totalram);
> +
> + /* Current memory for the guest */
> + seq_printf(f, "%-22s: %lu\n", "current_pages", i.totalram-num_pages);

spaces around -

> +
> + return 0;
> +}
> +
> +DEFINE_SHOW_ATTRIBUTE(virtio_balloon_debug);
> +
> +static void virtio_balloon_debugfs_init(struct virtio_balloon *b)
> +{
> + debugfs_create_file("virtio-balloon", 0444, NULL, b,
> + &virtio_balloon_debug_fops);
> +}
> +
> +static void virtio_balloon_debugfs_exit(struct virtio_balloon *b)
> +{
> + debugfs_remove(debugfs_lookup("virtio-balloon", NULL));
> +}
> +
> +#else
> +
> +static inline void virtio_balloon_debugfs_init(struct virtio_balloon *b)
> +{
> +}
> +
> +static inline void virtio_balloon_debugfs_exit(struct virtio_balloon *b)
> +{
> +}
> +
> +#endif /* CONFIG_DEBUG_FS */
> +
> #ifdef CONFIG_BALLOON_COMPACTION
> /*
> * virtballoon_migratepage - perform the balloon page migration on behalf of
> @@ -1019,6 +1105,9 @@ static int virtballoon_probe(struct virtio_device *vdev)
>
> if (towards_target(vb))
> virtballoon_changed(vdev);
> +
> + virtio_balloon_debugfs_init(vb);
> +
> return 0;
>
> out_unregister_oom:
> @@ -1065,6 +1154,8 @@ static void virtballoon_remove(struct virtio_device *vdev)
> {
> struct virtio_balloon *vb = vdev->priv;
>
> + virtio_balloon_debugfs_exit(vb);
> +
> if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING))
> page_reporting_unregister(&vb->pr_dev_info);
> if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
> --
> 2.25.1

2022-06-27 19:42:44

by Alexander Atanasov

[permalink] [raw]
Subject: [PATCH 1/1] Create debugfs file with virtio balloon usage information

Allow the guest to know how much it is ballooned by the host.
It is useful when debugging out of memory conditions.

When host gets back memory from the guest it is accounted
as used memory in the guest but the guest have no way to know
how much it is actually ballooned.

No pretty printing and fixed as per coding style.

Signed-off-by: Alexander Atanasov <[email protected]>
---
drivers/virtio/virtio_balloon.c | 71 +++++++++++++++++++++++++++++++++
1 file changed, 71 insertions(+)

- Leave pretty print to userspace
- Fixed coding style

diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c
index b9737da6c4dd..1bb6a6d0e37b 100644
--- a/drivers/virtio/virtio_balloon.c
+++ b/drivers/virtio/virtio_balloon.c
@@ -10,6 +10,7 @@
#include <linux/virtio_balloon.h>
#include <linux/swap.h>
#include <linux/workqueue.h>
+#include <linux/debugfs.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/module.h>
@@ -731,6 +732,71 @@ static void report_free_page_func(struct work_struct *work)
}
}

+/*
+ * DEBUGFS Interface
+ */
+#ifdef CONFIG_DEBUG_FS
+
+/**
+ * virtio_balloon_debug_show - shows statistics of balloon operations.
+ * @f: pointer to the &struct seq_file.
+ * @offset: ignored.
+ *
+ * Provides the statistics that can be accessed in virtio-balloon in the debugfs.
+ *
+ * Return: zero on success or an error code.
+ */
+static int virtio_balloon_debug_show(struct seq_file *f, void *offset)
+{
+ struct virtio_balloon *b = f->private;
+ u32 num_pages;
+ struct sysinfo i;
+
+ si_meminfo(&i);
+
+ seq_printf(f, "%-22s: %llx\n", "capabilities", b->vdev->features);
+
+ seq_printf(f, "%-22s: %d\n", "page_size", 4096);
+
+ virtio_cread_le(b->vdev, struct virtio_balloon_config, actual,
+ &num_pages);
+ /* Memory returned to host or amount we can inflate if possible */
+ seq_printf(f, "%-22s: %u\n", "ballooned_pages", num_pages);
+
+ /* Total Memory for the guest from host */
+ seq_printf(f, "%-22s: %lu\n", "total_pages", i.totalram);
+
+ /* Current memory for the guest */
+ seq_printf(f, "%-22s: %lu\n", "current_pages", i.totalram - num_pages);
+
+ return 0;
+}
+
+DEFINE_SHOW_ATTRIBUTE(virtio_balloon_debug);
+
+static void virtio_balloon_debugfs_init(struct virtio_balloon *b)
+{
+ debugfs_create_file("virtio-balloon", 0444, NULL, b,
+ &virtio_balloon_debug_fops);
+}
+
+static void virtio_balloon_debugfs_exit(struct virtio_balloon *b)
+{
+ debugfs_remove(debugfs_lookup("virtio-balloon", NULL));
+}
+
+#else
+
+static inline void virtio_balloon_debugfs_init(struct virtio_balloon *b)
+{
+}
+
+static inline void virtio_balloon_debugfs_exit(struct virtio_balloon *b)
+{
+}
+
+#endif /* CONFIG_DEBUG_FS */
+
#ifdef CONFIG_BALLOON_COMPACTION
/*
* virtballoon_migratepage - perform the balloon page migration on behalf of
@@ -1019,6 +1085,9 @@ static int virtballoon_probe(struct virtio_device *vdev)

if (towards_target(vb))
virtballoon_changed(vdev);
+
+ virtio_balloon_debugfs_init(vb);
+
return 0;

out_unregister_oom:
@@ -1065,6 +1134,8 @@ static void virtballoon_remove(struct virtio_device *vdev)
{
struct virtio_balloon *vb = vdev->priv;

+ virtio_balloon_debugfs_exit(vb);
+
if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING))
page_reporting_unregister(&vb->pr_dev_info);
if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
--
2.25.1

2022-06-27 21:12:40

by Michael S. Tsirkin

[permalink] [raw]
Subject: Re: [PATCH 1/1] Create debugfs file with virtio balloon usage information

On Mon, Jun 27, 2022 at 07:19:09PM +0000, Alexander Atanasov wrote:
> Allow the guest to know how much it is ballooned by the host.
> It is useful when debugging out of memory conditions.
>
> When host gets back memory from the guest it is accounted
> as used memory in the guest but the guest have no way to know
> how much it is actually ballooned.
>
> No pretty printing and fixed as per coding style.
>
> Signed-off-by: Alexander Atanasov <[email protected]>
> ---
> drivers/virtio/virtio_balloon.c | 71 +++++++++++++++++++++++++++++++++
> 1 file changed, 71 insertions(+)
>
> - Leave pretty print to userspace
> - Fixed coding style
>
> diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c
> index b9737da6c4dd..1bb6a6d0e37b 100644
> --- a/drivers/virtio/virtio_balloon.c
> +++ b/drivers/virtio/virtio_balloon.c
> @@ -10,6 +10,7 @@
> #include <linux/virtio_balloon.h>
> #include <linux/swap.h>
> #include <linux/workqueue.h>
> +#include <linux/debugfs.h>
> #include <linux/delay.h>
> #include <linux/slab.h>
> #include <linux/module.h>
> @@ -731,6 +732,71 @@ static void report_free_page_func(struct work_struct *work)
> }
> }
>
> +/*
> + * DEBUGFS Interface
> + */
> +#ifdef CONFIG_DEBUG_FS
> +
> +/**
> + * virtio_balloon_debug_show - shows statistics of balloon operations.
> + * @f: pointer to the &struct seq_file.
> + * @offset: ignored.
> + *
> + * Provides the statistics that can be accessed in virtio-balloon in the debugfs.
> + *
> + * Return: zero on success or an error code.
> + */
> +static int virtio_balloon_debug_show(struct seq_file *f, void *offset)
> +{
> + struct virtio_balloon *b = f->private;
> + u32 num_pages;
> + struct sysinfo i;
> +
> + si_meminfo(&i);
> +
> + seq_printf(f, "%-22s: %llx\n", "capabilities", b->vdev->features);

why do we need this in debugfs? Isn't this available in sysfs already?

> + seq_printf(f, "%-22s: %d\n", "page_size", 4096);

I suspect this function doesn't work properly when page size is not 4K.

> +
> + virtio_cread_le(b->vdev, struct virtio_balloon_config, actual,
> + &num_pages);
> + /* Memory returned to host or amount we can inflate if possible */
> + seq_printf(f, "%-22s: %u\n", "ballooned_pages", num_pages);

I don't really get the comment here.

> +
> + /* Total Memory for the guest from host */
> + seq_printf(f, "%-22s: %lu\n", "total_pages", i.totalram);
> +
> + /* Current memory for the guest */
> + seq_printf(f, "%-22s: %lu\n", "current_pages", i.totalram - num_pages);

Are you sure these are in units of 4Kbyte pages?

> + return 0;
> +}
> +
> +DEFINE_SHOW_ATTRIBUTE(virtio_balloon_debug);
> +
> +static void virtio_balloon_debugfs_init(struct virtio_balloon *b)
> +{
> + debugfs_create_file("virtio-balloon", 0444, NULL, b,
> + &virtio_balloon_debug_fops);
> +}
> +
> +static void virtio_balloon_debugfs_exit(struct virtio_balloon *b)
> +{
> + debugfs_remove(debugfs_lookup("virtio-balloon", NULL));
> +}
> +
> +#else
> +
> +static inline void virtio_balloon_debugfs_init(struct virtio_balloon *b)
> +{
> +}
> +
> +static inline void virtio_balloon_debugfs_exit(struct virtio_balloon *b)
> +{
> +}
> +
> +#endif /* CONFIG_DEBUG_FS */
> +
> #ifdef CONFIG_BALLOON_COMPACTION
> /*
> * virtballoon_migratepage - perform the balloon page migration on behalf of
> @@ -1019,6 +1085,9 @@ static int virtballoon_probe(struct virtio_device *vdev)
>
> if (towards_target(vb))
> virtballoon_changed(vdev);
> +
> + virtio_balloon_debugfs_init(vb);
> +
> return 0;
>
> out_unregister_oom:
> @@ -1065,6 +1134,8 @@ static void virtballoon_remove(struct virtio_device *vdev)
> {
> struct virtio_balloon *vb = vdev->priv;
>
> + virtio_balloon_debugfs_exit(vb);
> +
> if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING))
> page_reporting_unregister(&vb->pr_dev_info);
> if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
> --
> 2.25.1

2022-06-28 09:41:55

by Alexander Atanasov

[permalink] [raw]
Subject: Re: [PATCH 1/1] Create debugfs file with virtio balloon usage information

Hello,

On 27/06/2022 23:42, Michael S. Tsirkin wrote:
> On Mon, Jun 27, 2022 at 07:19:09PM +0000, Alexander Atanasov wrote:
>> Allow the guest to know how much it is ballooned by the host.
>> It is useful when debugging out of memory conditions.
>>
>> When host gets back memory from the guest it is accounted
>> as used memory in the guest but the guest have no way to know
>> how much it is actually ballooned.
>>
>> No pretty printing and fixed as per coding style.
>> ....
>> +static int virtio_balloon_debug_show(struct seq_file *f, void *offset)
>> +{
>> + struct virtio_balloon *b = f->private;
>> + u32 num_pages;
>> + struct sysinfo i;
>> +
>> + si_meminfo(&i);
>> +
>> + seq_printf(f, "%-22s: %llx\n", "capabilities", b->vdev->features);
> why do we need this in debugfs? Isn't this available in sysfs already?

Yes, it doesn't make sense to have it without pretty printing. I will
remove it.

>> + seq_printf(f, "%-22s: %d\n", "page_size", 4096);
> I suspect this function doesn't work properly when page size is not 4K.

It is the page size used by the balloon and it is always 4K and not the
page size used by the guest which can be different.

/*
 * Balloon device works in 4K page units.  So each page is pointed to by
 * multiple balloon pages.  All memory counters in this driver are in
balloon
 * page units.
 */

And the code agrees with the comment. To be consistent the file must use
the same units.


>> +
>> + virtio_cread_le(b->vdev, struct virtio_balloon_config, actual,
>> + &num_pages);
>> + /* Memory returned to host or amount we can inflate if possible */
>> + seq_printf(f, "%-22s: %u\n", "ballooned_pages", num_pages);
> I don't really get the comment here.

I will try to reword it to be more clear .

    /*
     * Pages allocated by host from the guest memory.
     * Host inflates the balloon to get more memory.
     * Guest needs to deflate the balloon to get more memory.
     */

>> + /* Total Memory for the guest from host */
>> + seq_printf(f, "%-22s: %lu\n", "total_pages", i.totalram);
>> +
>> + /* Current memory for the guest */
>> + seq_printf(f, "%-22s: %lu\n", "current_pages", i.totalram - num_pages);
> Are you sure these are in units of 4Kbyte pages?

The guest can have a different page size, so a conversion to balloon
page size is required - fix in the following patch .

--
Regards,
Alexander Atanasov

2022-06-28 09:47:24

by Alexander Atanasov

[permalink] [raw]
Subject: [PATCH v3 1/1] Create debugfs file with virtio balloon usage information

Allow the guest to know how much it is ballooned by the host.
It is useful when debugging out of memory conditions.

When host gets back memory from the guest it is accounted
as used memory in the guest but the guest have no way to know
how much it is actually ballooned.

Signed-off-by: Alexander Atanasov <[email protected]>
---
drivers/virtio/virtio_balloon.c | 78 +++++++++++++++++++++++++++++++++
1 file changed, 78 insertions(+)

V2:
- fixed coding style
- removed pretty print
V3:
- removed dublicate of features
- comment about balooned_pages more clear
- convert host pages to balloon pages

diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c
index b9737da6c4dd..65964afd4f13 100644
--- a/drivers/virtio/virtio_balloon.c
+++ b/drivers/virtio/virtio_balloon.c
@@ -10,6 +10,7 @@
#include <linux/virtio_balloon.h>
#include <linux/swap.h>
#include <linux/workqueue.h>
+#include <linux/debugfs.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/module.h>
@@ -731,6 +732,78 @@ static void report_free_page_func(struct work_struct *work)
}
}

+/*
+ * DEBUGFS Interface
+ */
+#ifdef CONFIG_DEBUG_FS
+
+#define guest_to_balloon_pages(i) ((i)*VIRTIO_BALLOON_PAGES_PER_PAGE)
+
+/**
+ * virtio_balloon_debug_show - shows statistics of balloon operations.
+ * @f: pointer to the &struct seq_file.
+ * @offset: ignored.
+ *
+ * Provides the statistics that can be accessed in virtio-balloon in the debugfs.
+ *
+ * Return: zero on success or an error code.
+ */
+
+static int virtio_balloon_debug_show(struct seq_file *f, void *offset)
+{
+ struct virtio_balloon *b = f->private;
+ u32 num_pages;
+ struct sysinfo i;
+
+ si_meminfo(&i);
+
+ seq_printf(f, "%-22s: %d\n", "page_size", 4096);
+
+ virtio_cread_le(b->vdev, struct virtio_balloon_config, actual,
+ &num_pages);
+ /*
+ * Pages allocated by host from the guest memory.
+ * Host inflates the balloon to get more memory.
+ * Guest needs to deflate the balloon to get more memory.
+ */
+ seq_printf(f, "%-22s: %u\n", "ballooned_pages", num_pages);
+
+ /* Total Memory for the guest from host */
+ seq_printf(f, "%-22s: %lu\n", "total_pages",
+ guest_to_balloon_pages(i.totalram));
+
+ /* Current memory for the guest */
+ seq_printf(f, "%-22s: %lu\n", "current_pages",
+ guest_to_balloon_pages(i.totalram) - num_pages);
+
+ return 0;
+}
+
+DEFINE_SHOW_ATTRIBUTE(virtio_balloon_debug);
+
+static void virtio_balloon_debugfs_init(struct virtio_balloon *b)
+{
+ debugfs_create_file("virtio-balloon", 0444, NULL, b,
+ &virtio_balloon_debug_fops);
+}
+
+static void virtio_balloon_debugfs_exit(struct virtio_balloon *b)
+{
+ debugfs_remove(debugfs_lookup("virtio-balloon", NULL));
+}
+
+#else
+
+static inline void virtio_balloon_debugfs_init(struct virtio_balloon *b)
+{
+}
+
+static inline void virtio_balloon_debugfs_exit(struct virtio_balloon *b)
+{
+}
+
+#endif /* CONFIG_DEBUG_FS */
+
#ifdef CONFIG_BALLOON_COMPACTION
/*
* virtballoon_migratepage - perform the balloon page migration on behalf of
@@ -1019,6 +1092,9 @@ static int virtballoon_probe(struct virtio_device *vdev)

if (towards_target(vb))
virtballoon_changed(vdev);
+
+ virtio_balloon_debugfs_init(vb);
+
return 0;

out_unregister_oom:
@@ -1065,6 +1141,8 @@ static void virtballoon_remove(struct virtio_device *vdev)
{
struct virtio_balloon *vb = vdev->priv;

+ virtio_balloon_debugfs_exit(vb);
+
if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING))
page_reporting_unregister(&vb->pr_dev_info);
if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
--
2.25.1

2022-06-28 13:07:46

by Vasily Averin

[permalink] [raw]
Subject: Re: [PATCH v3 1/1] Create debugfs file with virtio balloon usage information

On 6/28/22 12:23, Alexander Atanasov wrote:
> +static int virtio_balloon_debug_show(struct seq_file *f, void *offset)
> +{
> + struct virtio_balloon *b = f->private;
> + u32 num_pages;
> + struct sysinfo i;
> +
> + si_meminfo(&i);
> +
> + seq_printf(f, "%-22s: %d\n", "page_size", 4096);

This output of the constant looks strange for me.
Could you please explain why this is required?

Thank you,
Vasily Averin

2022-06-28 13:16:29

by Alexander Atanasov

[permalink] [raw]
Subject: Re: [PATCH v3 1/1] Create debugfs file with virtio balloon usage information

Hello,

On 28/06/2022 15:55, Vasily Averin wrote:
> On 6/28/22 12:23, Alexander Atanasov wrote:
>> +static int virtio_balloon_debug_show(struct seq_file *f, void *offset)
>> +{
>> + struct virtio_balloon *b = f->private;
>> + u32 num_pages;
>> + struct sysinfo i;
>> +
>> + si_meminfo(&i);
>> +
>> + seq_printf(f, "%-22s: %d\n", "page_size", 4096);
> This output of the constant looks strange for me.
> Could you please explain why this is required?

Ballon driver always works in 4K page units. The values in the file are in the driver units.
Guests need it to be able to perform calculations of actual values.
In the case of virtio_balloon it is always 4K but other balloon drivers can have different page_sizes.


--
Regards,
Alexander Atanasov