Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752804AbdI0MGq (ORCPT ); Wed, 27 Sep 2017 08:06:46 -0400 Received: from mailout2.w1.samsung.com ([210.118.77.12]:36713 "EHLO mailout2.w1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752731AbdI0MGm (ORCPT ); Wed, 27 Sep 2017 08:06:42 -0400 X-AuditID: cbfec7ef-f79ee6d000003120-79-59cb944ee47e Subject: Re: [PATCH v2] crypto: s5p-sss: Add HASH support for Exynos To: Krzysztof Kozlowski Cc: linux-crypto@vger.kernel.org, Herbert Xu , Vladimir Zapolskiy , "David S. Miller" , Bartlomiej Zolnierkiewicz , linux-samsung-soc@vger.kernel.org, linux-kernel@vger.kernel.org From: Kamil Konieczny Message-id: Date: Wed, 27 Sep 2017 14:06:37 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 MIME-version: 1.0 In-reply-to: <20170926192853.qe6boek63huc7hce@kozik-lap> Content-type: text/plain; charset="utf-8" Content-language: en-US Content-transfer-encoding: 7bit X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFlrBKsWRmVeSWpSXmKPExsWy7djP87p+U05HGpx+rmOxccZ6Vos551tY LLpfyVicP7+B3eL+vZ9MFpd3zWGzmHF+H5PF/1/NzA4cHltW3mTy2HZA1WPTqk42j38Lp7B4 9G1ZxejxeZNcAFsUl01Kak5mWWqRvl0CV8blyQdYChrVKzZ+nMrYwPhHrouRk0NCwETi/YI+ NghbTOLCvfVANheHkMAyRol/Z3+wQjifGSX2He1n72LkAOs48UQBrujNpBdQRc8YJX4vvcgK MkpYwEVi99EesLEiApoS1/9+BytiFpjMJLHt+DlmkASbgLnEo+1nmEBsXgE3if7lu8HiLAKq EpOXngYbJCoQIXFh00+oGkGJH5PvsYDYnAIWEtcv7GMEsZmBFrz4MokFwhaXaG69CWXLS2xe 85YZZLGEwGM2id8r9zJBPOo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y8qzUktPsQozcGiJM7bu2d1 pJBAemJJanZqakFqEUyWiYNTqoGR4dPs8yq7+5/2TUlSaBQ4wrJ4WZBYLeOMVOuw4CknRU1C bj1tvMOmGtfVGypuKygzv/tAVdj0SW4/OUr+/Fq+eP2+m3M9ZSvbDhevmbHp7m7PTzqFqyNq smunauv9rpl6mZNrj/zV/zM2C/y7otz48qPvNXnlG4m2Mny3X4fxzF3cqzi52WOiEktxRqKh FnNRcSIAMHygrosCAAA= X-CMS-MailID: 20170927120638eucas1p1610abac8f5f6b98d87966bef4b4b187a X-Msg-Generator: CA X-Sender-IP: 182.198.249.179 X-Local-Sender: =?UTF-8?B?S2FtaWwgS29uaWVjem55G1NSUE9MLUtlcm5lbCAoVFApGw==?= =?UTF-8?B?7IK87ISx7KCE7J6QG1NlbmlvciBTb2Z0d2FyZSBFbmdpbmVlcg==?= X-Global-Sender: =?UTF-8?B?S2FtaWwgS29uaWVjem55G1NSUE9MLUtlcm5lbCAoVFApG1Nh?= =?UTF-8?B?bXN1bmcgRWxlY3Ryb25pY3MbU2VuaW9yIFNvZnR3YXJlIEVuZ2luZWVy?= X-Sender-Code: =?UTF-8?B?QzEwG0VIURtDMTBDRDAyQ0QwMjczOTI=?= CMS-TYPE: 201P X-CMS-RootMailID: 20170915175007eucas1p2912e7a9e1b44e976f96a53d8c8e442ec X-RootMTR: 20170915175007eucas1p2912e7a9e1b44e976f96a53d8c8e442ec References: <20170919190341.5vi5icaxnmynywre@kozik-lap> <8924d30c-6581-28f3-c223-c2a24c491887@partner.samsung.com> <20170925182713.ti4esyznyrjr7xh7@kozik-lap> <20170926192853.qe6boek63huc7hce@kozik-lap> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4864 Lines: 127 On 26.09.2017 21:28, Krzysztof Kozlowski wrote: > On Tue, Sep 26, 2017 at 05:54:42PM +0200, Kamil Konieczny wrote: >> On 25.09.2017 20:27, Krzysztof Kozlowski wrote: >> [...] >>>>>> + struct tasklet_struct hash_tasklet; >>>>>> + u8 xmit_buf[BUFLEN] SSS_ALIGNED; >>>>>> + >>>>>> + unsigned long hash_flags; >>>>> >>>>> This should be probably DECLARE_BITMAP. >>>> >>>> I do not get it, it is used for bits HASH_FLAGS_... and is not HW related. >>>> This will grow this patch even longer. >>> >>> Why longer? Only declaration changes, rest should be the same. >>> Just instead of playing with bits over unsigned long explicitly use a >>> type for that purpose - DECLARE_BITMAP. >> >> It will make code ugly, using &dev->hash_flags[0] instead of &dev->hash_flags. > > Wait, all the set_bit and clear_bit should be even simpler: > -set_bit(HASH_FLAGS_DMA_READY, &dev->hash_flags); > +set_bit(HASH_FLAGS_DMA_READY, dev->hash_flags); > This looks actually better.. > >> I have 9 bits used, and I will not anticipate it grow above 64. > > You mean 32. > >> If you insist, I can add DECLARE_BITMAP and rewrite all calls to _bits() >> functions. > > Actually not, I do not insist. Just for me it makes the code simpler > (lack of "&" operand) and more robust as you explicitly declare number > of used bits (instead of assumption that your data type on given > compiler matches the amount of flags). Both arguments are not that > important. I see, but I will postpone this to later refactoring and code cleanup, as none crypto drivers uses it now, and the declaration after macro unwind will look like: unsigned long hash_flags[1]; [...] >>>>>> + } else { >>>>>> + ctx->flags |= BIT(HASH_FLAGS_ERROR); >>>>>> + } >>>>>> + >>>>>> + /* atomic operation is not needed here */ >>>>> >>>>> Ok, but why? >>>>> >>>> >>>> Good question, I frankly copy&paste this. The hash vars in s5p_aes_dev >>>> are no longer used as all transfers ended, we have req context here >>>> so after complete we can work on next request, >>>> and clearing bit HASH_FLAGS_BUSY allows this. >>> >>> I think you need them. Here and few lines before. In >>> s5p_hash_handle_queue() you use spin-lock because it might be executed >>> multiple times. Having that assumption (multiple calls to >>> s5p_hash_handle_queue()) you can have: >>> >>> Thread #1: Thread #2 >>> s5p_hash_finish_req() s5p_hash_handle_queue() >>> dd->hash_flags &= ... >>> which equals to: >> >> somewhere here will be (in Thread #2): >> if(test_bit(HASH_FLAGS_BUSY,dd->hash_flags)) { >> unlock, return; } > > Right, good point! The HASH_FLAGS_BUSY bit is cleared only in > s5p_hash_finish_req() which will be executed only if the bit was set > (checked with test_bits()). This should be then put next to the sentence > "atomics are not needed here". > >> >>> tmp = dd->hash_flags; >>> tmp &= ~(BIT...) >>> set_bit(HASH_FLAGS_BUSY, &dd->hash_flags); >>> dd->hash_flags = tmp >>> >>> Which means that in last assignment you effectively lost the >>> HASH_FLAGS_BUSY bit. >>> >>> In general, you need to use atomics (or locks) everywhere, unless you >>> are 100% sure that given lockless section cannot be executed with other >>> code which uses locks. Otherwise the atomics/locks become uneffective. >> >> I can add spinlock as I am not 100% sure if below is true: >> >> HASH_FLAGS_BUSY is protected by order of instructions and by test_bit. > > In SMP code, one should not expect too much of order. :) > For sections not guarded by barriers (thus also locks) compiler can > re-order a lot. CPU can re-order even more. Although these > test_bit() and clear_bit() are atomic operations but they do not provide > barriers so they can be re-ordered. > >> First, this bit is set only in one place, in s5p_hash_handle_queue() and cleared >> also only in one place, s5p_hash_finish_req(). >> >> In function s5p_hash_handle_queue(), after spinlock, bit HASH_FLAGS_BUSY >> is tested and if set, there is unlock and exit, and the flow reaches next >> instructions only when this bit was not set. Setting bit is in the same spinlock >> section, so as you write, concurrent calls to s5p_hash_handle_queue are >> protected by spinlock. > > Yes, but on the other hand writing the HASH_FLAGS_BUSY bit in > s5p_hash_finish_req() is not protected anyhow and it will be called on > different CPU in different thread (through tasklet). Although > s5p_hash_finish_req() is called only after checking the bits... but it > is I think unusual pattern (in s5p_hash_tasklet_cb()): > - test_bit(hash_flags) > - non-atomic write to hash_flags > - test_bit(hash_flags) > OK, as I wrote I am not sure, so I add spinlock in s5p_hash_finish_req() to protect code clearing HASH_FLAGS_BUSY. -- Best regards, Kamil Konieczny Samsung R&D Institute Poland