From mboxrd@z Thu Jan 1 00:00:00 1970 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 1ECF9E6FE48 for ; Fri, 6 Sep 2024 20:12:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4F50E6B0085; Fri, 6 Sep 2024 16:12:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4A4D16B0088; Fri, 6 Sep 2024 16:12:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 36C986B0089; Fri, 6 Sep 2024 16:12:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 158476B0085 for ; Fri, 6 Sep 2024 16:12:02 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 7AC521C555B for ; Fri, 6 Sep 2024 20:12:01 +0000 (UTC) X-FDA: 82535409642.30.25DA040 Received: from out-175.mta1.migadu.com (out-175.mta1.migadu.com [95.215.58.175]) by imf04.hostedemail.com (Postfix) with ESMTP id 68B7640029 for ; Fri, 6 Sep 2024 20:11:59 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=dZ5j9eRh; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf04.hostedemail.com: domain of martin.lau@linux.dev designates 95.215.58.175 as permitted sender) smtp.mailfrom=martin.lau@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725653519; a=rsa-sha256; cv=none; b=e9li4bmAc5N1RYtP/CLxUQG0Ty+5HqABkmY6pOwhJrurwA2CLbyOFeYc8gY5maVCw6pgip zXP7AocaKfhTIz9TZYHVNE5ZQQF3jvcNTrGx8OAVHzBUeWlj7yzmAGRfv9Hlq081YDo8zI GCry066w7z5IH6bI3CgW5/aTPbfrqak= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=dZ5j9eRh; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf04.hostedemail.com: domain of martin.lau@linux.dev designates 95.215.58.175 as permitted sender) smtp.mailfrom=martin.lau@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725653519; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=jhhhpzp5I00d7lFzLvbJIXA1AyZbtxT5pcsvaV59gg0=; b=4itUsOVPaLOFdXp2mo9CQpNwlKAp7zYfNWdWJH22LvhBEOYIWikXAHLyV8GHWU0L2oRL19 3lIGPJFcMFMpFzEdr26VizeQaOBsatnElQI1ynaT6PCPYgHCCvy8Kn+Miv2+oNFzWuaFTk pVt5h+Dzbh3CZVBMBHBIPG2/AFLcnSI= Message-ID: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1725653517; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jhhhpzp5I00d7lFzLvbJIXA1AyZbtxT5pcsvaV59gg0=; b=dZ5j9eRhml/hXYrJ+CWC/TEXyOmLVGVXzbeAoAZVZC6yTch7lMKt2sD9TWkQjDtMWX/bKB 8oJcONqKzITjVzbEPtGZ+csVqoa1JkvWqMOQ5K1UE4BDbT83sAxG7UQz3GUF9AZVKifKov 1RfrlvTrZ6Nk2NWXWR6uhn0FXJoaUCk= Date: Fri, 6 Sep 2024 13:11:48 -0700 MIME-Version: 1.0 Subject: Re: [RFC bpf-next v4 4/6] bpf: pin, translate, and unpin __uptr from syscalls. X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Martin KaFai Lau To: Alexei Starovoitov Cc: Kui-Feng Lee , bpf , Alexei Starovoitov , Andrii Nakryiko , Kui-Feng Lee , linux-mm References: <20240816191213.35573-1-thinker.li@gmail.com> <20240816191213.35573-5-thinker.li@gmail.com> <70a1b24f-84cd-464c-8fb6-a2c52fd3d703@linux.dev> Content-Language: en-US In-Reply-To: <70a1b24f-84cd-464c-8fb6-a2c52fd3d703@linux.dev> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Stat-Signature: 4softp4rorww1wy5n7p7ek1bcmetyyex X-Rspamd-Queue-Id: 68B7640029 X-Rspamd-Server: rspam02 X-HE-Tag: 1725653519-614080 X-HE-Meta: U2FsdGVkX19XVeP4D/N1R+2m1k8b6pJ8x6ca1iqdlBz1A2eZfb5REFaR3RIc/bpiTbCViQKzDfXeQyT5BsrwBAqHRIkumGfFidwuA4+dIulUkV809vtX+ywyKSUl046TmuwKnFeiQ2Wslh9TYL6Sc23jSWNYy2EpRArmmt7LZChOZKr1V7UqfR9Ap4tuxxICmuYmyf3ZofwuBPpyZnseFWQGUBi+RV/OHKmRiMfg4wMveyOUmtsJLhaOZtmbgQzFrJ4P2Xv+X7kXUrIeAdsp6985VIMYdZ4N4QLPfxC3wEU9GoybyNyn1bpzNv7LJVz+qa3OFL/Pl+n9AOFAQ9j3ovgyJ9LztL/W5yVxvHzu+hiQcX+oFWDDgkM55uhxf5gskxuwvboIZIECiSTwOJEJp292aCgDP2a5yd68QbFDkfGnqtTfIfsmZn35Jc154MOk/W8HqA022OX4z8zUNTwlq9VdtGgAW6DiqhS28/2LHnYjkbp+WxUzS2HeT5KXTv1jRxclq8twlnCtvilyT0BhRkvTUQOCXLow5MnXTcWVyzZt9ksWOoWO1z33W68ZN5XoOoL0aTfEv20b9/Jvb42ZDduD9cKISONadWSXDZlljQiE5b6tHCL1uvX6EoABP7lJUQ2qvbBf60cN7JM1BIf1CWC9ZukDLCLRHI/vje9gDEeXY8GqY/TzLwzEN3mqvJ5Rcl4gQiNVuNqRrin/xU0vCdtNdfKX7bN9YMdMXjhxH3hL1yud9+9yIWZNiU9kBY80YkHixR3BaoHyo77HuLJw85LBSVkPyrqpZJz3vwUIN2Wy1iUGNDYpSQs1xFQYGJvQ8KgrekMm+E9zDIh8QcLoormqO4rvwbZh5p03WNsKPMNandoRmNgjwlAKmHFtLZ7mMTA6lDnXlk/6oKqculWkyRfAkoQ2GjuBCaph3Nq5vRGZfMGaBi112nM7T9z3FP5jjoqp3/3iPOtW1BsbRMs WpdnqN/Q 7rkorcDMITFJlcOG30KpzYlTDyL+a5sJkkEdqacmwyO75w3y93lHeLSYNrnEJRZ06TIbmOhlM3g9P0cTCtjz9rNYdnye/yHqLKVhUYQkEy/qNVGnaxRY9l+/luMTIA3Op8m15howy3cG0xOR5IZI5Xz2WUe4US1jhMEHJUt0VOcqvbNJK/dVBUHUh3kNKUunmI3EtrD2XeVElWK9fMFIS2hpaxrf3rXgOQPO7IukuVpcPa2GCZEWH6VXMUcH/64RgWPZP 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: List-Subscribe: List-Unsubscribe: On 9/4/24 3:21 PM, Martin KaFai Lau wrote: > On 8/28/24 4:24 PM, Alexei Starovoitov wrote: >>> @@ -714,6 +869,11 @@ void bpf_obj_free_fields(const struct btf_record *rec, >>> void *obj) >>>                                  field->kptr.dtor(xchgd_field); >>>                          } >>>                          break; >>> +               case BPF_UPTR: >>> +                       if (*(void **)field_ptr) >>> +                               bpf_obj_unpin_uptr(field, *(void **)field_ptr); >>> +                       *(void **)field_ptr = NULL; >> This one will be called from >>   task_storage_delete->bpf_selem_free->bpf_obj_free_fields >> >> and even if upin was safe to do from that context >> we cannot just do: >> *(void **)field_ptr = NULL; >> >> since bpf prog might be running in parallel, >> it could have just read that addr and now is using it. >> >> The first thought of a way to fix this was to split >> bpf_obj_free_fields() into the current one plus >> bpf_obj_free_fields_after_gp() >> that will do the above unpin bit. >> and call the later one from bpf_selem_free_rcu() >> while bpf_obj_free_fields() from bpf_selem_free() >> will not touch uptr. >> >> But after digging further I realized that task_storage >> already switched to use bpf_ma, so the above won't work. >> >> So we need something similar to BPF_KPTR_REF logic: >> xchgd_field = (void *)xchg((unsigned long *)field_ptr, 0); >> and then delay of uptr unpin for that address into call_rcu. >> >> Any better ideas? > I think the existing reuse_now arg in the bpf_selem_free can be used. reuse_now (renamed from the earlier use_trace_rcu) was added to avoid call_rcu_tasks_trace for the common case. selem (in type "struct bpf_local_storage_elem") is the one exposed to the bpf prog. bpf_selem_free knows whether a selem can be reused immediately based on the caller. It is currently flagged in the reuse_now arg: "bpf_selem_free(...., bool reuse_now)". If a selem cannot be reuse_now (i.e. == false), it is currently going through "call_rcu_tasks_trace(&selem->rcu, bpf_selem_free_trace_rcu)". We can do unpin_user_page() in the rcu callback. A selem can be reuse_now (i.e. == true) if the selem is no longer needed because either its owner (i.e. the task_struct here) is going away in free_task() or the bpf map is being destructed in bpf_local_storage_map_free(). No bpf prog should have a hold on the selem at this point. I think for these two cases, the unpin_user_page() can be directly called in bpf_selem_free(). One existing bug is, from looking at patch 6, I don't think the free_task() case can be "reuse_now == true" anymore because of the bpf_task_release kfunc did not mark the previously obtained task_storage to be invalid: data_task = bpf_task_from_pid(parent_pid); ptr = bpf_task_storage_get(&datamap, data_task, 0, ...); bpf_task_release(data_task); if (!ptr) return 0; /* The prog still holds a valid task storage ptr. */ udata = ptr->udata; It can be fixed by marking the ref_obj_id of the "ptr". Although it is more correct to make the task storage "ptr" invalid after task_release, it may break the existing progs. The same issue probably is true for cgroup_storage. There is no release kfunc for inode and sk, so inode and sk storage should be fine.