Received: by 2002:ab2:3350:0:b0:1f4:6588:b3a7 with SMTP id o16csp2028773lqe; Tue, 9 Apr 2024 07:41:52 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVbTsmaL7OLByUfFlS9iU3hcM9yLvXlJnO/Khpo2l2UxPb19NRTNKik2TUIzUqGUEXPXTV2Et1MI/layzMYnsNV/FHpRrcWzFj7nfD+Pw== X-Google-Smtp-Source: AGHT+IHKNk+yxw7OAV2Jvye5evRi8UgfG28Ip7on6ac/Whe/kl7EiFpXqVdvoB/hZqSuJ7IFBhsM X-Received: by 2002:a05:6a00:124e:b0:6ec:f3e0:861e with SMTP id u14-20020a056a00124e00b006ecf3e0861emr4209324pfi.1.1712673712235; Tue, 09 Apr 2024 07:41:52 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712673712; cv=pass; d=google.com; s=arc-20160816; b=JAaaYg67RS7a62WrgDKfSqFPp9rQWb3OEn+/ExY07BZLXBiJG6Yg/5a5I77WAX4NDc PmUDo53OnLLUUtotLM7Cz84Ik2LZONr7dBljwfnlJvWA6ZAgsfJggytlBIeWH0xGON9F hYfhvaP6oqRkRqntGMzHPRn8lLWpHirukfgm6vWLbkm7qUS5GUj/Ke2qiz7VnoOyMjDn JYtps9qQcrzBeCorPKG3GJxUOdiLUNu276PiQklwS0I166OvLm0ZjmfBxiYSm7bcKHbZ yzT7xmbHMhe9fpE0EXX45oi57agiYZOYs4T3f0wgw0E39H4mfFg1eUQRmqHK0eRpSDPT d8Ag== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to:subject :user-agent:mime-version:list-unsubscribe:list-subscribe:list-id :precedence:date:message-id; bh=WXA2X92hHcN+zgIyYNH3MvNJnCqx6ZN/dsKmbiS2oAU=; fh=ZR2NabeE2UQJY7OP6PHRNFOl+I7+u9BnHDN5sLkX04Q=; b=ig5eXROC0qCDLpbsR7KEw8Gn2K0nUlbix0SI6pWspK/4faQNroFXZfy0CeNR6qprjt 2guphqoIT4UOKK2mYYpPNzwPGOOvpl+YDZSqKLjCuOgzywn3wLSYSd4ObjxhkIvgvxvn cMFFk5lAr1KBM63RMpQPoSpD3pQyAhZLUDSgyvzUEaW0rNBS/bsPf0hAaJDQ9SyePIU4 9N6qvu/whRJr3B+c0g7emTtPgQu/3ePmAAiTnQqwapPUbqxcQr/lsXNNMqMStRdXY2ks Df04NtSZFIEO3t/uyqtK2fpukMvS0OIfuE8vHLDM2Oc93ghCblC0KPa6jWZgjvGRIsGk 1f6Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-137074-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-137074-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id m17-20020a62f211000000b006ecfc7f651asi8490851pfh.285.2024.04.09.07.41.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Apr 2024 07:41:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-137074-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-137074-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-137074-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 8C550B236A3 for ; Tue, 9 Apr 2024 14:16:35 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 44F2C12FF8F; Tue, 9 Apr 2024 14:15:49 +0000 (UTC) Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 37DDA12FB0F for ; Tue, 9 Apr 2024 14:15:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.188 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712672148; cv=none; b=azlSq8V+mEfJHbyk22xC6ZqWV7PV0M7e9G6wlbf28MjRmlZbX+tHwiHM0LW317bVn5OmU/PvwlVtsZVhaUYtxW0+pXZUssK9fyR1rs0F5uVsp1XJwuB+qC1uqz4QdPL1OML2RFOChNwfaogLHLpv/sZ4ICApVEUPM7P2YiIxyYA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712672148; c=relaxed/simple; bh=r5H2wFPyQ8ZrN4fvic1xS376fv3+AcK6k51m9j+GChY=; h=Message-ID:Date:MIME-Version:Subject:To:CC:References:From: In-Reply-To:Content-Type; b=jYY3wzwJoW7kPEfC1KDZZQkRASmWphoT0a2efuqkwKMflOxLmNaJB3A/k6/jN95YRENka38btADYA72cuJHD8RdzV0z+NvG24M8fusPfs6BAYkC4GP7bCzmyB/sdlSdTCQZ8Nlips747M6b3L8+s7EoDSUTR8yTOc8ZSxla3psU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.188 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.174]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4VDSdb1kB8zFr7V; Tue, 9 Apr 2024 22:14:43 +0800 (CST) Received: from dggpemd100001.china.huawei.com (unknown [7.185.36.94]) by mail.maildlp.com (Postfix) with ESMTPS id 0A09D1400FD; Tue, 9 Apr 2024 22:15:41 +0800 (CST) Received: from [10.174.179.155] (10.174.179.155) by dggpemd100001.china.huawei.com (7.185.36.94) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.28; Tue, 9 Apr 2024 22:15:40 +0800 Message-ID: <4d331659-badd-749d-fba1-271543631a8a@huawei.com> Date: Tue, 9 Apr 2024 22:15:39 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:104.0) Gecko/20100101 Thunderbird/104.0 Subject: Re: [PATCH] softirq: fix memory corruption when freeing tasklet_struct To: Mikulas Patocka , Linus Torvalds , Thomas Gleixner CC: , , Mike Snitzer , Ignat Korchagin , Damien Le Moal , Bob Liu , Hou Tao , Nathan Huckleberry , Peter Zijlstra , Ingo Molnar , "yukuai (C)" , yangerkun , "zhangyi (F)" , , Li Lingfeng References: <82b964f0-c2c8-a2c6-5b1f-f3145dc2c8e5@redhat.com> From: Li Lingfeng In-Reply-To: <82b964f0-c2c8-a2c6-5b1f-f3145dc2c8e5@redhat.com> Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 8bit X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggpemd100001.china.huawei.com (7.185.36.94) Hi I tried to reproduce the problem by adding mdelay() in tasklet_action_common() after t->func() but failed. Since cc->io_queue is created without WQ_UNBOUND, the work queued to it will be executed by the CPU which queued the work, which means bio_endio() will not be called until tasklet_action_common has finished. Could you please clarify how to reproduce the problem mentioned here? Thanks 在 2024/1/26 2:29, Mikulas Patocka 写道: > Hi > > There's a problem with the tasklet API - there is no reliable way how to > free a structure that contains tasklet_struct. The problem is that the > function tasklet_action_common calls task_unlock(t) after it called the > callback. If the callback does something that frees tasklet_struct, > task_unlock(t) would write into free memory. > > dm-crypt does this - it schedules a tasklet with tasklet_schedule, it does > encryption inside the tasklet handler (because it performs better than > doing the encryption in a workqueue), then it submits a workqueue entry > and calls bio_endio from the workqueue entry. > > However, if the workqueue preempts ksoftirqd, this race condition happens: > > ksoftirqd: > * tasklet_action_common > * t->func(t->data) (that points to kcryptd_crypt_tasklet) > * kcryptd_crypt_tasklet > * kcryptd_crypt > * kcryptd_crypt_read_convert > * crypt_dec_pending > * queue_work(cc->io_queue, &io->work); > now we switch to the workqueue process: > * kcryptd_io_bio_endio > * bio_endio(io->base_bio) (this calls clone_endio) > * clone_endio > * free_tio > * bio_put(clone) - the bio is freed > now we switch back to ksoftirqd: > * tasklet_action_common calls task_unlock(t) > * task_unlock(t) touches memory that was already freed when the bio was freed > > dm-verity has a similar problem. > > In order to fix this bug, I am proposing to add a new flag > TASKLET_STATE_ONESHOT. The flag indicates that the tasklet will be > submitted only once and it prevents tasklet_action_common from touching > the tasklet after the callback completed. > > If you have another idea how to solve this bug, let me know. > > Signed-off-by: Mikulas Patocka > Fixes: 39d42fa96ba1 ("dm crypt: add flags to optionally bypass kcryptd workqueues") > Fixes: 5721d4e5a9cd ("dm verity: Add optional "try_verify_in_tasklet" feature") > Cc: stable@vger.kernel.org # v5.9+ > > --- > drivers/md/dm-crypt.c | 1 + > drivers/md/dm-verity-target.c | 1 + > include/linux/interrupt.h | 9 ++++++++- > kernel/softirq.c | 22 +++++++++++++++------- > 4 files changed, 25 insertions(+), 8 deletions(-) > > Index: linux-2.6/drivers/md/dm-crypt.c > =================================================================== > --- linux-2.6.orig/drivers/md/dm-crypt.c 2024-01-18 19:18:30.000000000 +0100 > +++ linux-2.6/drivers/md/dm-crypt.c 2024-01-25 16:42:17.000000000 +0100 > @@ -2265,6 +2265,7 @@ static void kcryptd_queue_crypt(struct d > if (in_hardirq() || irqs_disabled()) { > io->in_tasklet = true; > tasklet_init(&io->tasklet, kcryptd_crypt_tasklet, (unsigned long)&io->work); > + tasklet_set_oneshot(&io->tasklet); > tasklet_schedule(&io->tasklet); > return; > } > Index: linux-2.6/include/linux/interrupt.h > =================================================================== > --- linux-2.6.orig/include/linux/interrupt.h 2023-11-13 17:39:50.000000000 +0100 > +++ linux-2.6/include/linux/interrupt.h 2024-01-25 16:41:52.000000000 +0100 > @@ -684,7 +684,9 @@ struct tasklet_struct name = { \ > enum > { > TASKLET_STATE_SCHED, /* Tasklet is scheduled for execution */ > - TASKLET_STATE_RUN /* Tasklet is running (SMP only) */ > + TASKLET_STATE_RUN, /* Tasklet is running (SMP only) */ > + TASKLET_STATE_ONESHOT /* Don't unlock the tasklet after the callback > + to avoid writing to free memory */ > }; > > #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT) > @@ -756,6 +758,11 @@ extern void tasklet_init(struct tasklet_ > extern void tasklet_setup(struct tasklet_struct *t, > void (*callback)(struct tasklet_struct *)); > > +static inline void tasklet_set_oneshot(struct tasklet_struct *t) > +{ > + __set_bit(TASKLET_STATE_ONESHOT, &t->state); > +} > + > /* > * Autoprobing for irqs: > * > Index: linux-2.6/kernel/softirq.c > =================================================================== > --- linux-2.6.orig/kernel/softirq.c 2023-10-31 15:31:42.000000000 +0100 > +++ linux-2.6/kernel/softirq.c 2024-01-25 17:10:03.000000000 +0100 > @@ -774,18 +774,26 @@ static void tasklet_action_common(struct > > if (tasklet_trylock(t)) { > if (!atomic_read(&t->count)) { > + /* > + * If oneshot is set, we must not touch the > + * tasklet after the callback. > + */ > + bool oneshot = test_bit(TASKLET_STATE_ONESHOT, &t->state); > if (tasklet_clear_sched(t)) { > if (t->use_callback) { > - trace_tasklet_entry(t, t->callback); > - t->callback(t); > - trace_tasklet_exit(t, t->callback); > + void (*callback)(struct tasklet_struct *) = t->callback; > + trace_tasklet_entry(t, callback); > + callback(t); > + trace_tasklet_exit(t, callback); > } else { > - trace_tasklet_entry(t, t->func); > - t->func(t->data); > - trace_tasklet_exit(t, t->func); > + void (*func)(unsigned long) = t->func; > + trace_tasklet_entry(t, func); > + func(t->data); > + trace_tasklet_exit(t, func); > } > } > - tasklet_unlock(t); > + if (!oneshot) > + tasklet_unlock(t); > continue; > } > tasklet_unlock(t); > Index: linux-2.6/drivers/md/dm-verity-target.c > =================================================================== > --- linux-2.6.orig/drivers/md/dm-verity-target.c 2024-01-18 19:18:30.000000000 +0100 > +++ linux-2.6/drivers/md/dm-verity-target.c 2024-01-25 18:12:09.000000000 +0100 > @@ -676,6 +676,7 @@ static void verity_end_io(struct bio *bi > > if (static_branch_unlikely(&use_tasklet_enabled) && io->v->use_tasklet) { > tasklet_init(&io->tasklet, verity_tasklet, (unsigned long)io); > + tasklet_set_oneshot(&io->tasklet); > tasklet_schedule(&io->tasklet); > } else { > INIT_WORK(&io->work, verity_work); >