Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp2120361imm; Sat, 15 Sep 2018 09:35:38 -0700 (PDT) X-Google-Smtp-Source: ANB0VdZQS5660r9mQgaWhNVpIjxc8YTCHd8L6sp8gEBA/kXu8qmMK9vczvHwkWgwa3Z/ZtIlUMJJ X-Received: by 2002:a63:d04f:: with SMTP id s15-v6mr16682882pgi.42.1537029338402; Sat, 15 Sep 2018 09:35:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537029338; cv=none; d=google.com; s=arc-20160816; b=aboFa/O7ZtJKZOWxJKzDJu9trZFOJteCveVqgmyzkAX5jbiicolwWcwbCru8arj7Dd lccHfkCs4fI3DwYCTomdsKhLegCJaLi4E6PhkTSEenhR4e5yTjJyUFYodUxiIYJAc8N4 oIr6fMTkgrB/23cnDrAJKaiP+oyvjWJ/xhhu1EK7YuUIbCPLXw3bxVy4rJbLx+khyl+N 8f90/HN1McLoYv+4YJmeM8A9o6mAc+8NX58a8QejOb6qNlPk6ZI2M2EnlLn66RIkFsx/ cpC0L8C4ZYoSfYsdgd8Lbgm+oQ/KDPpgNtZRcljg6PKUe2uPmS6Zt2GbECqI16gGzMtM JNAg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:cc:to:from :subject; bh=6+eKTRMHGxIuQXh7ZEMX8h+NaT+2wxnpQFWD6QFiAhc=; b=jXqjwL2vwqFcSJNZ1GpC9VesOjA0FbNZpxr310LJTKXw1Dp6QeE+/EZ8bZWRgS92Cl o/IIFHdl+FedmCikUKKE/5kvsRZ8vaxXUPcZpEF8bMpTtfbcQlo/SCC380TVkq26gzUv nyeBWH1ZMokC04RQ+MBJ6p6KXf/TXq6knZMkYD19+uhCwfxU+Jb95IiCD+s/cRCp9ohE Br+rBELT8ivimYZCE6eEL57ofa+qNyy2TPS5+IEaqLN9r1NO78K0li+f+k84rufESB1Y G0ItQ/4u832zRbV8/oSrbkimEq9L5tlR5ZSkeWfrhG2Zi/00ilr22+AR2XBS7NXvJeTD tRTQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id s184-v6si9777472pgb.161.2018.09.15.09.35.23; Sat, 15 Sep 2018 09:35:38 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728089AbeIOVys (ORCPT + 99 others); Sat, 15 Sep 2018 17:54:48 -0400 Received: from mga04.intel.com ([192.55.52.120]:20642 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728042AbeIOVyr (ORCPT ); Sat, 15 Sep 2018 17:54:47 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 15 Sep 2018 09:35:15 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.53,377,1531810800"; d="scan'208";a="83785006" Received: from dwillia2-desk3.jf.intel.com (HELO dwillia2-desk3.amr.corp.intel.com) ([10.54.39.16]) by orsmga003.jf.intel.com with ESMTP; 15 Sep 2018 09:35:00 -0700 Subject: [PATCH 3/3] mm: Maintain randomization of page free lists From: Dan Williams To: akpm@linux-foundation.org Cc: Michal Hocko , Kees Cook , Dave Hansen , linux-mm@kvack.org, linux-kernel@vger.kernel.org Date: Sat, 15 Sep 2018 09:23:18 -0700 Message-ID: <153702859851.1603922.5390659652135091505.stgit@dwillia2-desk3.amr.corp.intel.com> In-Reply-To: <153702858249.1603922.12913911825267831671.stgit@dwillia2-desk3.amr.corp.intel.com> References: <153702858249.1603922.12913911825267831671.stgit@dwillia2-desk3.amr.corp.intel.com> User-Agent: StGit/0.18-2-gc94f MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When freeing a page with an order >= shuffle_page_order randomly select the front or back of the list for insertion. While the mm tries to defragment physical pages into huge pages this can tend to make the page allocator more predictable over time. Inject the front-back randomness to preserve the initial randomness established by shuffle_free_memory() when the kernel was booted. The overhead of this manipulation is constrained by only being applied for MAX_ORDER sized pages by default. Cc: Michal Hocko Cc: Kees Cook Cc: Dave Hansen Signed-off-by: Dan Williams --- include/linux/mmzone.h | 2 ++ mm/page_alloc.c | 27 +++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index adf9b3a7440d..4a095432843d 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -98,6 +98,8 @@ extern int page_group_by_mobility_disabled; struct free_area { struct list_head free_list[MIGRATE_TYPES]; unsigned long nr_free; + u64 rand; + u8 rand_bits; }; /* Used for pages not on another list */ diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 175f2e5f9e50..33a6b40ae463 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -43,6 +43,7 @@ #include #include #include +#include #include #include #include @@ -746,6 +747,22 @@ static inline int page_is_buddy(struct page *page, struct page *buddy, return 0; } +static void add_to_free_area_random(struct page *page, struct free_area *area, + int migratetype) +{ + if (area->rand_bits == 0) { + area->rand_bits = 64; + area->rand = get_random_u64(); + } + + if (area->rand & 1) + add_to_free_area(page, area, migratetype); + else + add_to_free_area_tail(page, area, migratetype); + area->rand_bits--; + area->rand >>= 1; +} + /* * Freeing function for a buddy system allocator. * @@ -851,7 +868,8 @@ static inline void __free_one_page(struct page *page, * so it's less likely to be used soon and more likely to be merged * as a higher order page */ - if ((order < MAX_ORDER-2) && pfn_valid_within(buddy_pfn)) { + if ((order < MAX_ORDER-2) && pfn_valid_within(buddy_pfn) + && order < shuffle_page_order) { struct page *higher_page, *higher_buddy; combined_pfn = buddy_pfn & pfn; higher_page = page + (combined_pfn - pfn); @@ -865,7 +883,12 @@ static inline void __free_one_page(struct page *page, } } - add_to_free_area(page, &zone->free_area[order], migratetype); + if (order < shuffle_page_order) + add_to_free_area(page, &zone->free_area[order], migratetype); + else + add_to_free_area_random(page, &zone->free_area[order], + migratetype); + } /*