Received: by 2002:a25:824b:0:0:0:0:0 with SMTP id d11csp7437691ybn; Mon, 30 Sep 2019 13:58:00 -0700 (PDT) X-Google-Smtp-Source: APXvYqwknjG+/8JXiAblFZlNqnJz3KH19ygGXWL3Zk/k/ktdCEB/Js96xM/SJUpbWry2/SkdIvwi X-Received: by 2002:a50:8a9a:: with SMTP id j26mr22089447edj.251.1569877080383; Mon, 30 Sep 2019 13:58:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1569877080; cv=none; d=google.com; s=arc-20160816; b=cDFPYKPTXDSeDIM5nt4Wm2TDprH3nHnT2s+GmB4tUifqiA5xis5RiEj2612tL3JSPM ynmWN+Nlw8/Da3OcN8vkrzu+tifV7E3vAuATfWWzk5QFzQUwH/bz1HCOXju8ti7a42RA nZ7lGiyDMMAjkm57uppNqKHAvx8nXLGI4yEBk5/HHoGthMdAhpLXBblWGvYXBNXlY6kh qGZ9vKcHPcHOp4K+WMCZNHOUBUpedzbN8NWHPqJShAZ7IIxPw+uH/YUemXeFp4QGFy25 QtcJCjplCUgBZmo6diYPLzKKTWJzCBETvfS+4sygiPwe00NcQzZJC+de4sAf515L8JHw UD+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature; bh=BEh0LfONw8WWbMYYoNzYq2HBhr7sqFPDv5NouMGgnRI=; b=zUDjExNSVs/Tr/bmb6c+cTo5ru9cHDij/ffiAf645iARasoihRwcnkNdDiBog+HhEP +fG2o1YsQoRrwRQLB6rEG0pNdGMpjZzBxxtFUoPyaPGtPi6W/Qbq+tmOXm9IPjAASEaJ f61JqkveGYYD7DukTAsWFdgWZKAQOHqPzf08VC7t86DVd3LPDg2mv5dfuf3sbxbxZA91 vT+Z7ebifBgQGFeBW2wmml/8VuUb77pTnfpSVFB/cbCfulu1x6dusHkQJBGom+TgdyzZ RsVP6DgIsDbEOfxVnDxdOGN2eXvAkb9siGFLPM5/G8c7vToNJHEzJF40i7l275Gxh92n xaFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=mL+wonIS; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f1si7605435ejw.217.2019.09.30.13.57.34; Mon, 30 Sep 2019 13:58:00 -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; dkim=pass header.i=@gmail.com header.s=20161025 header.b=mL+wonIS; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731071AbfI3U4P (ORCPT + 99 others); Mon, 30 Sep 2019 16:56:15 -0400 Received: from mail-qk1-f193.google.com ([209.85.222.193]:44870 "EHLO mail-qk1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726314AbfI3U4P (ORCPT ); Mon, 30 Sep 2019 16:56:15 -0400 Received: by mail-qk1-f193.google.com with SMTP id u22so9111433qkk.11 for ; Mon, 30 Sep 2019 13:56:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=BEh0LfONw8WWbMYYoNzYq2HBhr7sqFPDv5NouMGgnRI=; b=mL+wonISTN9PlMPaMX0BT3yopdGljaFqMOjYIYk42pG2SXxOoN0BOcCi3ZRop9Ga90 5tQdhA+raRVQM7iTIQTIxNSWHpDKSySYYS+bWtBACqghZmHl1txliDIQKkYC+knzrTYq KZ5sglKEe1Md7nj23AebrKQKNt8mTaPpk9CBhZINTwZ430kPMSzYBX5UGJV9Rle+fhYF 65C9PhiB8oESqG/YZXuQQY5rsoLxvqdwWcM3cueLqirHWPgAhh2Noj9UcHQV7NytpmXA KpiawEdDMXb2TXpuYXcOcC9BKtH67lb9RfKMFeV5F0rqaa4euGvGVxI0H8NlerUg1+qg DRsQ== 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; bh=BEh0LfONw8WWbMYYoNzYq2HBhr7sqFPDv5NouMGgnRI=; b=FO8vDH5ZWxrhAiq8NcarI76F5NZoHpqJv695vEVjhb1cKXLhahKuO1jjyHgEP1eSjp d0XrWrmL/J/SWRbnnVYO9tK9+eCjzkkQzfwj5FqmOnsOucCl2XD3LzF9TZtLow3MA40A g8fOH1FI5iqbG224mDbzQDWh7ZLAsh+fbsN9I7HPvv2Ojd9A0irNnjZUg2TbDtb4ZDbz 7XyN1FMwC1cgg42z/92ebhxaEjsRQBANB3zyapd4DyaYuehF7LXNDUBZBbLmfdDY+wTg Bt1l9YnksKobf1f+nRHBA+ONa0oaTkZIVziShkjQF6/M2t+yuHceZeE7dzQ+heyfXGSy M5gA== X-Gm-Message-State: APjAAAVA7ay7ZmNNQ83ERiw/AnseHZhVVd+UckTxir/ykeCg2XuUj0ko FngxQ4ktY6u8ASSz7feTHQ== X-Received: by 2002:a37:bc84:: with SMTP id m126mr2247705qkf.196.1569876973826; Mon, 30 Sep 2019 13:56:13 -0700 (PDT) Received: from gabell.bos.redhat.com (nat-pool-bos-t.redhat.com. [66.187.233.206]) by smtp.gmail.com with ESMTPSA id j17sm6213041qki.99.2019.09.30.13.56.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Sep 2019 13:56:12 -0700 (PDT) From: Masayoshi Mizuma To: Dave Martin , Julien Grall , Catalin Marinas , Will Deacon , linux-arm-kernel@lists.infradead.org Cc: Masayoshi Mizuma , Masayoshi Mizuma , linux-kernel@vger.kernel.org Subject: [PATCH v3] arm64/sve: Fix wrong free for task->thread.sve_state Date: Mon, 30 Sep 2019 16:56:00 -0400 Message-Id: <20190930205600.25542-1-msys.mizuma@gmail.com> X-Mailer: git-send-email 2.17.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Masayoshi Mizuma The system which has SVE feature crashed because of the memory pointed by task->thread.sve_state was destroyed by someone. That is because sve_state is freed while the forking the child process. The child process has the pointer of sve_state which is same as the parent's because the child's task_struct is copied from the parent's one. If the copy_process() fails as an error on somewhere, for example, copy_creds(), then the sve_state is freed even if the parent is alive. The flow is as follows. copy_process p = dup_task_struct => arch_dup_task_struct *dst = *src; // copy the entire region. : retval = copy_creds if (retval < 0) goto bad_fork_free; : bad_fork_free: ... delayed_free_task(p); => free_task => arch_release_task_struct => fpsimd_release_task => __sve_free => kfree(task->thread.sve_state); // free the parent's sve_state Move child's sve_state = NULL and clearing TIF_SVE flag to arch_dup_task_struct() so that the child doesn't free the parent's one. There is no need to wait until copy_process() to clear TIF_SVE for dst, because the thread flags for dst are initialized already by copying the src task_struct. This change simplifies the code, so get rid of comments that are no longer needed. As a note, arm64 used to have thread_info on the stack. So it would not be possible to clear TIF_SVE until the stack is initialized. From commit c02433dd6de3 ("arm64: split thread_info from task stack"), the thread_info is part of the task, so it should be valid to modify the flag from arch_dup_task_struct(). Cc: stable@vger.kernel.org # 4.15.x- Fixes: bc0ee4760364 ("arm64/sve: Core task context handling") Signed-off-by: Masayoshi Mizuma Reported-by: Hidetoshi Seto Suggested-by: Dave Martin Tested-by: Julien Grall --- arch/arm64/kernel/process.c | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/arch/arm64/kernel/process.c b/arch/arm64/kernel/process.c index a47462def04b..ef7aa909bfda 100644 --- a/arch/arm64/kernel/process.c +++ b/arch/arm64/kernel/process.c @@ -332,22 +332,27 @@ void arch_release_task_struct(struct task_struct *tsk) fpsimd_release_task(tsk); } -/* - * src and dst may temporarily have aliased sve_state after task_struct - * is copied. We cannot fix this properly here, because src may have - * live SVE state and dst's thread_info may not exist yet, so tweaking - * either src's or dst's TIF_SVE is not safe. - * - * The unaliasing is done in copy_thread() instead. This works because - * dst is not schedulable or traceable until both of these functions - * have been called. - */ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src) { if (current->mm) fpsimd_preserve_current_state(); *dst = *src; + /* We rely on the above assignment to initialize dst's thread_flags: */ + BUILD_BUG_ON(!IS_ENABLED(CONFIG_THREAD_INFO_IN_TASK)); + + /* + * Detach src's sve_state (if any) from dst so that it does not + * get erroneously used or freed prematurely. dst's sve_state + * will be allocated on demand later on if dst uses SVE. + * For consistency, also clear TIF_SVE here: this could be done + * later in copy_process(), but to avoid tripping up future + * maintainers it is best not to leave TIF_SVE and sve_state in + * an inconsistent state, even temporarily. + */ + dst->thread.sve_state = NULL; + clear_tsk_thread_flag(dst, TIF_SVE); + return 0; } @@ -360,13 +365,6 @@ int copy_thread(unsigned long clone_flags, unsigned long stack_start, memset(&p->thread.cpu_context, 0, sizeof(struct cpu_context)); - /* - * Unalias p->thread.sve_state (if any) from the parent task - * and disable discard SVE state for p: - */ - clear_tsk_thread_flag(p, TIF_SVE); - p->thread.sve_state = NULL; - /* * In case p was allocated the same task_struct pointer as some * other recently-exited task, make sure p is disassociated from -- 2.18.1