From patchwork Thu Dec 17 18:15:44 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Gordon X-Patchwork-Id: 7875971 Return-Path: X-Original-To: patchwork-intel-gfx@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 591FB9F387 for ; Thu, 17 Dec 2015 18:16:16 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 6CFEB20437 for ; Thu, 17 Dec 2015 18:16:15 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id 8732D2025B for ; Thu, 17 Dec 2015 18:16:14 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 0B8916E9E9; Thu, 17 Dec 2015 10:16:14 -0800 (PST) X-Original-To: intel-gfx@lists.freedesktop.org Delivered-To: intel-gfx@lists.freedesktop.org Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by gabe.freedesktop.org (Postfix) with ESMTP id A758C6E9E9 for ; Thu, 17 Dec 2015 10:16:12 -0800 (PST) Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga103.jf.intel.com with ESMTP; 17 Dec 2015 10:16:12 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.20,442,1444719600"; d="scan'208";a="619712305" Received: from dsgordon-linux2.isw.intel.com ([10.102.226.88]) by FMSMGA003.fm.intel.com with ESMTP; 17 Dec 2015 10:16:07 -0800 From: Dave Gordon To: intel-gfx@lists.freedesktop.org Date: Thu, 17 Dec 2015 18:15:44 +0000 Message-Id: <1450376144-32792-1-git-send-email-david.s.gordon@intel.com> X-Mailer: git-send-email 1.9.1 Organization: Intel Corporation (UK) Ltd. - Co. Reg. #1134945 - Pipers Way, Swindon SN3 1RJ References: <1449244734-25733-1-git-send-email-chris@chris-wilson.co.uk> References: <20151208112225.GB25800@dhcp22.suse.cz> Cc: Michal Hocko , linux-mm@kvack.org, "Goel, Akash" , Johannes Weiner Subject: [Intel-gfx] [PATCH v3] mm: Export {__}get_nr_swap_pages() X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Some modules, like i915.ko, use swappable objects and may try to swap them out under memory pressure (via the shrinker). Before doing so, they want to check using get_nr_swap_pages() to see if any swap space is available as otherwise they will waste time purging the object from the device without recovering any memory for the system. This requires the kernel function get_nr_swap_pages() to be exported to the modules. The current implementation of this function is as a static inline inside the header file swap.h>; this doesn't work when compiled in a module, as the necessary global data is not visible. The original proposed solution was to export the kernel global variable to modules, but this was considered poor practice as it exposed more than necessary, and in an uncontrolled fashion. Another idea was to turn it into a real (non-inline) function; however this was considered to unnecessarily add overhead for users within the base kernel. Therefore, to avoid both objections, this patch leaves the base kernel implementation unchanged, but adds a separate (read-only) functional interface for callers in loadable kernel modules (LKMs). Which definition is visible to code depends on the compile-time symbol MODULE, defined by the Kbuild system when building an LKM. Signed-off-by: Dave Gordon Cc: Chris Wilson Cc: "Goel, Akash" Cc: Michal Hocko Cc: Johannes Weiner Cc: linux-mm@kvack.org Cc: intel-gfx@lists.freedesktop.org --- include/linux/swap.h | 12 ++++++++++++ mm/swapfile.c | 7 +++++++ 2 files changed, 19 insertions(+) diff --git a/include/linux/swap.h b/include/linux/swap.h index 7ba7dcc..7dac1fe 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -413,6 +413,10 @@ extern struct page *swapin_readahead(swp_entry_t, gfp_t, struct vm_area_struct *vma, unsigned long addr); /* linux/mm/swapfile.c */ + +#ifndef MODULE + +/* Inside the base kernel, code can see these variables */ extern atomic_long_t nr_swap_pages; extern long total_swap_pages; @@ -427,6 +431,14 @@ static inline long get_nr_swap_pages(void) return atomic_long_read(&nr_swap_pages); } +#else /* MODULE */ + +/* Only this read-only interface is available to modules */ +extern long __get_nr_swap_pages(void); +#define get_nr_swap_pages __get_nr_swap_pages + +#endif /* MODULE */ + extern void si_swapinfo(struct sysinfo *); extern swp_entry_t get_swap_page(void); extern swp_entry_t get_swap_page_of_type(int); diff --git a/mm/swapfile.c b/mm/swapfile.c index 5887731..9309d6e 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -2754,6 +2754,13 @@ pgoff_t __page_file_index(struct page *page) } EXPORT_SYMBOL_GPL(__page_file_index); +/* Trivial accessor exported to kernel modules */ +long __get_nr_swap_pages(void) +{ + return get_nr_swap_pages(); +} +EXPORT_SYMBOL_GPL(__get_nr_swap_pages); + /* * add_swap_count_continuation - called when a swap count is duplicated * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's