Received: by 2002:a25:ca44:0:0:0:0:0 with SMTP id a65csp77480ybg; Mon, 27 Jul 2020 16:08:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyjPeuXEA906A1y0A/2OF0+RZN+jkImrkKGzuMXm2ywCW2Yhl+KpE0Y5jcw4aR57DGwrZX7 X-Received: by 2002:a17:906:26c3:: with SMTP id u3mr22542814ejc.483.1595891327911; Mon, 27 Jul 2020 16:08:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595891327; cv=none; d=google.com; s=arc-20160816; b=kAqXK5HHzP1ah21Dv6aXk90coTTSV8Pn4VJZzLIurkOorhCzrMqZaR4DZ1UlKg1NmM C/czlw+bPqg9zXc/G7HPffo0xq+DdV1QnOUIXKe000INPW9sr60mE8XFgGrTVU/LbjCY q01IYFtn11RxQngIiCZTbsBLvBfeIfKSWmdERGpW9bLp4mw4wt0ihdGuEWOScrHdTFeA jBufS4KGGrAhhvYa7pO3osCFy/4gcm4I4uRMwed7SG1shHI1zvUffp/I9aKi/e3Q2OgG I6LdjLbIQfGsyIVP2ruCUVHJXUD4Nj59qUEB96ij7K2IuXMW7EMv/4e/EHm1o6ddlnI/ CH0Q== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=2nZl/wfZCwqJz0Uq6l8YavpgpAx0NQEkZ1cBoy2s3YA=; b=lGsy6Rwr1IA1s4sdEGLbiUrZYecXJH3Z/BT6OA/3XbQw+jFq79443EyjRWfY3HikDJ 8Iu/koR0PhcmBjXR938Sl/F+y35EjvShqYIGINUE+XxIcaoLaN1rlO/mUTcYKyms0gxr eKZQvQcuTmBbhXmQo+Y4E6IzxbDQB/DA+upOSWYqbV/xKt5mI+epJMs59zO4IvTcz0ts pwy7aZDGVOz8FjybXBtRsplT05PZvpJG1Ii+auT9J7g7Lstk4q/iRmZsgMUJ7Q1yqHnP QDhpWuJ3DRs+XK667QyeJBDnftBt+44agjNDDSP9+EI0qxBCHVe6JXT3vkTDi6jjwwIM poXQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o7si6664616ejh.78.2020.07.27.16.08.25; Mon, 27 Jul 2020 16:08:47 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727782AbgG0XIM (ORCPT + 99 others); Mon, 27 Jul 2020 19:08:12 -0400 Received: from mail-qk1-f193.google.com ([209.85.222.193]:32843 "EHLO mail-qk1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726861AbgG0XIJ (ORCPT ); Mon, 27 Jul 2020 19:08:09 -0400 Received: by mail-qk1-f193.google.com with SMTP id l23so17031060qkk.0 for ; Mon, 27 Jul 2020 16:08:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2nZl/wfZCwqJz0Uq6l8YavpgpAx0NQEkZ1cBoy2s3YA=; b=UyBDuUIEo0rt8k+dt2SjPGBI9KafJ9krcXMV73ySL+MEV9jQjprVtWg6gnWyniAo1b 3gQu3NinyqJNjJyzOtyNmbOAu6XjA1ZR/UKdiH6MJXmvKdeQPLQC7dM1YLjPhC4B77Qz oI3sT/0WxVO3wrw4ohwA7zoe62pdznCdTmRyx91bu26MLFEyoiQ0euLp3HqXyGAcHoTM Hi98UtEZnvBGqYkU0pGOK0PfvexYRwShb6qWmQ+asv2QRIFElvhdx11RPMvX+37IBhgW S9jR6ioUXrpWopRgmP27q8fDnIevdW3L2cFbnltTzDny+GxcPK8/EWqFlpp5dKm87wg2 3rmA== X-Gm-Message-State: AOAM533fZ9CUYBFMUTBozLa3fTOqutX7R4V57BoWae4H80gjxZ0kRQr1 CvnTaqF2MpKZRoExU5UczPU= X-Received: by 2002:a05:620a:553:: with SMTP id o19mr24110744qko.46.1595891288401; Mon, 27 Jul 2020 16:08:08 -0700 (PDT) Received: from rani.riverdale.lan ([2001:470:1f07:5f3::b55f]) by smtp.gmail.com with ESMTPSA id j61sm8216984qtd.52.2020.07.27.16.08.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Jul 2020 16:08:07 -0700 (PDT) From: Arvind Sankar To: Kees Cook , x86@kernel.org Cc: linux-kernel@vger.kernel.org Subject: [PATCH v2 6/8] x86/kaslr: Simplify process_gb_huge_pages Date: Mon, 27 Jul 2020 19:07:59 -0400 Message-Id: <20200727230801.3468620-7-nivedita@alum.mit.edu> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200727215047.3341098-1-nivedita@alum.mit.edu> References: <20200727215047.3341098-1-nivedita@alum.mit.edu> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Short-circuit the whole function on 32-bit. Replace the loop to determine the number of 1Gb pages with arithmetic. Fix one minor bug: if the end of the region is aligned on a 1Gb boundary, the current code will not use the last available 1Gb page due to an off-by-one error. Signed-off-by: Arvind Sankar --- arch/x86/boot/compressed/kaslr.c | 48 ++++++++++++++------------------ 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/kaslr.c index d3d8f1f6d5d3..91c5f9771f42 100644 --- a/arch/x86/boot/compressed/kaslr.c +++ b/arch/x86/boot/compressed/kaslr.c @@ -546,49 +546,43 @@ static void store_slot_info(struct mem_vector *region, unsigned long image_size) static void process_gb_huge_pages(struct mem_vector *region, unsigned long image_size) { - unsigned long addr, size = 0; + unsigned long pud_start, pud_end, gb_huge_pages; struct mem_vector tmp; - int i = 0; - if (!max_gb_huge_pages) { + if (IS_ENABLED(CONFIG_X86_32) || !max_gb_huge_pages) { store_slot_info(region, image_size); return; } - addr = ALIGN(region->start, PUD_SIZE); - /* Did we raise the address above the passed in memory entry? */ - if (addr < region->start + region->size) - size = region->size - (addr - region->start); - - /* Check how many 1GB huge pages can be filtered out: */ - while (size > PUD_SIZE && max_gb_huge_pages) { - size -= PUD_SIZE; - max_gb_huge_pages--; - i++; - } + /* Are there any 1GB pages in the region? */ + pud_start = ALIGN(region->start, PUD_SIZE); + pud_end = ALIGN_DOWN(region->start + region->size, PUD_SIZE); /* No good 1GB huge pages found: */ - if (!i) { + if (pud_start >= pud_end) { store_slot_info(region, image_size); return; } - /* - * Skip those 'i'*1GB good huge pages, and continue checking and - * processing the remaining head or tail part of the passed region - * if available. - */ - - if (addr >= region->start + image_size) { + /* Check if the head part of the region is usable. */ + if (pud_start - region->start >= image_size) { tmp.start = region->start; - tmp.size = addr - region->start; + tmp.size = pud_start - region->start; store_slot_info(&tmp, image_size); } - size = region->size - (addr - region->start) - i * PUD_SIZE; - if (size >= image_size) { - tmp.start = addr + i * PUD_SIZE; - tmp.size = size; + /* Skip the good 1GB pages. */ + gb_huge_pages = (pud_end - pud_start) >> PUD_SHIFT; + if (gb_huge_pages > max_gb_huge_pages) { + pud_end = pud_start + (max_gb_huge_pages << PUD_SHIFT); + max_gb_huge_pages = 0; + } else + max_gb_huge_pages -= gb_huge_pages; + + /* Check if the tail part of the region is usable. */ + if (region->start + region->size - pud_end >= image_size) { + tmp.start = pud_end; + tmp.size = region->start + region->size - pud_end; store_slot_info(&tmp, image_size); } } -- 2.26.2