From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.6 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7B13EC2D0A8 for ; Mon, 28 Sep 2020 10:37:40 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 08DA7221EC for ; Mon, 28 Sep 2020 10:37:39 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="FMq3kve5" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 08DA7221EC Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amazon.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 910926B006C; Mon, 28 Sep 2020 06:37:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8E5E26B006E; Mon, 28 Sep 2020 06:37:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7AE426B0070; Mon, 28 Sep 2020 06:37:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0218.hostedemail.com [216.40.44.218]) by kanga.kvack.org (Postfix) with ESMTP id 65D5D6B006C for ; Mon, 28 Sep 2020 06:37:39 -0400 (EDT) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 30004181AE863 for ; Mon, 28 Sep 2020 10:37:39 +0000 (UTC) X-FDA: 77312119038.20.roll02_2a0118927180 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin20.hostedemail.com (Postfix) with ESMTP id 09472180C07AB for ; Mon, 28 Sep 2020 10:37:39 +0000 (UTC) X-HE-Tag: roll02_2a0118927180 X-Filterd-Recvd-Size: 8775 Received: from smtp-fw-4101.amazon.com (smtp-fw-4101.amazon.com [72.21.198.25]) by imf42.hostedemail.com (Postfix) with ESMTP for ; Mon, 28 Sep 2020 10:37:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1601289458; x=1632825458; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=tdNdw6IfHLO/vMz+dCnfV6saHBkcPkuM1L7pDo6BFss=; b=FMq3kve59mMh30uA204nEgDsLNGwdyqquVWL7EmgYlSre4hIVmcjGwyP wXW/0FVcxmtAoWvfBwq6GbNwcpbpMdZR0VlDufqmLbNuqJTg2T4I7Pfrs cObjnqv7ubsvq0H5DwXHFEddf9HiZpORoepcRQ87cWHEkEayMZ8bQYP8J Y=; X-IronPort-AV: E=Sophos;i="5.77,313,1596499200"; d="scan'208";a="56682864" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-1a-16acd5e0.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-out-4101.iad4.amazon.com with ESMTP; 28 Sep 2020 10:37:37 +0000 Received: from EX13D31EUA001.ant.amazon.com (iad12-ws-svc-p26-lb9-vlan3.iad.amazon.com [10.40.163.38]) by email-inbound-relay-1a-16acd5e0.us-east-1.amazon.com (Postfix) with ESMTPS id 09157A1F79; Mon, 28 Sep 2020 10:37:24 +0000 (UTC) Received: from u3f2cd687b01c55.ant.amazon.com (10.43.161.237) by EX13D31EUA001.ant.amazon.com (10.43.165.15) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Mon, 28 Sep 2020 10:37:06 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [RFC PATCH 4/5] mm/page_idle: Avoid interferences from concurrent users Date: Mon, 28 Sep 2020 12:35:27 +0200 Message-ID: <20200928103528.4256-5-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200928103528.4256-1-sjpark@amazon.com> References: <20200928103528.4256-1-sjpark@amazon.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.43.161.237] X-ClientProxiedBy: EX13D10UWA003.ant.amazon.com (10.43.160.248) To EX13D31EUA001.ant.amazon.com (10.43.165.15) 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: From: SeongJae Park Concurrent Idle Page Tracking users can interfere each other because the interface doesn't provide a central rule for synchronization between the users. Users could implement their own synchronization rule, but even in that case, applications developed by different users would not know how to synchronize with others. To help this situation, this commit introduces a centralized synchronization infrastructure of Idle Page Tracking. In detail, this commit introduces a mutex lock for Idle Page Tracking, called 'page_idle_lock'. It is exposed to user space via a new bool sysfs file, '/sys/kernel/mm/page_idle/lock'. By writing to and reading from the file, users can hold/release and read status of the mutex. Writes to the Idle Page Tracking 'bitmap' file fails if the lock is not held, while reads of the file can be done regardless of the lock status. Note that users could still interfere each other if they abuse this locking rule. Nevertheless, this change will let them notice the rule. Signed-off-by: SeongJae Park --- .../admin-guide/mm/idle_page_tracking.rst | 22 +++++++--- mm/page_idle.c | 40 +++++++++++++++++++ 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/Documentation/admin-guide/mm/idle_page_tracking.rst b/Documentation/admin-guide/mm/idle_page_tracking.rst index df9394fb39c2..3f5e7a8b5b78 100644 --- a/Documentation/admin-guide/mm/idle_page_tracking.rst +++ b/Documentation/admin-guide/mm/idle_page_tracking.rst @@ -21,13 +21,13 @@ User API ======== The idle page tracking API is located at ``/sys/kernel/mm/page_idle``. -Currently, it consists of the only read-write file, -``/sys/kernel/mm/page_idle/bitmap``. +Currently, it consists of two read-write file, +``/sys/kernel/mm/page_idle/bitmap`` and ``/sys/kernel/mm/page_idle/lock``. -The file implements a bitmap where each bit corresponds to a memory page. The -bitmap is represented by an array of 8-byte integers, and the page at PFN #i is -mapped to bit #i%64 of array element #i/64, byte order is native. When a bit is -set, the corresponding page is idle. +The ``bitmap`` file implements a bitmap where each bit corresponds to a memory +page. The bitmap is represented by an array of 8-byte integers, and the page at +PFN #i is mapped to bit #i%64 of array element #i/64, byte order is native. +When a bit is set, the corresponding page is idle. A page is considered idle if it has not been accessed since it was marked idle (for more details on what "accessed" actually means see the :ref:`Implementation @@ -74,6 +74,16 @@ See :ref:`Documentation/admin-guide/mm/pagemap.rst ` for more information about ``/proc/pid/pagemap``, ``/proc/kpageflags``, and ``/proc/kpagecgroup``. +The ``lock`` file is for avoidance of interference from concurrent users. If +the content of the ``lock`` file is ``1``, it means the ``bitmap`` file is +currently being used by someone. While the content of the ``lock`` file is +``1``, writing ``1`` to the file fails. Therefore, users should first +successfully write ``1`` to the ``lock`` file before starting use of ``bitmap`` +file and write ``0`` to the ``lock`` file after they finished use of the +``bitmap`` file. If a user writes the ``bitmap`` file while the ``lock`` is +``0``, the write fails. Meanwhile, reads of the ``bitmap`` file success +regardless of the ``lock`` status. + .. _impl_details: Implementation Details diff --git a/mm/page_idle.c b/mm/page_idle.c index 144fb4ed961d..0aa45f848570 100644 --- a/mm/page_idle.c +++ b/mm/page_idle.c @@ -16,6 +16,8 @@ #define BITMAP_CHUNK_SIZE sizeof(u64) #define BITMAP_CHUNK_BITS (BITMAP_CHUNK_SIZE * BITS_PER_BYTE) +static DEFINE_MUTEX(page_idle_lock); + /* * Idle page tracking only considers user memory pages, for other types of * pages the idle flag is always unset and an attempt to set it is silently @@ -169,6 +171,9 @@ static ssize_t page_idle_bitmap_write(struct file *file, struct kobject *kobj, unsigned long pfn, end_pfn; int bit; + if (!mutex_is_locked(&page_idle_lock)) + return -EPERM; + if (pos % BITMAP_CHUNK_SIZE || count % BITMAP_CHUNK_SIZE) return -EINVAL; @@ -197,17 +202,52 @@ static ssize_t page_idle_bitmap_write(struct file *file, struct kobject *kobj, return (char *)in - buf; } +static ssize_t page_idle_lock_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + return sprintf(buf, "%d\n", mutex_is_locked(&page_idle_lock)); +} + +static ssize_t page_idle_lock_store(struct kobject *kobj, + struct kobj_attribute *attr, const char *buf, size_t count) +{ + bool do_lock; + int ret; + + ret = kstrtobool(buf, &do_lock); + if (ret < 0) + return ret; + + if (do_lock) { + if (!mutex_trylock(&page_idle_lock)) + return -EBUSY; + } else { + mutex_unlock(&page_idle_lock); + } + + return count; +} + static struct bin_attribute page_idle_bitmap_attr = __BIN_ATTR(bitmap, 0600, page_idle_bitmap_read, page_idle_bitmap_write, 0); +static struct kobj_attribute page_idle_lock_attr = + __ATTR(lock, 0600, page_idle_lock_show, page_idle_lock_store); + static struct bin_attribute *page_idle_bin_attrs[] = { &page_idle_bitmap_attr, NULL, }; +static struct attribute *page_idle_lock_attrs[] = { + &page_idle_lock_attr.attr, + NULL, +}; + static const struct attribute_group page_idle_attr_group = { .bin_attrs = page_idle_bin_attrs, + .attrs = page_idle_lock_attrs, .name = "page_idle", }; -- 2.17.1