Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp3201158pxk; Mon, 5 Oct 2020 04:00:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyyzXpkkZ33dKJ+5yrJFysDArPTpiZOAZHzveWsVQ8HrfWys0tW/kAm8wGHVbvRUhOb6oxT X-Received: by 2002:a17:906:e0cb:: with SMTP id gl11mr14840595ejb.486.1601895628771; Mon, 05 Oct 2020 04:00:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601895628; cv=none; d=google.com; s=arc-20160816; b=Ki+6Qi4blcOQvYI2vFVXFf3XSnwZ9wDGypUpwhWJjmmNcpvebUyboFxIX4LWOcCxDN R/hvaLO+QIoJt+MpoSv9QGA9CIg3mBjr6/0Vt2XC3jIwu//3VMC1T9SHFRgp/IJ/tj6l WQiFyXV3A4oARKW05BXhrgb3/Kp5BTeOyTDNySfkEV1HQAU8qGwPVubueQs1xu9fuH8G 96rKRRs7DIvA7Qmo0kb8eMmywIyBNBRBlh923ZuQpsdseBCX+4ILqXBf5LuXNYJBDBGF zRSg3zTajBe8qjTagW2bj/Ea6wyqa03fHzTr+KxJk8P+Bf/P+34oEWqGJA3g5CySWPe1 08Gw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=tdNdw6IfHLO/vMz+dCnfV6saHBkcPkuM1L7pDo6BFss=; b=yq0IK2hmyI5NTouwDl174K64G0EF9unQc/vCebLrP4X8VoPgbH7zI2JUssa89YKJA4 wKAWVwjBX4c4K0tOCW0fttwUK1qJoxkFbWA2LCV+9FSl2e3LunZJ/GXGAeQYVzAMhri6 F9RpkROlWCsKeI80BfVC0xHEzUqoRVSFsHRMcZ0kRh59bVUf7g/o4RVwLl9u51gozhwJ KgaoVt5TRpUVR29dvmLEm/upqEMPyti2YfAQ+d6m4k1UZIqvMgXtlTv6QlqXBlq3HH4G IHcOK1cEy2hkRvxM13zPe/9AgurZWjwnzS6ceNg1WiH1OxK1kaxeIYYb5Xs4nSqKcfRb rr5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=l8rp9WNl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x4si3377251edr.111.2020.10.05.04.00.05; Mon, 05 Oct 2020 04:00:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=l8rp9WNl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726258AbgJEK6E (ORCPT + 99 others); Mon, 5 Oct 2020 06:58:04 -0400 Received: from smtp-fw-4101.amazon.com ([72.21.198.25]:37858 "EHLO smtp-fw-4101.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725974AbgJEK6B (ORCPT ); Mon, 5 Oct 2020 06:58:01 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1601895480; x=1633431480; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=tdNdw6IfHLO/vMz+dCnfV6saHBkcPkuM1L7pDo6BFss=; b=l8rp9WNlr5Hs7JiaywwDs4EHpc99l2ZqYgeDEoMvnr4CH+sTyQVemGxS KfzgRhDOHKwHoX/JgVcID7Yj66hofYorspZooqHOckF0EdUepgtq2OFki SvGJMyZAj2HQpkD6sCvwLBke7GXFaswfjfhD57vReV9ypmz8ylushjpVl Y=; X-IronPort-AV: E=Sophos;i="5.77,338,1596499200"; d="scan'208";a="58022590" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-1d-38ae4ad2.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-out-4101.iad4.amazon.com with ESMTP; 05 Oct 2020 10:57:59 +0000 Received: from EX13D31EUA001.ant.amazon.com (iad12-ws-svc-p26-lb9-vlan2.iad.amazon.com [10.40.163.34]) by email-inbound-relay-1d-38ae4ad2.us-east-1.amazon.com (Postfix) with ESMTPS id 57206A1CCB; Mon, 5 Oct 2020 10:57:56 +0000 (UTC) Received: from u3f2cd687b01c55.ant.amazon.com (10.43.160.146) by EX13D31EUA001.ant.amazon.com (10.43.165.15) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Mon, 5 Oct 2020 10:57:40 +0000 From: SeongJae Park To: CC: SeongJae Park , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v21 07/18] mm/page_idle: Avoid interferences from concurrent users Date: Mon, 5 Oct 2020 12:55:11 +0200 Message-ID: <20201005105522.23841-8-sjpark@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20201005105522.23841-1-sjpark@amazon.com> References: <20201005105522.23841-1-sjpark@amazon.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.43.160.146] X-ClientProxiedBy: EX13d09UWC002.ant.amazon.com (10.43.162.102) To EX13D31EUA001.ant.amazon.com (10.43.165.15) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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