Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp5867693ioo; Wed, 1 Jun 2022 14:29:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwTdR+Pv8p89ND6Xfjrl+Z3dC3JLokCnYlo+Ni/X5hL2BqRcFXfwU0oPKb4SEyVZU+/1mha X-Received: by 2002:a17:902:ec88:b0:166:33fe:a60c with SMTP id x8-20020a170902ec8800b0016633fea60cmr1377180plg.157.1654118982243; Wed, 01 Jun 2022 14:29:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654118982; cv=none; d=google.com; s=arc-20160816; b=LB8bY7hUg3lt87bPRyfMFrRoz0Hu/x9A6iXvQ8zjSKDXkgzDbUl87F47Akk88SsCJL fASzrvOJNkMiA0GkjeqUBRIfedyPgC+QSay00wVQFxsvf0LsP7UZvHmE0aQSk+2ioDps lPstlN1pRv0ssUmGZ6D1Jw78yWv0WXYjDeXgrs91dpbFLcqWfx0dQu0qHCGDvP/R9QE9 u5+lLYOnzBr79BPvkeCwjhDDFYBUMWoxCriM6ypphL+4FFw81UbHR3cmxAHxPVGuBFWz hl3CoiImIQnJf7mZlLIquBXxnVf4nzqLgQR/Ys+LekHr2fklhBqQQR/V7GghjCun2cLS HbuQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:subject:mime-version:user-agent:message-id :in-reply-to:date:references:cc:to:from; bh=ZGXEqPWJGvMOWJkyXou4hU2g0yIlRFmf0V2md8pOf54=; b=H3eAQ2wBzQw7vyuoudwzXMMBtM2PRvD16Daj6cMRz8NtAZ+IUmel2OA4uff+1OWLW+ DWD4SBorAmxSt82OW9bmCOSvS9xx8PFx8s8Svofhx4CNFOqpAaUyMABVM6ncGVnQSW3b +BfxygqSrdHIZmBn4XziCVmh1sGAaZ19JjmKfsuBaNoLHER6RUnOx6J7avtykaP4BwUl LD6+Y5nX7SQiP8gN9Aa4QS3NGJ+UTJCdgk0FgUBF9gQz52gdM50XjJMDwr1Q9vih8NWE nhMc3Xi7ydk5JW7D8G4G44CXJFaGqX18ri4nm5gw4JxAAGxSDe/ZNmduy/MgXWFR3A0/ PU3Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=xmission.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id p17-20020a634f51000000b003fc9d7a73cfsi3511140pgl.647.2022.06.01.14.29.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Jun 2022 14:29:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=xmission.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 6B48E314992; Wed, 1 Jun 2022 13:19:16 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353455AbiFAOhz (ORCPT + 99 others); Wed, 1 Jun 2022 10:37:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40110 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229602AbiFAOhw (ORCPT ); Wed, 1 Jun 2022 10:37:52 -0400 Received: from out03.mta.xmission.com (out03.mta.xmission.com [166.70.13.233]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 542F131233 for ; Wed, 1 Jun 2022 07:37:51 -0700 (PDT) Received: from in02.mta.xmission.com ([166.70.13.52]:39050) by out03.mta.xmission.com with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1nwPTm-003wch-Kk; Wed, 01 Jun 2022 08:37:46 -0600 Received: from ip68-227-174-4.om.om.cox.net ([68.227.174.4]:47918 helo=email.froward.int.ebiederm.org.xmission.com) by in02.mta.xmission.com with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1nwPTk-00ASIR-BF; Wed, 01 Jun 2022 08:37:46 -0600 From: "Eric W. Biederman" To: Miaohe Lin Cc: Ying Huang , , , , , , , , , , , , References: <20220530113016.16663-1-linmiaohe@huawei.com> <20220530113016.16663-2-linmiaohe@huawei.com> <87bkvdfzvm.fsf@email.froward.int.ebiederm.org> Date: Wed, 01 Jun 2022 09:37:21 -0500 In-Reply-To: (Miaohe Lin's message of "Wed, 1 Jun 2022 14:33:02 +0800") Message-ID: <87y1yga1r2.fsf@email.froward.int.ebiederm.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-XM-SPF: eid=1nwPTk-00ASIR-BF;;;mid=<87y1yga1r2.fsf@email.froward.int.ebiederm.org>;;;hst=in02.mta.xmission.com;;;ip=68.227.174.4;;;frm=ebiederm@xmission.com;;;spf=softfail X-XM-AID: U2FsdGVkX19dRjWhftz57Cs5rYGnFs7Ohp6QmfecC1w= X-SA-Exim-Connect-IP: 68.227.174.4 X-SA-Exim-Mail-From: ebiederm@xmission.com X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-DCC: XMission; sa07 1397; Body=1 Fuz1=1 Fuz2=1 X-Spam-Combo: ****;Miaohe Lin X-Spam-Relay-Country: X-Spam-Timing: total 1647 ms - load_scoreonly_sql: 0.10 (0.0%), signal_user_changed: 12 (0.7%), b_tie_ro: 10 (0.6%), parse: 1.61 (0.1%), extract_message_metadata: 17 (1.0%), get_uri_detail_list: 3.8 (0.2%), tests_pri_-1000: 8 (0.5%), tests_pri_-950: 1.43 (0.1%), tests_pri_-900: 1.08 (0.1%), tests_pri_-90: 71 (4.3%), check_bayes: 70 (4.2%), b_tokenize: 11 (0.7%), b_tok_get_all: 12 (0.7%), b_comp_prob: 3.8 (0.2%), b_tok_touch_all: 38 (2.3%), b_finish: 1.02 (0.1%), tests_pri_0: 1513 (91.8%), check_dkim_signature: 0.71 (0.0%), check_dkim_adsp: 2.9 (0.2%), poll_dns_idle: 0.94 (0.1%), tests_pri_10: 3.4 (0.2%), tests_pri_500: 15 (0.9%), rewrite_mail: 0.00 (0.0%) Subject: Re: [PATCH v4 1/4] mm: reduce the rcu lock duration X-SA-Exim-Version: 4.2.1 (built Sat, 08 Feb 2020 21:53:50 +0000) X-SA-Exim-Scanned: Yes (on in02.mta.xmission.com) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Miaohe Lin writes: > On 2022/6/1 0:09, Eric W. Biederman wrote: >> Miaohe Lin writes: > snip >>> >>> " >>> Commit 3268c63eded4 ("mm: fix move/migrate_pages() race on task struct") >>> extends the period of the rcu_read_lock until after the permissions checks >>> are done because it suspects the permissions checks are not safe unless >>> performed under both rcu_read_lock and task_lock to ensure the task<->mm >>> association does not change on us while we are working [1]. But extended >>> rcu read lock does not add much value. Because after permission checking >>> the permission may still be changed. There's no much difference. So it's >>> unnecessary to extend the period of the rcu_read_lock. Release the rcu >>> lock after task refcount is successfully grabbed to reduce the rcu holding >>> time. >>> >>> [1] https://lore.kernel.org/lkml/87sjhzun47.fsf@xmission.com/ >>> " >> >> It doesn't make sense to me. >> >> I don't see any sleeping functions called from find_mm_struct or >> kernel_migrate_pages in the area kernel_migrate_pages in the area of the >> code protected by get_task_struct. So at a very basic level I see a >> justification for dirtying a cache line twice with get_task_struct and >> put_task_struct to reduce rcu_read_lock hold times. >> >> I would contend that a reasonable cleanup based up on the current state >> of the code would be to extend the rcu_read_lock over get_task_mm so > > If so, security_task_movememory will be called inside rcu lock. It might > call sleeping functions, e.g. smack_log(). I think it's not a good > idea. In general the security functions are not allowed to sleep. The audit mechanism typically preallocates memory so it does not need to sleep. From a quick skim through the code I don't see smack_log sleeping. Certainly the security hooks are not supposed to be inserted into the code that they prevent reasonable implementation decisions. Which is to say if there is a good (non-security hook reason) for supporting sleeping deal with it. Otherwise the security hooks has a bug and needs to get fixed/removed. >> that a reference to task_struct does not need to be taken. That has >> the potential to reduce contention and reduce lock hold times. >> >> >> The code is missing a big fat comment with the assertion that it is ok >> if the permission checks are racy because the race is small, and the >> worst case thing that happens is the page is migrated to another >> numa node. >> >> >> Given that the get_mm_task takes task_lock the cost of dirtying the >> cache line is already being paid. Perhaps not extending task_lock hold >> times a little bit is justified, but I haven't seen that case made. >> >> This seems like code that is called little enough it would be better for >> it to be correct, and not need big fat comments explaining why it >> doesn't matter that they code is deliberately buggy. >> > > Agree. A big fat comments will make code hard to follow. No. The code is impossible to follow currently. The code either requires a comment pointing out that it is deliberately racy, or the code needs to be fixed. Clever and subtle code always requires a comment if for no other reason then to alert the reader that something a typical is going on. >> In short it does not make sense to me to justify a patch for performance >> reasons when it appears that extending the rcu_read_lock hold time and >> not touch the task reference count would stop dirtying a cache line and >> likely have more impact. > > IMHO, incremented task refcount should make code works correctly. And extending > the rcu_read_lock over get_task_mm will break the things because sleeping functions > might be called while holding rcu lock. Which sleeping functions? I can't find any. In particular smack_task_movememory calls smk_curacc_on_task which is the same function called by security_task_getpgid. security_task_getpgid is called under rcu_read_lock. So smack won't sleep. > Does the patch itself makes sense for you? Should I rephase the commit log further? > I'm afraid I didn't get your point correctly. The patch makes no sense to me because I don't see it doing anything worth doing. get/put_task_struct both dirty a cache line and are expensive especially when contended. Dirtying a cache line that is contended is the pretty much the most expensive native cpu operation. In pathological scenarios I have seen it take up to 1s. Realistically in a cache cold scenario (which is not as bad as a contended scenario) you are looking at 100ns or more just to execute get_task_struct/put_task_struct. That is the kind of cost it would be nice to avoid all together (even if the pathological case never comes up). So I see two pieces of code where we could use the cheap operation rcu_read_lock/rcu_read_unlock to remove the expensive operation get_task_struct/put_task_struct. Instead I see people removing rcu_read_lock/rcu_read_unlock. That makes no sense. Especially as your implied reason for making this change is to make the code have better performance. Improving performance is the reason for making critical sections smaller isn't it? Eric