From: Minchan Kim <minchan@kernel.org>
To: Dan Streetman <ddstreet@ieee.org>
Cc: Andrew Morton <akpm@linux-foundation.org>,
Linux-MM <linux-mm@kvack.org>,
linux-kernel <linux-kernel@vger.kernel.org>,
Sergey Senozhatsky <sergey.senozhatsky@gmail.com>,
Jerome Marchand <jmarchan@redhat.com>,
juno.choi@lge.com, seungho1.park@lge.com,
Luigi Semenzato <semenzato@google.com>,
Nitin Gupta <ngupta@vflare.org>,
Seth Jennings <sjennings@variantweb.net>,
ds2horner@gmail.com
Subject: Re: [PATCH 3/3] zram: add mem_used_max via sysfs
Date: Mon, 18 Aug 2014 08:39:40 +0900 [thread overview]
Message-ID: <20140817233940.GD11367@bbox> (raw)
In-Reply-To: <CALZtONDB5q56f1TUHgqbiJ4ZaP6Yk=GcNQw9DhvLhNyExdfQ4w@mail.gmail.com>
Hello Dan,
On Thu, Aug 14, 2014 at 11:09:05AM -0400, Dan Streetman wrote:
> On Wed, Aug 13, 2014 at 9:12 PM, Minchan Kim <minchan@kernel.org> wrote:
> > Normally, zram user can get maximum memory zsmalloc consumed via
> > polling mem_used_total with sysfs in userspace.
> >
> > But it has a critical problem because user can miss peak memory
> > usage during update interval of polling. For avoiding that,
> > user should poll it frequently with mlocking to avoid delay
> > when memory pressure is heavy so it would be handy if the
> > kernel supports the function.
> >
> > This patch adds mem_used_max via sysfs.
> >
> > Signed-off-by: Minchan Kim <minchan@kernel.org>
> > ---
> > Documentation/blockdev/zram.txt | 1 +
> > drivers/block/zram/zram_drv.c | 35 +++++++++++++++++++++++++++++++++--
> > drivers/block/zram/zram_drv.h | 2 ++
> > 3 files changed, 36 insertions(+), 2 deletions(-)
> >
> > diff --git a/Documentation/blockdev/zram.txt b/Documentation/blockdev/zram.txt
> > index 9f239ff8c444..3b2247c2d4cf 100644
> > --- a/Documentation/blockdev/zram.txt
> > +++ b/Documentation/blockdev/zram.txt
> > @@ -107,6 +107,7 @@ size of the disk when not in use so a huge zram is wasteful.
> > orig_data_size
> > compr_data_size
> > mem_used_total
> > + mem_used_max
> >
> > 8) Deactivate:
> > swapoff /dev/zram0
> > diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
> > index b48a3d0e9031..311699f18bd5 100644
> > --- a/drivers/block/zram/zram_drv.c
> > +++ b/drivers/block/zram/zram_drv.c
> > @@ -109,6 +109,30 @@ static ssize_t mem_used_total_show(struct device *dev,
> > return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
> > }
> >
> > +static ssize_t mem_used_max_reset(struct device *dev,
> > + struct device_attribute *attr, const char *buf, size_t len)
> > +{
> > + struct zram *zram = dev_to_zram(dev);
> > +
> > + down_write(&zram->init_lock);
> > + zram->max_used_bytes = 0;
> > + up_write(&zram->init_lock);
> > + return len;
> > +}
> > +
> > +static ssize_t mem_used_max_show(struct device *dev,
> > + struct device_attribute *attr, char *buf)
> > +{
> > + u64 max_used_bytes;
> > + struct zram *zram = dev_to_zram(dev);
> > +
> > + down_read(&zram->init_lock);
> > + max_used_bytes = zram->max_used_bytes;
> > + up_read(&zram->init_lock);
> > +
> > + return scnprintf(buf, PAGE_SIZE, "%llu\n", max_used_bytes);
> > +}
> > +
> > static ssize_t max_comp_streams_show(struct device *dev,
> > struct device_attribute *attr, char *buf)
> > {
> > @@ -474,6 +498,7 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
> > struct zram_meta *meta = zram->meta;
> > struct zcomp_strm *zstrm;
> > bool locked = false;
> > + u64 total_bytes;
> >
> > page = bvec->bv_page;
> > if (is_partial_io(bvec)) {
> > @@ -543,8 +568,8 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
> > goto out;
> > }
> >
> > - if (zram->limit_bytes &&
> > - zs_get_total_size_bytes(meta->mem_pool) > zram->limit_bytes) {
> > + total_bytes = zs_get_total_size_bytes(meta->mem_pool);
> > + if (zram->limit_bytes && total_bytes > zram->limit_bytes) {
>
> do you need to take the init_lock to read limit_bytes here? It could
> be getting changed between these checks...
The zram_bvec_write is protected by read-side init_lock while mem_limit_store
is proteced by write-side init_lock.
>
> > zs_free(meta->mem_pool, handle);
> > ret = -ENOMEM;
> > goto out;
> > @@ -578,6 +603,8 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
> > /* Update stats */
> > atomic64_add(clen, &zram->stats.compr_data_size);
> > atomic64_inc(&zram->stats.pages_stored);
> > +
> > + zram->max_used_bytes = max(zram->max_used_bytes, total_bytes);
>
> shouldn't max_used_bytes be atomic64_t? Or take the init_lock here?
>
> > out:
> > if (locked)
> > zcomp_strm_release(zram->comp, zstrm);
> > @@ -656,6 +683,7 @@ static void zram_reset_device(struct zram *zram, bool reset_capacity)
> > down_write(&zram->init_lock);
> >
> > zram->limit_bytes = 0;
> > + zram->max_used_bytes = 0;
> >
> > if (!init_done(zram)) {
> > up_write(&zram->init_lock);
> > @@ -897,6 +925,8 @@ static DEVICE_ATTR(initstate, S_IRUGO, initstate_show, NULL);
> > static DEVICE_ATTR(reset, S_IWUSR, NULL, reset_store);
> > static DEVICE_ATTR(orig_data_size, S_IRUGO, orig_data_size_show, NULL);
> > static DEVICE_ATTR(mem_used_total, S_IRUGO, mem_used_total_show, NULL);
> > +static DEVICE_ATTR(mem_used_max, S_IRUGO | S_IWUSR, mem_used_max_show,
> > + mem_used_max_reset);
> > static DEVICE_ATTR(mem_limit, S_IRUGO | S_IWUSR, mem_limit_show,
> > mem_limit_store);
> > static DEVICE_ATTR(max_comp_streams, S_IRUGO | S_IWUSR,
> > @@ -927,6 +957,7 @@ static struct attribute *zram_disk_attrs[] = {
> > &dev_attr_orig_data_size.attr,
> > &dev_attr_compr_data_size.attr,
> > &dev_attr_mem_used_total.attr,
> > + &dev_attr_mem_used_max.attr,
> > &dev_attr_mem_limit.attr,
> > &dev_attr_max_comp_streams.attr,
> > &dev_attr_comp_algorithm.attr,
> > diff --git a/drivers/block/zram/zram_drv.h b/drivers/block/zram/zram_drv.h
> > index 086c51782e75..aca09b18fcbd 100644
> > --- a/drivers/block/zram/zram_drv.h
> > +++ b/drivers/block/zram/zram_drv.h
> > @@ -111,6 +111,8 @@ struct zram {
> > */
> > u64 disksize; /* bytes */
> > u64 limit_bytes;
> > + u64 max_used_bytes;
> > +
> > int max_comp_streams;
> > struct zram_stats stats;
> > char compressor[10];
> > --
> > 2.0.0
> >
>
> --
> To unsubscribe, send a message with 'unsubscribe linux-mm' in
> the body to majordomo@kvack.org. For more info on Linux MM,
> see: http://www.linux-mm.org/ .
> Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
--
Kind regards,
Minchan Kim
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
prev parent reply other threads:[~2014-08-17 23:39 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-08-14 1:12 [PATCH 1/3] zsmalloc: move pages_allocated to zs_pool Minchan Kim
2014-08-14 1:12 ` [PATCH 2/3] zram: limit memory size for zram Minchan Kim
2014-08-14 1:12 ` [PATCH 3/3] zram: add mem_used_max via sysfs Minchan Kim
2014-08-14 10:29 ` David Horner
2014-08-17 23:37 ` Minchan Kim
2014-08-14 15:09 ` Dan Streetman
2014-08-14 15:32 ` David Horner
2014-08-14 16:23 ` David Horner
2014-08-14 19:11 ` Dan Streetman
2014-08-14 20:07 ` David Horner
2014-08-17 23:53 ` Minchan Kim
2014-08-17 23:39 ` Minchan Kim [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20140817233940.GD11367@bbox \
--to=minchan@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=ddstreet@ieee.org \
--cc=ds2horner@gmail.com \
--cc=jmarchan@redhat.com \
--cc=juno.choi@lge.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ngupta@vflare.org \
--cc=semenzato@google.com \
--cc=sergey.senozhatsky@gmail.com \
--cc=seungho1.park@lge.com \
--cc=sjennings@variantweb.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).