Received: by 2002:a25:ca44:0:0:0:0:0 with SMTP id a65csp666335ybg; Tue, 28 Jul 2020 15:58:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwAZFOVrs9hU4YGD3r4/nj3VKHD+SUyjRhMgbUtNZHQbSC8bIEo3H1aKJvSNo3hlxhKVuBs X-Received: by 2002:a17:906:8782:: with SMTP id za2mr24926088ejb.419.1595977125061; Tue, 28 Jul 2020 15:58:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595977125; cv=none; d=google.com; s=arc-20160816; b=lOSzXH8MR2rosLKzSqYwxA9skzrUL/Ug9yCzIzEi+VvbAP0zE6ku2i4nUj2xpTfzhK PpZJ4fnNEnBzb6NXFtGHkgZL2BJqc2tiIBEgv4O9lGs+DPakH6gC/GcXTiHMucBYNzDy wNgXlwdQGPHJq9ZVbSy2OHz3WNv1fPWG0G469UvHs6JOOKaC1P99pFXEOYehoBK1olTf 8mtHjpnGbm4L99T46REnGa0//TVLphlGR0U8+dst3rgvmo3ppDkO5gApfRVybb9ixhEX DK4+1uOqEZ1UekXz+yDYzNlObd40aBrqiWKh8bm2i+kRTgadfKLRMVWOzGXHRZEI8RQf MRrQ== 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=ZQMmTnajY5eYCOmwZRBHbur3Flx5GWzOMVTRLQeeOzo=; b=uDmkFjkxnymSlFDNvQirfTvdrun+5d/lYZrHcL2XcQ14Fs+liNjWSzOrsToUajQIN1 QvojhyOqM7um2t3tzLlga5vG26qITdVM/rJMk9lNgV38AeNYQRytUK3E0bOhLgN1vfSW ftHjoAmR/L2QpfYwh4limzrq3XdniBb+WgeiAXgXTei1VDjlTGR6qSFDvAI2RVOVZIFK qzm14b/KQtNziFai6+I0/Gee4Eb5EJV69hTwBPqEgKgUn1DMhHlhs2Xu1OumsAAFTtMj ZdXbiKPdYRRQ/0xlVtpChEB2HZSTaePD5nVWJ1zV4NwhdBLf6AoLAewg3F8bYVi7AxgB EiRg== 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 c19si8107731ejz.484.2020.07.28.15.58.23; Tue, 28 Jul 2020 15:58:45 -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 S1730191AbgG1W5w (ORCPT + 99 others); Tue, 28 Jul 2020 18:57:52 -0400 Received: from mail-qk1-f195.google.com ([209.85.222.195]:40583 "EHLO mail-qk1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729978AbgG1W5e (ORCPT ); Tue, 28 Jul 2020 18:57:34 -0400 Received: by mail-qk1-f195.google.com with SMTP id h7so20438007qkk.7 for ; Tue, 28 Jul 2020 15:57:33 -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=ZQMmTnajY5eYCOmwZRBHbur3Flx5GWzOMVTRLQeeOzo=; b=nPVBkKWaRy5zDtP8jTcHwn9Fhj/E1MVMuHysbRGrLeeH84WZOIGfSQ8cH6+ZsGaEaK 1IH0bGk4oXNvykMcp1O5EoFh6Yrw7u4QEFBLNuxhCoEk+Q9OKdxGEbGrwbsn3KxRWmcE anpD6ryF/XUhEhPwsHgGf8+14IeItP9kQ6uRg2vW6d2H04bzlx04yhGTmFg/f4UTTk8a zvOWgb2dMud/ikH5cdxhIUWIsNapiAXeJwIphub9GSQLNk3+RBICQqe7Ol1gwoax4uvD qSp5dC+s/I2A/Lte+Z74BnWAzUvouu+IRg9/1oZ9yrX5udU6i12SA1aFKK4bR6aYrOiH yJIw== X-Gm-Message-State: AOAM530iWDC87qI0goMt59iuiSZyJAdeOJKRpnqMPmAL/UUj94KNAgbt bkm6NSYP0Vp7DFUonKfOGPM= X-Received: by 2002:a37:8142:: with SMTP id c63mr28406869qkd.97.1595977053103; Tue, 28 Jul 2020 15:57:33 -0700 (PDT) Received: from rani.riverdale.lan ([2001:470:1f07:5f3::b55f]) by smtp.gmail.com with ESMTPSA id o187sm88118qkd.112.2020.07.28.15.57.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Jul 2020 15:57:32 -0700 (PDT) From: Arvind Sankar To: Kees Cook , x86@kernel.org Cc: linux-kernel@vger.kernel.org Subject: [PATCH v3 12/21] x86/kaslr: Simplify process_gb_huge_pages Date: Tue, 28 Jul 2020 18:57:13 -0400 Message-Id: <20200728225722.67457-13-nivedita@alum.mit.edu> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200727230801.3468620-1-nivedita@alum.mit.edu> References: <20200727230801.3468620-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 Replace the loop to determine the number of 1Gb pages with arithmetic. Signed-off-by: Arvind Sankar --- arch/x86/boot/compressed/kaslr.c | 46 ++++++++++++++------------------ 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/kaslr.c index 3727e9708690..7fb699aae74e 100644 --- a/arch/x86/boot/compressed/kaslr.c +++ b/arch/x86/boot/compressed/kaslr.c @@ -547,49 +547,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 (!IS_ENABLED(CONFIG_X86_64) || !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