From patchwork Fri Dec 17 10:28:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe Leroy X-Patchwork-Id: 12684223 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1E6BCC433F5 for ; Fri, 17 Dec 2021 10:33:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2A9BB6B0085; Fri, 17 Dec 2021 05:28:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 231526B0087; Fri, 17 Dec 2021 05:28:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0842E6B0088; Fri, 17 Dec 2021 05:28:25 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0079.hostedemail.com [216.40.44.79]) by kanga.kvack.org (Postfix) with ESMTP id E9CF06B0085 for ; Fri, 17 Dec 2021 05:28:24 -0500 (EST) Received: from smtpin30.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id B272189118 for ; Fri, 17 Dec 2021 10:28:14 +0000 (UTC) X-FDA: 78926911308.30.8380024 Received: from FRA01-PR2-obe.outbound.protection.outlook.com (mail-eopbgr120042.outbound.protection.outlook.com [40.107.12.42]) by imf31.hostedemail.com (Postfix) with ESMTP id 5767020009 for ; Fri, 17 Dec 2021 10:28:06 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=FyNkiVRke2J1xKlBnHOKZw3/92E1Hko9kPm5Bchy9eJP4AU7/hy5Kdg77FqNJkWGJD8gU0kKSHjPnyvh9hnou1zOot63MVf10utrZGCTdKl8D2aDsaALBab9kAEzUbRvkEg03Bq09gBsOfZ2GZd85OTA2G4vbyq4xyxzoGE9STQQ42hk/ijJj56SZq8RtmvAznd8tNJA9vrbCZ9hNpevfPv1lQS/lWqRsypAfla5uqo/dVgfYyLFimMCuxf5rv0HzO/us1bMMaWoMV+dekvyo1HnTfGtYCho4Fz+MNzD3eEcKLdjU82kla437TRFIjj1WjYspoyXzBlaa0vougdvLQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=PM1tsV2E7+/dx7vpR2aV9rqndz5KU8OyEwLYpGyZ5i4=; b=Fb71TIPa4s5aUA+ZwwH7Gy4PWjgftazMb7MRrkfGw7iOrPBlIZqx2/qn4bpQsR95ITF8GIVSjVKmbrUIVLR/04y4NHrs1TSR5kUQXQsnx2PV+ghjAPL8yH4byhiVOJcav+80ZZRP1V2cbZZN6TPhEfDIwEm2K/IDc8g+lyeyqHKZtrJMRIAE80QqqrlkRdQZvghZtRuhHzddICxz3sd4kPWJ1BqDSYnkKClEKnUUBYQ5F+NB2lRrvBXRUwt37RTLM++yE9S7i9L7fSO2hDu4t8ezBwKtOMN9o4tn0aloEhJJCkE5QCXMqx/N2QHYfG4utsd5bwl3GLYSSbAnZFiYRA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=csgroup.eu; dmarc=pass action=none header.from=csgroup.eu; dkim=pass header.d=csgroup.eu; arc=none Received: from MRZP264MB2988.FRAP264.PROD.OUTLOOK.COM (2603:10a6:501:31::15) by MR2P264MB0146.FRAP264.PROD.OUTLOOK.COM (2603:10a6:500:e::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4801.14; Fri, 17 Dec 2021 10:28:12 +0000 Received: from MRZP264MB2988.FRAP264.PROD.OUTLOOK.COM ([fe80::f0ef:856d:b0de:e85d]) by MRZP264MB2988.FRAP264.PROD.OUTLOOK.COM ([fe80::f0ef:856d:b0de:e85d%5]) with mapi id 15.20.4801.016; Fri, 17 Dec 2021 10:28:12 +0000 From: Christophe Leroy To: Benjamin Herrenschmidt , Paul Mackerras , Michael Ellerman , "alex@ghiti.fr" CC: Christophe Leroy , "linux-kernel@vger.kernel.org" , "linuxppc-dev@lists.ozlabs.org" , "linux-mm@kvack.org" , "akpm@linux-foundation.org" , "catalin.marinas@arm.com" , "will@kernel.org" , "linux-arm-kernel@lists.infradead.org" Subject: [PATCH v6 12/14] powerpc/mm: Enable full randomisation of memory mappings Thread-Topic: [PATCH v6 12/14] powerpc/mm: Enable full randomisation of memory mappings Thread-Index: AQHX8zDLLCiS7XjV0k2/ODdJkMHmPg== Date: Fri, 17 Dec 2021 10:28:12 +0000 Message-ID: References: In-Reply-To: Accept-Language: fr-FR, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 522cd451-6b85-45f7-5c94-08d9c147edea x-ms-traffictypediagnostic: MR2P264MB0146:EE_ x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:2887; x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: LnsmmboUaiK9s/wOIRSLQAfAV7zRsFWHVe1lNvsf8iKPWgwVdAeTGsYODfR0KZh4X0IHBiWX+Jm/rQKDcINm8JHsHaisZmCc5UBf4xc1VSXoMCwVNbyAaavcQhTN53qP9bC7qi4AFCEnjEvSezmlk1zd5wRGCS/UJwcMWWW5C+DWa/07g52JIVkT6lLUHorjfnR6icGqVzA/AboWCeS0LmgDqktNw7LbOYN9YBNjrd5bFfnv/d8tFIaO8KxY/dUGiRP8fWeNPSSJtklbC1CMzk9g5h0M3GVr3w+yn9/LSy7iBzRpaK3ylVL9nzLvxnfOybdGTeWZCbUzoIL+mdMo8aLkkjOrQjinVvENhkn+N2idR9fxUnRHnD4qo2FKl1Zk0871ZvXo7oyfoT6Mk+7P30nGuIEcfYij8fufZRMHvRgbO+gE/q8strvsBvSpxu5jgPjHXbLrFYqfEuvTKw+AsMOWeAmC1lIKqXq77fsjWJFwgKopOEiar8m39FBdWE++8scY3BBbomMpPPyH0kaQhLUURA1K3uDMDj7/vz8i+1NGzZ34G8vBQiZtcmxcDVehYfdTQ5+E8aOz+bw0tmefN2E1hzwcFQNHWN5/+Ek8pXJN8VF5rM67LF3qyQz0qk43m1QpufSMYe9Fpfex0ZomyO6oH+FVxExyEhEeAlNnKKAR51QW3aH69Btyaku2TK98XV/k5Q4nCm8X8VQgroB+gw== x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:MRZP264MB2988.FRAP264.PROD.OUTLOOK.COM;PTR:;CAT:NONE;SFS:(4636009)(366004)(186003)(91956017)(71200400001)(86362001)(83380400001)(66476007)(66556008)(66446008)(2906002)(64756008)(38070700005)(7416002)(44832011)(6506007)(76116006)(4326008)(36756003)(26005)(2616005)(5660300002)(122000001)(8936002)(38100700002)(508600001)(6486002)(54906003)(110136005)(66946007)(8676002)(316002)(6512007);DIR:OUT;SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?q?EK+Qv0TpiNOrqIL/pQsYenp?= =?iso-8859-1?q?/vFBKv+6z9YyxcR0CGrTUu144keSw5RWe3NseLp3O+8tQTjCD9rTCItafofl?= =?iso-8859-1?q?2ywimy5km7YXSa1vliwlmfdKreaor8q08+ewBjv7K9A2E2TUsKHG0FQF0YyI?= =?iso-8859-1?q?aEBY7KpxmZKnmR5lTbSVy0I88sh8u895jikXBUjzRAYnYL3AAIGxwjAPrmJ+?= =?iso-8859-1?q?4qku+ekAhwy6k798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ZBb2WWBd+Z08kfy62r2?= =?iso-8859-1?q?ACM1nrlBV/Y9NiL7VRoTr6p2q2gorn3CGbq8VuQlHb52t2GrV0jV1f1fWBf9?= =?iso-8859-1?q?dLcPY293Kl787VejWV6h+tSkBCiyHGSL5nKBmm4Jk25inGT0eO43R8wGN++A?= =?iso-8859-1?q?q2wuIeVwzC8jjlB+oAXvX5JuLO8Zr/y+1FiPs8yOapkE0o45fH1RC+tGSs7U?= =?iso-8859-1?q?7Q4L1yRBtczM04Q7VlJMt3p9TcKC/hqv0i9m4eD6fYFhKsWDuMC9ivUFq/LC?= =?iso-8859-1?q?ynmxzcAs7nod2vLUxeuH0TabrBRzGWCIF5MscNqMgFjUa0AjyhggW2DpL9Bg?= =?iso-8859-1?q?Qt2Ai8efnJRFb8kVJ66KEFd7yqr3T04fjfSKNYqPxV54/zwCSXjhSzSrKgEA?= =?iso-8859-1?q?VG66I240skCFDmW+l5yjApleSGyKcGcQb8n1tUG5ZEvJgT1WbCkJ9F09zXQC?= =?iso-8859-1?q?chIl+xKlTUL0bbVgEXp80mhi7D/5zz2CEIYtHBZr7NfgAy9ZiVHIp8A+yUL5?= =?iso-8859-1?q?Ohw2GvTL76WsENVrbKkyYDJT72x4DiJIRYabeDQ46RdXzrRq4S8A7BAu/LM1?= =?iso-8859-1?q?pwzIbJdv7Z86tPX5dwvND6BZR0eFVQtlzP8xkwzr9OarP6lNZxSVEK6KETZA?= =?iso-8859-1?q?f3b8=3D?= MIME-Version: 1.0 X-OriginatorOrg: csgroup.eu X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: MRZP264MB2988.FRAP264.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-Network-Message-Id: 522cd451-6b85-45f7-5c94-08d9c147edea X-MS-Exchange-CrossTenant-originalarrivaltime: 17 Dec 2021 10:28:12.7252 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 9914def7-b676-4fda-8815-5d49fb3b45c8 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: pa4WcehptrpkrFq8k/qD8f3psE3PYPFbGGiqOXxobK+zXURVkNme9OFbw5YUmlEkhnKQvzluoN36PkpPE4ggHbnSquPqR8EgWoubqghviiM= X-MS-Exchange-Transport-CrossTenantHeadersStamped: MR2P264MB0146 X-Stat-Signature: xw17sdn9iiqfho3rh7ouazfeubtdjh11 X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 5767020009 Authentication-Results: imf31.hostedemail.com; dkim=none; spf=pass (imf31.hostedemail.com: domain of christophe.leroy@csgroup.eu designates 40.107.12.42 as permitted sender) smtp.mailfrom=christophe.leroy@csgroup.eu; dmarc=none X-HE-Tag: 1639736886-205537 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Do like most other architectures and provide randomisation also to "legacy" memory mappings, by adding the random factor to mm->mmap_base in arch_pick_mmap_layout(). See commit 8b8addf891de ("x86/mm/32: Enable full randomization on i386 and X86_32") for all explanations and benefits of that mmap randomisation. At the moment, slice_find_area_bottomup() doesn't use mm->mmap_base but uses the fixed TASK_UNMAPPED_BASE instead. slice_find_area_bottomup() being used as a fallback to slice_find_area_topdown(), it can't use mm->mmap_base directly. Instead of always using TASK_UNMAPPED_BASE as base address, leave it to the caller. When called from slice_find_area_topdown() TASK_UNMAPPED_BASE is used. Otherwise mm->mmap_base is used. Signed-off-by: Christophe Leroy --- arch/powerpc/mm/book3s64/slice.c | 18 +++++++----------- arch/powerpc/mm/mmap.c | 2 +- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/arch/powerpc/mm/book3s64/slice.c b/arch/powerpc/mm/book3s64/slice.c index 03681042b807..c0b58afb9a47 100644 --- a/arch/powerpc/mm/book3s64/slice.c +++ b/arch/powerpc/mm/book3s64/slice.c @@ -276,20 +276,18 @@ static bool slice_scan_available(unsigned long addr, } static unsigned long slice_find_area_bottomup(struct mm_struct *mm, - unsigned long len, + unsigned long addr, unsigned long len, const struct slice_mask *available, int psize, unsigned long high_limit) { int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT); - unsigned long addr, found, next_end; + unsigned long found, next_end; struct vm_unmapped_area_info info; info.flags = 0; info.length = len; info.align_mask = PAGE_MASK & ((1ul << pshift) - 1); info.align_offset = 0; - - addr = TASK_UNMAPPED_BASE; /* * Check till the allow max value for this mmap request */ @@ -322,12 +320,12 @@ static unsigned long slice_find_area_bottomup(struct mm_struct *mm, } static unsigned long slice_find_area_topdown(struct mm_struct *mm, - unsigned long len, + unsigned long addr, unsigned long len, const struct slice_mask *available, int psize, unsigned long high_limit) { int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT); - unsigned long addr, found, prev; + unsigned long found, prev; struct vm_unmapped_area_info info; unsigned long min_addr = max(PAGE_SIZE, mmap_min_addr); @@ -335,8 +333,6 @@ static unsigned long slice_find_area_topdown(struct mm_struct *mm, info.length = len; info.align_mask = PAGE_MASK & ((1ul << pshift) - 1); info.align_offset = 0; - - addr = mm->mmap_base; /* * If we are trying to allocate above DEFAULT_MAP_WINDOW * Add the different to the mmap_base. @@ -377,7 +373,7 @@ static unsigned long slice_find_area_topdown(struct mm_struct *mm, * can happen with large stack limits and large mmap() * allocations. */ - return slice_find_area_bottomup(mm, len, available, psize, high_limit); + return slice_find_area_bottomup(mm, TASK_UNMAPPED_BASE, len, available, psize, high_limit); } @@ -386,9 +382,9 @@ static unsigned long slice_find_area(struct mm_struct *mm, unsigned long len, int topdown, unsigned long high_limit) { if (topdown) - return slice_find_area_topdown(mm, len, mask, psize, high_limit); + return slice_find_area_topdown(mm, mm->mmap_base, len, mask, psize, high_limit); else - return slice_find_area_bottomup(mm, len, mask, psize, high_limit); + return slice_find_area_bottomup(mm, mm->mmap_base, len, mask, psize, high_limit); } static inline void slice_copy_mask(struct slice_mask *dst, diff --git a/arch/powerpc/mm/mmap.c b/arch/powerpc/mm/mmap.c index 5972d619d274..d9eae456558a 100644 --- a/arch/powerpc/mm/mmap.c +++ b/arch/powerpc/mm/mmap.c @@ -96,7 +96,7 @@ void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) * bit is set, or if the expected stack growth is unlimited: */ if (mmap_is_legacy(rlim_stack)) { - mm->mmap_base = TASK_UNMAPPED_BASE; + mm->mmap_base = TASK_UNMAPPED_BASE + random_factor; mm->get_unmapped_area = arch_get_unmapped_area; } else { mm->mmap_base = mmap_base(random_factor, rlim_stack);