linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Usama Arif <usamaarif642@gmail.com>
To: Shakeel Butt <shakeel.butt@linux.dev>, yosryahmed@google.com
Cc: akpm@linux-foundation.org, hannes@cmpxchg.org,
	willy@infradead.org, nphamcs@gmail.com, chengming.zhou@linux.dev,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	kernel-team@meta.com
Subject: Re: [PATCH v2 1/2] mm: clear pte for folios that are zero filled
Date: Fri, 7 Jun 2024 11:40:39 +0100	[thread overview]
Message-ID: <e695bf8e-9745-41ba-9986-fc888df71d3f@gmail.com> (raw)
In-Reply-To: <nes73bwc5p6yhwt5tw3upxcqrn5kenn6lvqb6exrf4yppmz6jx@ywhuevpkxlvh>


On 05/06/2024 09:55, Shakeel Butt wrote:
> On Tue, Jun 04, 2024 at 11:58:24AM GMT, Usama Arif wrote:
> [...]
>>   
>> +static bool is_folio_page_zero_filled(struct folio *folio, int i)
>> +{
>> +	unsigned long *data;
>> +	unsigned int pos, last_pos = PAGE_SIZE / sizeof(*data) - 1;
>> +	bool ret = false;
>> +
>> +	data = kmap_local_folio(folio, i * PAGE_SIZE);
>> +
>> +	if (data[last_pos])
>> +		goto out;
>> +
> Use memchr_inv() instead of the following.

I had done some benchmarking before sending v1 and this version is 35% 
faster than using memchr_inv(). Its likely because this does long 
comparison, while memchr_inv does a byte comparison using check_bytes8 
[1]. I will stick with the current version for my next revision. I have 
added the kernel module I used for benchmarking below:

[308797.975269] Time taken for orig: 2850 ms
[308801.911439] Time taken for memchr_inv: 3936 ms

[1] https://elixir.bootlin.com/linux/v6.9.3/source/lib/string.c#L800


#include <linux/time.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/string.h>

#define ITERATIONS 10000000
static int is_page_zero_filled(void *ptr, unsigned long *value)
{
     unsigned long *page;
     unsigned long val;
     unsigned int pos, last_pos = PAGE_SIZE / sizeof(*page) - 1;

     page = (unsigned long *)ptr;
     val = page[0];

     if (page[last_pos] != 0)
         return 0;

     for (pos = 1; pos < last_pos; pos++) {
         if (page[pos] != 0)
             return 0;
     }

     *value = val;

     return 1;
}

static int is_page_zero_filled_memchr_inv(void *ptr, unsigned long *value)
{
     unsigned long *page;
     unsigned long val;
     unsigned long *ret;
     page = (unsigned long *)ptr;

     val = page[0];
     *value = val;

     ret = memchr_inv(ptr, 0, PAGE_SIZE);

     return ret == NULL ? 1: 0;
}

static int __init zsmalloc_test_init(void)
{
     unsigned long *src;
     unsigned long value;
     ktime_t start_time, end_time;
     volatile int res = 0;
     unsigned long milliseconds;

     src = kmalloc(PAGE_SIZE, GFP_KERNEL);
     if (!src)
         return -ENOMEM;

     for (unsigned int pos = 0; pos <= PAGE_SIZE / sizeof(*src) - 1; 
pos++) {
         src[pos] = 0x0;
     }

     start_time = ktime_get();
     for (int i = 0; i < ITERATIONS; i++)
         res = is_page_zero_filled(src, &value);
     end_time = ktime_get();
     milliseconds = ktime_ms_delta(end_time, start_time);
     // printk(KERN_INFO "Result: %d, Value: %lu\n", res, value);
     printk(KERN_INFO "Time taken for orig: %lu ms\n", milliseconds);

     start_time = ktime_get();
     for (int i = 0; i < ITERATIONS; i++)
         res = is_page_zero_filled_memchr_inv(src, &value);
     end_time = ktime_get();
     milliseconds = ktime_ms_delta(end_time, start_time);
     // printk(KERN_INFO "Result: %d, Value: %lu\n", res, value);
     printk(KERN_INFO "Time taken for memchr_inv: %lu ms\n", milliseconds);

     kfree(src);
     // Dont insmod so that you can re-run
     return -1;
}

module_init(zsmalloc_test_init);
MODULE_LICENSE("GPL");



  reply	other threads:[~2024-06-07 10:40 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-04 10:58 [PATCH v2 0/2] " Usama Arif
2024-06-04 10:58 ` [PATCH v2 1/2] " Usama Arif
2024-06-04 12:18   ` Matthew Wilcox
2024-06-04 12:42     ` Usama Arif
2024-06-04 12:30   ` David Hildenbrand
2024-06-04 12:43     ` David Hildenbrand
2024-06-07 10:24       ` Usama Arif
2024-06-07 11:16         ` David Hildenbrand
2024-06-05  8:55   ` Shakeel Butt
2024-06-07 10:40     ` Usama Arif [this message]
2024-06-04 10:58 ` [PATCH v2 2/2] mm: remove code to handle same filled pages Usama Arif

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=e695bf8e-9745-41ba-9986-fc888df71d3f@gmail.com \
    --to=usamaarif642@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=chengming.zhou@linux.dev \
    --cc=hannes@cmpxchg.org \
    --cc=kernel-team@meta.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=nphamcs@gmail.com \
    --cc=shakeel.butt@linux.dev \
    --cc=willy@infradead.org \
    --cc=yosryahmed@google.com \
    /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