Received: by 2002:a25:824b:0:0:0:0:0 with SMTP id d11csp3280412ybn; Fri, 27 Sep 2019 04:05:34 -0700 (PDT) X-Google-Smtp-Source: APXvYqwksYbFsbBjxFR6nBg7jlY+kaLG+/9SujTyySLeqPF+MOoKjGU79CaYG0worayyxj7VrQge X-Received: by 2002:aa7:d988:: with SMTP id u8mr3745265eds.116.1569582334486; Fri, 27 Sep 2019 04:05:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1569582334; cv=none; d=google.com; s=arc-20160816; b=dcRb2lMm89YJbvdAJMFqcVcXy8wWJkMdN12qtLJTMMKSSYZtOQgh2+rbH7VOYSRy+O vJVBSPnFKSY6HPP7W2MvEA8pu4yN7ZVqB6EqZNRDsvVQhdOpb+9MRMvr8meNx7W4MDmI fBl+8X+5MX3+jQUQfMwrJQO2h6FoeJ6JTn8ozVn/nu5FFu8f2gTVx8LYGStq4gG4RpTA RL1ehI05xANlY17r0nc2cYWTrMqjWMzVU9KPrAu+dkyeLBJLN+IhUtWNvBfy7vf2A7C1 xS5kxArSc8ibfG5rJfkUgyT6uc6ICM6IkwxXw7s9oPaOWxAPLpc4MiC9mfE6wtvrz2ma ewKg== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=jZGYXTq0xP4vTT2k61E3DuYXatkzbhEUOr5FCzDTHVU=; b=S5tc5WTwVqn4RUZlZB3HNnMKZMO0JU1K3sFvSbArMc7moyYo1YIesvgau+Iw5nM8oZ Zd2432+obAoIXHu51zfnabh7bQ+wAfDJeHR3OTGVS480aCrLW/keUyz6KMtkA+Sr6u+M fFvvc+KR6sf2ws82muryw1CpQEeNIVJTXHDJW5NOf85+pYPUrP0NnsAnokCwt8PnUsT1 +3GtS9dmIWCRke/jxu/7CdVhWHpGQXFbPinDMuTPFSYA3hyLMT5E2iHb0gOaBBvwiSdm 3t0mpin+GZGeCduFhJd6ogD9ra0M1wNLvU8VC7Uif4OY2+ZiE4wTY34IuVOid1dKizvQ 4xSw== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id rn4si2490141ejb.251.2019.09.27.04.05.07; Fri, 27 Sep 2019 04:05:34 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726178AbfI0LEk (ORCPT + 99 others); Fri, 27 Sep 2019 07:04:40 -0400 Received: from foss.arm.com ([217.140.110.172]:49192 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725882AbfI0LEk (ORCPT ); Fri, 27 Sep 2019 07:04:40 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 9EA0928; Fri, 27 Sep 2019 04:04:39 -0700 (PDT) Received: from [10.1.196.50] (e108454-lin.cambridge.arm.com [10.1.196.50]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id AFD8C3F67D; Fri, 27 Sep 2019 04:04:38 -0700 (PDT) Subject: Re: [PATCH 1/1] arm64/sve: Fix wrong free for task->thread.sve_state To: Masayoshi Mizuma , Catalin Marinas , Will Deacon , linux-arm-kernel@lists.infradead.org Cc: Masayoshi Mizuma , linux-kernel@vger.kernel.org, Dave P Martin References: <20190926190846.3072-1-msys.mizuma@gmail.com> <20190926190846.3072-2-msys.mizuma@gmail.com> From: Julien Grall Message-ID: <32b59c08-cc59-60d6-16c7-ffb5582c2901@arm.com> Date: Fri, 27 Sep 2019 12:04:37 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: <20190926190846.3072-2-msys.mizuma@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org (+ Dave) Hi, Thank you for the patch. On 26/09/2019 20:08, Masayoshi Mizuma wrote: > 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 The flow makes sense to me and I agree you would end up to free the parent's state. > > Add a flag in task->thread which shows the fork is in progress. > If the fork is in progress, that means the child has the pointer > to the parent's sve_state, doesn't free the sve_state. I haven't fully investigate it yet but I was wondering if we could just clear task->thread.sve_state for the child in arch_dup_task_struct(). I saw the comment on top of function mentioning potential issue to do it there. I understand that you may not be able to clear TIF_SVE in the function, but I don't understand why clearing just task->thread.sve_state would be an issue. The only risk I can see is TIF_SVE may be set with task->thread.sve_state to be NULL. But this is a new task, so I don't think there are risk here to have it unsync. Dave? > > Signed-off-by: Masayoshi Mizuma > Reported-by: Hidetoshi Seto > --- > arch/arm64/include/asm/processor.h | 1 + > arch/arm64/kernel/fpsimd.c | 6 ++++-- > arch/arm64/kernel/process.c | 2 ++ > 3 files changed, 7 insertions(+), 2 deletions(-) > > diff --git a/arch/arm64/include/asm/processor.h b/arch/arm64/include/asm/processor.h > index 5623685c7d13..3ca3e350145a 100644 > --- a/arch/arm64/include/asm/processor.h > +++ b/arch/arm64/include/asm/processor.h > @@ -143,6 +143,7 @@ struct thread_struct { > unsigned long fault_address; /* fault info */ > unsigned long fault_code; /* ESR_EL1 value */ > struct debug_info debug; /* debugging */ > + unsigned int fork_in_progress; > #ifdef CONFIG_ARM64_PTR_AUTH > struct ptrauth_keys keys_user; > #endif > diff --git a/arch/arm64/kernel/fpsimd.c b/arch/arm64/kernel/fpsimd.c > index 37d3912cfe06..8641db4cb062 100644 > --- a/arch/arm64/kernel/fpsimd.c > +++ b/arch/arm64/kernel/fpsimd.c > @@ -202,8 +202,10 @@ static bool have_cpu_fpsimd_context(void) > */ > static void __sve_free(struct task_struct *task) > { > - kfree(task->thread.sve_state); > - task->thread.sve_state = NULL; > + if (!task->thread.fork_in_progress) { > + kfree(task->thread.sve_state); > + task->thread.sve_state = NULL; > + } > } > > static void sve_free(struct task_struct *task) > diff --git a/arch/arm64/kernel/process.c b/arch/arm64/kernel/process.c > index a47462def04b..8ac0ee4e5f76 100644 > --- a/arch/arm64/kernel/process.c > +++ b/arch/arm64/kernel/process.c > @@ -347,6 +347,7 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src) > if (current->mm) > fpsimd_preserve_current_state(); > *dst = *src; > + dst->thread.fork_in_progress = 1; > > return 0; > } > @@ -365,6 +366,7 @@ int copy_thread(unsigned long clone_flags, unsigned long stack_start, > * and disable discard SVE state for p: > */ > clear_tsk_thread_flag(p, TIF_SVE); > + p->thread.fork_in_progress = 0; > p->thread.sve_state = NULL; > > /* > Cheers, -- Julien Grall